blob: 533f301aea47f6a60717efd19dc6b1781c91adbd [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48/*
49 * The SSL 3.0 specification was drafted by Netscape in 1996,
50 * and became an IETF standard in 1999.
51 *
52 * http://wp.netscape.com/eng/ssl3/
53 * http://www.ietf.org/rfc/rfc2246.txt
54 * http://www.ietf.org/rfc/rfc4346.txt
55 */
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000064
SimonBd5800b72016-04-26 07:43:27 +010065#if defined(MBEDTLS_PLATFORM_C)
66#include "mbedtls/platform.h"
67#else
68#include <stdlib.h>
69#define mbedtls_calloc calloc
70#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010071#endif
72
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/debug.h"
74#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020075#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050076#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020077
Rich Evans00ab4702015-02-06 13:43:58 +000078#include <string.h>
79
Janos Follath23bdca02016-10-07 14:47:14 +010080#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020082#endif
83
Hanno Becker2a43f6f2018-08-10 11:12:52 +010084static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010085static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010086
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010087/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020091 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010092 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010093#else
94 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010095#endif
96 return( 0 );
97}
98
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020099/*
100 * Start a timer.
101 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200105 if( ssl->f_set_timer == NULL )
106 return;
107
108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
109 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110}
111
112/*
113 * Return -1 is timer is expired, 0 if it isn't.
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200116{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200117 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200118 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200119
120 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200121 {
122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200123 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200124 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200125
126 return( 0 );
127}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200128
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100129static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
130 mbedtls_ssl_transform *transform );
131static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
132 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100133
134#define SSL_DONT_FORCE_FLUSH 0
135#define SSL_FORCE_FLUSH 1
136
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200137#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100138
Hanno Beckerd5847772018-08-28 10:09:23 +0100139/* Forward declarations for functions related to message buffering. */
140static void ssl_buffering_free( mbedtls_ssl_context *ssl );
141static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
142 uint8_t slot );
143static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
144static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
145static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
146static int ssl_buffer_message( mbedtls_ssl_context *ssl );
147static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100148static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100149
Hanno Beckera67dee22018-08-22 10:05:20 +0100150static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100151static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100152{
Hanno Becker11682cc2018-08-22 14:41:02 +0100153 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100154
155 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100156 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100157
158 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
159}
160
Hanno Becker67bc7c32018-08-06 11:33:50 +0100161static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
162{
Hanno Becker11682cc2018-08-22 14:41:02 +0100163 size_t const bytes_written = ssl->out_left;
164 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
166 /* Double-check that the write-index hasn't gone
167 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100168 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100169 {
170 /* Should never happen... */
171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
172 }
173
174 return( (int) ( mtu - bytes_written ) );
175}
176
177static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
178{
179 int ret;
180 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400181 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100182
183#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
184 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
185
186 if( max_len > mfl )
187 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100188
189 /* By the standard (RFC 6066 Sect. 4), the MFL extension
190 * only limits the maximum record payload size, so in theory
191 * we would be allowed to pack multiple records of payload size
192 * MFL into a single datagram. However, this would mean that there's
193 * no way to explicitly communicate MTU restrictions to the peer.
194 *
195 * The following reduction of max_len makes sure that we never
196 * write datagrams larger than MFL + Record Expansion Overhead.
197 */
198 if( max_len <= ssl->out_left )
199 return( 0 );
200
201 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100202#endif
203
204 ret = ssl_get_remaining_space_in_datagram( ssl );
205 if( ret < 0 )
206 return( ret );
207 remaining = (size_t) ret;
208
209 ret = mbedtls_ssl_get_record_expansion( ssl );
210 if( ret < 0 )
211 return( ret );
212 expansion = (size_t) ret;
213
214 if( remaining <= expansion )
215 return( 0 );
216
217 remaining -= expansion;
218 if( remaining >= max_len )
219 remaining = max_len;
220
221 return( (int) remaining );
222}
223
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200224/*
225 * Double the retransmit timeout value, within the allowed range,
226 * returning -1 if the maximum value has already been reached.
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229{
230 uint32_t new_timeout;
231
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200232 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200233 return( -1 );
234
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200235 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
236 * in the following way: after the initial transmission and a first
237 * retransmission, back off to a temporary estimated MTU of 508 bytes.
238 * This value is guaranteed to be deliverable (if not guaranteed to be
239 * delivered) of any compliant IPv4 (and IPv6) network, and should work
240 * on most non-IP stacks too. */
241 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400242 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200243 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
245 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200246
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200247 new_timeout = 2 * ssl->handshake->retransmit_timeout;
248
249 /* Avoid arithmetic overflow and range overflow */
250 if( new_timeout < ssl->handshake->retransmit_timeout ||
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 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200253 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200254 }
255
256 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200258 ssl->handshake->retransmit_timeout ) );
259
260 return( 0 );
261}
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200264{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200265 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200267 ssl->handshake->retransmit_timeout ) );
268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200272/*
273 * Convert max_fragment_length codes to length.
274 * RFC 6066 says:
275 * enum{
276 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
277 * } MaxFragmentLength;
278 * and we add 0 -> extension unused
279 */
Angus Grattond8213d02016-05-25 20:56:48 +1000280static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281{
Angus Grattond8213d02016-05-25 20:56:48 +1000282 switch( mfl )
283 {
284 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
285 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
286 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
287 return 512;
288 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
289 return 1024;
290 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
291 return 2048;
292 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
293 return 4096;
294 default:
295 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
296 }
297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200299
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200300#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_ssl_session_free( dst );
304 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200307 if( src->peer_cert != NULL )
308 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200309 int ret;
310
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200311 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200312 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200313 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200318 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200321 dst->peer_cert = NULL;
322 return( ret );
323 }
324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200326
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200327#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200328 if( src->ticket != NULL )
329 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200330 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200331 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200332 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200333
334 memcpy( dst->ticket, src->ticket, src->ticket_len );
335 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200336#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200337
338 return( 0 );
339}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200340#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
343int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200344 const unsigned char *key_enc, const unsigned char *key_dec,
345 size_t keylen,
346 const unsigned char *iv_enc, const unsigned char *iv_dec,
347 size_t ivlen,
348 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200349 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
351int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
353int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
354int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
355#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000356
Paul Bakker5121ce52009-01-03 21:22:43 +0000357/*
358 * Key material generation
359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200361static int ssl3_prf( const unsigned char *secret, size_t slen,
362 const char *label,
363 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000364 unsigned char *dstbuf, size_t dlen )
365{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100366 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000367 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200368 mbedtls_md5_context md5;
369 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000370 unsigned char padding[16];
371 unsigned char sha1sum[20];
372 ((void)label);
373
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200374 mbedtls_md5_init( &md5 );
375 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200376
Paul Bakker5f70b252012-09-13 14:23:06 +0000377 /*
378 * SSLv3:
379 * block =
380 * MD5( secret + SHA1( 'A' + secret + random ) ) +
381 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
382 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
383 * ...
384 */
385 for( i = 0; i < dlen / 16; i++ )
386 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200387 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000388
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 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, padding, 1 + i ) ) != 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, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100398 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 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, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100406 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100407 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000408 }
409
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100410exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200411 mbedtls_md5_free( &md5 );
412 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000413
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500414 mbedtls_platform_zeroize( padding, sizeof( padding ) );
415 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100417 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200422static int tls1_prf( const unsigned char *secret, size_t slen,
423 const char *label,
424 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000425 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000426{
Paul Bakker23986e52011-04-24 08:57:21 +0000427 size_t nb, hs;
428 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200429 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 unsigned char tmp[128];
431 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 const mbedtls_md_info_t *md_info;
433 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100434 int ret;
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441 hs = ( slen + 1 ) / 2;
442 S1 = secret;
443 S2 = secret + slen - hs;
444
445 nb = strlen( label );
446 memcpy( tmp + 20, label, nb );
447 memcpy( tmp + 20 + nb, random, rlen );
448 nb += rlen;
449
450 /*
451 * First compute P_md5(secret,label+random)[0..dlen]
452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100457 return( ret );
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
460 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
461 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 for( i = 0; i < dlen; i += 16 )
464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_md_hmac_reset ( &md_ctx );
466 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
467 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_md_hmac_reset ( &md_ctx );
470 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
471 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
474
475 for( j = 0; j < k; j++ )
476 dstbuf[i + j] = h_i[j];
477 }
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100480
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 /*
482 * XOR out with P_sha1(secret,label+random)[0..dlen]
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100488 return( ret );
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
491 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
492 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 for( i = 0; i < dlen; i += 20 )
495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_md_hmac_reset ( &md_ctx );
497 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
498 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_md_hmac_reset ( &md_ctx );
501 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
502 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
505
506 for( j = 0; j < k; j++ )
507 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
508 }
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100511
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500512 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
513 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515 return( 0 );
516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
520static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100521 const unsigned char *secret, size_t slen,
522 const char *label,
523 const unsigned char *random, size_t rlen,
524 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000525{
526 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100527 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000528 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
530 const mbedtls_md_info_t *md_info;
531 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100532 int ret;
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100540
541 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000543
544 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100545 memcpy( tmp + md_len, label, nb );
546 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000547 nb += rlen;
548
549 /*
550 * Compute P_<hash>(secret, label + random)[0..dlen]
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100553 return( ret );
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
556 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
557 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100558
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100559 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_md_hmac_reset ( &md_ctx );
562 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
563 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_hmac_reset ( &md_ctx );
566 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
567 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000568
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100569 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000570
571 for( j = 0; j < k; j++ )
572 dstbuf[i + j] = h_i[j];
573 }
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100576
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500577 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
578 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000579
580 return( 0 );
581}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100584static int tls_prf_sha256( const unsigned char *secret, size_t slen,
585 const char *label,
586 const unsigned char *random, size_t rlen,
587 unsigned char *dstbuf, size_t dlen )
588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100590 label, random, rlen, dstbuf, dlen ) );
591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200595static int tls_prf_sha384( const unsigned char *secret, size_t slen,
596 const char *label,
597 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000598 unsigned char *dstbuf, size_t dlen )
599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100601 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_SHA512_C */
604#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
609 defined(MBEDTLS_SSL_PROTO_TLS1_1)
610static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200611#endif
Paul Bakker380da532012-04-18 16:10:25 +0000612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_SSL_PROTO_SSL3)
614static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
615static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200616#endif
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
619static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
620static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200621#endif
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
624#if defined(MBEDTLS_SHA256_C)
625static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
626static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
627static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200628#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#if defined(MBEDTLS_SHA512_C)
631static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
632static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
633static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100634#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200639 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 unsigned char keyblk[256];
642 unsigned char *key1;
643 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100644 unsigned char *mac_enc;
645 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000646 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200647 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 const mbedtls_cipher_info_t *cipher_info;
649 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_ssl_session *session = ssl->session_negotiate;
652 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
653 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100658 if( cipher_info == NULL )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100661 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100663 }
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100666 if( md_info == NULL )
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100669 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100671 }
672
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000674 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_SSL_PROTO_SSL3)
677 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000678 {
Paul Bakker48916f92012-09-16 19:57:18 +0000679 handshake->tls_prf = ssl3_prf;
680 handshake->calc_verify = ssl_calc_verify_ssl;
681 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200683 else
684#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
686 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000687 {
Paul Bakker48916f92012-09-16 19:57:18 +0000688 handshake->tls_prf = tls1_prf;
689 handshake->calc_verify = ssl_calc_verify_tls;
690 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200692 else
693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
695#if defined(MBEDTLS_SHA512_C)
696 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
697 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000698 {
Paul Bakker48916f92012-09-16 19:57:18 +0000699 handshake->tls_prf = tls_prf_sha384;
700 handshake->calc_verify = ssl_calc_verify_tls_sha384;
701 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000702 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SHA256_C)
706 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000707 {
Paul Bakker48916f92012-09-16 19:57:18 +0000708 handshake->tls_prf = tls_prf_sha256;
709 handshake->calc_verify = ssl_calc_verify_tls_sha256;
710 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200712 else
713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200718 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000719
720 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 * SSLv3:
722 * master =
723 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
724 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
725 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200726 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200727 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 * master = PRF( premaster, "master secret", randbytes )[0..47]
729 */
Paul Bakker0a597072012-09-25 21:55:46 +0000730 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000733 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
736 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200737 {
738 unsigned char session_hash[48];
739 size_t hash_len;
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200742
743 ssl->handshake->calc_verify( ssl, session_hash );
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
746 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200749 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200751 {
752 hash_len = 48;
753 }
754 else
755#endif
756 hash_len = 32;
757 }
758 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200760 hash_len = 36;
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200763
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100764 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
765 "extended master secret",
766 session_hash, hash_len,
767 session->master, 48 );
768 if( ret != 0 )
769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100771 return( ret );
772 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200773
774 }
775 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200776#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100777 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
778 "master secret",
779 handshake->randbytes, 64,
780 session->master, 48 );
781 if( ret != 0 )
782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100784 return( ret );
785 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200786
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500787 mbedtls_platform_zeroize( handshake->premaster,
788 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 }
790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793 /*
794 * Swap the client and server random values.
795 */
Paul Bakker48916f92012-09-16 19:57:18 +0000796 memcpy( tmp, handshake->randbytes, 64 );
797 memcpy( handshake->randbytes, tmp + 32, 32 );
798 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500799 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000800
801 /*
802 * SSLv3:
803 * key block =
804 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
805 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
806 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
807 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
808 * ...
809 *
810 * TLSv1:
811 * key block = PRF( master, "key expansion", randbytes )
812 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100813 ret = handshake->tls_prf( session->master, 48, "key expansion",
814 handshake->randbytes, 64, keyblk, 256 );
815 if( ret != 0 )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100818 return( ret );
819 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
822 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
823 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
824 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
825 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500827 mbedtls_platform_zeroize( handshake->randbytes,
828 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
830 /*
831 * Determine the appropriate key, IV and MAC length.
832 */
Paul Bakker68884e32013-01-07 18:20:04 +0100833
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200834 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200837 cipher_info->mode == MBEDTLS_MODE_CCM ||
838 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200840 size_t taglen, explicit_ivlen;
841
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200842 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000843 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200844
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200845 /* All modes haves 96-bit IVs;
846 * GCM and CCM has 4 implicit and 8 explicit bytes
847 * ChachaPoly has all 12 bytes implicit
848 */
Paul Bakker68884e32013-01-07 18:20:04 +0100849 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200850 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
851 transform->fixed_ivlen = 12;
852 else
853 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200854
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200855 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
856 taglen = transform->ciphersuite_info->flags &
857 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
858
859
860 /* Minimum length of encrypted record */
861 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
862 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100863 }
864 else
865 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
868 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200871 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100872 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000873
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200874 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000875 mac_key_len = mbedtls_md_get_size( md_info );
876 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200879 /*
880 * If HMAC is to be truncated, we shall keep the leftmost bytes,
881 * (rfc 6066 page 13 or rfc 2104 section 4),
882 * so we only need to adjust the length here.
883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000887
888#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
889 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000890 * HMAC implementation which also truncates the key
891 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000892 mac_key_len = transform->maclen;
893#endif
894 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200896
897 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100898 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200900 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200902 transform->minlen = transform->maclen;
903 else
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200905 /*
906 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100907 * 1. if EtM is in use: one block plus MAC
908 * otherwise: * first multiple of blocklen greater than maclen
909 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
912 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100913 {
914 transform->minlen = transform->maclen
915 + cipher_info->block_size;
916 }
917 else
918#endif
919 {
920 transform->minlen = transform->maclen
921 + cipher_info->block_size
922 - transform->maclen % cipher_info->block_size;
923 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
926 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
927 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200928 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100929 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
932 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
933 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200934 {
935 transform->minlen += transform->ivlen;
936 }
937 else
938#endif
939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200942 }
Paul Bakker68884e32013-01-07 18:20:04 +0100943 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 }
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000947 transform->keylen, transform->minlen, transform->ivlen,
948 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
950 /*
951 * Finally setup the cipher contexts, IVs and MAC secrets.
952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000956 key1 = keyblk + mac_key_len * 2;
957 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakker68884e32013-01-07 18:20:04 +0100959 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000960 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000962 /*
963 * This is not used in TLS v1.1.
964 */
Paul Bakker48916f92012-09-16 19:57:18 +0000965 iv_copy_len = ( transform->fixed_ivlen ) ?
966 transform->fixed_ivlen : transform->ivlen;
967 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
968 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000969 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 }
971 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_SSL_CLI_C */
973#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000976 key1 = keyblk + mac_key_len * 2 + transform->keylen;
977 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Hanno Becker81c7b182017-11-09 18:39:33 +0000979 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100980 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000982 /*
983 * This is not used in TLS v1.1.
984 */
Paul Bakker48916f92012-09-16 19:57:18 +0000985 iv_copy_len = ( transform->fixed_ivlen ) ?
986 transform->fixed_ivlen : transform->ivlen;
987 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
988 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000989 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100996 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#if defined(MBEDTLS_SSL_PROTO_SSL3)
999 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001000 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001005 }
1006
Hanno Becker81c7b182017-11-09 18:39:33 +00001007 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1008 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001009 }
1010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1012#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1013 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1014 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001015 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001016 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1017 For AEAD-based ciphersuites, there is nothing to do here. */
1018 if( mac_key_len != 0 )
1019 {
1020 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1021 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1022 }
Paul Bakker68884e32013-01-07 18:20:04 +01001023 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001024 else
1025#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1028 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001029 }
Paul Bakker68884e32013-01-07 18:20:04 +01001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1032 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001037 transform->iv_enc, transform->iv_dec,
1038 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001039 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001040 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1043 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001044 }
1045 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001047
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001048#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1049 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001050 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001051 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1052 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001053 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001054 iv_copy_len );
1055 }
1056#endif
1057
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001058 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001059 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001062 return( ret );
1063 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001064
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001065 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001066 cipher_info ) ) != 0 )
1067 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001069 return( ret );
1070 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001073 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001077 return( ret );
1078 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001081 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001085 return( ret );
1086 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_CIPHER_MODE_CBC)
1089 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1092 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001096 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1099 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001102 return( ret );
1103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001107 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001110 // Initialize compression
1111 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001113 {
Paul Bakker16770332013-10-11 09:59:44 +02001114 if( ssl->compress_buf == NULL )
1115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001117 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001118 if( ssl->compress_buf == NULL )
1119 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001121 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001122 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001123 }
1124 }
1125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001127
Paul Bakker48916f92012-09-16 19:57:18 +00001128 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1129 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001130
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001131 if( deflateInit( &transform->ctx_deflate,
1132 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001133 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1136 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001137 }
1138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
1143 return( 0 );
1144}
1145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_SSL_PROTO_SSL3)
1147void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001148{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001149 mbedtls_md5_context md5;
1150 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 unsigned char pad_1[48];
1152 unsigned char pad_2[48];
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001156 mbedtls_md5_init( &md5 );
1157 mbedtls_sha1_init( &sha1 );
1158
1159 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1160 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Paul Bakker380da532012-04-18 16:10:25 +00001162 memset( pad_1, 0x36, 48 );
1163 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001165 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1166 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1167 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001169 mbedtls_md5_starts_ret( &md5 );
1170 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1171 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1172 mbedtls_md5_update_ret( &md5, hash, 16 );
1173 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001175 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1176 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1177 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001178
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001179 mbedtls_sha1_starts_ret( &sha1 );
1180 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1181 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1182 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1183 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001187
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001188 mbedtls_md5_free( &md5 );
1189 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001190
Paul Bakker380da532012-04-18 16:10:25 +00001191 return;
1192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1196void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001197{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001198 mbedtls_md5_context md5;
1199 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001202
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001203 mbedtls_md5_init( &md5 );
1204 mbedtls_sha1_init( &sha1 );
1205
1206 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1207 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001208
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001209 mbedtls_md5_finish_ret( &md5, hash );
1210 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001214
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001215 mbedtls_md5_free( &md5 );
1216 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001217
Paul Bakker380da532012-04-18 16:10:25 +00001218 return;
1219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1223#if defined(MBEDTLS_SHA256_C)
1224void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001225{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001226 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001227
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001228 mbedtls_sha256_init( &sha256 );
1229
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001231
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001232 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001233 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001237
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001238 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001239
Paul Bakker380da532012-04-18 16:10:25 +00001240 return;
1241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_SHA512_C)
1245void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001246{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001247 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001248
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001249 mbedtls_sha512_init( &sha512 );
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001252
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001253 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001254 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001259 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001260
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 return;
1262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SHA512_C */
1264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1267int 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 +02001268{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001269 unsigned char *p = ssl->handshake->premaster;
1270 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001271 const unsigned char *psk = ssl->conf->psk;
1272 size_t psk_len = ssl->conf->psk_len;
1273
1274 /* If the psk callback was called, use its result */
1275 if( ssl->handshake->psk != NULL )
1276 {
1277 psk = ssl->handshake->psk;
1278 psk_len = ssl->handshake->psk_len;
1279 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001280
1281 /*
1282 * PMS = struct {
1283 * opaque other_secret<0..2^16-1>;
1284 * opaque psk<0..2^16-1>;
1285 * };
1286 * with "other_secret" depending on the particular key exchange
1287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1289 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001290 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001291 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001293
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001294 *(p++) = (unsigned char)( psk_len >> 8 );
1295 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001296
1297 if( end < p || (size_t)( end - p ) < psk_len )
1298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1299
1300 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001301 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001302 }
1303 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1305#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1306 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001307 {
1308 /*
1309 * other_secret already set by the ClientKeyExchange message,
1310 * and is 48 bytes long
1311 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001312 if( end - p < 2 )
1313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1314
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001315 *p++ = 0;
1316 *p++ = 48;
1317 p += 48;
1318 }
1319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1321#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1322 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001323 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001324 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001325 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001326
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001327 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001329 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001330 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001333 return( ret );
1334 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001335 *(p++) = (unsigned char)( len >> 8 );
1336 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001337 p += len;
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001340 }
1341 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1343#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1344 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001345 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001346 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001347 size_t zlen;
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001350 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001351 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001354 return( ret );
1355 }
1356
1357 *(p++) = (unsigned char)( zlen >> 8 );
1358 *(p++) = (unsigned char)( zlen );
1359 p += zlen;
1360
Janos Follath3fbdada2018-08-15 10:26:53 +01001361 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1362 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001363 }
1364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001369 }
1370
1371 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001372 if( end - p < 2 )
1373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001374
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001375 *(p++) = (unsigned char)( psk_len >> 8 );
1376 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001377
1378 if( end < p || (size_t)( end - p ) < psk_len )
1379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1380
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001381 memcpy( p, psk, psk_len );
1382 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001383
1384 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1385
1386 return( 0 );
1387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001391/*
1392 * SSLv3.0 MAC functions
1393 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001394#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001395static void ssl_mac( mbedtls_md_context_t *md_ctx,
1396 const unsigned char *secret,
1397 const unsigned char *buf, size_t len,
1398 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001399 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400{
1401 unsigned char header[11];
1402 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001403 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1405 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001406
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001407 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001409 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001410 else
Paul Bakker68884e32013-01-07 18:20:04 +01001411 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
1413 memcpy( header, ctr, 8 );
1414 header[ 8] = (unsigned char) type;
1415 header[ 9] = (unsigned char)( len >> 8 );
1416 header[10] = (unsigned char)( len );
1417
Paul Bakker68884e32013-01-07 18:20:04 +01001418 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_md_starts( md_ctx );
1420 mbedtls_md_update( md_ctx, secret, md_size );
1421 mbedtls_md_update( md_ctx, padding, padlen );
1422 mbedtls_md_update( md_ctx, header, 11 );
1423 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001424 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Paul Bakker68884e32013-01-07 18:20:04 +01001426 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_md_starts( md_ctx );
1428 mbedtls_md_update( md_ctx, secret, md_size );
1429 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001430 mbedtls_md_update( md_ctx, out, md_size );
1431 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001436 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001437#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001438#endif
1439
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001440/* The function below is only used in the Lucky 13 counter-measure in
1441 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1442#if defined(SSL_SOME_MODES_USE_MAC) && \
1443 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1444 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1445 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1446/* This function makes sure every byte in the memory region is accessed
1447 * (in ascending addresses order) */
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001448static void ssl_read_memory( const unsigned char *p, size_t len )
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001449{
1450 unsigned char acc = 0;
1451 volatile unsigned char force;
1452
1453 for( ; len != 0; p++, len-- )
1454 acc ^= *p;
1455
1456 force = acc;
1457 (void) force;
1458}
1459#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1460
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001461/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001467 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001471 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001475 }
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001480 ssl->out_msg, ssl->out_msglen );
1481
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001483 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001485#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 if( mode == MBEDTLS_MODE_STREAM ||
1487 ( mode == MBEDTLS_MODE_CBC
1488#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1489 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001490#endif
1491 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_SSL_PROTO_SSL3)
1494 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001495 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001496 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001497
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001498 ssl_mac( &ssl->transform_out->md_ctx_enc,
1499 ssl->transform_out->mac_enc,
1500 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001501 ssl->out_ctr, ssl->out_msgtype,
1502 mac );
1503
1504 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001505 }
1506 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1509 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1510 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001511 {
Hanno Becker992b6872017-11-09 18:57:39 +00001512 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1515 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1516 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1517 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001518 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001519 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001521
1522 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001523 }
1524 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001525#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001529 }
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001532 ssl->out_msg + ssl->out_msglen,
1533 ssl->transform_out->maclen );
1534
1535 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001536 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001537 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001538#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001540 /*
1541 * Encrypt
1542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1544 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001546 int ret;
1547 size_t olen = 0;
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 "including %d bytes of padding",
1551 ssl->out_msglen, 0 ) );
1552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001554 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001555 ssl->transform_out->ivlen,
1556 ssl->out_msg, ssl->out_msglen,
1557 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001560 return( ret );
1561 }
1562
1563 if( ssl->out_msglen != olen )
1564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 }
Paul Bakker68884e32013-01-07 18:20:04 +01001569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001571#if defined(MBEDTLS_GCM_C) || \
1572 defined(MBEDTLS_CCM_C) || \
1573 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001575 mode == MBEDTLS_MODE_CCM ||
1576 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001577 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001578 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001579 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001580 unsigned char *enc_msg;
1581 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001582 unsigned char iv[12];
1583 mbedtls_ssl_transform *transform = ssl->transform_out;
1584 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001587
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001588 /*
1589 * Prepare additional authenticated data
1590 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001591 memcpy( add_data, ssl->out_ctr, 8 );
1592 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001594 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001595 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1596 add_data[12] = ssl->out_msglen & 0xFF;
1597
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001598 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001599
Paul Bakker68884e32013-01-07 18:20:04 +01001600 /*
1601 * Generate IV
1602 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001603 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1604 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001605 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001606 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1607 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1608 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1609
1610 }
1611 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1612 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001613 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001614 unsigned char i;
1615
1616 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1617
1618 for( i = 0; i < 8; i++ )
1619 iv[i+4] ^= ssl->out_ctr[i];
1620 }
1621 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001622 {
1623 /* Reminder if we ever add an AEAD mode with a different size */
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é-Gonnardd056ce02014-10-29 22:29:20 +01001626 }
1627
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001628 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1629 iv, transform->ivlen );
1630 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1631 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001632
Paul Bakker68884e32013-01-07 18:20:04 +01001633 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001634 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001635 */
1636 enc_msg = ssl->out_msg;
1637 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001638 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001641 "including 0 bytes of padding",
1642 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001643
Paul Bakker68884e32013-01-07 18:20:04 +01001644 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001645 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001646 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001647 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1648 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001649 add_data, 13,
1650 enc_msg, enc_msglen,
1651 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001652 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001655 return( ret );
1656 }
1657
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001658 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001662 }
1663
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001664 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001665 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001671#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001674 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001675 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001676 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001677
Paul Bakker48916f92012-09-16 19:57:18 +00001678 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1679 ssl->transform_out->ivlen;
1680 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 padlen = 0;
1682
1683 for( i = 0; i <= padlen; i++ )
1684 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1685
1686 ssl->out_msglen += padlen + 1;
1687
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001688 enc_msglen = ssl->out_msglen;
1689 enc_msg = ssl->out_msg;
1690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001692 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001693 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1694 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001697 {
1698 /*
1699 * Generate IV
1700 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001701 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001702 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001703 if( ret != 0 )
1704 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001705
Paul Bakker92be97b2013-01-02 17:30:03 +01001706 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001707 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001708
1709 /*
1710 * Fix pointer positions and message length with added IV
1711 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001712 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001713 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001714 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001719 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001720 ssl->out_msglen, ssl->transform_out->ivlen,
1721 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001724 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001725 ssl->transform_out->ivlen,
1726 enc_msg, enc_msglen,
1727 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001730 return( ret );
1731 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001732
Paul Bakkercca5b812013-08-31 17:40:26 +02001733 if( enc_msglen != olen )
1734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1740 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001741 {
1742 /*
1743 * Save IV in SSL3 and TLS1
1744 */
1745 memcpy( ssl->transform_out->iv_enc,
1746 ssl->transform_out->cipher_ctx_enc.iv,
1747 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001749#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001752 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001753 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001754 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1755
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001756 /*
1757 * MAC(MAC_write_key, seq_num +
1758 * TLSCipherText.type +
1759 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001760 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001761 * IV + // except for TLS 1.0
1762 * ENC(content + padding + padding_length));
1763 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001764 unsigned char pseudo_hdr[13];
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001767
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1769 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001770 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1771 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1776 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001778 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001780
Hanno Becker3d8c9072018-01-05 16:24:22 +00001781 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1782 ssl->transform_out->maclen );
1783
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001784 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001789 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001790#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001796 /* Make extra sure authentication was performed, exactly once */
1797 if( auth_done != 1 )
1798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001801 }
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
1805 return( 0 );
1806}
1807
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001808#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001809/*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001810 * Constant-flow conditional memcpy:
1811 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1812 * - otherwise, a no-op,
1813 * but with execution flow independent of the values of c1 and c2.
1814 *
1815 * Use only bit operations to avoid branches that could be used by some
1816 * compilers on some platforms to translate comparison operators.
1817 */
Manuel Pégourié-Gonnard94fd8dc2020-07-28 11:56:05 +02001818static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1819 const unsigned char *src,
1820 size_t len,
1821 size_t c1, size_t c2 )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001822{
1823 /* diff = 0 if c1 == c2, non-zero otherwise */
1824 const size_t diff = c1 ^ c2;
1825
1826 /* MSVC has a warning about unary minus on unsigned integer types,
1827 * but this is well-defined and precisely what we want to do here. */
1828#if defined(_MSC_VER)
1829#pragma warning( push )
1830#pragma warning( disable : 4146 )
1831#endif
1832
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001833 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001834 const size_t diff_msb = ( diff | -diff );
1835
1836 /* diff1 = c1 != c2 */
1837 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1838
1839 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001840 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001841
1842#if defined(_MSC_VER)
1843#pragma warning( pop )
1844#endif
1845
1846 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
1847 for( size_t i = 0; i < len; i++ )
1848 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1849}
1850
1851/*
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001852 * Compute HMAC of variable-length data with constant flow.
1853 */
1854int mbedtls_ssl_cf_hmac(
1855 mbedtls_md_context_t *ctx,
1856 const unsigned char *add_data, size_t add_data_len,
1857 const unsigned char *data, size_t data_len_secret,
1858 size_t min_data_len, size_t max_data_len,
1859 unsigned char *output )
1860{
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001861 /*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001862 * This function breaks the HMAC abstraction and uses the md_clone()
1863 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001864 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001865 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001866 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001867 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001868 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001869 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1870 * minlen, then cloning the context, and for each byte up to maxlen
1871 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001872 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001873 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001874 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001875 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001876 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1877 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001878 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnarda6c13172020-07-28 11:45:02 +02001879 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001880 const unsigned char * const okey = ikey + block_size;
1881 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001882
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001883 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1884 mbedtls_md_context_t aux;
1885 size_t offset;
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001886 int ret;
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001887
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001888 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001889
1890#define MD_CHK( func_call ) \
1891 do { \
1892 ret = (func_call); \
1893 if( ret != 0 ) \
1894 goto cleanup; \
1895 } while( 0 )
1896
1897 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001898
1899 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1900 * so we can start directly with the message */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001901 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1902 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001903
1904 /* For each possible length, compute the hash up to that point */
1905 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001906 {
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001907 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1908 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001909 /* Keep only the correct inner_hash in the output buffer */
1910 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1911 offset, data_len_secret );
1912
1913 if( offset < max_data_len )
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001914 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001915 }
1916
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001917 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001918 MD_CHK( mbedtls_md_starts( ctx ) );
1919 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1920 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1921 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001922
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001923 /* Done, get ready for next time */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001924 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001925
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001926#undef MD_CHK
1927
1928cleanup:
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001929 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001930 return( ret );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001931}
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001932#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001935{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001937 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001938#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001939 size_t padlen = 0, correct = 1;
1940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001944 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001948 }
1949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001951
Paul Bakker48916f92012-09-16 19:57:18 +00001952 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001955 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 }
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1960 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001961 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001962 int ret;
1963 size_t olen = 0;
1964
Paul Bakker68884e32013-01-07 18:20:04 +01001965 padlen = 0;
1966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001968 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001969 ssl->transform_in->ivlen,
1970 ssl->in_msg, ssl->in_msglen,
1971 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001974 return( ret );
1975 }
1976
1977 if( ssl->in_msglen != olen )
1978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 }
Paul Bakker68884e32013-01-07 18:20:04 +01001983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001985#if defined(MBEDTLS_GCM_C) || \
1986 defined(MBEDTLS_CCM_C) || \
1987 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001989 mode == MBEDTLS_MODE_CCM ||
1990 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001991 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001992 int ret;
1993 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001994 unsigned char *dec_msg;
1995 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001996 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001997 unsigned char iv[12];
1998 mbedtls_ssl_transform *transform = ssl->transform_in;
1999 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002001 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002002
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002003 /*
2004 * Compute and update sizes
2005 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002006 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002009 "+ taglen (%d)", ssl->in_msglen,
2010 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002012 }
2013 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2014
Paul Bakker68884e32013-01-07 18:20:04 +01002015 dec_msg = ssl->in_msg;
2016 dec_msg_result = ssl->in_msg;
2017 ssl->in_msglen = dec_msglen;
2018
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002019 /*
2020 * Prepare additional authenticated data
2021 */
Paul Bakker68884e32013-01-07 18:20:04 +01002022 memcpy( add_data, ssl->in_ctr, 8 );
2023 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002025 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002026 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2027 add_data[12] = ssl->in_msglen & 0xFF;
2028
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002029 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002030
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002031 /*
2032 * Prepare IV
2033 */
2034 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2035 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002036 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002037 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2038 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002039
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 }
2041 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2042 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002043 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002044 unsigned char i;
2045
2046 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2047
2048 for( i = 0; i < 8; i++ )
2049 iv[i+4] ^= ssl->in_ctr[i];
2050 }
2051 else
2052 {
2053 /* Reminder if we ever add an AEAD mode with a different size */
2054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2056 }
2057
2058 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002060
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002061 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002062 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002065 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002066 add_data, 13,
2067 dec_msg, dec_msglen,
2068 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002069 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2074 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002075
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002076 return( ret );
2077 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002078 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002079
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002080 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002084 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002088#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 {
Paul Bakker45829992013-01-03 14:52:21 +01002091 /*
2092 * Decrypt and check the padding
2093 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002094 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002095 unsigned char *dec_msg;
2096 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002097 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002098 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002099 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002100
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 /*
Paul Bakker45829992013-01-03 14:52:21 +01002102 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2105 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002106 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002107#endif
Paul Bakker45829992013-01-03 14:52:21 +01002108
2109 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2110 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002113 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2114 ssl->transform_in->ivlen,
2115 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002117 }
2118
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002119 dec_msglen = ssl->in_msglen;
2120 dec_msg = ssl->in_msg;
2121 dec_msg_result = ssl->in_msg;
2122
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002123 /*
2124 * Authenticate before decrypt if enabled
2125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2127 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002128 {
Hanno Becker992b6872017-11-09 18:57:39 +00002129 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002130 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002134 dec_msglen -= ssl->transform_in->maclen;
2135 ssl->in_msglen -= ssl->transform_in->maclen;
2136
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002137 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2138 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2139 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2140 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2145 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002146 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002147 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002151 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002152 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002153 ssl->transform_in->maclen );
2154
Hanno Becker992b6872017-11-09 18:57:39 +00002155 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2156 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002161 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002162 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002165
2166 /*
2167 * Check length sanity
2168 */
2169 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002172 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002174 }
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002177 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002178 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002181 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002182 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002183 dec_msglen -= ssl->transform_in->ivlen;
2184 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002185
Paul Bakker48916f92012-09-16 19:57:18 +00002186 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002187 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002192 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002193 ssl->transform_in->ivlen,
2194 dec_msg, dec_msglen,
2195 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002198 return( ret );
2199 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002200
Paul Bakkercca5b812013-08-31 17:40:26 +02002201 if( dec_msglen != olen )
2202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002205 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2208 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002209 {
2210 /*
2211 * Save IV in SSL3 and TLS1
2212 */
2213 memcpy( ssl->transform_in->iv_dec,
2214 ssl->transform_in->cipher_ctx_dec.iv,
2215 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002217#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
2219 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002220
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002222 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224#if defined(MBEDTLS_SSL_DEBUG_ALL)
2225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002226 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002227#endif
Paul Bakker45829992013-01-03 14:52:21 +01002228 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002229 correct = 0;
2230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232#if defined(MBEDTLS_SSL_PROTO_SSL3)
2233 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 {
Paul Bakker48916f92012-09-16 19:57:18 +00002235 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#if defined(MBEDTLS_SSL_DEBUG_ALL)
2238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002240 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002241#endif
Paul Bakker45829992013-01-03 14:52:21 +01002242 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244 }
2245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2247#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2248 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2249 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 {
2251 /*
Paul Bakker45829992013-01-03 14:52:21 +01002252 * TLSv1+: always check the padding up to the first failure
2253 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002255 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002256 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002257 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002258
Paul Bakker956c9e02013-12-19 14:42:28 +01002259 /*
2260 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002261 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002262 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002263 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002264 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002265 *
2266 * In both cases we reset padding_idx to a safe value (0) to
2267 * prevent out-of-buffer reads.
2268 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002269 correct &= ( padlen <= ssl->in_msglen );
2270 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002271 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002272
2273 padding_idx *= correct;
2274
Angus Grattonb512bc12018-06-19 15:57:50 +10002275 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002276 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002277 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002278 pad_count += real_count *
2279 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2280 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002281
2282 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002285 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002287#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002288 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002290 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2292 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2295 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002296 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002297
2298 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002300 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002301#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002306
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002307#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002311
2312 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002313 * Authenticate if not done yet.
2314 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002315 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002316#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002317 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002318 {
Hanno Becker992b6872017-11-09 18:57:39 +00002319 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002320
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002321 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002323 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2324 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_SSL_PROTO_SSL3)
2327 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002328 {
2329 ssl_mac( &ssl->transform_in->md_ctx_dec,
2330 ssl->transform_in->mac_dec,
2331 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002332 ssl->in_ctr, ssl->in_msgtype,
2333 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002334 }
2335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2337#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2338 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2339 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002340 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002341 int ret;
2342 unsigned char add_data[13];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002343
2344 /*
2345 * The next two sizes are the minimum and maximum values of
2346 * in_msglen over all padlen values.
2347 *
2348 * They're independent of padlen, since we previously did
2349 * in_msglen -= padlen.
2350 *
2351 * Note that max_len + maclen is never more than the buffer
2352 * length, as we previously did in_msglen -= maclen too.
2353 */
2354 const size_t max_len = ssl->in_msglen + padlen;
2355 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2356
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002357 memcpy( add_data + 0, ssl->in_ctr, 8 );
2358 memcpy( add_data + 8, ssl->in_hdr, 3 );
2359 memcpy( add_data + 11, ssl->in_len, 2 );
2360
2361 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2362 add_data, sizeof( add_data ),
2363 ssl->in_msg, ssl->in_msglen,
2364 min_len, max_len,
2365 mac_expect );
2366 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02002367 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2369 return( ret );
Gilles Peskine20b44082018-05-29 14:06:49 +02002370 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002371
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002372 /* Make sure we access all the memory that could contain the MAC,
2373 * before we check it in the next code block. This makes the
2374 * synchronisation requirements for just-in-time Prime+Probe
2375 * attacks much tighter and hopefully impractical. */
2376 ssl_read_memory( ssl->in_msg + min_len,
2377 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002378 }
2379 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2381 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2384 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002387#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002388 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2389 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2390 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002391#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Hanno Becker992b6872017-11-09 18:57:39 +00002393 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2394 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396#if defined(MBEDTLS_SSL_DEBUG_ALL)
2397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002398#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002399 correct = 0;
2400 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002401 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002402 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002403
2404 /*
2405 * Finally check the correct flag
2406 */
2407 if( correct == 0 )
2408 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002409#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002410
2411 /* Make extra sure authentication was performed, exactly once */
2412 if( auth_done != 1 )
2413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
2418 if( ssl->in_msglen == 0 )
2419 {
Angus Gratton34817922018-06-19 15:58:22 +10002420#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2421 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2422 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2423 {
2424 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2426 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2427 }
2428#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 ssl->nb_zero++;
2431
2432 /*
2433 * Three or more empty messages may be a DoS attack
2434 * (excessive CPU consumption).
2435 */
2436 if( ssl->nb_zero > 3 )
2437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 }
2442 }
2443 else
2444 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002447 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002448 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002449 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002450 }
2451 else
2452#endif
2453 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002454 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002455 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2456 if( ++ssl->in_ctr[i - 1] != 0 )
2457 break;
2458
2459 /* The loop goes to its end iff the counter is wrapping */
2460 if( i == ssl_ep_len( ssl ) )
2461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2463 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002464 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002465 }
2466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 return( 0 );
2470}
2471
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002472#undef MAC_NONE
2473#undef MAC_PLAINTEXT
2474#undef MAC_CIPHERTEXT
2475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002477/*
2478 * Compression/decompression functions
2479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002481{
2482 int ret;
2483 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002484 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002485 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002486 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002489
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002490 if( len_pre == 0 )
2491 return( 0 );
2492
Paul Bakker2770fbd2012-07-03 13:30:23 +00002493 memcpy( msg_pre, ssl->out_msg, len_pre );
2494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002496 ssl->out_msglen ) );
2497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002499 ssl->out_msg, ssl->out_msglen );
2500
Paul Bakker48916f92012-09-16 19:57:18 +00002501 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2502 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2503 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002504 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002505
Paul Bakker48916f92012-09-16 19:57:18 +00002506 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002507 if( ret != Z_OK )
2508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2510 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002511 }
2512
Angus Grattond8213d02016-05-25 20:56:48 +10002513 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002514 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002517 ssl->out_msglen ) );
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002520 ssl->out_msg, ssl->out_msglen );
2521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002523
2524 return( 0 );
2525}
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002528{
2529 int ret;
2530 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002531 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002532 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002533 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002536
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002537 if( len_pre == 0 )
2538 return( 0 );
2539
Paul Bakker2770fbd2012-07-03 13:30:23 +00002540 memcpy( msg_pre, ssl->in_msg, len_pre );
2541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002543 ssl->in_msglen ) );
2544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002546 ssl->in_msg, ssl->in_msglen );
2547
Paul Bakker48916f92012-09-16 19:57:18 +00002548 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2549 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2550 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002551 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002552 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002553
Paul Bakker48916f92012-09-16 19:57:18 +00002554 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002555 if( ret != Z_OK )
2556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2558 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002559 }
2560
Angus Grattond8213d02016-05-25 20:56:48 +10002561 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002562 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002565 ssl->in_msglen ) );
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002568 ssl->in_msg, ssl->in_msglen );
2569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002571
2572 return( 0 );
2573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2577static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_SSL_PROTO_DTLS)
2580static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002581{
2582 /* If renegotiation is not enforced, retransmit until we would reach max
2583 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002584 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002585 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002586 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002587 unsigned char doublings = 1;
2588
2589 while( ratio != 0 )
2590 {
2591 ++doublings;
2592 ratio >>= 1;
2593 }
2594
2595 if( ++ssl->renego_records_seen > doublings )
2596 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002598 return( 0 );
2599 }
2600 }
2601
2602 return( ssl_write_hello_request( ssl ) );
2603}
2604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002606
Paul Bakker5121ce52009-01-03 21:22:43 +00002607/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002608 * Fill the input message buffer by appending data to it.
2609 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002610 *
2611 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2612 * available (from this read and/or a previous one). Otherwise, an error code
2613 * is returned (possibly EOF or WANT_READ).
2614 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002615 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2616 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2617 * since we always read a whole datagram at once.
2618 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002619 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002620 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002623{
Paul Bakker23986e52011-04-24 08:57:21 +00002624 int ret;
2625 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002629 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002632 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002634 }
2635
Angus Grattond8213d02016-05-25 20:56:48 +10002636 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002640 }
2641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002643 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002645 uint32_t timeout;
2646
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002647 /* Just to be sure */
2648 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2649 {
2650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2651 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2653 }
2654
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002655 /*
2656 * The point is, we need to always read a full datagram at once, so we
2657 * sometimes read more then requested, and handle the additional data.
2658 * It could be the rest of the current record (while fetching the
2659 * header) and/or some other records in the same datagram.
2660 */
2661
2662 /*
2663 * Move to the next record in the already read datagram if applicable
2664 */
2665 if( ssl->next_record_offset != 0 )
2666 {
2667 if( ssl->in_left < ssl->next_record_offset )
2668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002671 }
2672
2673 ssl->in_left -= ssl->next_record_offset;
2674
2675 if( ssl->in_left != 0 )
2676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002678 ssl->next_record_offset ) );
2679 memmove( ssl->in_hdr,
2680 ssl->in_hdr + ssl->next_record_offset,
2681 ssl->in_left );
2682 }
2683
2684 ssl->next_record_offset = 0;
2685 }
2686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002689
2690 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002691 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002692 */
2693 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002696 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002697 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002698
2699 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002700 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002701 * are not at the beginning of a new record, the caller did something
2702 * wrong.
2703 */
2704 if( ssl->in_left != 0 )
2705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002708 }
2709
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002710 /*
2711 * Don't even try to read if time's out already.
2712 * This avoids by-passing the timer when repeatedly receiving messages
2713 * that will end up being dropped.
2714 */
2715 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002716 {
2717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002718 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002719 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002720 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002721 {
Angus Grattond8213d02016-05-25 20:56:48 +10002722 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002725 timeout = ssl->handshake->retransmit_timeout;
2726 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002727 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002730
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002731 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002732 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2733 timeout );
2734 else
2735 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002738
2739 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002741 }
2742
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002743 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002746 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002749 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002750 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002753 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002754 }
2755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002759 return( ret );
2760 }
2761
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002762 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002765 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002767 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002768 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002771 return( ret );
2772 }
2773
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002774 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002777 }
2778
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 if( ret < 0 )
2780 return( ret );
2781
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002782 ssl->in_left = ret;
2783 }
2784 else
2785#endif
2786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002788 ssl->in_left, nb_want ) );
2789
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002790 while( ssl->in_left < nb_want )
2791 {
2792 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002793
2794 if( ssl_check_timer( ssl ) != 0 )
2795 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2796 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002797 {
2798 if( ssl->f_recv_timeout != NULL )
2799 {
2800 ret = ssl->f_recv_timeout( ssl->p_bio,
2801 ssl->in_hdr + ssl->in_left, len,
2802 ssl->conf->read_timeout );
2803 }
2804 else
2805 {
2806 ret = ssl->f_recv( ssl->p_bio,
2807 ssl->in_hdr + ssl->in_left, len );
2808 }
2809 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002812 ssl->in_left, nb_want ) );
2813 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002814
2815 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002817
2818 if( ret < 0 )
2819 return( ret );
2820
mohammad160352aecb92018-03-28 23:41:40 -07002821 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002822 {
Darryl Green11999bb2018-03-13 15:22:58 +00002823 MBEDTLS_SSL_DEBUG_MSG( 1,
2824 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002825 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2827 }
2828
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002829 ssl->in_left += ret;
2830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002831 }
2832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
2835 return( 0 );
2836}
2837
2838/*
2839 * Flush any data not yet written
2840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002843 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002844 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002848 if( ssl->f_send == NULL )
2849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002851 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002853 }
2854
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002855 /* Avoid incrementing counter if data is flushed */
2856 if( ssl->out_left == 0 )
2857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002859 return( 0 );
2860 }
2861
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 while( ssl->out_left > 0 )
2863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2865 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Hanno Becker2b1e3542018-08-06 11:19:13 +01002867 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002868 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
2872 if( ret <= 0 )
2873 return( ret );
2874
mohammad160352aecb92018-03-28 23:41:40 -07002875 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002876 {
Darryl Green11999bb2018-03-13 15:22:58 +00002877 MBEDTLS_SSL_DEBUG_MSG( 1,
2878 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002879 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2881 }
2882
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 ssl->out_left -= ret;
2884 }
2885
Hanno Becker2b1e3542018-08-06 11:19:13 +01002886#if defined(MBEDTLS_SSL_PROTO_DTLS)
2887 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002888 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002889 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002890 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002891 else
2892#endif
2893 {
2894 ssl->out_hdr = ssl->out_buf + 8;
2895 }
2896 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
2900 return( 0 );
2901}
2902
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002903/*
2904 * Functions to handle the DTLS retransmission state machine
2905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002907/*
2908 * Append current handshake message to current outgoing flight
2909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2914 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2915 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002916
2917 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002918 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002919 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002922 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002923 }
2924
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002925 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002926 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002929 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002930 }
2931
2932 /* Copy current handshake message with headers */
2933 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2934 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002935 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002936 msg->next = NULL;
2937
2938 /* Append to the current flight */
2939 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002940 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002941 else
2942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002944 while( cur->next != NULL )
2945 cur = cur->next;
2946 cur->next = msg;
2947 }
2948
Hanno Becker3b235902018-08-06 09:54:53 +01002949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002950 return( 0 );
2951}
2952
2953/*
2954 * Free the current flight of handshake messages
2955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 mbedtls_ssl_flight_item *cur = flight;
2959 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002960
2961 while( cur != NULL )
2962 {
2963 next = cur->next;
2964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 mbedtls_free( cur->p );
2966 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002967
2968 cur = next;
2969 }
2970}
2971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2973static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002974#endif
2975
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002976/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002977 * Swap transform_out and out_ctr with the alternative ones
2978 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002979static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002980{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002982 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002983#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2984 int ret;
2985#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002986
2987 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002990 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002991 }
2992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002994
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002995 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002996 tmp_transform = ssl->transform_out;
2997 ssl->transform_out = ssl->handshake->alt_transform_out;
2998 ssl->handshake->alt_transform_out = tmp_transform;
2999
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003000 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003001 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3002 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003003 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003004
3005 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003006 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3009 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3014 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003015 }
3016 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003017#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
3018
3019 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003020}
3021
3022/*
3023 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003024 */
3025int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3026{
3027 int ret = 0;
3028
3029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3030
3031 ret = mbedtls_ssl_flight_transmit( ssl );
3032
3033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3034
3035 return( ret );
3036}
3037
3038/*
3039 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003040 *
3041 * Need to remember the current message in case flush_output returns
3042 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003043 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003044 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003045int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003046{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003047 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003051 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003053
3054 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003055 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003056 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3057 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003060 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061
3062 while( ssl->handshake->cur_msg != NULL )
3063 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003064 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003065 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003066
Hanno Beckere1dcb032018-08-17 16:47:58 +01003067 int const is_finished =
3068 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3069 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3070
Hanno Becker04da1892018-08-14 13:22:10 +01003071 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3072 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3073
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003074 /* Swap epochs before sending Finished: we can't do it after
3075 * sending ChangeCipherSpec, in case write returns WANT_READ.
3076 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003077 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003078 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003080 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3081 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003082 }
3083
Hanno Becker67bc7c32018-08-06 11:33:50 +01003084 ret = ssl_get_remaining_payload_in_datagram( ssl );
3085 if( ret < 0 )
3086 return( ret );
3087 max_frag_len = (size_t) ret;
3088
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003089 /* CCS is copied as is, while HS messages may need fragmentation */
3090 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3091 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003092 if( max_frag_len == 0 )
3093 {
3094 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3095 return( ret );
3096
3097 continue;
3098 }
3099
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003100 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003101 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003102 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003103
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003104 /* Update position inside current message */
3105 ssl->handshake->cur_msg_p += cur->len;
3106 }
3107 else
3108 {
3109 const unsigned char * const p = ssl->handshake->cur_msg_p;
3110 const size_t hs_len = cur->len - 12;
3111 const size_t frag_off = p - ( cur->p + 12 );
3112 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003113 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003114
Hanno Beckere1dcb032018-08-17 16:47:58 +01003115 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003116 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003117 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003118 {
3119 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3120 return( ret );
3121 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003122
Hanno Becker67bc7c32018-08-06 11:33:50 +01003123 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3124 return( ret );
3125
3126 continue;
3127 }
3128 max_hs_frag_len = max_frag_len - 12;
3129
3130 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3131 max_hs_frag_len : rem_len;
3132
3133 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003134 {
3135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003136 (unsigned) cur_hs_frag_len,
3137 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003138 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003139
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003140 /* Messages are stored with handshake headers as if not fragmented,
3141 * copy beginning of headers then fill fragmentation fields.
3142 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3143 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003144
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003145 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3146 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3147 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3148
Hanno Becker67bc7c32018-08-06 11:33:50 +01003149 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3150 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3151 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003152
3153 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3154
Hanno Becker3f7b9732018-08-28 09:53:25 +01003155 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003156 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3157 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003158 ssl->out_msgtype = cur->type;
3159
3160 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003161 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003162 }
3163
3164 /* If done with the current message move to the next one if any */
3165 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3166 {
3167 if( cur->next != NULL )
3168 {
3169 ssl->handshake->cur_msg = cur->next;
3170 ssl->handshake->cur_msg_p = cur->next->p + 12;
3171 }
3172 else
3173 {
3174 ssl->handshake->cur_msg = NULL;
3175 ssl->handshake->cur_msg_p = NULL;
3176 }
3177 }
3178
3179 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003180 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003183 return( ret );
3184 }
3185 }
3186
Hanno Becker67bc7c32018-08-06 11:33:50 +01003187 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3188 return( ret );
3189
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003190 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3192 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003193 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003196 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3197 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003198
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200
3201 return( 0 );
3202}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003203
3204/*
3205 * To be called when the last message of an incoming flight is received.
3206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003208{
3209 /* We won't need to resend that one any more */
3210 ssl_flight_free( ssl->handshake->flight );
3211 ssl->handshake->flight = NULL;
3212 ssl->handshake->cur_msg = NULL;
3213
3214 /* The next incoming flight will start with this msg_seq */
3215 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3216
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003217 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003218 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003219
Hanno Becker0271f962018-08-16 13:23:47 +01003220 /* Clear future message buffering structure. */
3221 ssl_buffering_free( ssl );
3222
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003223 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003224 ssl_set_timer( ssl, 0 );
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3227 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003230 }
3231 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003233}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003234
3235/*
3236 * To be called when the last message of an outgoing flight is send.
3237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003239{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003240 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003241 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3244 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003247 }
3248 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252
Paul Bakker5121ce52009-01-03 21:22:43 +00003253/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003254 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003256
3257/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003258 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003259 *
3260 * - fill in handshake headers
3261 * - update handshake checksum
3262 * - DTLS: save message for resending
3263 * - then pass to the record layer
3264 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003265 * DTLS: except for HelloRequest, messages are only queued, and will only be
3266 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003267 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003268 * Inputs:
3269 * - ssl->out_msglen: 4 + actual handshake message len
3270 * (4 is the size of handshake headers for TLS)
3271 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3272 * - ssl->out_msg + 4: the handshake message body
3273 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003274 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003275 * - ssl->out_msglen: the length of the record contents
3276 * (including handshake headers but excluding record headers)
3277 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003278 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003279int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003280{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003281 int ret;
3282 const size_t hs_len = ssl->out_msglen - 4;
3283 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3286
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003287 /*
3288 * Sanity checks
3289 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003290 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003291 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3292 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003293 /* In SSLv3, the client might send a NoCertificate alert. */
3294#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3295 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3296 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3297 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3298#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3299 {
3300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3302 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003303 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003305 /* Whenever we send anything different from a
3306 * HelloRequest we should be in a handshake - double check. */
3307 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3308 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003309 ssl->handshake == NULL )
3310 {
3311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3312 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003316 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003317 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003322 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003324
Hanno Beckerb50a2532018-08-06 11:52:54 +01003325 /* Double-check that we did not exceed the bounds
3326 * of the outgoing record buffer.
3327 * This should never fail as the various message
3328 * writing functions must obey the bounds of the
3329 * outgoing record buffer, but better be safe.
3330 *
3331 * Note: We deliberately do not check for the MTU or MFL here.
3332 */
3333 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3334 {
3335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3336 "size %u, maximum %u",
3337 (unsigned) ssl->out_msglen,
3338 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3340 }
3341
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003342 /*
3343 * Fill handshake headers
3344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003347 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3348 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3349 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003350
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003351 /*
3352 * DTLS has additional fields in the Handshake layer,
3353 * between the length field and the actual payload:
3354 * uint16 message_seq;
3355 * uint24 fragment_offset;
3356 * uint24 fragment_length;
3357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003360 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003361 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003362 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003363 {
3364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3365 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003366 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003367 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3369 }
3370
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003371 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003372 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003373
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003374 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003375 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003376 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003377 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3378 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3379 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003380 }
3381 else
3382 {
3383 ssl->out_msg[4] = 0;
3384 ssl->out_msg[5] = 0;
3385 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003386
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003387 /* Handshake hashes are computed without fragmentation,
3388 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003389 memset( ssl->out_msg + 6, 0x00, 3 );
3390 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003391 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003393
Hanno Becker0207e532018-08-28 10:28:28 +01003394 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003395 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3396 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397 }
3398
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003399 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003401 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003402 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3403 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003404 {
3405 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003408 return( ret );
3409 }
3410 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003411 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003412#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003413 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003414 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003415 {
3416 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3417 return( ret );
3418 }
3419 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003420
3421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3422
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003423 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003424}
3425
3426/*
3427 * Record layer functions
3428 */
3429
3430/*
3431 * Write current record.
3432 *
3433 * Uses:
3434 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3435 * - ssl->out_msglen: length of the record content (excl headers)
3436 * - ssl->out_msg: record content
3437 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003438int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003439{
3440 int ret, done = 0;
3441 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003442 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003443
3444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003447 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003449 {
3450 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003453 return( ret );
3454 }
3455
3456 len = ssl->out_msglen;
3457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3461 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 ret = mbedtls_ssl_hw_record_write( ssl );
3466 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3469 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003470 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003471
3472 if( ret == 0 )
3473 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003476 if( !done )
3477 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003478 unsigned i;
3479 size_t protected_record_size;
3480
Paul Bakker05ef8352012-05-08 09:17:57 +00003481 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003483 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003484
Hanno Becker19859472018-08-06 09:40:20 +01003485 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003486 ssl->out_len[0] = (unsigned char)( len >> 8 );
3487 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003488
Paul Bakker48916f92012-09-16 19:57:18 +00003489 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003490 {
3491 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003494 return( ret );
3495 }
3496
3497 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003498 ssl->out_len[0] = (unsigned char)( len >> 8 );
3499 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003500 }
3501
Hanno Becker2b1e3542018-08-06 11:19:13 +01003502 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3503
3504#if defined(MBEDTLS_SSL_PROTO_DTLS)
3505 /* In case of DTLS, double-check that we don't exceed
3506 * the remaining space in the datagram. */
3507 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3508 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003509 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003510 if( ret < 0 )
3511 return( ret );
3512
3513 if( protected_record_size > (size_t) ret )
3514 {
3515 /* Should never happen */
3516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3517 }
3518 }
3519#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003522 "version = [%d:%d], msglen = %d",
3523 ssl->out_hdr[0], ssl->out_hdr[1],
3524 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003527 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003528
3529 ssl->out_left += protected_record_size;
3530 ssl->out_hdr += protected_record_size;
3531 ssl_update_out_pointers( ssl, ssl->transform_out );
3532
Hanno Becker04484622018-08-06 09:49:38 +01003533 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3534 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3535 break;
3536
3537 /* The loop goes to its end iff the counter is wrapping */
3538 if( i == ssl_ep_len( ssl ) )
3539 {
3540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3541 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 }
3544
Hanno Becker67bc7c32018-08-06 11:33:50 +01003545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003546 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3547 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003548 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003549 size_t remaining;
3550 ret = ssl_get_remaining_payload_in_datagram( ssl );
3551 if( ret < 0 )
3552 {
3553 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3554 ret );
3555 return( ret );
3556 }
3557
3558 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003560 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003561 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003562 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003563 else
3564 {
Hanno Becker513815a2018-08-20 11:56:09 +01003565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003566 }
3567 }
3568#endif /* MBEDTLS_SSL_PROTO_DTLS */
3569
3570 if( ( flush == SSL_FORCE_FLUSH ) &&
3571 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 return( ret );
3575 }
3576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003578
3579 return( 0 );
3580}
3581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003583
3584static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3585{
3586 if( ssl->in_msglen < ssl->in_hslen ||
3587 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3588 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3589 {
3590 return( 1 );
3591 }
3592 return( 0 );
3593}
Hanno Becker44650b72018-08-16 12:51:11 +01003594
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003595static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003596{
3597 return( ( ssl->in_msg[9] << 16 ) |
3598 ( ssl->in_msg[10] << 8 ) |
3599 ssl->in_msg[11] );
3600}
3601
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003602static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003603{
3604 return( ( ssl->in_msg[6] << 16 ) |
3605 ( ssl->in_msg[7] << 8 ) |
3606 ssl->in_msg[8] );
3607}
3608
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003609static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003610{
3611 uint32_t msg_len, frag_off, frag_len;
3612
3613 msg_len = ssl_get_hs_total_len( ssl );
3614 frag_off = ssl_get_hs_frag_off( ssl );
3615 frag_len = ssl_get_hs_frag_len( ssl );
3616
3617 if( frag_off > msg_len )
3618 return( -1 );
3619
3620 if( frag_len > msg_len - frag_off )
3621 return( -1 );
3622
3623 if( frag_len + 12 > ssl->in_msglen )
3624 return( -1 );
3625
3626 return( 0 );
3627}
3628
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003629/*
3630 * Mark bits in bitmask (used for DTLS HS reassembly)
3631 */
3632static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3633{
3634 unsigned int start_bits, end_bits;
3635
3636 start_bits = 8 - ( offset % 8 );
3637 if( start_bits != 8 )
3638 {
3639 size_t first_byte_idx = offset / 8;
3640
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003641 /* Special case */
3642 if( len <= start_bits )
3643 {
3644 for( ; len != 0; len-- )
3645 mask[first_byte_idx] |= 1 << ( start_bits - len );
3646
3647 /* Avoid potential issues with offset or len becoming invalid */
3648 return;
3649 }
3650
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003651 offset += start_bits; /* Now offset % 8 == 0 */
3652 len -= start_bits;
3653
3654 for( ; start_bits != 0; start_bits-- )
3655 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3656 }
3657
3658 end_bits = len % 8;
3659 if( end_bits != 0 )
3660 {
3661 size_t last_byte_idx = ( offset + len ) / 8;
3662
3663 len -= end_bits; /* Now len % 8 == 0 */
3664
3665 for( ; end_bits != 0; end_bits-- )
3666 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3667 }
3668
3669 memset( mask + offset / 8, 0xFF, len / 8 );
3670}
3671
3672/*
3673 * Check that bitmask is full
3674 */
3675static int ssl_bitmask_check( unsigned char *mask, size_t len )
3676{
3677 size_t i;
3678
3679 for( i = 0; i < len / 8; i++ )
3680 if( mask[i] != 0xFF )
3681 return( -1 );
3682
3683 for( i = 0; i < len % 8; i++ )
3684 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3685 return( -1 );
3686
3687 return( 0 );
3688}
3689
Hanno Becker56e205e2018-08-16 09:06:12 +01003690/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003691static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003692 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003693{
Hanno Becker56e205e2018-08-16 09:06:12 +01003694 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003695
Hanno Becker56e205e2018-08-16 09:06:12 +01003696 alloc_len = 12; /* Handshake header */
3697 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003698
Hanno Beckerd07df862018-08-16 09:14:58 +01003699 if( add_bitmap )
3700 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003701
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003702 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003703}
Hanno Becker56e205e2018-08-16 09:06:12 +01003704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003706
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003707static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003708{
3709 return( ( ssl->in_msg[1] << 16 ) |
3710 ( ssl->in_msg[2] << 8 ) |
3711 ssl->in_msg[3] );
3712}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003713
Simon Butcher99000142016-10-13 17:21:01 +01003714int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003715{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003719 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003721 }
3722
Hanno Becker12555c62018-08-16 12:47:53 +01003723 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003726 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003727 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003731 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003732 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003733 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003734
Hanno Becker44650b72018-08-16 12:51:11 +01003735 if( ssl_check_hs_header( ssl ) != 0 )
3736 {
3737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3738 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3739 }
3740
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003741 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003742 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3743 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3744 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3745 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003746 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003747 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3748 {
3749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3750 recv_msg_seq,
3751 ssl->handshake->in_msg_seq ) );
3752 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3753 }
3754
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003755 /* Retransmit only on last message from previous flight, to avoid
3756 * too many retransmissions.
3757 * Besides, No sane server ever retransmits HelloVerifyRequest */
3758 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003762 "message_seq = %d, start_of_flight = %d",
3763 recv_msg_seq,
3764 ssl->handshake->in_flight_start_seq ) );
3765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003769 return( ret );
3770 }
3771 }
3772 else
3773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003775 "message_seq = %d, expected = %d",
3776 recv_msg_seq,
3777 ssl->handshake->in_msg_seq ) );
3778 }
3779
Hanno Becker90333da2017-10-10 11:27:13 +01003780 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003781 }
3782 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003783
Hanno Becker6d97ef52018-08-16 13:09:04 +01003784 /* Message reassembly is handled alongside buffering of future
3785 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003786 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003787 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003788 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003791 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003792 }
3793 }
3794 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003796 /* With TLS we don't handle fragmentation (for now) */
3797 if( ssl->in_msglen < ssl->in_hslen )
3798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3800 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003801 }
3802
Simon Butcher99000142016-10-13 17:21:01 +01003803 return( 0 );
3804}
3805
3806void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3807{
Hanno Becker0271f962018-08-16 13:23:47 +01003808 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003809
Hanno Becker0271f962018-08-16 13:23:47 +01003810 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003811 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003812 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003813 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003814
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003815 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003817 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003818 ssl->handshake != NULL )
3819 {
Hanno Becker0271f962018-08-16 13:23:47 +01003820 unsigned offset;
3821 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003822
Hanno Becker0271f962018-08-16 13:23:47 +01003823 /* Increment handshake sequence number */
3824 hs->in_msg_seq++;
3825
3826 /*
3827 * Clear up handshake buffering and reassembly structure.
3828 */
3829
3830 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003831 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003832
3833 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003834 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3835 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003836 offset++, hs_buf++ )
3837 {
3838 *hs_buf = *(hs_buf + 1);
3839 }
3840
3841 /* Create a fresh last entry */
3842 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003843 }
3844#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003845}
3846
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003847/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003848 * DTLS anti-replay: RFC 6347 4.1.2.6
3849 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003850 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3851 * Bit n is set iff record number in_window_top - n has been seen.
3852 *
3853 * Usually, in_window_top is the last record number seen and the lsb of
3854 * in_window is set. The only exception is the initial state (record number 0
3855 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3858static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003859{
3860 ssl->in_window_top = 0;
3861 ssl->in_window = 0;
3862}
3863
3864static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3865{
3866 return( ( (uint64_t) buf[0] << 40 ) |
3867 ( (uint64_t) buf[1] << 32 ) |
3868 ( (uint64_t) buf[2] << 24 ) |
3869 ( (uint64_t) buf[3] << 16 ) |
3870 ( (uint64_t) buf[4] << 8 ) |
3871 ( (uint64_t) buf[5] ) );
3872}
3873
3874/*
3875 * Return 0 if sequence number is acceptable, -1 otherwise
3876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003878{
3879 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3880 uint64_t bit;
3881
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003882 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003883 return( 0 );
3884
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003885 if( rec_seqnum > ssl->in_window_top )
3886 return( 0 );
3887
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003888 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003889
3890 if( bit >= 64 )
3891 return( -1 );
3892
3893 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3894 return( -1 );
3895
3896 return( 0 );
3897}
3898
3899/*
3900 * Update replay window on new validated record
3901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003903{
3904 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3905
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003906 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003907 return;
3908
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003909 if( rec_seqnum > ssl->in_window_top )
3910 {
3911 /* Update window_top and the contents of the window */
3912 uint64_t shift = rec_seqnum - ssl->in_window_top;
3913
3914 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003915 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003916 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003917 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003918 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003919 ssl->in_window |= 1;
3920 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003921
3922 ssl->in_window_top = rec_seqnum;
3923 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003924 else
3925 {
3926 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003927 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003928
3929 if( bit < 64 ) /* Always true, but be extra sure */
3930 ssl->in_window |= (uint64_t) 1 << bit;
3931 }
3932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003934
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003935#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003936/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003937static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3938
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003939/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003940 * Without any SSL context, check if a datagram looks like a ClientHello with
3941 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003942 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003943 *
3944 * - if cookie is valid, return 0
3945 * - if ClientHello looks superficially valid but cookie is not,
3946 * fill obuf and set olen, then
3947 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3948 * - otherwise return a specific error code
3949 */
3950static int ssl_check_dtls_clihlo_cookie(
3951 mbedtls_ssl_cookie_write_t *f_cookie_write,
3952 mbedtls_ssl_cookie_check_t *f_cookie_check,
3953 void *p_cookie,
3954 const unsigned char *cli_id, size_t cli_id_len,
3955 const unsigned char *in, size_t in_len,
3956 unsigned char *obuf, size_t buf_len, size_t *olen )
3957{
3958 size_t sid_len, cookie_len;
3959 unsigned char *p;
3960
3961 if( f_cookie_write == NULL || f_cookie_check == NULL )
3962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3963
3964 /*
3965 * Structure of ClientHello with record and handshake headers,
3966 * and expected values. We don't need to check a lot, more checks will be
3967 * done when actually parsing the ClientHello - skipping those checks
3968 * avoids code duplication and does not make cookie forging any easier.
3969 *
3970 * 0-0 ContentType type; copied, must be handshake
3971 * 1-2 ProtocolVersion version; copied
3972 * 3-4 uint16 epoch; copied, must be 0
3973 * 5-10 uint48 sequence_number; copied
3974 * 11-12 uint16 length; (ignored)
3975 *
3976 * 13-13 HandshakeType msg_type; (ignored)
3977 * 14-16 uint24 length; (ignored)
3978 * 17-18 uint16 message_seq; copied
3979 * 19-21 uint24 fragment_offset; copied, must be 0
3980 * 22-24 uint24 fragment_length; (ignored)
3981 *
3982 * 25-26 ProtocolVersion client_version; (ignored)
3983 * 27-58 Random random; (ignored)
3984 * 59-xx SessionID session_id; 1 byte len + sid_len content
3985 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3986 * ...
3987 *
3988 * Minimum length is 61 bytes.
3989 */
3990 if( in_len < 61 ||
3991 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3992 in[3] != 0 || in[4] != 0 ||
3993 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3994 {
3995 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3996 }
3997
3998 sid_len = in[59];
3999 if( sid_len > in_len - 61 )
4000 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4001
4002 cookie_len = in[60 + sid_len];
4003 if( cookie_len > in_len - 60 )
4004 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4005
4006 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4007 cli_id, cli_id_len ) == 0 )
4008 {
4009 /* Valid cookie */
4010 return( 0 );
4011 }
4012
4013 /*
4014 * If we get here, we've got an invalid cookie, let's prepare HVR.
4015 *
4016 * 0-0 ContentType type; copied
4017 * 1-2 ProtocolVersion version; copied
4018 * 3-4 uint16 epoch; copied
4019 * 5-10 uint48 sequence_number; copied
4020 * 11-12 uint16 length; olen - 13
4021 *
4022 * 13-13 HandshakeType msg_type; hello_verify_request
4023 * 14-16 uint24 length; olen - 25
4024 * 17-18 uint16 message_seq; copied
4025 * 19-21 uint24 fragment_offset; copied
4026 * 22-24 uint24 fragment_length; olen - 25
4027 *
4028 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4029 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4030 *
4031 * Minimum length is 28.
4032 */
4033 if( buf_len < 28 )
4034 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4035
4036 /* Copy most fields and adapt others */
4037 memcpy( obuf, in, 25 );
4038 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4039 obuf[25] = 0xfe;
4040 obuf[26] = 0xff;
4041
4042 /* Generate and write actual cookie */
4043 p = obuf + 28;
4044 if( f_cookie_write( p_cookie,
4045 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4046 {
4047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4048 }
4049
4050 *olen = p - obuf;
4051
4052 /* Go back and fill length fields */
4053 obuf[27] = (unsigned char)( *olen - 28 );
4054
4055 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4056 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4057 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4058
4059 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4060 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4061
4062 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4063}
4064
4065/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004066 * Handle possible client reconnect with the same UDP quadruplet
4067 * (RFC 6347 Section 4.2.8).
4068 *
4069 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4070 * that looks like a ClientHello.
4071 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004072 * - if the input looks like a ClientHello without cookies,
4073 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004074 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004075 * - if the input looks like a ClientHello with a valid cookie,
4076 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004077 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004078 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004079 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004080 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004081 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4082 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004083 */
4084static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4085{
4086 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004087 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004088
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004089 ret = ssl_check_dtls_clihlo_cookie(
4090 ssl->conf->f_cookie_write,
4091 ssl->conf->f_cookie_check,
4092 ssl->conf->p_cookie,
4093 ssl->cli_id, ssl->cli_id_len,
4094 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004095 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004096
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004097 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4098
4099 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004100 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004101 int send_ret;
4102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4103 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4104 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004105 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004106 * If the error is permanent we'll catch it later,
4107 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004108 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4109 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4110 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004111
4112 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004113 }
4114
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004115 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004116 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004118 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4119 {
4120 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4121 return( ret );
4122 }
4123
4124 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004125 }
4126
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004127 return( ret );
4128}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004129#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004130
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004131/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004132 * ContentType type;
4133 * ProtocolVersion version;
4134 * uint16 epoch; // DTLS only
4135 * uint48 sequence_number; // DTLS only
4136 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004137 *
4138 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004139 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004140 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4141 *
4142 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004143 * 1. proceed with the record if this function returns 0
4144 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4145 * 3. return CLIENT_RECONNECT if this function return that value
4146 * 4. drop the whole datagram if this function returns anything else.
4147 * Point 2 is needed when the peer is resending, and we have already received
4148 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004151{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004152 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154 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 +02004155
Paul Bakker5121ce52009-01-03 21:22:43 +00004156 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004157 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004158 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004162 ssl->in_msgtype,
4163 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004164
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004165 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4167 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4168 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4169 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004172
4173#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004174 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4175 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004176 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4177#endif /* MBEDTLS_SSL_PROTO_DTLS */
4178 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4179 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004182 }
4183
4184 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004185 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4188 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004189 }
4190
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004191 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4194 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 }
4196
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004197 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004198 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004199 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4202 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004203 }
4204
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004205 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004206 * DTLS-related tests.
4207 * Check epoch before checking length constraint because
4208 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4209 * message gets duplicated before the corresponding Finished message,
4210 * the second ChangeCipherSpec should be discarded because it belongs
4211 * to an old epoch, but not because its length is shorter than
4212 * the minimum record length for packets using the new record transform.
4213 * Note that these two kinds of failures are handled differently,
4214 * as an unexpected record is silently skipped but an invalid
4215 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004216 */
4217#if defined(MBEDTLS_SSL_PROTO_DTLS)
4218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4219 {
4220 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4221
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004222 /* Check epoch (and sequence number) with DTLS */
4223 if( rec_epoch != ssl->in_epoch )
4224 {
4225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4226 "expected %d, received %d",
4227 ssl->in_epoch, rec_epoch ) );
4228
4229#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4230 /*
4231 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4232 * access the first byte of record content (handshake type), as we
4233 * have an active transform (possibly iv_len != 0), so use the
4234 * fact that the record header len is 13 instead.
4235 */
4236 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4237 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4238 rec_epoch == 0 &&
4239 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4240 ssl->in_left > 13 &&
4241 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4242 {
4243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4244 "from the same port" ) );
4245 return( ssl_handle_possible_reconnect( ssl ) );
4246 }
4247 else
4248#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004249 {
4250 /* Consider buffering the record. */
4251 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4252 {
4253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4254 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4255 }
4256
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004257 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004258 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004259 }
4260
4261#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4262 /* Replay detection only works for the current epoch */
4263 if( rec_epoch == ssl->in_epoch &&
4264 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4265 {
4266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4267 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4268 }
4269#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004270
Hanno Becker52c6dc62017-05-26 16:07:36 +01004271 /* Drop unexpected ApplicationData records,
4272 * except at the beginning of renegotiations */
4273 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4274 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4275#if defined(MBEDTLS_SSL_RENEGOTIATION)
4276 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4277 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4278#endif
4279 )
4280 {
4281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4282 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4283 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004284 }
4285#endif /* MBEDTLS_SSL_PROTO_DTLS */
4286
Hanno Becker52c6dc62017-05-26 16:07:36 +01004287
4288 /* Check length against bounds of the current transform and version */
4289 if( ssl->transform_in == NULL )
4290 {
4291 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004292 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004293 {
4294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4295 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4296 }
4297 }
4298 else
4299 {
4300 if( ssl->in_msglen < ssl->transform_in->minlen )
4301 {
4302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4303 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4304 }
4305
4306#if defined(MBEDTLS_SSL_PROTO_SSL3)
4307 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004308 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004309 {
4310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4311 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4312 }
4313#endif
4314#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4315 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4316 /*
4317 * TLS encrypted messages can have up to 256 bytes of padding
4318 */
4319 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4320 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004321 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004322 {
4323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4324 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4325 }
4326#endif
4327 }
4328
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004329 return( 0 );
4330}
Paul Bakker5121ce52009-01-03 21:22:43 +00004331
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004332/*
4333 * If applicable, decrypt (and decompress) record content
4334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004336{
4337 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4340 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4343 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 ret = mbedtls_ssl_hw_record_read( ssl );
4348 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4351 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004352 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004353
4354 if( ret == 0 )
4355 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004356 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004358 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 {
4360 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 return( ret );
4364 }
4365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 ssl->in_msg, ssl->in_msglen );
4368
Angus Grattond8213d02016-05-25 20:56:48 +10004369 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4372 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 }
4374 }
4375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004377 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004379 {
4380 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004383 return( ret );
4384 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004389 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004392 }
4393#endif
4394
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004395 return( 0 );
4396}
4397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004399
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004400/*
4401 * Read a record.
4402 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004403 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4404 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4405 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004406 */
Hanno Becker1097b342018-08-15 14:09:41 +01004407
4408/* Helper functions for mbedtls_ssl_read_record(). */
4409static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004410static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4411static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004412
Hanno Becker327c93b2018-08-15 13:56:18 +01004413int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004414 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004415{
4416 int ret;
4417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004419
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004420 if( ssl->keep_current_message == 0 )
4421 {
4422 do {
Simon Butcher99000142016-10-13 17:21:01 +01004423
Hanno Becker26994592018-08-15 14:14:59 +01004424 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004425 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004426 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004427
Hanno Beckere74d5562018-08-15 14:26:08 +01004428 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004429 {
Hanno Becker40f50842018-08-15 14:48:01 +01004430#if defined(MBEDTLS_SSL_PROTO_DTLS)
4431 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004432
Hanno Becker40f50842018-08-15 14:48:01 +01004433 /* We only check for buffered messages if the
4434 * current datagram is fully consumed. */
4435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004436 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004437 {
Hanno Becker40f50842018-08-15 14:48:01 +01004438 if( ssl_load_buffered_message( ssl ) == 0 )
4439 have_buffered = 1;
4440 }
4441
4442 if( have_buffered == 0 )
4443#endif /* MBEDTLS_SSL_PROTO_DTLS */
4444 {
4445 ret = ssl_get_next_record( ssl );
4446 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4447 continue;
4448
4449 if( ret != 0 )
4450 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004451 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004452 return( ret );
4453 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004454 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004455 }
4456
4457 ret = mbedtls_ssl_handle_message_type( ssl );
4458
Hanno Becker40f50842018-08-15 14:48:01 +01004459#if defined(MBEDTLS_SSL_PROTO_DTLS)
4460 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4461 {
4462 /* Buffer future message */
4463 ret = ssl_buffer_message( ssl );
4464 if( ret != 0 )
4465 return( ret );
4466
4467 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4468 }
4469#endif /* MBEDTLS_SSL_PROTO_DTLS */
4470
Hanno Becker90333da2017-10-10 11:27:13 +01004471 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4472 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004473
4474 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004475 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004476 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004477 return( ret );
4478 }
4479
Hanno Becker327c93b2018-08-15 13:56:18 +01004480 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004481 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004482 {
4483 mbedtls_ssl_update_handshake_status( ssl );
4484 }
Simon Butcher99000142016-10-13 17:21:01 +01004485 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004486 else
Simon Butcher99000142016-10-13 17:21:01 +01004487 {
Hanno Becker02f59072018-08-15 14:00:24 +01004488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004489 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004490 }
4491
4492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4493
4494 return( 0 );
4495}
4496
Hanno Becker40f50842018-08-15 14:48:01 +01004497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004498static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004499{
Hanno Becker40f50842018-08-15 14:48:01 +01004500 if( ssl->in_left > ssl->next_record_offset )
4501 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004502
Hanno Becker40f50842018-08-15 14:48:01 +01004503 return( 0 );
4504}
4505
4506static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4507{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004508 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004509 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004510 int ret = 0;
4511
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004512 if( hs == NULL )
4513 return( -1 );
4514
Hanno Beckere00ae372018-08-20 09:39:42 +01004515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4516
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004517 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4518 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4519 {
4520 /* Check if we have seen a ChangeCipherSpec before.
4521 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004522 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004523 {
4524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4525 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004526 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004527 }
4528
Hanno Becker39b8bc92018-08-28 17:17:13 +01004529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004530 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4531 ssl->in_msglen = 1;
4532 ssl->in_msg[0] = 1;
4533
4534 /* As long as they are equal, the exact value doesn't matter. */
4535 ssl->in_left = 0;
4536 ssl->next_record_offset = 0;
4537
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004538 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004539 goto exit;
4540 }
Hanno Becker37f95322018-08-16 13:55:32 +01004541
Hanno Beckerb8f50142018-08-28 10:01:34 +01004542#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004543 /* Debug only */
4544 {
4545 unsigned offset;
4546 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4547 {
4548 hs_buf = &hs->buffering.hs[offset];
4549 if( hs_buf->is_valid == 1 )
4550 {
4551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4552 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004553 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004554 }
4555 }
4556 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004557#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004558
4559 /* Check if we have buffered and/or fully reassembled the
4560 * next handshake message. */
4561 hs_buf = &hs->buffering.hs[0];
4562 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4563 {
4564 /* Synthesize a record containing the buffered HS message. */
4565 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4566 ( hs_buf->data[2] << 8 ) |
4567 hs_buf->data[3];
4568
4569 /* Double-check that we haven't accidentally buffered
4570 * a message that doesn't fit into the input buffer. */
4571 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4572 {
4573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4575 }
4576
4577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4578 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4579 hs_buf->data, msg_len + 12 );
4580
4581 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4582 ssl->in_hslen = msg_len + 12;
4583 ssl->in_msglen = msg_len + 12;
4584 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4585
4586 ret = 0;
4587 goto exit;
4588 }
4589 else
4590 {
4591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4592 hs->in_msg_seq ) );
4593 }
4594
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004595 ret = -1;
4596
4597exit:
4598
4599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4600 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004601}
4602
Hanno Beckera02b0b42018-08-21 17:20:27 +01004603static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4604 size_t desired )
4605{
4606 int offset;
4607 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4609 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004610
Hanno Becker01315ea2018-08-21 17:22:17 +01004611 /* Get rid of future records epoch first, if such exist. */
4612 ssl_free_buffered_record( ssl );
4613
4614 /* Check if we have enough space available now. */
4615 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4616 hs->buffering.total_bytes_buffered ) )
4617 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004619 return( 0 );
4620 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004621
Hanno Becker4f432ad2018-08-28 10:02:32 +01004622 /* We don't have enough space to buffer the next expected handshake
4623 * message. Remove buffers used for future messages to gain space,
4624 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004625 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4626 offset >= 0; offset-- )
4627 {
4628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4629 offset ) );
4630
Hanno Beckerb309b922018-08-23 13:18:05 +01004631 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004632
4633 /* Check if we have enough space available now. */
4634 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4635 hs->buffering.total_bytes_buffered ) )
4636 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004638 return( 0 );
4639 }
4640 }
4641
4642 return( -1 );
4643}
4644
Hanno Becker40f50842018-08-15 14:48:01 +01004645static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4646{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004647 int ret = 0;
4648 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4649
4650 if( hs == NULL )
4651 return( 0 );
4652
4653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4654
4655 switch( ssl->in_msgtype )
4656 {
4657 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004659
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004660 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004661 break;
4662
4663 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004664 {
4665 unsigned recv_msg_seq_offset;
4666 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4667 mbedtls_ssl_hs_buffer *hs_buf;
4668 size_t msg_len = ssl->in_hslen - 12;
4669
4670 /* We should never receive an old handshake
4671 * message - double-check nonetheless. */
4672 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4673 {
4674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4675 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4676 }
4677
4678 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4679 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4680 {
4681 /* Silently ignore -- message too far in the future */
4682 MBEDTLS_SSL_DEBUG_MSG( 2,
4683 ( "Ignore future HS message with sequence number %u, "
4684 "buffering window %u - %u",
4685 recv_msg_seq, ssl->handshake->in_msg_seq,
4686 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4687
4688 goto exit;
4689 }
4690
4691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4692 recv_msg_seq, recv_msg_seq_offset ) );
4693
4694 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4695
4696 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004697 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004698 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004699 size_t reassembly_buf_sz;
4700
Hanno Becker37f95322018-08-16 13:55:32 +01004701 hs_buf->is_fragmented =
4702 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4703
4704 /* We copy the message back into the input buffer
4705 * after reassembly, so check that it's not too large.
4706 * This is an implementation-specific limitation
4707 * and not one from the standard, hence it is not
4708 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004709 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004710 {
4711 /* Ignore message */
4712 goto exit;
4713 }
4714
Hanno Beckere0b150f2018-08-21 15:51:03 +01004715 /* Check if we have enough space to buffer the message. */
4716 if( hs->buffering.total_bytes_buffered >
4717 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4718 {
4719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4721 }
4722
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004723 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4724 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004725
4726 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4727 hs->buffering.total_bytes_buffered ) )
4728 {
4729 if( recv_msg_seq_offset > 0 )
4730 {
4731 /* If we can't buffer a future message because
4732 * of space limitations -- ignore. */
4733 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",
4734 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4735 (unsigned) hs->buffering.total_bytes_buffered ) );
4736 goto exit;
4737 }
Hanno Beckere1801392018-08-21 16:51:05 +01004738 else
4739 {
4740 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",
4741 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4742 (unsigned) hs->buffering.total_bytes_buffered ) );
4743 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004744
Hanno Beckera02b0b42018-08-21 17:20:27 +01004745 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004746 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004747 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",
4748 (unsigned) msg_len,
4749 (unsigned) reassembly_buf_sz,
4750 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004751 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004752 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4753 goto exit;
4754 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004755 }
4756
4757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4758 msg_len ) );
4759
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004760 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4761 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004762 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004763 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004764 goto exit;
4765 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004766 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004767
4768 /* Prepare final header: copy msg_type, length and message_seq,
4769 * then add standardised fragment_offset and fragment_length */
4770 memcpy( hs_buf->data, ssl->in_msg, 6 );
4771 memset( hs_buf->data + 6, 0, 3 );
4772 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4773
4774 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004775
4776 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004777 }
4778 else
4779 {
4780 /* Make sure msg_type and length are consistent */
4781 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4782 {
4783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4784 /* Ignore */
4785 goto exit;
4786 }
4787 }
4788
Hanno Becker4422bbb2018-08-20 09:40:19 +01004789 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004790 {
4791 size_t frag_len, frag_off;
4792 unsigned char * const msg = hs_buf->data + 12;
4793
4794 /*
4795 * Check and copy current fragment
4796 */
4797
4798 /* Validation of header fields already done in
4799 * mbedtls_ssl_prepare_handshake_record(). */
4800 frag_off = ssl_get_hs_frag_off( ssl );
4801 frag_len = ssl_get_hs_frag_len( ssl );
4802
4803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4804 frag_off, frag_len ) );
4805 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4806
4807 if( hs_buf->is_fragmented )
4808 {
4809 unsigned char * const bitmask = msg + msg_len;
4810 ssl_bitmask_set( bitmask, frag_off, frag_len );
4811 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4812 msg_len ) == 0 );
4813 }
4814 else
4815 {
4816 hs_buf->is_complete = 1;
4817 }
4818
4819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4820 hs_buf->is_complete ? "" : "not yet " ) );
4821 }
4822
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004823 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004824 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004825
4826 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004827 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004828 break;
4829 }
4830
4831exit:
4832
4833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4834 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004835}
4836#endif /* MBEDTLS_SSL_PROTO_DTLS */
4837
Hanno Becker1097b342018-08-15 14:09:41 +01004838static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004839{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004840 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004841 * Consume last content-layer message and potentially
4842 * update in_msglen which keeps track of the contents'
4843 * consumption state.
4844 *
4845 * (1) Handshake messages:
4846 * Remove last handshake message, move content
4847 * and adapt in_msglen.
4848 *
4849 * (2) Alert messages:
4850 * Consume whole record content, in_msglen = 0.
4851 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004852 * (3) Change cipher spec:
4853 * Consume whole record content, in_msglen = 0.
4854 *
4855 * (4) Application data:
4856 * Don't do anything - the record layer provides
4857 * the application data as a stream transport
4858 * and consumes through mbedtls_ssl_read only.
4859 *
4860 */
4861
4862 /* Case (1): Handshake messages */
4863 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004864 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004865 /* Hard assertion to be sure that no application data
4866 * is in flight, as corrupting ssl->in_msglen during
4867 * ssl->in_offt != NULL is fatal. */
4868 if( ssl->in_offt != NULL )
4869 {
4870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4872 }
4873
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004874 /*
4875 * Get next Handshake message in the current record
4876 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004877
Hanno Becker4a810fb2017-05-24 16:27:30 +01004878 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004879 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004880 * current handshake content: If DTLS handshake
4881 * fragmentation is used, that's the fragment
4882 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004883 * size here is faulty and should be changed at
4884 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004885 * (2) While it doesn't seem to cause problems, one
4886 * has to be very careful not to assume that in_hslen
4887 * is always <= in_msglen in a sensible communication.
4888 * Again, it's wrong for DTLS handshake fragmentation.
4889 * The following check is therefore mandatory, and
4890 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004891 * Additionally, ssl->in_hslen might be arbitrarily out of
4892 * bounds after handling a DTLS message with an unexpected
4893 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004894 */
4895 if( ssl->in_hslen < ssl->in_msglen )
4896 {
4897 ssl->in_msglen -= ssl->in_hslen;
4898 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4899 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004900
Hanno Becker4a810fb2017-05-24 16:27:30 +01004901 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4902 ssl->in_msg, ssl->in_msglen );
4903 }
4904 else
4905 {
4906 ssl->in_msglen = 0;
4907 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004908
Hanno Becker4a810fb2017-05-24 16:27:30 +01004909 ssl->in_hslen = 0;
4910 }
4911 /* Case (4): Application data */
4912 else if( ssl->in_offt != NULL )
4913 {
4914 return( 0 );
4915 }
4916 /* Everything else (CCS & Alerts) */
4917 else
4918 {
4919 ssl->in_msglen = 0;
4920 }
4921
Hanno Becker1097b342018-08-15 14:09:41 +01004922 return( 0 );
4923}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004924
Hanno Beckere74d5562018-08-15 14:26:08 +01004925static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4926{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004927 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004928 return( 1 );
4929
4930 return( 0 );
4931}
4932
Hanno Becker5f066e72018-08-16 14:56:31 +01004933#if defined(MBEDTLS_SSL_PROTO_DTLS)
4934
4935static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4936{
4937 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4938 if( hs == NULL )
4939 return;
4940
Hanno Becker01315ea2018-08-21 17:22:17 +01004941 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004942 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004943 hs->buffering.total_bytes_buffered -=
4944 hs->buffering.future_record.len;
4945
4946 mbedtls_free( hs->buffering.future_record.data );
4947 hs->buffering.future_record.data = NULL;
4948 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004949}
4950
4951static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4952{
4953 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4954 unsigned char * rec;
4955 size_t rec_len;
4956 unsigned rec_epoch;
4957
4958 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4959 return( 0 );
4960
4961 if( hs == NULL )
4962 return( 0 );
4963
Hanno Becker5f066e72018-08-16 14:56:31 +01004964 rec = hs->buffering.future_record.data;
4965 rec_len = hs->buffering.future_record.len;
4966 rec_epoch = hs->buffering.future_record.epoch;
4967
4968 if( rec == NULL )
4969 return( 0 );
4970
Hanno Becker4cb782d2018-08-20 11:19:05 +01004971 /* Only consider loading future records if the
4972 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004973 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004974 return( 0 );
4975
Hanno Becker5f066e72018-08-16 14:56:31 +01004976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4977
4978 if( rec_epoch != ssl->in_epoch )
4979 {
4980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4981 goto exit;
4982 }
4983
4984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4985
4986 /* Double-check that the record is not too large */
4987 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4988 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4989 {
4990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4992 }
4993
4994 memcpy( ssl->in_hdr, rec, rec_len );
4995 ssl->in_left = rec_len;
4996 ssl->next_record_offset = 0;
4997
4998 ssl_free_buffered_record( ssl );
4999
5000exit:
5001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5002 return( 0 );
5003}
5004
5005static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5006{
5007 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5008 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005009 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005010
5011 /* Don't buffer future records outside handshakes. */
5012 if( hs == NULL )
5013 return( 0 );
5014
5015 /* Only buffer handshake records (we are only interested
5016 * in Finished messages). */
5017 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5018 return( 0 );
5019
5020 /* Don't buffer more than one future epoch record. */
5021 if( hs->buffering.future_record.data != NULL )
5022 return( 0 );
5023
Hanno Becker01315ea2018-08-21 17:22:17 +01005024 /* Don't buffer record if there's not enough buffering space remaining. */
5025 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5026 hs->buffering.total_bytes_buffered ) )
5027 {
5028 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",
5029 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5030 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005031 return( 0 );
5032 }
5033
Hanno Becker5f066e72018-08-16 14:56:31 +01005034 /* Buffer record */
5035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5036 ssl->in_epoch + 1 ) );
5037 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5038 rec_hdr_len + ssl->in_msglen );
5039
5040 /* ssl_parse_record_header() only considers records
5041 * of the next epoch as candidates for buffering. */
5042 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005043 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005044
5045 hs->buffering.future_record.data =
5046 mbedtls_calloc( 1, hs->buffering.future_record.len );
5047 if( hs->buffering.future_record.data == NULL )
5048 {
5049 /* If we run out of RAM trying to buffer a
5050 * record from the next epoch, just ignore. */
5051 return( 0 );
5052 }
5053
Hanno Becker01315ea2018-08-21 17:22:17 +01005054 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005055
Hanno Becker01315ea2018-08-21 17:22:17 +01005056 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005057 return( 0 );
5058}
5059
5060#endif /* MBEDTLS_SSL_PROTO_DTLS */
5061
Hanno Beckere74d5562018-08-15 14:26:08 +01005062static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005063{
5064 int ret;
5065
Hanno Becker5f066e72018-08-16 14:56:31 +01005066#if defined(MBEDTLS_SSL_PROTO_DTLS)
5067 /* We might have buffered a future record; if so,
5068 * and if the epoch matches now, load it.
5069 * On success, this call will set ssl->in_left to
5070 * the length of the buffered record, so that
5071 * the calls to ssl_fetch_input() below will
5072 * essentially be no-ops. */
5073 ret = ssl_load_buffered_record( ssl );
5074 if( ret != 0 )
5075 return( ret );
5076#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005081 return( ret );
5082 }
5083
5084 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005087 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5088 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005089 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005090 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5091 {
5092 ret = ssl_buffer_future_record( ssl );
5093 if( ret != 0 )
5094 return( ret );
5095
5096 /* Fall through to handling of unexpected records */
5097 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5098 }
5099
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005100 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5101 {
5102 /* Skip unexpected record (but not whole datagram) */
5103 ssl->next_record_offset = ssl->in_msglen
5104 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005105
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5107 "(header)" ) );
5108 }
5109 else
5110 {
5111 /* Skip invalid record and the rest of the datagram */
5112 ssl->next_record_offset = 0;
5113 ssl->in_left = 0;
5114
5115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5116 "(header)" ) );
5117 }
5118
5119 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005120 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005121 }
5122#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005123 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005124 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005125
5126 /*
5127 * Read and optionally decrypt the message contents
5128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5130 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005133 return( ret );
5134 }
5135
5136 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005141 if( ssl->next_record_offset < ssl->in_left )
5142 {
5143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5144 }
5145 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005146 else
5147#endif
5148 ssl->in_left = 0;
5149
5150 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005153 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005154 {
5155 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5157 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005158 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005159 /* Except when waiting for Finished as a bad mac here
5160 * probably means something went wrong in the handshake
5161 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5162 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5163 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5164 {
5165#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5166 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5167 {
5168 mbedtls_ssl_send_alert_message( ssl,
5169 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5170 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5171 }
5172#endif
5173 return( ret );
5174 }
5175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005177 if( ssl->conf->badmac_limit != 0 &&
5178 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5181 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005182 }
5183#endif
5184
Hanno Becker4a810fb2017-05-24 16:27:30 +01005185 /* As above, invalid records cause
5186 * dismissal of the whole datagram. */
5187
5188 ssl->next_record_offset = 0;
5189 ssl->in_left = 0;
5190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005192 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005193 }
5194
5195 return( ret );
5196 }
5197 else
5198#endif
5199 {
5200 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5202 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204 mbedtls_ssl_send_alert_message( ssl,
5205 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5206 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005207 }
5208#endif
5209 return( ret );
5210 }
5211 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005212
Simon Butcher99000142016-10-13 17:21:01 +01005213 return( 0 );
5214}
5215
5216int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5217{
5218 int ret;
5219
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005220 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005221 * Handle particular types of records
5222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005224 {
Simon Butcher99000142016-10-13 17:21:01 +01005225 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5226 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005227 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005229 }
5230
Hanno Beckere678eaa2018-08-21 14:57:46 +01005231 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005232 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005233 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005234 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5236 ssl->in_msglen ) );
5237 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005238 }
5239
Hanno Beckere678eaa2018-08-21 14:57:46 +01005240 if( ssl->in_msg[0] != 1 )
5241 {
5242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5243 ssl->in_msg[0] ) );
5244 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5245 }
5246
5247#if defined(MBEDTLS_SSL_PROTO_DTLS)
5248 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5249 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5250 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5251 {
5252 if( ssl->handshake == NULL )
5253 {
5254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5255 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5256 }
5257
5258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5259 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5260 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005261#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005262 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005265 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005266 if( ssl->in_msglen != 2 )
5267 {
5268 /* Note: Standard allows for more than one 2 byte alert
5269 to be packed in a single message, but Mbed TLS doesn't
5270 currently support this. */
5271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5272 ssl->in_msglen ) );
5273 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5274 }
5275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005277 ssl->in_msg[0], ssl->in_msg[1] ) );
5278
5279 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005280 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005285 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005287 }
5288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5290 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5293 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005294 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005295
5296#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5297 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5298 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5299 {
Hanno Becker90333da2017-10-10 11:27:13 +01005300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005301 /* Will be handled when trying to parse ServerHello */
5302 return( 0 );
5303 }
5304#endif
5305
5306#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5307 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5308 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5309 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5310 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5311 {
5312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5313 /* Will be handled in mbedtls_ssl_parse_certificate() */
5314 return( 0 );
5315 }
5316#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5317
5318 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005319 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005320 }
5321
Hanno Beckerc76c6192017-06-06 10:03:17 +01005322#if defined(MBEDTLS_SSL_PROTO_DTLS)
5323 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5324 ssl->handshake != NULL &&
5325 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5326 {
5327 ssl_handshake_wrapup_free_hs_transform( ssl );
5328 }
5329#endif
5330
Paul Bakker5121ce52009-01-03 21:22:43 +00005331 return( 0 );
5332}
5333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005335{
5336 int ret;
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5339 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5340 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005341 {
5342 return( ret );
5343 }
5344
5345 return( 0 );
5346}
5347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005349 unsigned char level,
5350 unsigned char message )
5351{
5352 int ret;
5353
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005354 if( ssl == NULL || ssl->conf == NULL )
5355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005361 ssl->out_msglen = 2;
5362 ssl->out_msg[0] = level;
5363 ssl->out_msg[1] = message;
5364
Hanno Becker67bc7c32018-08-06 11:33:50 +01005365 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005368 return( ret );
5369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005371
5372 return( 0 );
5373}
5374
Paul Bakker5121ce52009-01-03 21:22:43 +00005375/*
5376 * Handshake functions
5377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5379 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5380 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5381 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5382 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5383 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5384 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005385/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5393 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005394 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5395 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005398 ssl->state++;
5399 return( 0 );
5400 }
5401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005404}
5405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5413 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005414 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5415 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005418 ssl->state++;
5419 return( 0 );
5420 }
5421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005424}
Gilles Peskinef9828522017-05-03 12:28:43 +02005425
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005426#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005427/* Some certificate support -> implement write and parse */
5428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005430{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005432 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 const mbedtls_x509_crt *crt;
5434 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5439 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005440 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5441 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005444 ssl->state++;
5445 return( 0 );
5446 }
5447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005449 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 {
5451 if( ssl->client_auth == 0 )
5452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 ssl->state++;
5455 return( 0 );
5456 }
5457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005459 /*
5460 * If using SSLv3 and got no cert, send an Alert message
5461 * (otherwise an empty Certificate message will be sent).
5462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5464 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 {
5466 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5468 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5469 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 goto write_msg;
5473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#endif /* MBEDTLS_SSL_CLI_C */
5477#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005478 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5483 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 }
5485 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005486#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
5490 /*
5491 * 0 . 0 handshake type
5492 * 1 . 3 handshake length
5493 * 4 . 6 length of all certs
5494 * 7 . 9 length of cert. 1
5495 * 10 . n-1 peer certificate
5496 * n . n+2 length of cert. 2
5497 * n+3 . ... upper level cert, etc.
5498 */
5499 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005501
Paul Bakker29087132010-03-21 21:03:34 +00005502 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005503 {
5504 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005505 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005508 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 }
5511
5512 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5513 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5514 ssl->out_msg[i + 2] = (unsigned char)( n );
5515
5516 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5517 i += n; crt = crt->next;
5518 }
5519
5520 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5521 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5522 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5523
5524 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5526 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005527
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005528#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005529write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005530#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005531
5532 ssl->state++;
5533
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005534 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005535 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005537 return( ret );
5538 }
5539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005541
Paul Bakkered27a042013-04-18 22:46:23 +02005542 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005543}
5544
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005545/*
5546 * Once the certificate message is read, parse it into a cert chain and
5547 * perform basic checks, but leave actual verification to the caller
5548 */
5549static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005550{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005551 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005552 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005553 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555#if defined(MBEDTLS_SSL_SRV_C)
5556#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005557 /*
5558 * Check if the client sent an empty certificate
5559 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005562 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005563 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5565 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5566 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005569
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005570 /* The client was asked for a certificate but didn't send
5571 one. The client should know what's going on, so we
5572 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005573 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005574 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005575 }
5576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5580 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005581 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5585 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5586 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5587 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005591 /* The client was asked for a certificate but didn't send
5592 one. The client should know what's going on, so we
5593 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005594 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005595 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005596 }
5597 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5599 MBEDTLS_SSL_PROTO_TLS1_2 */
5600#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005605 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5606 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 }
5609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5611 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005614 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5615 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005617 }
5618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005620
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005624 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005625
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005626 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005630 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5631 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 }
5634
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005635 /* In case we tried to reuse a session but it failed */
5636 if( ssl->session_negotiate->peer_cert != NULL )
5637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5639 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005640 }
5641
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005642 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005647 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5648 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005649 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 }
5651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005653
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005654 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005655
5656 while( i < ssl->in_hslen )
5657 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005658 if ( i + 3 > ssl->in_hslen ) {
5659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5660 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5661 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5662 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005664 if( ssl->in_msg[i] != 0 )
5665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005667 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5668 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005670 }
5671
5672 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5673 | (unsigned int) ssl->in_msg[i + 2];
5674 i += 3;
5675
5676 if( n < 128 || i + n > ssl->in_hslen )
5677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005679 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5680 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005682 }
5683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005685 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005686 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005687 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005688 case 0: /*ok*/
5689 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5690 /* Ignore certificate with an unknown algorithm: maybe a
5691 prior certificate was already trusted. */
5692 break;
5693
5694 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5695 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5696 goto crt_parse_der_failed;
5697
5698 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5699 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5700 goto crt_parse_der_failed;
5701
5702 default:
5703 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5704 crt_parse_der_failed:
5705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 return( ret );
5708 }
5709
5710 i += n;
5711 }
5712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005714
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005715 /*
5716 * On client, make sure the server cert doesn't change during renego to
5717 * avoid "triple handshake" attack: https://secure-resumption.com/
5718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005722 {
5723 if( ssl->session->peer_cert == NULL )
5724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5727 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005729 }
5730
5731 if( ssl->session->peer_cert->raw.len !=
5732 ssl->session_negotiate->peer_cert->raw.len ||
5733 memcmp( ssl->session->peer_cert->raw.p,
5734 ssl->session_negotiate->peer_cert->raw.p,
5735 ssl->session->peer_cert->raw.len ) != 0 )
5736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005738 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5739 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005741 }
5742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005744
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005745 return( 0 );
5746}
5747
5748int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5749{
5750 int ret;
5751 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5752 ssl->transform_negotiate->ciphersuite_info;
5753#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5754 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5755 ? ssl->handshake->sni_authmode
5756 : ssl->conf->authmode;
5757#else
5758 const int authmode = ssl->conf->authmode;
5759#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005760 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005761
5762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5763
5764 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5765 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5766 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5767 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5768 {
5769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5770 ssl->state++;
5771 return( 0 );
5772 }
5773
5774#if defined(MBEDTLS_SSL_SRV_C)
5775 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5776 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5777 {
5778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5779 ssl->state++;
5780 return( 0 );
5781 }
5782
5783 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5784 authmode == MBEDTLS_SSL_VERIFY_NONE )
5785 {
5786 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005788
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005789 ssl->state++;
5790 return( 0 );
5791 }
5792#endif
5793
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005794#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5795 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005796 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005797 {
5798 goto crt_verify;
5799 }
5800#endif
5801
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005802 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005803 {
5804 /* mbedtls_ssl_read_record may have sent an alert already. We
5805 let it decide whether to alert. */
5806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5807 return( ret );
5808 }
5809
5810 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5811 {
5812#if defined(MBEDTLS_SSL_SRV_C)
5813 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5814 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5815 {
5816 ret = 0;
5817 }
5818#endif
5819
5820 ssl->state++;
5821 return( ret );
5822 }
5823
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005824#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5825 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005826 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005827
5828crt_verify:
5829 if( ssl->handshake->ecrs_enabled)
5830 rs_ctx = &ssl->handshake->ecrs_ctx;
5831#endif
5832
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005833 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005835 mbedtls_x509_crt *ca_chain;
5836 mbedtls_x509_crl *ca_crl;
5837
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005838#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005839 if( ssl->handshake->sni_ca_chain != NULL )
5840 {
5841 ca_chain = ssl->handshake->sni_ca_chain;
5842 ca_crl = ssl->handshake->sni_ca_crl;
5843 }
5844 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005845#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005846 {
5847 ca_chain = ssl->conf->ca_chain;
5848 ca_crl = ssl->conf->ca_crl;
5849 }
5850
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005851 /*
5852 * Main check: verify certificate
5853 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005854 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005855 ssl->session_negotiate->peer_cert,
5856 ca_chain, ca_crl,
5857 ssl->conf->cert_profile,
5858 ssl->hostname,
5859 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005860 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005861
5862 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005865 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005866
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005867#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5868 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005869 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005870#endif
5871
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005872 /*
5873 * Secondary checks: always done, but change 'ret' only if it was 0
5874 */
5875
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005876#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005879
5880 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005882 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005883 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005884 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005887 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005889 }
5890 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005891#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005894 ciphersuite_info,
5895 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005896 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005899 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005901 }
5902
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005903 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5904 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5905 * with details encoded in the verification flags. All other kinds
5906 * of error codes, including those from the user provided f_vrfy
5907 * functions, are treated as fatal and lead to a failure of
5908 * ssl_parse_certificate even if verification was optional. */
5909 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5910 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5911 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5912 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005913 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005914 }
5915
5916 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5917 {
5918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5919 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5920 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005921
5922 if( ret != 0 )
5923 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005924 uint8_t alert;
5925
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005926 /* The certificate may have been rejected for several reasons.
5927 Pick one and send the corresponding alert. Which alert to send
5928 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005929 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5930 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5931 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5932 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5933 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5934 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5935 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5936 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5937 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5938 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5939 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5940 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5941 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5942 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5943 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5944 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5945 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5946 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5947 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5948 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005949 else
5950 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005951 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5952 alert );
5953 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005954
Hanno Beckere6706e62017-05-15 16:05:15 +01005955#if defined(MBEDTLS_DEBUG_C)
5956 if( ssl->session_negotiate->verify_result != 0 )
5957 {
5958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5959 ssl->session_negotiate->verify_result ) );
5960 }
5961 else
5962 {
5963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5964 }
5965#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005966 }
5967
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005968 ssl->state++;
5969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005971
5972 return( ret );
5973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5975 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5976 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5977 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5978 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5979 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5980 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005983{
5984 int ret;
5985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005989 ssl->out_msglen = 1;
5990 ssl->out_msg[0] = 1;
5991
Paul Bakker5121ce52009-01-03 21:22:43 +00005992 ssl->state++;
5993
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005994 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005995 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005997 return( ret );
5998 }
5999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006001
6002 return( 0 );
6003}
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006006{
6007 int ret;
6008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006010
Hanno Becker327c93b2018-08-15 13:56:18 +01006011 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 return( ret );
6015 }
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006020 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6021 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006023 }
6024
Hanno Beckere678eaa2018-08-21 14:57:46 +01006025 /* CCS records are only accepted if they have length 1 and content '1',
6026 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006028 /*
6029 * Switch to our negotiated transform and session parameters for inbound
6030 * data.
6031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006033 ssl->transform_in = ssl->transform_negotiate;
6034 ssl->session_in = ssl->session_negotiate;
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006037 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006040 ssl_dtls_replay_reset( ssl );
6041#endif
6042
6043 /* Increment epoch */
6044 if( ++ssl->in_epoch == 0 )
6045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006047 /* This is highly unlikely to happen for legitimate reasons, so
6048 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006050 }
6051 }
6052 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006054 memset( ssl->in_ctr, 0, 8 );
6055
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006056 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6059 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006064 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6065 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006067 }
6068 }
6069#endif
6070
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 ssl->state++;
6072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074
6075 return( 0 );
6076}
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6079 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006080{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006081 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6084 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6085 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006086 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006087 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6090#if defined(MBEDTLS_SHA512_C)
6091 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006092 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6093 else
6094#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095#if defined(MBEDTLS_SHA256_C)
6096 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006097 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006098 else
6099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006103 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006104 }
Paul Bakker380da532012-04-18 16:10:25 +00006105}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6110 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006111 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6112 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6115#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006116 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006117#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006119 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006122}
6123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006125 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6128 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006129 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6130 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6133#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006134 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006137 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006138#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006140}
6141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6143 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6144static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006145 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006146{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006147 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6148 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006149}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006150#endif
Paul Bakker380da532012-04-18 16:10:25 +00006151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6153#if defined(MBEDTLS_SHA256_C)
6154static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006155 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006156{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006157 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006158}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006159#endif
Paul Bakker380da532012-04-18 16:10:25 +00006160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161#if defined(MBEDTLS_SHA512_C)
6162static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006163 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006164{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006165 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006166}
Paul Bakker769075d2012-11-24 11:26:46 +01006167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006171static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006173{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006174 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006175 mbedtls_md5_context md5;
6176 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006177
Paul Bakker5121ce52009-01-03 21:22:43 +00006178 unsigned char padbuf[48];
6179 unsigned char md5sum[16];
6180 unsigned char sha1sum[20];
6181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006183 if( !session )
6184 session = ssl->session;
6185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006187
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006188 mbedtls_md5_init( &md5 );
6189 mbedtls_sha1_init( &sha1 );
6190
6191 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6192 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006193
6194 /*
6195 * SSLv3:
6196 * hash =
6197 * MD5( master + pad2 +
6198 * MD5( handshake + sender + master + pad1 ) )
6199 * + SHA1( master + pad2 +
6200 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 */
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006204 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6205 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006206#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006209 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6210 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006214 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006215
Paul Bakker1ef83d62012-04-11 12:09:53 +00006216 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006217
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006218 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6219 mbedtls_md5_update_ret( &md5, session->master, 48 );
6220 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6221 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006222
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006223 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6224 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6225 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6226 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006227
Paul Bakker1ef83d62012-04-11 12:09:53 +00006228 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006230 mbedtls_md5_starts_ret( &md5 );
6231 mbedtls_md5_update_ret( &md5, session->master, 48 );
6232 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6233 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6234 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006235
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006236 mbedtls_sha1_starts_ret( &sha1 );
6237 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6238 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6239 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6240 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006244 mbedtls_md5_free( &md5 );
6245 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006246
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006247 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6248 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6249 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006256static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006258{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006259 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006260 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006261 mbedtls_md5_context md5;
6262 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006263 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006266 if( !session )
6267 session = ssl->session;
6268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006270
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006271 mbedtls_md5_init( &md5 );
6272 mbedtls_sha1_init( &sha1 );
6273
6274 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6275 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006276
Paul Bakker1ef83d62012-04-11 12:09:53 +00006277 /*
6278 * TLSv1:
6279 * hash = PRF( master, finished_label,
6280 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6281 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006284 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6285 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006286#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006289 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6290 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006291#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006294 ? "client finished"
6295 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006296
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006297 mbedtls_md5_finish_ret( &md5, padbuf );
6298 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006299
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006300 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006301 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006304
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006305 mbedtls_md5_free( &md5 );
6306 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006307
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006308 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6315#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006316static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006318{
6319 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006320 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006321 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006322 unsigned char padbuf[32];
6323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006325 if( !session )
6326 session = ssl->session;
6327
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006328 mbedtls_sha256_init( &sha256 );
6329
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006331
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006332 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006333
6334 /*
6335 * TLSv1.2:
6336 * hash = PRF( master, finished_label,
6337 * Hash( handshake ) )[0.11]
6338 */
6339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340#if !defined(MBEDTLS_SHA256_ALT)
6341 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006342 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006343#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006346 ? "client finished"
6347 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006348
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006349 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006350
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006351 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006352 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006355
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006356 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006357
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006358 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006365static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006367{
6368 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006369 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006370 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006371 unsigned char padbuf[48];
6372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006374 if( !session )
6375 session = ssl->session;
6376
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006377 mbedtls_sha512_init( &sha512 );
6378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006380
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006381 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006382
6383 /*
6384 * TLSv1.2:
6385 * hash = PRF( master, finished_label,
6386 * Hash( handshake ) )[0.11]
6387 */
6388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006390 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6391 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006392#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006395 ? "client finished"
6396 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006398 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006399
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006400 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006401 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006404
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006405 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006406
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006407 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411#endif /* MBEDTLS_SHA512_C */
6412#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006417
6418 /*
6419 * Free our handshake params
6420 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006421 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006423 ssl->handshake = NULL;
6424
6425 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006426 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006427 */
6428 if( ssl->transform )
6429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 mbedtls_ssl_transform_free( ssl->transform );
6431 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006432 }
6433 ssl->transform = ssl->transform_negotiate;
6434 ssl->transform_negotiate = NULL;
6435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006437}
6438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006440{
6441 int resume = ssl->handshake->resume;
6442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_RENEGOTIATION)
6446 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006449 ssl->renego_records_seen = 0;
6450 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006451#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006452
6453 /*
6454 * Free the previous session and switch in the current one
6455 */
Paul Bakker0a597072012-09-25 21:55:46 +00006456 if( ssl->session )
6457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006459 /* RFC 7366 3.1: keep the EtM state */
6460 ssl->session_negotiate->encrypt_then_mac =
6461 ssl->session->encrypt_then_mac;
6462#endif
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 mbedtls_ssl_session_free( ssl->session );
6465 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006466 }
6467 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006468 ssl->session_negotiate = NULL;
6469
Paul Bakker0a597072012-09-25 21:55:46 +00006470 /*
6471 * Add cache entry
6472 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006473 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006474 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006475 resume == 0 )
6476 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006477 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006479 }
Paul Bakker0a597072012-09-25 21:55:46 +00006480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006482 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006483 ssl->handshake->flight != NULL )
6484 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006485 /* Cancel handshake timer */
6486 ssl_set_timer( ssl, 0 );
6487
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006488 /* Keep last flight around in case we need to resend it:
6489 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006491 }
6492 else
6493#endif
6494 ssl_handshake_wrapup_free_hs_transform( ssl );
6495
Paul Bakker48916f92012-09-16 19:57:18 +00006496 ssl->state++;
6497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006499}
6500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006502{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006503 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006506
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006507 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006508
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006509 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006510
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006511 /*
6512 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6513 * may define some other value. Currently (early 2016), no defined
6514 * ciphersuite does this (and this is unlikely to change as activity has
6515 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006520 ssl->verify_data_len = hash_len;
6521 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006522#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006523
Paul Bakker5121ce52009-01-03 21:22:43 +00006524 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6526 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006527
6528 /*
6529 * In case of session resuming, invert the client and server
6530 * ChangeCipherSpec messages order.
6531 */
Paul Bakker0a597072012-09-25 21:55:46 +00006532 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006537#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006539 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006541#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 }
6543 else
6544 ssl->state++;
6545
Paul Bakker48916f92012-09-16 19:57:18 +00006546 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006547 * Switch to our negotiated transform and session parameters for outbound
6548 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006553 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006554 {
6555 unsigned char i;
6556
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006557 /* Remember current epoch settings for resending */
6558 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006559 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006560
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006561 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006562 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006563
6564 /* Increment epoch */
6565 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006566 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006567 break;
6568
6569 /* The loop goes to its end iff the counter is wrapping */
6570 if( i == 0 )
6571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6573 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006574 }
6575 }
6576 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006578 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006579
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006580 ssl->transform_out = ssl->transform_negotiate;
6581 ssl->session_out = ssl->session_negotiate;
6582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6584 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6589 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006590 }
6591 }
6592#endif
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006597#endif
6598
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006599 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006600 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 return( ret );
6603 }
6604
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006605#if defined(MBEDTLS_SSL_PROTO_DTLS)
6606 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6607 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6608 {
6609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6610 return( ret );
6611 }
6612#endif
6613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006615
6616 return( 0 );
6617}
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006620#define SSL_MAX_HASH_LEN 36
6621#else
6622#define SSL_MAX_HASH_LEN 12
6623#endif
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006626{
Paul Bakker23986e52011-04-24 08:57:21 +00006627 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006628 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006629 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006632
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006633 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006634
Hanno Becker327c93b2018-08-15 13:56:18 +01006635 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 return( ret );
6639 }
6640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006644 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6645 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006647 }
6648
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006649 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650#if defined(MBEDTLS_SSL_PROTO_SSL3)
6651 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006652 hash_len = 36;
6653 else
6654#endif
6655 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6658 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6662 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006664 }
6665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006667 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006670 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6671 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 }
6674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006676 ssl->verify_data_len = hash_len;
6677 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006678#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006679
Paul Bakker0a597072012-09-25 21:55:46 +00006680 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006683 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006687 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006689#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006690 }
6691 else
6692 ssl->state++;
6693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006697#endif
6698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700
6701 return( 0 );
6702}
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6709 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6710 mbedtls_md5_init( &handshake->fin_md5 );
6711 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006712 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6713 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6716#if defined(MBEDTLS_SHA256_C)
6717 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006718 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720#if defined(MBEDTLS_SHA512_C)
6721 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006722 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006725
6726 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006727
6728#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6729 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6730 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6731#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733#if defined(MBEDTLS_DHM_C)
6734 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736#if defined(MBEDTLS_ECDH_C)
6737 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006738#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006739#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006740 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006741#if defined(MBEDTLS_SSL_CLI_C)
6742 handshake->ecjpake_cache = NULL;
6743 handshake->ecjpake_cache_len = 0;
6744#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006745#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006746
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006747#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006748 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006749#endif
6750
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006751#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6752 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6753#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006754}
6755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6761 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 mbedtls_md_init( &transform->md_ctx_enc );
6764 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006765}
6766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006770}
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006773{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006774 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006775 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006777 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006779 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006780 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006781
6782 /*
6783 * Either the pointers are now NULL or cleared properly and can be freed.
6784 * Now allocate missing structures.
6785 */
6786 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006787 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006788 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006789 }
Paul Bakker48916f92012-09-16 19:57:18 +00006790
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006791 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006792 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006793 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006794 }
Paul Bakker48916f92012-09-16 19:57:18 +00006795
Paul Bakker82788fb2014-10-20 13:59:19 +02006796 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006797 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006798 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006799 }
Paul Bakker48916f92012-09-16 19:57:18 +00006800
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006801 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006802 if( ssl->handshake == NULL ||
6803 ssl->transform_negotiate == NULL ||
6804 ssl->session_negotiate == NULL )
6805 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 mbedtls_free( ssl->handshake );
6809 mbedtls_free( ssl->transform_negotiate );
6810 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006811
6812 ssl->handshake = NULL;
6813 ssl->transform_negotiate = NULL;
6814 ssl->session_negotiate = NULL;
6815
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006816 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006817 }
6818
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006819 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006821 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006822 ssl_handshake_params_init( ssl->handshake );
6823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006825 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6826 {
6827 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006828
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006829 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6830 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6831 else
6832 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006833
6834 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006835 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006836#endif
6837
Paul Bakker48916f92012-09-16 19:57:18 +00006838 return( 0 );
6839}
6840
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006841#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006842/* Dummy cookie callbacks for defaults */
6843static int ssl_cookie_write_dummy( void *ctx,
6844 unsigned char **p, unsigned char *end,
6845 const unsigned char *cli_id, size_t cli_id_len )
6846{
6847 ((void) ctx);
6848 ((void) p);
6849 ((void) end);
6850 ((void) cli_id);
6851 ((void) cli_id_len);
6852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006854}
6855
6856static int ssl_cookie_check_dummy( void *ctx,
6857 const unsigned char *cookie, size_t cookie_len,
6858 const unsigned char *cli_id, size_t cli_id_len )
6859{
6860 ((void) ctx);
6861 ((void) cookie);
6862 ((void) cookie_len);
6863 ((void) cli_id);
6864 ((void) cli_id_len);
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006867}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006868#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006869
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006870/* Once ssl->out_hdr as the address of the beginning of the
6871 * next outgoing record is set, deduce the other pointers.
6872 *
6873 * Note: For TLS, we save the implicit record sequence number
6874 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6875 * and the caller has to make sure there's space for this.
6876 */
6877
6878static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6879 mbedtls_ssl_transform *transform )
6880{
6881#if defined(MBEDTLS_SSL_PROTO_DTLS)
6882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6883 {
6884 ssl->out_ctr = ssl->out_hdr + 3;
6885 ssl->out_len = ssl->out_hdr + 11;
6886 ssl->out_iv = ssl->out_hdr + 13;
6887 }
6888 else
6889#endif
6890 {
6891 ssl->out_ctr = ssl->out_hdr - 8;
6892 ssl->out_len = ssl->out_hdr + 3;
6893 ssl->out_iv = ssl->out_hdr + 5;
6894 }
6895
6896 /* Adjust out_msg to make space for explicit IV, if used. */
6897 if( transform != NULL &&
6898 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6899 {
6900 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6901 }
6902 else
6903 ssl->out_msg = ssl->out_iv;
6904}
6905
6906/* Once ssl->in_hdr as the address of the beginning of the
6907 * next incoming record is set, deduce the other pointers.
6908 *
6909 * Note: For TLS, we save the implicit record sequence number
6910 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6911 * and the caller has to make sure there's space for this.
6912 */
6913
6914static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6915 mbedtls_ssl_transform *transform )
6916{
6917#if defined(MBEDTLS_SSL_PROTO_DTLS)
6918 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6919 {
6920 ssl->in_ctr = ssl->in_hdr + 3;
6921 ssl->in_len = ssl->in_hdr + 11;
6922 ssl->in_iv = ssl->in_hdr + 13;
6923 }
6924 else
6925#endif
6926 {
6927 ssl->in_ctr = ssl->in_hdr - 8;
6928 ssl->in_len = ssl->in_hdr + 3;
6929 ssl->in_iv = ssl->in_hdr + 5;
6930 }
6931
6932 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6933 if( transform != NULL &&
6934 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6935 {
6936 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6937 }
6938 else
6939 ssl->in_msg = ssl->in_iv;
6940}
6941
Paul Bakker5121ce52009-01-03 21:22:43 +00006942/*
6943 * Initialize an SSL context
6944 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006945void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6946{
6947 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6948}
6949
6950/*
6951 * Setup an SSL context
6952 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006953
6954static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6955{
6956 /* Set the incoming and outgoing record pointers. */
6957#if defined(MBEDTLS_SSL_PROTO_DTLS)
6958 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6959 {
6960 ssl->out_hdr = ssl->out_buf;
6961 ssl->in_hdr = ssl->in_buf;
6962 }
6963 else
6964#endif /* MBEDTLS_SSL_PROTO_DTLS */
6965 {
6966 ssl->out_hdr = ssl->out_buf + 8;
6967 ssl->in_hdr = ssl->in_buf + 8;
6968 }
6969
6970 /* Derive other internal pointers. */
6971 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6972 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6973}
6974
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006975int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006976 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006977{
Paul Bakker48916f92012-09-16 19:57:18 +00006978 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006979
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006980 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006981
6982 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006983 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006984 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006985
6986 /* Set to NULL in case of an error condition */
6987 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006988
Angus Grattond8213d02016-05-25 20:56:48 +10006989 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6990 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 {
Angus Grattond8213d02016-05-25 20:56:48 +10006992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006993 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006994 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006995 }
6996
6997 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6998 if( ssl->out_buf == NULL )
6999 {
7000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007001 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007002 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007003 }
7004
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007005 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007006
Paul Bakker48916f92012-09-16 19:57:18 +00007007 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007008 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
7010 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007011
7012error:
7013 mbedtls_free( ssl->in_buf );
7014 mbedtls_free( ssl->out_buf );
7015
7016 ssl->conf = NULL;
7017
7018 ssl->in_buf = NULL;
7019 ssl->out_buf = NULL;
7020
7021 ssl->in_hdr = NULL;
7022 ssl->in_ctr = NULL;
7023 ssl->in_len = NULL;
7024 ssl->in_iv = NULL;
7025 ssl->in_msg = NULL;
7026
7027 ssl->out_hdr = NULL;
7028 ssl->out_ctr = NULL;
7029 ssl->out_len = NULL;
7030 ssl->out_iv = NULL;
7031 ssl->out_msg = NULL;
7032
k-stachowiak9f7798e2018-07-31 16:52:32 +02007033 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007034}
7035
7036/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007037 * Reset an initialized and used SSL context for re-use while retaining
7038 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007039 *
7040 * If partial is non-zero, keep data in the input buffer and client ID.
7041 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007042 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007043static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007044{
Paul Bakker48916f92012-09-16 19:57:18 +00007045 int ret;
7046
Hanno Becker7e772132018-08-10 12:38:21 +01007047#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7048 !defined(MBEDTLS_SSL_SRV_C)
7049 ((void) partial);
7050#endif
7051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007053
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007054 /* Cancel any possibly running timer */
7055 ssl_set_timer( ssl, 0 );
7056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057#if defined(MBEDTLS_SSL_RENEGOTIATION)
7058 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007059 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007060
7061 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7063 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007066
Paul Bakker7eb013f2011-10-06 12:37:39 +00007067 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007068 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007069
7070 ssl->in_msgtype = 0;
7071 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007073 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007074 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007077 ssl_dtls_replay_reset( ssl );
7078#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007079
7080 ssl->in_hslen = 0;
7081 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007082
7083 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007084
7085 ssl->out_msgtype = 0;
7086 ssl->out_msglen = 0;
7087 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7089 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007090 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007091#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007092
Hanno Becker19859472018-08-06 09:40:20 +01007093 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7094
Paul Bakker48916f92012-09-16 19:57:18 +00007095 ssl->transform_in = NULL;
7096 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007097
Hanno Becker78640902018-08-13 16:35:15 +01007098 ssl->session_in = NULL;
7099 ssl->session_out = NULL;
7100
Angus Grattond8213d02016-05-25 20:56:48 +10007101 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007102
7103#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007104 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007105#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7106 {
7107 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007108 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007109 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7112 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7115 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7118 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007119 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007120 }
7121#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007122
Paul Bakker48916f92012-09-16 19:57:18 +00007123 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 mbedtls_ssl_transform_free( ssl->transform );
7126 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007127 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007128 }
Paul Bakker48916f92012-09-16 19:57:18 +00007129
Paul Bakkerc0463502013-02-14 11:19:38 +01007130 if( ssl->session )
7131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 mbedtls_ssl_session_free( ssl->session );
7133 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007134 ssl->session = NULL;
7135 }
7136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007138 ssl->alpn_chosen = NULL;
7139#endif
7140
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007141#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007142#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007143 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007144#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007145 {
7146 mbedtls_free( ssl->cli_id );
7147 ssl->cli_id = NULL;
7148 ssl->cli_id_len = 0;
7149 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007150#endif
7151
Paul Bakker48916f92012-09-16 19:57:18 +00007152 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7153 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007154
7155 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007156}
7157
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007158/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007159 * Reset an initialized and used SSL context for re-use while retaining
7160 * all application-set variables, function pointers and data.
7161 */
7162int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7163{
7164 return( ssl_session_reset_int( ssl, 0 ) );
7165}
7166
7167/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007168 * SSL set accessors
7169 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007170void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007171{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007172 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007173}
7174
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007175void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007177 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007178}
7179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007181void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007182{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007183 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007184}
7185#endif
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007188void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007189{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007190 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007191}
7192#endif
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007195
Hanno Becker1841b0a2018-08-24 11:13:57 +01007196void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7197 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007198{
7199 ssl->disable_datagram_packing = !allow_packing;
7200}
7201
7202void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7203 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007204{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007205 conf->hs_timeout_min = min;
7206 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007207}
7208#endif
7209
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007210void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007212 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007213}
7214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007216void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007217 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007218 void *p_vrfy )
7219{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007220 conf->f_vrfy = f_vrfy;
7221 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007224
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007225void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007226 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007227 void *p_rng )
7228{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007229 conf->f_rng = f_rng;
7230 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007231}
7232
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007233void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007234 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007235 void *p_dbg )
7236{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007237 conf->f_dbg = f_dbg;
7238 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007239}
7240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007242 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007243 mbedtls_ssl_send_t *f_send,
7244 mbedtls_ssl_recv_t *f_recv,
7245 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007246{
7247 ssl->p_bio = p_bio;
7248 ssl->f_send = f_send;
7249 ssl->f_recv = f_recv;
7250 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007251}
7252
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007253#if defined(MBEDTLS_SSL_PROTO_DTLS)
7254void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7255{
7256 ssl->mtu = mtu;
7257}
7258#endif
7259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007260void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007261{
7262 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007263}
7264
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007265void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7266 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007267 mbedtls_ssl_set_timer_t *f_set_timer,
7268 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007269{
7270 ssl->p_timer = p_timer;
7271 ssl->f_set_timer = f_set_timer;
7272 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007273
7274 /* Make sure we start with no timer running */
7275 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007276}
7277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007279void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007280 void *p_cache,
7281 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7282 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007283{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007284 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007285 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007286 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#if defined(MBEDTLS_SSL_CLI_C)
7291int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007292{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007293 int ret;
7294
7295 if( ssl == NULL ||
7296 session == NULL ||
7297 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007298 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007301 }
7302
7303 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7304 return( ret );
7305
Paul Bakker0a597072012-09-25 21:55:46 +00007306 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007307
7308 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007311
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007312void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007313 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007314{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007315 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7316 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7317 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7318 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007319}
7320
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007321void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007322 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007323 int major, int minor )
7324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007326 return;
7327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007329 return;
7330
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007331 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007332}
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007335void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007336 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007337{
7338 conf->cert_profile = profile;
7339}
7340
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007341/* Append a new keycert entry to a (possibly empty) list */
7342static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7343 mbedtls_x509_crt *cert,
7344 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007345{
niisato8ee24222018-06-25 19:05:48 +09007346 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007347
niisato8ee24222018-06-25 19:05:48 +09007348 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7349 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007350 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007351
niisato8ee24222018-06-25 19:05:48 +09007352 new_cert->cert = cert;
7353 new_cert->key = key;
7354 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007355
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007356 /* Update head is the list was null, else add to the end */
7357 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007358 {
niisato8ee24222018-06-25 19:05:48 +09007359 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007360 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007361 else
7362 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007363 mbedtls_ssl_key_cert *cur = *head;
7364 while( cur->next != NULL )
7365 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007366 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007367 }
7368
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007369 return( 0 );
7370}
7371
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007372int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007373 mbedtls_x509_crt *own_cert,
7374 mbedtls_pk_context *pk_key )
7375{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007376 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007377}
7378
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007379void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007380 mbedtls_x509_crt *ca_chain,
7381 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007382{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007383 conf->ca_chain = ca_chain;
7384 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007387
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007388#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7389int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7390 mbedtls_x509_crt *own_cert,
7391 mbedtls_pk_context *pk_key )
7392{
7393 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7394 own_cert, pk_key ) );
7395}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007396
7397void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7398 mbedtls_x509_crt *ca_chain,
7399 mbedtls_x509_crl *ca_crl )
7400{
7401 ssl->handshake->sni_ca_chain = ca_chain;
7402 ssl->handshake->sni_ca_crl = ca_crl;
7403}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007404
7405void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7406 int authmode )
7407{
7408 ssl->handshake->sni_authmode = authmode;
7409}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007410#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7411
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007412#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007413/*
7414 * Set EC J-PAKE password for current handshake
7415 */
7416int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7417 const unsigned char *pw,
7418 size_t pw_len )
7419{
7420 mbedtls_ecjpake_role role;
7421
Janos Follath8eb64132016-06-03 15:40:57 +01007422 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7424
7425 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7426 role = MBEDTLS_ECJPAKE_SERVER;
7427 else
7428 role = MBEDTLS_ECJPAKE_CLIENT;
7429
7430 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7431 role,
7432 MBEDTLS_MD_SHA256,
7433 MBEDTLS_ECP_DP_SECP256R1,
7434 pw, pw_len ) );
7435}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007436#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007439int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007440 const unsigned char *psk, size_t psk_len,
7441 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007442{
Paul Bakker6db455e2013-09-18 17:29:31 +02007443 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007448
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007449 /* Identity len will be encoded on two bytes */
7450 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007451 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007452 {
7453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7454 }
7455
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007456 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007457 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007458 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007459
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007460 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007461 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007462 conf->psk_len = 0;
7463 }
7464 if( conf->psk_identity != NULL )
7465 {
7466 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007467 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007468 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007469 }
7470
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007471 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7472 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007473 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007474 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007475 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007476 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007477 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007478 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007479 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007480
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007481 conf->psk_len = psk_len;
7482 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007483
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007484 memcpy( conf->psk, psk, conf->psk_len );
7485 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007486
7487 return( 0 );
7488}
7489
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007490int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7491 const unsigned char *psk, size_t psk_len )
7492{
7493 if( psk == NULL || ssl->handshake == NULL )
7494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7495
7496 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7498
7499 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007500 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007501 mbedtls_platform_zeroize( ssl->handshake->psk,
7502 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007503 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007504 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007505 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007506
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007507 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007508 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007509
7510 ssl->handshake->psk_len = psk_len;
7511 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7512
7513 return( 0 );
7514}
7515
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007516void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007518 size_t),
7519 void *p_psk )
7520{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007521 conf->f_psk = f_psk;
7522 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007525
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007526#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007527
7528#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007529int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007530{
7531 int ret;
7532
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007533 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7534 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7535 {
7536 mbedtls_mpi_free( &conf->dhm_P );
7537 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007540
7541 return( 0 );
7542}
Hanno Becker470a8c42017-10-04 15:28:46 +01007543#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007544
Hanno Beckera90658f2017-10-04 15:29:08 +01007545int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7546 const unsigned char *dhm_P, size_t P_len,
7547 const unsigned char *dhm_G, size_t G_len )
7548{
7549 int ret;
7550
7551 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7552 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7553 {
7554 mbedtls_mpi_free( &conf->dhm_P );
7555 mbedtls_mpi_free( &conf->dhm_G );
7556 return( ret );
7557 }
7558
7559 return( 0 );
7560}
Paul Bakker5121ce52009-01-03 21:22:43 +00007561
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007562int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007563{
7564 int ret;
7565
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007566 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7567 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7568 {
7569 mbedtls_mpi_free( &conf->dhm_P );
7570 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007571 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007572 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007573
7574 return( 0 );
7575}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007576#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007577
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007578#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7579/*
7580 * Set the minimum length for Diffie-Hellman parameters
7581 */
7582void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7583 unsigned int bitlen )
7584{
7585 conf->dhm_min_bitlen = bitlen;
7586}
7587#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7588
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007589#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007590/*
7591 * Set allowed/preferred hashes for handshake signatures
7592 */
7593void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7594 const int *hashes )
7595{
7596 conf->sig_hashes = hashes;
7597}
Hanno Becker947194e2017-04-07 13:25:49 +01007598#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007599
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007600#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007601/*
7602 * Set the allowed elliptic curves
7603 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007604void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007605 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007606{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007607 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007608}
Hanno Becker947194e2017-04-07 13:25:49 +01007609#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007610
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007611#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007613{
Hanno Becker947194e2017-04-07 13:25:49 +01007614 /* Initialize to suppress unnecessary compiler warning */
7615 size_t hostname_len = 0;
7616
7617 /* Check if new hostname is valid before
7618 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007619 if( hostname != NULL )
7620 {
7621 hostname_len = strlen( hostname );
7622
7623 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7625 }
7626
7627 /* Now it's clear that we will overwrite the old hostname,
7628 * so we can free it safely */
7629
7630 if( ssl->hostname != NULL )
7631 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007632 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007633 mbedtls_free( ssl->hostname );
7634 }
7635
7636 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007637
Paul Bakker5121ce52009-01-03 21:22:43 +00007638 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007639 {
7640 ssl->hostname = NULL;
7641 }
7642 else
7643 {
7644 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007645 if( ssl->hostname == NULL )
7646 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007647
Hanno Becker947194e2017-04-07 13:25:49 +01007648 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007649
Hanno Becker947194e2017-04-07 13:25:49 +01007650 ssl->hostname[hostname_len] = '\0';
7651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007652
7653 return( 0 );
7654}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007655#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007656
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007657#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007658void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007660 const unsigned char *, size_t),
7661 void *p_sni )
7662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007663 conf->f_sni = f_sni;
7664 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007669int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007670{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007671 size_t cur_len, tot_len;
7672 const char **p;
7673
7674 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007675 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7676 * MUST NOT be truncated."
7677 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007678 */
7679 tot_len = 0;
7680 for( p = protos; *p != NULL; p++ )
7681 {
7682 cur_len = strlen( *p );
7683 tot_len += cur_len;
7684
Ronald Cron157cffe2020-04-23 16:41:44 +02007685 if( ( cur_len == 0 ) ||
7686 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7687 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007689 }
7690
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007691 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007692
7693 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007694}
7695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007697{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007698 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007701
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007702void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007704 conf->max_major_ver = major;
7705 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007706}
7707
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007708void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007709{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007710 conf->min_major_ver = major;
7711 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007712}
7713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007715void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007716{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007717 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007718}
7719#endif
7720
Janos Follath088ce432017-04-10 12:42:31 +01007721#if defined(MBEDTLS_SSL_SRV_C)
7722void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7723 char cert_req_ca_list )
7724{
7725 conf->cert_req_ca_list = cert_req_ca_list;
7726}
7727#endif
7728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007730void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007731{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007732 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007733}
7734#endif
7735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007737void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007739 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007740}
7741#endif
7742
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007743#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007744void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007745{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007746 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007747}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007748#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007751int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007754 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007757 }
7758
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007759 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007760
7761 return( 0 );
7762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007766void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007767{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007768 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007773void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007774{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007775 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007776}
7777#endif
7778
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007779void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007780{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007781 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007782}
7783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007785void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007787 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007788}
7789
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007790void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007791{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007792 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007793}
7794
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007795void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007796 const unsigned char period[8] )
7797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007798 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007803#if defined(MBEDTLS_SSL_CLI_C)
7804void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007805{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007806 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007807}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007808#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007809
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007810#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007811void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7812 mbedtls_ssl_ticket_write_t *f_ticket_write,
7813 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7814 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007815{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007816 conf->f_ticket_write = f_ticket_write;
7817 conf->f_ticket_parse = f_ticket_parse;
7818 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007819}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007822
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007823#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7824void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7825 mbedtls_ssl_export_keys_t *f_export_keys,
7826 void *p_export_keys )
7827{
7828 conf->f_export_keys = f_export_keys;
7829 conf->p_export_keys = p_export_keys;
7830}
7831#endif
7832
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007833#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007834void mbedtls_ssl_conf_async_private_cb(
7835 mbedtls_ssl_config *conf,
7836 mbedtls_ssl_async_sign_t *f_async_sign,
7837 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7838 mbedtls_ssl_async_resume_t *f_async_resume,
7839 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007840 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007841{
7842 conf->f_async_sign_start = f_async_sign;
7843 conf->f_async_decrypt_start = f_async_decrypt;
7844 conf->f_async_resume = f_async_resume;
7845 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007846 conf->p_async_config_data = async_config_data;
7847}
7848
Gilles Peskine8f97af72018-04-26 11:46:10 +02007849void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7850{
7851 return( conf->p_async_config_data );
7852}
7853
Gilles Peskine1febfef2018-04-30 11:54:39 +02007854void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007855{
7856 if( ssl->handshake == NULL )
7857 return( NULL );
7858 else
7859 return( ssl->handshake->user_async_ctx );
7860}
7861
Gilles Peskine1febfef2018-04-30 11:54:39 +02007862void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007863 void *ctx )
7864{
7865 if( ssl->handshake != NULL )
7866 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007867}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007868#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007869
Paul Bakker5121ce52009-01-03 21:22:43 +00007870/*
7871 * SSL get accessors
7872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007874{
7875 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7876}
7877
Hanno Becker8b170a02017-10-10 11:51:19 +01007878int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7879{
7880 /*
7881 * Case A: We're currently holding back
7882 * a message for further processing.
7883 */
7884
7885 if( ssl->keep_current_message == 1 )
7886 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007888 return( 1 );
7889 }
7890
7891 /*
7892 * Case B: Further records are pending in the current datagram.
7893 */
7894
7895#if defined(MBEDTLS_SSL_PROTO_DTLS)
7896 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7897 ssl->in_left > ssl->next_record_offset )
7898 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007900 return( 1 );
7901 }
7902#endif /* MBEDTLS_SSL_PROTO_DTLS */
7903
7904 /*
7905 * Case C: A handshake message is being processed.
7906 */
7907
Hanno Becker8b170a02017-10-10 11:51:19 +01007908 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7909 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007911 return( 1 );
7912 }
7913
7914 /*
7915 * Case D: An application data message is being processed
7916 */
7917 if( ssl->in_offt != NULL )
7918 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007920 return( 1 );
7921 }
7922
7923 /*
7924 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007925 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007926 * we implement support for multiple alerts in single records.
7927 */
7928
7929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7930 return( 0 );
7931}
7932
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007933uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007934{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007935 if( ssl->session != NULL )
7936 return( ssl->session->verify_result );
7937
7938 if( ssl->session_negotiate != NULL )
7939 return( ssl->session_negotiate->verify_result );
7940
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007941 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007942}
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007945{
Paul Bakker926c8e42013-03-06 10:23:34 +01007946 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007947 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007950}
7951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007956 {
7957 switch( ssl->minor_ver )
7958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007960 return( "DTLSv1.0" );
7961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007963 return( "DTLSv1.2" );
7964
7965 default:
7966 return( "unknown (DTLS)" );
7967 }
7968 }
7969#endif
7970
Paul Bakker43ca69c2011-01-15 17:35:19 +00007971 switch( ssl->minor_ver )
7972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007974 return( "SSLv3.0" );
7975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007977 return( "TLSv1.0" );
7978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007980 return( "TLSv1.1" );
7981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007983 return( "TLSv1.2" );
7984
Paul Bakker43ca69c2011-01-15 17:35:19 +00007985 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007986 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007987 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007988}
7989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007991{
Hanno Becker3136ede2018-08-17 15:28:19 +01007992 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007994 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007995
Hanno Becker78640902018-08-13 16:35:15 +01007996 if( transform == NULL )
7997 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_ZLIB_SUPPORT)
8000 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8001 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008002#endif
8003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 case MBEDTLS_MODE_GCM:
8007 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008008 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008010 transform_expansion = transform->minlen;
8011 break;
8012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008014
8015 block_size = mbedtls_cipher_get_block_size(
8016 &transform->cipher_ctx_enc );
8017
Hanno Becker3136ede2018-08-17 15:28:19 +01008018 /* Expansion due to the addition of the MAC. */
8019 transform_expansion += transform->maclen;
8020
8021 /* Expansion due to the addition of CBC padding;
8022 * Theoretically up to 256 bytes, but we never use
8023 * more than the block size of the underlying cipher. */
8024 transform_expansion += block_size;
8025
8026 /* For TLS 1.1 or higher, an explicit IV is added
8027 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008028#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8029 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008030 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008031#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008032
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008033 break;
8034
8035 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008038 }
8039
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008040 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008041}
8042
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008043#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8044size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8045{
8046 size_t max_len;
8047
8048 /*
8049 * Assume mfl_code is correct since it was checked when set
8050 */
Angus Grattond8213d02016-05-25 20:56:48 +10008051 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008052
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008053 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008054 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008055 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008056 {
Angus Grattond8213d02016-05-25 20:56:48 +10008057 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008058 }
8059
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008060 /* During a handshake, use the value being negotiated */
8061 if( ssl->session_negotiate != NULL &&
8062 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8063 {
8064 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8065 }
8066
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008067 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008068}
8069#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8070
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008071#if defined(MBEDTLS_SSL_PROTO_DTLS)
8072static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8073{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008074 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8075 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8076 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8077 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8078 return ( 0 );
8079
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008080 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8081 return( ssl->mtu );
8082
8083 if( ssl->mtu == 0 )
8084 return( ssl->handshake->mtu );
8085
8086 return( ssl->mtu < ssl->handshake->mtu ?
8087 ssl->mtu : ssl->handshake->mtu );
8088}
8089#endif /* MBEDTLS_SSL_PROTO_DTLS */
8090
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008091int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8092{
8093 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8094
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008095#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8096 !defined(MBEDTLS_SSL_PROTO_DTLS)
8097 (void) ssl;
8098#endif
8099
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008100#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8101 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8102
8103 if( max_len > mfl )
8104 max_len = mfl;
8105#endif
8106
8107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008108 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008109 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008110 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008111 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8112 const size_t overhead = (size_t) ret;
8113
8114 if( ret < 0 )
8115 return( ret );
8116
8117 if( mtu <= overhead )
8118 {
8119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8120 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8121 }
8122
8123 if( max_len > mtu - overhead )
8124 max_len = mtu - overhead;
8125 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008126#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008127
Hanno Becker0defedb2018-08-10 12:35:02 +01008128#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8129 !defined(MBEDTLS_SSL_PROTO_DTLS)
8130 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008131#endif
8132
8133 return( (int) max_len );
8134}
8135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136#if defined(MBEDTLS_X509_CRT_PARSE_C)
8137const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008138{
8139 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008140 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008141
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008142 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#if defined(MBEDTLS_SSL_CLI_C)
8147int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008148{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008149 if( ssl == NULL ||
8150 dst == NULL ||
8151 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008152 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008155 }
8156
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008157 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008160
Paul Bakker5121ce52009-01-03 21:22:43 +00008161/*
Paul Bakker1961b702013-01-25 14:49:24 +01008162 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008167
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008168 if( ssl == NULL || ssl->conf == NULL )
8169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008172 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008176 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008178#endif
8179
Paul Bakker1961b702013-01-25 14:49:24 +01008180 return( ret );
8181}
8182
8183/*
8184 * Perform the SSL handshake
8185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008187{
8188 int ret = 0;
8189
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008190 if( ssl == NULL || ssl->conf == NULL )
8191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008198
8199 if( ret != 0 )
8200 break;
8201 }
8202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008204
8205 return( ret );
8206}
8207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208#if defined(MBEDTLS_SSL_RENEGOTIATION)
8209#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008210/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008211 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008214{
8215 int ret;
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008218
8219 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8221 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008222
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008223 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008224 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008226 return( ret );
8227 }
8228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008230
8231 return( 0 );
8232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008234
8235/*
8236 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 * - any side: calling mbedtls_ssl_renegotiate(),
8238 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8239 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008240 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008241 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008245{
8246 int ret;
8247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008249
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008250 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8251 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008252
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008253 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8254 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008258 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008259 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008260 ssl->handshake->out_msg_seq = 1;
8261 else
8262 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008263 }
8264#endif
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8267 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008272 return( ret );
8273 }
8274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008276
8277 return( 0 );
8278}
8279
8280/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008281 * Renegotiate current connection on client,
8282 * or request renegotiation on server
8283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008287
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008288 if( ssl == NULL || ssl->conf == NULL )
8289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008292 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008293 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008299
8300 /* Did we already try/start sending HelloRequest? */
8301 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008303
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008304 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008309 /*
8310 * On client, either start the renegotiation process or,
8311 * if already in progress, continue the handshake
8312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008317
8318 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008321 return( ret );
8322 }
8323 }
8324 else
8325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008329 return( ret );
8330 }
8331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008333
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008334 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008335}
8336
8337/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008338 * Check record counters and renegotiate if they're above the limit.
8339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008341{
Andres AG2196c7f2016-12-15 17:01:16 +00008342 size_t ep_len = ssl_ep_len( ssl );
8343 int in_ctr_cmp;
8344 int out_ctr_cmp;
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8347 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008348 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008349 {
8350 return( 0 );
8351 }
8352
Andres AG2196c7f2016-12-15 17:01:16 +00008353 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8354 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008355 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008356 ssl->conf->renego_period + ep_len, 8 - ep_len );
8357
8358 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008359 {
8360 return( 0 );
8361 }
8362
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008367
8368/*
8369 * Receive application data decrypted from the SSL layer
8370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008372{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008373 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008374 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008375
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, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008382 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008385 return( ret );
8386
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008387 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008389 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008390 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008391 return( ret );
8392 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008393 }
8394#endif
8395
Hanno Becker4a810fb2017-05-24 16:27:30 +01008396 /*
8397 * Check if renegotiation is necessary and/or handshake is
8398 * in process. If yes, perform/continue, and fall through
8399 * if an unexpected packet is received while the client
8400 * is waiting for the ServerHello.
8401 *
8402 * (There is no equivalent to the last condition on
8403 * the server-side as it is not treated as within
8404 * a handshake while waiting for the ClientHello
8405 * after a renegotiation request.)
8406 */
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008409 ret = ssl_check_ctr_renegotiate( ssl );
8410 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8411 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008414 return( ret );
8415 }
8416#endif
8417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008421 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8422 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008425 return( ret );
8426 }
8427 }
8428
Hanno Beckere41158b2017-10-23 13:30:32 +01008429 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008430 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008431 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008432 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008433 if( ssl->f_get_timer != NULL &&
8434 ssl->f_get_timer( ssl->p_timer ) == -1 )
8435 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008436 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008437 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008438
Hanno Becker327c93b2018-08-15 13:56:18 +01008439 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008440 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008441 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8442 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008443
Hanno Becker4a810fb2017-05-24 16:27:30 +01008444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8445 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008446 }
8447
8448 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 {
8451 /*
8452 * OpenSSL sends empty messages to randomize the IV
8453 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008454 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008457 return( 0 );
8458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008460 return( ret );
8461 }
8462 }
8463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008467
Hanno Becker4a810fb2017-05-24 16:27:30 +01008468 /*
8469 * - For client-side, expect SERVER_HELLO_REQUEST.
8470 * - For server-side, expect CLIENT_HELLO.
8471 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8472 */
8473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008475 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008477 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008480
8481 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008484 {
8485 continue;
8486 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008487#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008489 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008490#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008491
Hanno Becker4a810fb2017-05-24 16:27:30 +01008492#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008493 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008497
8498 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008501 {
8502 continue;
8503 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008504#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008506 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008507#endif /* MBEDTLS_SSL_SRV_C */
8508
Hanno Becker21df7f92017-10-17 11:03:26 +01008509#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008510 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008511 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8512 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8513 ssl->conf->allow_legacy_renegotiation ==
8514 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8515 {
8516 /*
8517 * Accept renegotiation request
8518 */
Paul Bakker48916f92012-09-16 19:57:18 +00008519
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008520 /* DTLS clients need to know renego is server-initiated */
8521#if defined(MBEDTLS_SSL_PROTO_DTLS)
8522 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8523 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8524 {
8525 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8526 }
8527#endif
8528 ret = ssl_start_renegotiation( ssl );
8529 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8530 ret != 0 )
8531 {
8532 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8533 return( ret );
8534 }
8535 }
8536 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008537#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008538 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008539 /*
8540 * Refuse renegotiation
8541 */
8542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545#if defined(MBEDTLS_SSL_PROTO_SSL3)
8546 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008547 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008548 /* SSLv3 does not have a "no_renegotiation" warning, so
8549 we send a fatal alert and abort the connection. */
8550 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8551 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8552 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008553 }
8554 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8556#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8557 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8558 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8561 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8562 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008563 {
8564 return( ret );
8565 }
Paul Bakker48916f92012-09-16 19:57:18 +00008566 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008567 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8569 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008573 }
Paul Bakker48916f92012-09-16 19:57:18 +00008574 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008575
Hanno Becker90333da2017-10-10 11:27:13 +01008576 /* At this point, we don't know whether the renegotiation has been
8577 * completed or not. The cases to consider are the following:
8578 * 1) The renegotiation is complete. In this case, no new record
8579 * has been read yet.
8580 * 2) The renegotiation is incomplete because the client received
8581 * an application data record while awaiting the ServerHello.
8582 * 3) The renegotiation is incomplete because the client received
8583 * a non-handshake, non-application data message while awaiting
8584 * the ServerHello.
8585 * In each of these case, looping will be the proper action:
8586 * - For 1), the next iteration will read a new record and check
8587 * if it's application data.
8588 * - For 2), the loop condition isn't satisfied as application data
8589 * is present, hence continue is the same as break
8590 * - For 3), the loop condition is satisfied and read_record
8591 * will re-deliver the message that was held back by the client
8592 * when expecting the ServerHello.
8593 */
8594 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008595 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008596#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008598 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008599 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008600 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008601 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008604 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008606 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008607 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008608 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8612 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008615 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008616 }
8617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8621 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008622 }
8623
8624 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008625
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008626 /* We're going to return something now, cancel timer,
8627 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008629 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008630
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008631#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008632 /* If we requested renego but received AppData, resend HelloRequest.
8633 * Do it now, after setting in_offt, to avoid taking this branch
8634 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008636 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008638 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008639 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008642 return( ret );
8643 }
8644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008646#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008647 }
8648
8649 n = ( len < ssl->in_msglen )
8650 ? len : ssl->in_msglen;
8651
8652 memcpy( buf, ssl->in_offt, n );
8653 ssl->in_msglen -= n;
8654
8655 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008656 {
8657 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008658 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008659 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008661 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008662 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008663 /* more data available */
8664 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008668
Paul Bakker23986e52011-04-24 08:57:21 +00008669 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008670}
8671
8672/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008673 * Send application data to be encrypted by the SSL layer, taking care of max
8674 * fragment length and buffer size.
8675 *
8676 * According to RFC 5246 Section 6.2.1:
8677 *
8678 * Zero-length fragments of Application data MAY be sent as they are
8679 * potentially useful as a traffic analysis countermeasure.
8680 *
8681 * Therefore, it is possible that the input message length is 0 and the
8682 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008683 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008684static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008685 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008686{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008687 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8688 const size_t max_len = (size_t) ret;
8689
8690 if( ret < 0 )
8691 {
8692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8693 return( ret );
8694 }
8695
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008696 if( len > max_len )
8697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008702 "maximum fragment length: %d > %d",
8703 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008705 }
8706 else
8707#endif
8708 len = max_len;
8709 }
Paul Bakker887bd502011-06-08 13:10:54 +00008710
Paul Bakker5121ce52009-01-03 21:22:43 +00008711 if( ssl->out_left != 0 )
8712 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008713 /*
8714 * The user has previously tried to send the data and
8715 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8716 * written. In this case, we expect the high-level write function
8717 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008722 return( ret );
8723 }
8724 }
Paul Bakker887bd502011-06-08 13:10:54 +00008725 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008726 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008727 /*
8728 * The user is trying to send a message the first time, so we need to
8729 * copy the data into the internal buffers and setup the data structure
8730 * to keep track of partial writes
8731 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008732 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008734 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008735
Hanno Becker67bc7c32018-08-06 11:33:50 +01008736 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008739 return( ret );
8740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008741 }
8742
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008743 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008744}
8745
8746/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008747 * Write application data, doing 1/n-1 splitting if necessary.
8748 *
8749 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008750 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008751 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008754static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008755 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008756{
8757 int ret;
8758
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008759 if( ssl->conf->cbc_record_splitting ==
8760 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008761 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8763 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8764 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008765 {
8766 return( ssl_write_real( ssl, buf, len ) );
8767 }
8768
8769 if( ssl->split_done == 0 )
8770 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008771 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008772 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008773 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008774 }
8775
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008776 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8777 return( ret );
8778 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008779
8780 return( ret + 1 );
8781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008783
8784/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008785 * Write application data (public-facing wrapper)
8786 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008787int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008788{
8789 int ret;
8790
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008792
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008793 if( ssl == NULL || ssl->conf == NULL )
8794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8795
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008796#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008797 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8798 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008799 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008800 return( ret );
8801 }
8802#endif
8803
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008804 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008805 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008806 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008807 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008809 return( ret );
8810 }
8811 }
8812
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008813#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008814 ret = ssl_write_split( ssl, buf, len );
8815#else
8816 ret = ssl_write_real( ssl, buf, len );
8817#endif
8818
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008820
8821 return( ret );
8822}
8823
8824/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008825 * Notify the peer that the connection is being closed
8826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008828{
8829 int ret;
8830
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008831 if( ssl == NULL || ssl->conf == NULL )
8832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008835
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008836 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8842 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8843 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008846 return( ret );
8847 }
8848 }
8849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008851
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008852 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008853}
8854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008856{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008857 if( transform == NULL )
8858 return;
8859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008861 deflateEnd( &transform->ctx_deflate );
8862 inflateEnd( &transform->ctx_inflate );
8863#endif
8864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8866 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 mbedtls_md_free( &transform->md_ctx_enc );
8869 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008870
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008871 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008872}
8873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#if defined(MBEDTLS_X509_CRT_PARSE_C)
8875static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008878
8879 while( cur != NULL )
8880 {
8881 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008883 cur = next;
8884 }
8885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008887
Hanno Becker0271f962018-08-16 13:23:47 +01008888#if defined(MBEDTLS_SSL_PROTO_DTLS)
8889
8890static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8891{
8892 unsigned offset;
8893 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8894
8895 if( hs == NULL )
8896 return;
8897
Hanno Becker283f5ef2018-08-24 09:34:47 +01008898 ssl_free_buffered_record( ssl );
8899
Hanno Becker0271f962018-08-16 13:23:47 +01008900 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008901 ssl_buffering_free_slot( ssl, offset );
8902}
8903
8904static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8905 uint8_t slot )
8906{
8907 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8908 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008909
8910 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8911 return;
8912
Hanno Beckere605b192018-08-21 15:59:07 +01008913 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008914 {
Hanno Beckere605b192018-08-21 15:59:07 +01008915 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008916 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008917 mbedtls_free( hs_buf->data );
8918 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008919 }
8920}
8921
8922#endif /* MBEDTLS_SSL_PROTO_DTLS */
8923
Gilles Peskine9b562d52018-04-25 20:32:43 +02008924void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008925{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008926 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8927
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008928 if( handshake == NULL )
8929 return;
8930
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008931#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8932 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8933 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008934 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008935 handshake->async_in_progress = 0;
8936 }
8937#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8938
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008939#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8940 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8941 mbedtls_md5_free( &handshake->fin_md5 );
8942 mbedtls_sha1_free( &handshake->fin_sha1 );
8943#endif
8944#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8945#if defined(MBEDTLS_SHA256_C)
8946 mbedtls_sha256_free( &handshake->fin_sha256 );
8947#endif
8948#if defined(MBEDTLS_SHA512_C)
8949 mbedtls_sha512_free( &handshake->fin_sha512 );
8950#endif
8951#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953#if defined(MBEDTLS_DHM_C)
8954 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956#if defined(MBEDTLS_ECDH_C)
8957 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008958#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008959#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008960 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008961#if defined(MBEDTLS_SSL_CLI_C)
8962 mbedtls_free( handshake->ecjpake_cache );
8963 handshake->ecjpake_cache = NULL;
8964 handshake->ecjpake_cache_len = 0;
8965#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008966#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008967
Janos Follath4ae5c292016-02-10 11:27:43 +00008968#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8969 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008970 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008972#endif
8973
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008974#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8975 if( handshake->psk != NULL )
8976 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008977 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008978 mbedtls_free( handshake->psk );
8979 }
8980#endif
8981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8983 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008984 /*
8985 * Free only the linked list wrapper, not the keys themselves
8986 * since the belong to the SNI callback
8987 */
8988 if( handshake->sni_key_cert != NULL )
8989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008991
8992 while( cur != NULL )
8993 {
8994 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008996 cur = next;
8997 }
8998 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009000
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009001#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009002 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009003#endif
9004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005#if defined(MBEDTLS_SSL_PROTO_DTLS)
9006 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009007 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009008 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009009#endif
9010
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009011 mbedtls_platform_zeroize( handshake,
9012 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009013}
9014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009016{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009017 if( session == NULL )
9018 return;
9019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009020#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009021 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023 mbedtls_x509_crt_free( session->peer_cert );
9024 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009025 }
Paul Bakkered27a042013-04-18 22:46:23 +02009026#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009027
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009028#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009030#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009031
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009032 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009033}
9034
Paul Bakker5121ce52009-01-03 21:22:43 +00009035/*
9036 * Free an SSL context
9037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009039{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009040 if( ssl == NULL )
9041 return;
9042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009044
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009045 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009046 {
Angus Grattond8213d02016-05-25 20:56:48 +10009047 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009049 }
9050
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009051 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009052 {
Angus Grattond8213d02016-05-25 20:56:48 +10009053 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009055 }
9056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009058 if( ssl->compress_buf != NULL )
9059 {
Angus Grattond8213d02016-05-25 20:56:48 +10009060 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009062 }
9063#endif
9064
Paul Bakker48916f92012-09-16 19:57:18 +00009065 if( ssl->transform )
9066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067 mbedtls_ssl_transform_free( ssl->transform );
9068 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009069 }
9070
9071 if( ssl->handshake )
9072 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009073 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9075 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 mbedtls_free( ssl->handshake );
9078 mbedtls_free( ssl->transform_negotiate );
9079 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009080 }
9081
Paul Bakkerc0463502013-02-14 11:19:38 +01009082 if( ssl->session )
9083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 mbedtls_ssl_session_free( ssl->session );
9085 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009086 }
9087
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009088#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009089 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009090 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009091 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009093 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9097 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9100 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009101 }
9102#endif
9103
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009104#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009106#endif
9107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009109
Paul Bakker86f04f42013-02-14 11:20:09 +01009110 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009111 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009112}
9113
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009114/*
9115 * Initialze mbedtls_ssl_config
9116 */
9117void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9118{
9119 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9120}
9121
Simon Butcherc97b6972015-12-27 23:48:17 +00009122#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009123static int ssl_preset_default_hashes[] = {
9124#if defined(MBEDTLS_SHA512_C)
9125 MBEDTLS_MD_SHA512,
9126 MBEDTLS_MD_SHA384,
9127#endif
9128#if defined(MBEDTLS_SHA256_C)
9129 MBEDTLS_MD_SHA256,
9130 MBEDTLS_MD_SHA224,
9131#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009132#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009133 MBEDTLS_MD_SHA1,
9134#endif
9135 MBEDTLS_MD_NONE
9136};
Simon Butcherc97b6972015-12-27 23:48:17 +00009137#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009138
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009139static int ssl_preset_suiteb_ciphersuites[] = {
9140 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9141 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9142 0
9143};
9144
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009145#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009146static int ssl_preset_suiteb_hashes[] = {
9147 MBEDTLS_MD_SHA256,
9148 MBEDTLS_MD_SHA384,
9149 MBEDTLS_MD_NONE
9150};
9151#endif
9152
9153#if defined(MBEDTLS_ECP_C)
9154static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009155#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009156 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009157#endif
9158#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009159 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009160#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009161 MBEDTLS_ECP_DP_NONE
9162};
9163#endif
9164
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009165/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009166 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009167 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009168int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009169 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009170{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009171#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009172 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009173#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009174
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009175 /* Use the functions here so that they are covered in tests,
9176 * but otherwise access member directly for efficiency */
9177 mbedtls_ssl_conf_endpoint( conf, endpoint );
9178 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009179
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009180 /*
9181 * Things that are common to all presets
9182 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009183#if defined(MBEDTLS_SSL_CLI_C)
9184 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9185 {
9186 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9187#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9188 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9189#endif
9190 }
9191#endif
9192
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009193#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009194 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009195#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009196
9197#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9198 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9199#endif
9200
9201#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9202 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9203#endif
9204
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009205#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9206 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9207#endif
9208
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009209#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009210 conf->f_cookie_write = ssl_cookie_write_dummy;
9211 conf->f_cookie_check = ssl_cookie_check_dummy;
9212#endif
9213
9214#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9215 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9216#endif
9217
Janos Follath088ce432017-04-10 12:42:31 +01009218#if defined(MBEDTLS_SSL_SRV_C)
9219 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9220#endif
9221
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009222#if defined(MBEDTLS_SSL_PROTO_DTLS)
9223 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9224 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9225#endif
9226
9227#if defined(MBEDTLS_SSL_RENEGOTIATION)
9228 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009229 memset( conf->renego_period, 0x00, 2 );
9230 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009231#endif
9232
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009233#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9234 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9235 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009236 const unsigned char dhm_p[] =
9237 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9238 const unsigned char dhm_g[] =
9239 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9240
Hanno Beckera90658f2017-10-04 15:29:08 +01009241 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9242 dhm_p, sizeof( dhm_p ),
9243 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009244 {
9245 return( ret );
9246 }
9247 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009248#endif
9249
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009250 /*
9251 * Preset-specific defaults
9252 */
9253 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009254 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009255 /*
9256 * NSA Suite B
9257 */
9258 case MBEDTLS_SSL_PRESET_SUITEB:
9259 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9260 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9261 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9262 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9263
9264 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9265 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9266 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9267 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9268 ssl_preset_suiteb_ciphersuites;
9269
9270#if defined(MBEDTLS_X509_CRT_PARSE_C)
9271 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009272#endif
9273
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009274#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009275 conf->sig_hashes = ssl_preset_suiteb_hashes;
9276#endif
9277
9278#if defined(MBEDTLS_ECP_C)
9279 conf->curve_list = ssl_preset_suiteb_curves;
9280#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009281 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009282
9283 /*
9284 * Default
9285 */
9286 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009287 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9288 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9289 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9290 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9291 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9292 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9293 MBEDTLS_SSL_MIN_MINOR_VERSION :
9294 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009295 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9296 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9297
9298#if defined(MBEDTLS_SSL_PROTO_DTLS)
9299 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9300 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9301#endif
9302
9303 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9304 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9305 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9306 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9307 mbedtls_ssl_list_ciphersuites();
9308
9309#if defined(MBEDTLS_X509_CRT_PARSE_C)
9310 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9311#endif
9312
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009313#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009314 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009315#endif
9316
9317#if defined(MBEDTLS_ECP_C)
9318 conf->curve_list = mbedtls_ecp_grp_id_list();
9319#endif
9320
9321#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9322 conf->dhm_min_bitlen = 1024;
9323#endif
9324 }
9325
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009326 return( 0 );
9327}
9328
9329/*
9330 * Free mbedtls_ssl_config
9331 */
9332void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9333{
9334#if defined(MBEDTLS_DHM_C)
9335 mbedtls_mpi_free( &conf->dhm_P );
9336 mbedtls_mpi_free( &conf->dhm_G );
9337#endif
9338
9339#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9340 if( conf->psk != NULL )
9341 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009342 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009343 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009344 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009345 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009346 }
9347
9348 if( conf->psk_identity != NULL )
9349 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009350 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009351 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009352 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009353 conf->psk_identity_len = 0;
9354 }
9355#endif
9356
9357#if defined(MBEDTLS_X509_CRT_PARSE_C)
9358 ssl_key_cert_free( conf->key_cert );
9359#endif
9360
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009361 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009362}
9363
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009364#if defined(MBEDTLS_PK_C) && \
9365 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009366/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371#if defined(MBEDTLS_RSA_C)
9372 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9373 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375#if defined(MBEDTLS_ECDSA_C)
9376 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9377 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009380}
9381
Hanno Becker7e5437a2017-04-28 17:15:26 +01009382unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9383{
9384 switch( type ) {
9385 case MBEDTLS_PK_RSA:
9386 return( MBEDTLS_SSL_SIG_RSA );
9387 case MBEDTLS_PK_ECDSA:
9388 case MBEDTLS_PK_ECKEY:
9389 return( MBEDTLS_SSL_SIG_ECDSA );
9390 default:
9391 return( MBEDTLS_SSL_SIG_ANON );
9392 }
9393}
9394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009396{
9397 switch( sig )
9398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399#if defined(MBEDTLS_RSA_C)
9400 case MBEDTLS_SSL_SIG_RSA:
9401 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009402#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403#if defined(MBEDTLS_ECDSA_C)
9404 case MBEDTLS_SSL_SIG_ECDSA:
9405 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009406#endif
9407 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009409 }
9410}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009411#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009412
Hanno Becker7e5437a2017-04-28 17:15:26 +01009413#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9414 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9415
9416/* Find an entry in a signature-hash set matching a given hash algorithm. */
9417mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9418 mbedtls_pk_type_t sig_alg )
9419{
9420 switch( sig_alg )
9421 {
9422 case MBEDTLS_PK_RSA:
9423 return( set->rsa );
9424 case MBEDTLS_PK_ECDSA:
9425 return( set->ecdsa );
9426 default:
9427 return( MBEDTLS_MD_NONE );
9428 }
9429}
9430
9431/* Add a signature-hash-pair to a signature-hash set */
9432void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9433 mbedtls_pk_type_t sig_alg,
9434 mbedtls_md_type_t md_alg )
9435{
9436 switch( sig_alg )
9437 {
9438 case MBEDTLS_PK_RSA:
9439 if( set->rsa == MBEDTLS_MD_NONE )
9440 set->rsa = md_alg;
9441 break;
9442
9443 case MBEDTLS_PK_ECDSA:
9444 if( set->ecdsa == MBEDTLS_MD_NONE )
9445 set->ecdsa = md_alg;
9446 break;
9447
9448 default:
9449 break;
9450 }
9451}
9452
9453/* Allow exactly one hash algorithm for each signature. */
9454void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9455 mbedtls_md_type_t md_alg )
9456{
9457 set->rsa = md_alg;
9458 set->ecdsa = md_alg;
9459}
9460
9461#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9462 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9463
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009464/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009465 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009468{
9469 switch( hash )
9470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471#if defined(MBEDTLS_MD5_C)
9472 case MBEDTLS_SSL_HASH_MD5:
9473 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475#if defined(MBEDTLS_SHA1_C)
9476 case MBEDTLS_SSL_HASH_SHA1:
9477 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479#if defined(MBEDTLS_SHA256_C)
9480 case MBEDTLS_SSL_HASH_SHA224:
9481 return( MBEDTLS_MD_SHA224 );
9482 case MBEDTLS_SSL_HASH_SHA256:
9483 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#if defined(MBEDTLS_SHA512_C)
9486 case MBEDTLS_SSL_HASH_SHA384:
9487 return( MBEDTLS_MD_SHA384 );
9488 case MBEDTLS_SSL_HASH_SHA512:
9489 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009490#endif
9491 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009493 }
9494}
9495
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009496/*
9497 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9498 */
9499unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9500{
9501 switch( md )
9502 {
9503#if defined(MBEDTLS_MD5_C)
9504 case MBEDTLS_MD_MD5:
9505 return( MBEDTLS_SSL_HASH_MD5 );
9506#endif
9507#if defined(MBEDTLS_SHA1_C)
9508 case MBEDTLS_MD_SHA1:
9509 return( MBEDTLS_SSL_HASH_SHA1 );
9510#endif
9511#if defined(MBEDTLS_SHA256_C)
9512 case MBEDTLS_MD_SHA224:
9513 return( MBEDTLS_SSL_HASH_SHA224 );
9514 case MBEDTLS_MD_SHA256:
9515 return( MBEDTLS_SSL_HASH_SHA256 );
9516#endif
9517#if defined(MBEDTLS_SHA512_C)
9518 case MBEDTLS_MD_SHA384:
9519 return( MBEDTLS_SSL_HASH_SHA384 );
9520 case MBEDTLS_MD_SHA512:
9521 return( MBEDTLS_SSL_HASH_SHA512 );
9522#endif
9523 default:
9524 return( MBEDTLS_SSL_HASH_NONE );
9525 }
9526}
9527
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009528#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009529/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009530 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009531 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009532 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009533int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009536
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009537 if( ssl->conf->curve_list == NULL )
9538 return( -1 );
9539
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009540 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009541 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009542 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009543
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009544 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009545}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009546#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009547
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009548#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009549/*
9550 * Check if a hash proposed by the peer is in our list.
9551 * Return 0 if we're willing to use it, -1 otherwise.
9552 */
9553int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9554 mbedtls_md_type_t md )
9555{
9556 const int *cur;
9557
9558 if( ssl->conf->sig_hashes == NULL )
9559 return( -1 );
9560
9561 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9562 if( *cur == (int) md )
9563 return( 0 );
9564
9565 return( -1 );
9566}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009567#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569#if defined(MBEDTLS_X509_CRT_PARSE_C)
9570int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9571 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009572 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009573 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009574{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009575 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009577 int usage = 0;
9578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009580 const char *ext_oid;
9581 size_t ext_len;
9582#endif
9583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9585 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009586 ((void) cert);
9587 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009588 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009589#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9592 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009593 {
9594 /* Server part of the key exchange */
9595 switch( ciphersuite->key_exchange )
9596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 case MBEDTLS_KEY_EXCHANGE_RSA:
9598 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009599 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009600 break;
9601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9603 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9604 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9605 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009606 break;
9607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9609 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009610 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009611 break;
9612
9613 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 case MBEDTLS_KEY_EXCHANGE_NONE:
9615 case MBEDTLS_KEY_EXCHANGE_PSK:
9616 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9617 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009618 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009619 usage = 0;
9620 }
9621 }
9622 else
9623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9625 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009626 }
9627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009629 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009630 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009631 ret = -1;
9632 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009633#else
9634 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9638 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9641 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009642 }
9643 else
9644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9646 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009647 }
9648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009650 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009651 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009652 ret = -1;
9653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009655
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009656 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009659
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009660/*
9661 * Convert version numbers to/from wire format
9662 * and, for DTLS, to/from TLS equivalent.
9663 *
9664 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009665 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009666 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9667 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009670 unsigned char ver[2] )
9671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672#if defined(MBEDTLS_SSL_PROTO_DTLS)
9673 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009676 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9677
9678 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9679 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9680 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009681 else
9682#else
9683 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009684#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009685 {
9686 ver[0] = (unsigned char) major;
9687 ver[1] = (unsigned char) minor;
9688 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009689}
9690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009692 const unsigned char ver[2] )
9693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694#if defined(MBEDTLS_SSL_PROTO_DTLS)
9695 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009696 {
9697 *major = 255 - ver[0] + 2;
9698 *minor = 255 - ver[1] + 1;
9699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009701 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9702 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009703 else
9704#else
9705 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009706#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009707 {
9708 *major = ver[0];
9709 *minor = ver[1];
9710 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009711}
9712
Simon Butcher99000142016-10-13 17:21:01 +01009713int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9714{
9715#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9716 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9717 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9718
9719 switch( md )
9720 {
9721#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9722#if defined(MBEDTLS_MD5_C)
9723 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009724 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009725#endif
9726#if defined(MBEDTLS_SHA1_C)
9727 case MBEDTLS_SSL_HASH_SHA1:
9728 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9729 break;
9730#endif
9731#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9732#if defined(MBEDTLS_SHA512_C)
9733 case MBEDTLS_SSL_HASH_SHA384:
9734 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9735 break;
9736#endif
9737#if defined(MBEDTLS_SHA256_C)
9738 case MBEDTLS_SSL_HASH_SHA256:
9739 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9740 break;
9741#endif
9742 default:
9743 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9744 }
9745
9746 return 0;
9747#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9748 (void) ssl;
9749 (void) md;
9750
9751 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9752#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9753}
9754
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009755#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9756 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9757int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9758 unsigned char *output,
9759 unsigned char *data, size_t data_len )
9760{
9761 int ret = 0;
9762 mbedtls_md5_context mbedtls_md5;
9763 mbedtls_sha1_context mbedtls_sha1;
9764
9765 mbedtls_md5_init( &mbedtls_md5 );
9766 mbedtls_sha1_init( &mbedtls_sha1 );
9767
9768 /*
9769 * digitally-signed struct {
9770 * opaque md5_hash[16];
9771 * opaque sha_hash[20];
9772 * };
9773 *
9774 * md5_hash
9775 * MD5(ClientHello.random + ServerHello.random
9776 * + ServerParams);
9777 * sha_hash
9778 * SHA(ClientHello.random + ServerHello.random
9779 * + ServerParams);
9780 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009781 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009782 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009784 goto exit;
9785 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009786 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009787 ssl->handshake->randbytes, 64 ) ) != 0 )
9788 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009790 goto exit;
9791 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009792 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009793 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009795 goto exit;
9796 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009797 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009798 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009800 goto exit;
9801 }
9802
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009803 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009804 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009806 goto exit;
9807 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009808 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009809 ssl->handshake->randbytes, 64 ) ) != 0 )
9810 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009812 goto exit;
9813 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009814 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009815 data_len ) ) != 0 )
9816 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009818 goto exit;
9819 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009820 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009821 output + 16 ) ) != 0 )
9822 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009824 goto exit;
9825 }
9826
9827exit:
9828 mbedtls_md5_free( &mbedtls_md5 );
9829 mbedtls_sha1_free( &mbedtls_sha1 );
9830
9831 if( ret != 0 )
9832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9833 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9834
9835 return( ret );
9836
9837}
9838#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9839 MBEDTLS_SSL_PROTO_TLS1_1 */
9840
9841#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9842 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9843int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009844 unsigned char *hash, size_t *hashlen,
9845 unsigned char *data, size_t data_len,
9846 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009847{
9848 int ret = 0;
9849 mbedtls_md_context_t ctx;
9850 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009851 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009852
9853 mbedtls_md_init( &ctx );
9854
9855 /*
9856 * digitally-signed struct {
9857 * opaque client_random[32];
9858 * opaque server_random[32];
9859 * ServerDHParams params;
9860 * };
9861 */
9862 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9863 {
9864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9865 goto exit;
9866 }
9867 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9868 {
9869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9870 goto exit;
9871 }
9872 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9873 {
9874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9875 goto exit;
9876 }
9877 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9878 {
9879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9880 goto exit;
9881 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009882 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009883 {
9884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9885 goto exit;
9886 }
9887
9888exit:
9889 mbedtls_md_free( &ctx );
9890
9891 if( ret != 0 )
9892 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9893 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9894
9895 return( ret );
9896}
9897#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9898 MBEDTLS_SSL_PROTO_TLS1_2 */
9899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900#endif /* MBEDTLS_SSL_TLS_C */