blob: 5f940f0c6fe8733c0b7bf4ef61aa837c3cdfaca3 [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 */
1818static 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 )
1822{
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
1833 /* diff_msb's most significant bit is bit equal to c1 != c2 */
1834 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 */
1840 unsigned char mask = (unsigned char) -diff1;
1841
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;
1879 const unsigned char * const ikey = (unsigned char *) ctx->hmac_ctx;
1880 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é-Gonnard368fc652020-07-28 10:43:03 +02001886
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001887 mbedtls_md_init( &aux );
1888 mbedtls_md_setup( &aux, ctx->md_info, 0 );
1889
1890 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1891 * so we can start directly with the message */
1892 mbedtls_md_update( ctx, add_data, add_data_len );
1893 mbedtls_md_update( ctx, data, min_data_len );
1894
1895 /* For each possible length, compute the hash up to that point */
1896 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001897 {
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001898 mbedtls_md_clone( &aux, ctx );
1899 mbedtls_md_finish( &aux, aux_out );
1900 /* Keep only the correct inner_hash in the output buffer */
1901 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1902 offset, data_len_secret );
1903
1904 if( offset < max_data_len )
1905 mbedtls_md_update( ctx, data + offset, 1 );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001906 }
1907
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001908 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001909 mbedtls_md_starts( ctx );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001910 mbedtls_md_update( ctx, okey, block_size );
1911 mbedtls_md_update( ctx, output, hash_size );
1912 mbedtls_md_finish( ctx, output );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001913
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001914 /* Done, get ready for next time */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001915 mbedtls_md_hmac_reset( ctx );
1916
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001917 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001918 return( 0 );
1919}
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001920#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001925 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001926#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001927 size_t padlen = 0, correct = 1;
1928#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001932 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001936 }
1937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001939
Paul Bakker48916f92012-09-16 19:57:18 +00001940 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001943 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 }
1946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1948 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001949 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001950 int ret;
1951 size_t olen = 0;
1952
Paul Bakker68884e32013-01-07 18:20:04 +01001953 padlen = 0;
1954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001956 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001957 ssl->transform_in->ivlen,
1958 ssl->in_msg, ssl->in_msglen,
1959 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001962 return( ret );
1963 }
1964
1965 if( ssl->in_msglen != olen )
1966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 }
Paul Bakker68884e32013-01-07 18:20:04 +01001971 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001973#if defined(MBEDTLS_GCM_C) || \
1974 defined(MBEDTLS_CCM_C) || \
1975 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001977 mode == MBEDTLS_MODE_CCM ||
1978 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001979 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001980 int ret;
1981 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001982 unsigned char *dec_msg;
1983 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001984 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001985 unsigned char iv[12];
1986 mbedtls_ssl_transform *transform = ssl->transform_in;
1987 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001989 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001990
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001991 /*
1992 * Compute and update sizes
1993 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001994 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001997 "+ taglen (%d)", ssl->in_msglen,
1998 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002000 }
2001 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2002
Paul Bakker68884e32013-01-07 18:20:04 +01002003 dec_msg = ssl->in_msg;
2004 dec_msg_result = ssl->in_msg;
2005 ssl->in_msglen = dec_msglen;
2006
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002007 /*
2008 * Prepare additional authenticated data
2009 */
Paul Bakker68884e32013-01-07 18:20:04 +01002010 memcpy( add_data, ssl->in_ctr, 8 );
2011 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002013 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002014 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2015 add_data[12] = ssl->in_msglen & 0xFF;
2016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002017 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002018
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002019 /*
2020 * Prepare IV
2021 */
2022 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2023 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002024 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002025 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2026 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002027
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002028 }
2029 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2030 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002031 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002032 unsigned char i;
2033
2034 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2035
2036 for( i = 0; i < 8; i++ )
2037 iv[i+4] ^= ssl->in_ctr[i];
2038 }
2039 else
2040 {
2041 /* Reminder if we ever add an AEAD mode with a different size */
2042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2043 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2044 }
2045
2046 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002048
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002049 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002050 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002053 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002054 add_data, 13,
2055 dec_msg, dec_msglen,
2056 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002057 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2062 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002063
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002064 return( ret );
2065 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002066 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002067
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002068 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002072 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002076#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 {
Paul Bakker45829992013-01-03 14:52:21 +01002079 /*
2080 * Decrypt and check the padding
2081 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002082 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002083 unsigned char *dec_msg;
2084 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002085 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002086 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002087 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002088
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 /*
Paul Bakker45829992013-01-03 14:52:21 +01002090 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2093 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002094 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002095#endif
Paul Bakker45829992013-01-03 14:52:21 +01002096
2097 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2098 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002101 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2102 ssl->transform_in->ivlen,
2103 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002105 }
2106
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002107 dec_msglen = ssl->in_msglen;
2108 dec_msg = ssl->in_msg;
2109 dec_msg_result = ssl->in_msg;
2110
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002111 /*
2112 * Authenticate before decrypt if enabled
2113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2115 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002116 {
Hanno Becker992b6872017-11-09 18:57:39 +00002117 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002118 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002122 dec_msglen -= ssl->transform_in->maclen;
2123 ssl->in_msglen -= ssl->transform_in->maclen;
2124
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002125 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2126 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2127 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2128 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2133 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002134 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002135 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002139 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002140 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002141 ssl->transform_in->maclen );
2142
Hanno Becker992b6872017-11-09 18:57:39 +00002143 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2144 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002149 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002150 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002153
2154 /*
2155 * Check length sanity
2156 */
2157 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002160 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002162 }
2163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002165 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002166 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002169 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002170 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002171 dec_msglen -= ssl->transform_in->ivlen;
2172 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173
Paul Bakker48916f92012-09-16 19:57:18 +00002174 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002175 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002180 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002181 ssl->transform_in->ivlen,
2182 dec_msg, dec_msglen,
2183 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002186 return( ret );
2187 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002188
Paul Bakkercca5b812013-08-31 17:40:26 +02002189 if( dec_msglen != olen )
2190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002193 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2196 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002197 {
2198 /*
2199 * Save IV in SSL3 and TLS1
2200 */
2201 memcpy( ssl->transform_in->iv_dec,
2202 ssl->transform_in->cipher_ctx_dec.iv,
2203 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
2207 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002208
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002209 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002210 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_SSL_DEBUG_ALL)
2213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002214 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002215#endif
Paul Bakker45829992013-01-03 14:52:21 +01002216 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002217 correct = 0;
2218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_PROTO_SSL3)
2221 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 {
Paul Bakker48916f92012-09-16 19:57:18 +00002223 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#if defined(MBEDTLS_SSL_DEBUG_ALL)
2226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002228 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002229#endif
Paul Bakker45829992013-01-03 14:52:21 +01002230 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 }
2232 }
2233 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2235#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2236 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2237 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
2239 /*
Paul Bakker45829992013-01-03 14:52:21 +01002240 * TLSv1+: always check the padding up to the first failure
2241 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002243 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002244 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002245 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002246
Paul Bakker956c9e02013-12-19 14:42:28 +01002247 /*
2248 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002249 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002250 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002251 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002252 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002253 *
2254 * In both cases we reset padding_idx to a safe value (0) to
2255 * prevent out-of-buffer reads.
2256 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002257 correct &= ( padlen <= ssl->in_msglen );
2258 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002259 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002260
2261 padding_idx *= correct;
2262
Angus Grattonb512bc12018-06-19 15:57:50 +10002263 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002264 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002265 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002266 pad_count += real_count *
2267 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2268 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002269
2270 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002273 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002275#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002276 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002278 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2280 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002284 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002285
2286 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002288 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002289#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002295#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002298#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002299
2300 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002301 * Authenticate if not done yet.
2302 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002304#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002305 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002306 {
Hanno Becker992b6872017-11-09 18:57:39 +00002307 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002308
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002309 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002311 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2312 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_SSL_PROTO_SSL3)
2315 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002316 {
2317 ssl_mac( &ssl->transform_in->md_ctx_dec,
2318 ssl->transform_in->mac_dec,
2319 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002320 ssl->in_ctr, ssl->in_msgtype,
2321 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002322 }
2323 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2325#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2326 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2327 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002328 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002329 int ret;
2330 unsigned char add_data[13];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002331
2332 /*
2333 * The next two sizes are the minimum and maximum values of
2334 * in_msglen over all padlen values.
2335 *
2336 * They're independent of padlen, since we previously did
2337 * in_msglen -= padlen.
2338 *
2339 * Note that max_len + maclen is never more than the buffer
2340 * length, as we previously did in_msglen -= maclen too.
2341 */
2342 const size_t max_len = ssl->in_msglen + padlen;
2343 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2344
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002345 memcpy( add_data + 0, ssl->in_ctr, 8 );
2346 memcpy( add_data + 8, ssl->in_hdr, 3 );
2347 memcpy( add_data + 11, ssl->in_len, 2 );
2348
2349 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2350 add_data, sizeof( add_data ),
2351 ssl->in_msg, ssl->in_msglen,
2352 min_len, max_len,
2353 mac_expect );
2354 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02002355 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2357 return( ret );
Gilles Peskine20b44082018-05-29 14:06:49 +02002358 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002359
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002360 /* Make sure we access all the memory that could contain the MAC,
2361 * before we check it in the next code block. This makes the
2362 * synchronisation requirements for just-in-time Prime+Probe
2363 * attacks much tighter and hopefully impractical. */
2364 ssl_read_memory( ssl->in_msg + min_len,
2365 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002366 }
2367 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2369 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2372 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002375#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002376 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2377 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2378 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Hanno Becker992b6872017-11-09 18:57:39 +00002381 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2382 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#if defined(MBEDTLS_SSL_DEBUG_ALL)
2385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002386#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002387 correct = 0;
2388 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002389 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002390 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002391
2392 /*
2393 * Finally check the correct flag
2394 */
2395 if( correct == 0 )
2396 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002397#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002398
2399 /* Make extra sure authentication was performed, exactly once */
2400 if( auth_done != 1 )
2401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
2406 if( ssl->in_msglen == 0 )
2407 {
Angus Gratton34817922018-06-19 15:58:22 +10002408#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2409 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2410 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2411 {
2412 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2414 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2415 }
2416#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2417
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 ssl->nb_zero++;
2419
2420 /*
2421 * Three or more empty messages may be a DoS attack
2422 * (excessive CPU consumption).
2423 */
2424 if( ssl->nb_zero > 3 )
2425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 }
2430 }
2431 else
2432 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002436 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002437 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002438 }
2439 else
2440#endif
2441 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002442 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002443 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2444 if( ++ssl->in_ctr[i - 1] != 0 )
2445 break;
2446
2447 /* The loop goes to its end iff the counter is wrapping */
2448 if( i == ssl_ep_len( ssl ) )
2449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2451 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002452 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002453 }
2454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
2457 return( 0 );
2458}
2459
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002460#undef MAC_NONE
2461#undef MAC_PLAINTEXT
2462#undef MAC_CIPHERTEXT
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002465/*
2466 * Compression/decompression functions
2467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002469{
2470 int ret;
2471 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002472 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002473 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002474 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002477
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002478 if( len_pre == 0 )
2479 return( 0 );
2480
Paul Bakker2770fbd2012-07-03 13:30:23 +00002481 memcpy( msg_pre, ssl->out_msg, len_pre );
2482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002484 ssl->out_msglen ) );
2485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002487 ssl->out_msg, ssl->out_msglen );
2488
Paul Bakker48916f92012-09-16 19:57:18 +00002489 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2490 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2491 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002492 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002493
Paul Bakker48916f92012-09-16 19:57:18 +00002494 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002495 if( ret != Z_OK )
2496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2498 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002499 }
2500
Angus Grattond8213d02016-05-25 20:56:48 +10002501 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002502 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002505 ssl->out_msglen ) );
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002508 ssl->out_msg, ssl->out_msglen );
2509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002511
2512 return( 0 );
2513}
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002516{
2517 int ret;
2518 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002519 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002520 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002521 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002524
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002525 if( len_pre == 0 )
2526 return( 0 );
2527
Paul Bakker2770fbd2012-07-03 13:30:23 +00002528 memcpy( msg_pre, ssl->in_msg, len_pre );
2529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002531 ssl->in_msglen ) );
2532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002534 ssl->in_msg, ssl->in_msglen );
2535
Paul Bakker48916f92012-09-16 19:57:18 +00002536 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2537 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2538 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002539 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002540 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002541
Paul Bakker48916f92012-09-16 19:57:18 +00002542 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002543 if( ret != Z_OK )
2544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2546 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002547 }
2548
Angus Grattond8213d02016-05-25 20:56:48 +10002549 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002550 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002553 ssl->in_msglen ) );
2554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002556 ssl->in_msg, ssl->in_msglen );
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002559
2560 return( 0 );
2561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2565static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_PROTO_DTLS)
2568static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002569{
2570 /* If renegotiation is not enforced, retransmit until we would reach max
2571 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002572 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002573 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002574 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002575 unsigned char doublings = 1;
2576
2577 while( ratio != 0 )
2578 {
2579 ++doublings;
2580 ratio >>= 1;
2581 }
2582
2583 if( ++ssl->renego_records_seen > doublings )
2584 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002586 return( 0 );
2587 }
2588 }
2589
2590 return( ssl_write_hello_request( ssl ) );
2591}
2592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002594
Paul Bakker5121ce52009-01-03 21:22:43 +00002595/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002596 * Fill the input message buffer by appending data to it.
2597 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002598 *
2599 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2600 * available (from this read and/or a previous one). Otherwise, an error code
2601 * is returned (possibly EOF or WANT_READ).
2602 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002603 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2604 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2605 * since we always read a whole datagram at once.
2606 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002607 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002608 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611{
Paul Bakker23986e52011-04-24 08:57:21 +00002612 int ret;
2613 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002617 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002620 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002622 }
2623
Angus Grattond8213d02016-05-25 20:56:48 +10002624 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002628 }
2629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002631 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002633 uint32_t timeout;
2634
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002635 /* Just to be sure */
2636 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2637 {
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2639 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2641 }
2642
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002643 /*
2644 * The point is, we need to always read a full datagram at once, so we
2645 * sometimes read more then requested, and handle the additional data.
2646 * It could be the rest of the current record (while fetching the
2647 * header) and/or some other records in the same datagram.
2648 */
2649
2650 /*
2651 * Move to the next record in the already read datagram if applicable
2652 */
2653 if( ssl->next_record_offset != 0 )
2654 {
2655 if( ssl->in_left < ssl->next_record_offset )
2656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002659 }
2660
2661 ssl->in_left -= ssl->next_record_offset;
2662
2663 if( ssl->in_left != 0 )
2664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002666 ssl->next_record_offset ) );
2667 memmove( ssl->in_hdr,
2668 ssl->in_hdr + ssl->next_record_offset,
2669 ssl->in_left );
2670 }
2671
2672 ssl->next_record_offset = 0;
2673 }
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002677
2678 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002679 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002680 */
2681 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002684 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002685 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002686
2687 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002688 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002689 * are not at the beginning of a new record, the caller did something
2690 * wrong.
2691 */
2692 if( ssl->in_left != 0 )
2693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002696 }
2697
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002698 /*
2699 * Don't even try to read if time's out already.
2700 * This avoids by-passing the timer when repeatedly receiving messages
2701 * that will end up being dropped.
2702 */
2703 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002704 {
2705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002706 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002707 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002708 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002709 {
Angus Grattond8213d02016-05-25 20:56:48 +10002710 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002713 timeout = ssl->handshake->retransmit_timeout;
2714 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002715 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002718
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002719 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002720 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2721 timeout );
2722 else
2723 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002726
2727 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002729 }
2730
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002731 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002734 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002737 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002738 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002741 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002742 }
2743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002747 return( ret );
2748 }
2749
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002750 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002753 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002755 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002756 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002759 return( ret );
2760 }
2761
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002762 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002765 }
2766
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 if( ret < 0 )
2768 return( ret );
2769
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002770 ssl->in_left = ret;
2771 }
2772 else
2773#endif
2774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002776 ssl->in_left, nb_want ) );
2777
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002778 while( ssl->in_left < nb_want )
2779 {
2780 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002781
2782 if( ssl_check_timer( ssl ) != 0 )
2783 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2784 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002785 {
2786 if( ssl->f_recv_timeout != NULL )
2787 {
2788 ret = ssl->f_recv_timeout( ssl->p_bio,
2789 ssl->in_hdr + ssl->in_left, len,
2790 ssl->conf->read_timeout );
2791 }
2792 else
2793 {
2794 ret = ssl->f_recv( ssl->p_bio,
2795 ssl->in_hdr + ssl->in_left, len );
2796 }
2797 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002800 ssl->in_left, nb_want ) );
2801 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002802
2803 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002805
2806 if( ret < 0 )
2807 return( ret );
2808
mohammad160352aecb92018-03-28 23:41:40 -07002809 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002810 {
Darryl Green11999bb2018-03-13 15:22:58 +00002811 MBEDTLS_SSL_DEBUG_MSG( 1,
2812 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002813 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2815 }
2816
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002817 ssl->in_left += ret;
2818 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 }
2820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
2823 return( 0 );
2824}
2825
2826/*
2827 * Flush any data not yet written
2828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002830{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002831 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002832 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002836 if( ssl->f_send == NULL )
2837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002839 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002841 }
2842
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002843 /* Avoid incrementing counter if data is flushed */
2844 if( ssl->out_left == 0 )
2845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002847 return( 0 );
2848 }
2849
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 while( ssl->out_left > 0 )
2851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2853 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Hanno Becker2b1e3542018-08-06 11:19:13 +01002855 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002856 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
2860 if( ret <= 0 )
2861 return( ret );
2862
mohammad160352aecb92018-03-28 23:41:40 -07002863 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002864 {
Darryl Green11999bb2018-03-13 15:22:58 +00002865 MBEDTLS_SSL_DEBUG_MSG( 1,
2866 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002867 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002868 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2869 }
2870
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 ssl->out_left -= ret;
2872 }
2873
Hanno Becker2b1e3542018-08-06 11:19:13 +01002874#if defined(MBEDTLS_SSL_PROTO_DTLS)
2875 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002876 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002877 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002878 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002879 else
2880#endif
2881 {
2882 ssl->out_hdr = ssl->out_buf + 8;
2883 }
2884 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
2888 return( 0 );
2889}
2890
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002891/*
2892 * Functions to handle the DTLS retransmission state machine
2893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002895/*
2896 * Append current handshake message to current outgoing flight
2897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2902 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2903 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002904
2905 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002906 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002907 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002910 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002911 }
2912
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002913 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002914 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002917 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002918 }
2919
2920 /* Copy current handshake message with headers */
2921 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2922 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002923 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002924 msg->next = NULL;
2925
2926 /* Append to the current flight */
2927 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002928 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002929 else
2930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002932 while( cur->next != NULL )
2933 cur = cur->next;
2934 cur->next = msg;
2935 }
2936
Hanno Becker3b235902018-08-06 09:54:53 +01002937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002938 return( 0 );
2939}
2940
2941/*
2942 * Free the current flight of handshake messages
2943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 mbedtls_ssl_flight_item *cur = flight;
2947 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002948
2949 while( cur != NULL )
2950 {
2951 next = cur->next;
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 mbedtls_free( cur->p );
2954 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002955
2956 cur = next;
2957 }
2958}
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2961static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002962#endif
2963
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002964/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002965 * Swap transform_out and out_ctr with the alternative ones
2966 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002967static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002970 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002971#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2972 int ret;
2973#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002974
2975 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002978 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002979 }
2980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002982
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002983 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002984 tmp_transform = ssl->transform_out;
2985 ssl->transform_out = ssl->handshake->alt_transform_out;
2986 ssl->handshake->alt_transform_out = tmp_transform;
2987
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002988 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002989 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2990 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002991 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002992
2993 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002994 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2997 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3002 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003003 }
3004 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003005#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
3006
3007 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003008}
3009
3010/*
3011 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003012 */
3013int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3014{
3015 int ret = 0;
3016
3017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3018
3019 ret = mbedtls_ssl_flight_transmit( ssl );
3020
3021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3022
3023 return( ret );
3024}
3025
3026/*
3027 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003028 *
3029 * Need to remember the current message in case flush_output returns
3030 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003031 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003032 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003033int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003034{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003035 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003039 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003041
3042 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003043 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003044 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3045 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003048 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003049
3050 while( ssl->handshake->cur_msg != NULL )
3051 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003052 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003053 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003054
Hanno Beckere1dcb032018-08-17 16:47:58 +01003055 int const is_finished =
3056 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3057 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3058
Hanno Becker04da1892018-08-14 13:22:10 +01003059 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3060 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3061
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003062 /* Swap epochs before sending Finished: we can't do it after
3063 * sending ChangeCipherSpec, in case write returns WANT_READ.
3064 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003065 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003066 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003068 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3069 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003070 }
3071
Hanno Becker67bc7c32018-08-06 11:33:50 +01003072 ret = ssl_get_remaining_payload_in_datagram( ssl );
3073 if( ret < 0 )
3074 return( ret );
3075 max_frag_len = (size_t) ret;
3076
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003077 /* CCS is copied as is, while HS messages may need fragmentation */
3078 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3079 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003080 if( max_frag_len == 0 )
3081 {
3082 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3083 return( ret );
3084
3085 continue;
3086 }
3087
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003088 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003089 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003090 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003091
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003092 /* Update position inside current message */
3093 ssl->handshake->cur_msg_p += cur->len;
3094 }
3095 else
3096 {
3097 const unsigned char * const p = ssl->handshake->cur_msg_p;
3098 const size_t hs_len = cur->len - 12;
3099 const size_t frag_off = p - ( cur->p + 12 );
3100 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003101 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003102
Hanno Beckere1dcb032018-08-17 16:47:58 +01003103 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003104 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003105 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003106 {
3107 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3108 return( ret );
3109 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003110
Hanno Becker67bc7c32018-08-06 11:33:50 +01003111 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3112 return( ret );
3113
3114 continue;
3115 }
3116 max_hs_frag_len = max_frag_len - 12;
3117
3118 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3119 max_hs_frag_len : rem_len;
3120
3121 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003122 {
3123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003124 (unsigned) cur_hs_frag_len,
3125 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003126 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003127
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003128 /* Messages are stored with handshake headers as if not fragmented,
3129 * copy beginning of headers then fill fragmentation fields.
3130 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3131 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003132
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003133 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3134 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3135 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3136
Hanno Becker67bc7c32018-08-06 11:33:50 +01003137 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3138 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3139 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003140
3141 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3142
Hanno Becker3f7b9732018-08-28 09:53:25 +01003143 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003144 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3145 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003146 ssl->out_msgtype = cur->type;
3147
3148 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003149 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003150 }
3151
3152 /* If done with the current message move to the next one if any */
3153 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3154 {
3155 if( cur->next != NULL )
3156 {
3157 ssl->handshake->cur_msg = cur->next;
3158 ssl->handshake->cur_msg_p = cur->next->p + 12;
3159 }
3160 else
3161 {
3162 ssl->handshake->cur_msg = NULL;
3163 ssl->handshake->cur_msg_p = NULL;
3164 }
3165 }
3166
3167 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003168 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003171 return( ret );
3172 }
3173 }
3174
Hanno Becker67bc7c32018-08-06 11:33:50 +01003175 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3176 return( ret );
3177
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003178 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3180 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003181 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003184 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3185 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003186
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003188
3189 return( 0 );
3190}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003191
3192/*
3193 * To be called when the last message of an incoming flight is received.
3194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003196{
3197 /* We won't need to resend that one any more */
3198 ssl_flight_free( ssl->handshake->flight );
3199 ssl->handshake->flight = NULL;
3200 ssl->handshake->cur_msg = NULL;
3201
3202 /* The next incoming flight will start with this msg_seq */
3203 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3204
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003205 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003206 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003207
Hanno Becker0271f962018-08-16 13:23:47 +01003208 /* Clear future message buffering structure. */
3209 ssl_buffering_free( ssl );
3210
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003211 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003212 ssl_set_timer( ssl, 0 );
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3215 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003218 }
3219 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003221}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003222
3223/*
3224 * To be called when the last message of an outgoing flight is send.
3225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003227{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003228 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003229 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3232 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003235 }
3236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003240
Paul Bakker5121ce52009-01-03 21:22:43 +00003241/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003242 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003244
3245/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003246 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003247 *
3248 * - fill in handshake headers
3249 * - update handshake checksum
3250 * - DTLS: save message for resending
3251 * - then pass to the record layer
3252 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003253 * DTLS: except for HelloRequest, messages are only queued, and will only be
3254 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003255 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003256 * Inputs:
3257 * - ssl->out_msglen: 4 + actual handshake message len
3258 * (4 is the size of handshake headers for TLS)
3259 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3260 * - ssl->out_msg + 4: the handshake message body
3261 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003262 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003263 * - ssl->out_msglen: the length of the record contents
3264 * (including handshake headers but excluding record headers)
3265 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003266 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003267int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003268{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003269 int ret;
3270 const size_t hs_len = ssl->out_msglen - 4;
3271 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3274
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003275 /*
3276 * Sanity checks
3277 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003278 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003279 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3280 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003281 /* In SSLv3, the client might send a NoCertificate alert. */
3282#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3283 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3284 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3285 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3286#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3287 {
3288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3290 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003293 /* Whenever we send anything different from a
3294 * HelloRequest we should be in a handshake - double check. */
3295 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3296 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003297 ssl->handshake == NULL )
3298 {
3299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003307 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003310 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003312
Hanno Beckerb50a2532018-08-06 11:52:54 +01003313 /* Double-check that we did not exceed the bounds
3314 * of the outgoing record buffer.
3315 * This should never fail as the various message
3316 * writing functions must obey the bounds of the
3317 * outgoing record buffer, but better be safe.
3318 *
3319 * Note: We deliberately do not check for the MTU or MFL here.
3320 */
3321 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3322 {
3323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3324 "size %u, maximum %u",
3325 (unsigned) ssl->out_msglen,
3326 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3328 }
3329
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003330 /*
3331 * Fill handshake headers
3332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003335 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3336 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3337 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003339 /*
3340 * DTLS has additional fields in the Handshake layer,
3341 * between the length field and the actual payload:
3342 * uint16 message_seq;
3343 * uint24 fragment_offset;
3344 * uint24 fragment_length;
3345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003347 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003348 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003349 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003350 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003351 {
3352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3353 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003354 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003355 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3357 }
3358
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003359 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003360 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003361
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003362 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003363 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003364 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003365 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3366 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3367 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003368 }
3369 else
3370 {
3371 ssl->out_msg[4] = 0;
3372 ssl->out_msg[5] = 0;
3373 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003374
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003375 /* Handshake hashes are computed without fragmentation,
3376 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003377 memset( ssl->out_msg + 6, 0x00, 3 );
3378 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003379 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003381
Hanno Becker0207e532018-08-28 10:28:28 +01003382 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003383 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3384 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003385 }
3386
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003387 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003389 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003390 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3391 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003392 {
3393 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003396 return( ret );
3397 }
3398 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003399 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003400#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003401 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003402 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003403 {
3404 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3405 return( ret );
3406 }
3407 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003408
3409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3410
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003411 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003412}
3413
3414/*
3415 * Record layer functions
3416 */
3417
3418/*
3419 * Write current record.
3420 *
3421 * Uses:
3422 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3423 * - ssl->out_msglen: length of the record content (excl headers)
3424 * - ssl->out_msg: record content
3425 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003426int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003427{
3428 int ret, done = 0;
3429 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003430 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003431
3432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003435 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003437 {
3438 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003441 return( ret );
3442 }
3443
3444 len = ssl->out_msglen;
3445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3449 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 ret = mbedtls_ssl_hw_record_write( ssl );
3454 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3457 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003458 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003459
3460 if( ret == 0 )
3461 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003464 if( !done )
3465 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003466 unsigned i;
3467 size_t protected_record_size;
3468
Paul Bakker05ef8352012-05-08 09:17:57 +00003469 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003471 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003472
Hanno Becker19859472018-08-06 09:40:20 +01003473 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003474 ssl->out_len[0] = (unsigned char)( len >> 8 );
3475 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003476
Paul Bakker48916f92012-09-16 19:57:18 +00003477 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003478 {
3479 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003482 return( ret );
3483 }
3484
3485 len = ssl->out_msglen;
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 }
3489
Hanno Becker2b1e3542018-08-06 11:19:13 +01003490 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3491
3492#if defined(MBEDTLS_SSL_PROTO_DTLS)
3493 /* In case of DTLS, double-check that we don't exceed
3494 * the remaining space in the datagram. */
3495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3496 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003497 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003498 if( ret < 0 )
3499 return( ret );
3500
3501 if( protected_record_size > (size_t) ret )
3502 {
3503 /* Should never happen */
3504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3505 }
3506 }
3507#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003510 "version = [%d:%d], msglen = %d",
3511 ssl->out_hdr[0], ssl->out_hdr[1],
3512 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003515 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003516
3517 ssl->out_left += protected_record_size;
3518 ssl->out_hdr += protected_record_size;
3519 ssl_update_out_pointers( ssl, ssl->transform_out );
3520
Hanno Becker04484622018-08-06 09:49:38 +01003521 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3522 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3523 break;
3524
3525 /* The loop goes to its end iff the counter is wrapping */
3526 if( i == ssl_ep_len( ssl ) )
3527 {
3528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3529 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 }
3532
Hanno Becker67bc7c32018-08-06 11:33:50 +01003533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3535 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003536 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003537 size_t remaining;
3538 ret = ssl_get_remaining_payload_in_datagram( ssl );
3539 if( ret < 0 )
3540 {
3541 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3542 ret );
3543 return( ret );
3544 }
3545
3546 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003547 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003548 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003549 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003550 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003551 else
3552 {
Hanno Becker513815a2018-08-20 11:56:09 +01003553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003554 }
3555 }
3556#endif /* MBEDTLS_SSL_PROTO_DTLS */
3557
3558 if( ( flush == SSL_FORCE_FLUSH ) &&
3559 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 return( ret );
3563 }
3564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
3567 return( 0 );
3568}
3569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003571
3572static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3573{
3574 if( ssl->in_msglen < ssl->in_hslen ||
3575 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3576 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3577 {
3578 return( 1 );
3579 }
3580 return( 0 );
3581}
Hanno Becker44650b72018-08-16 12:51:11 +01003582
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003583static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003584{
3585 return( ( ssl->in_msg[9] << 16 ) |
3586 ( ssl->in_msg[10] << 8 ) |
3587 ssl->in_msg[11] );
3588}
3589
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003590static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003591{
3592 return( ( ssl->in_msg[6] << 16 ) |
3593 ( ssl->in_msg[7] << 8 ) |
3594 ssl->in_msg[8] );
3595}
3596
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003597static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003598{
3599 uint32_t msg_len, frag_off, frag_len;
3600
3601 msg_len = ssl_get_hs_total_len( ssl );
3602 frag_off = ssl_get_hs_frag_off( ssl );
3603 frag_len = ssl_get_hs_frag_len( ssl );
3604
3605 if( frag_off > msg_len )
3606 return( -1 );
3607
3608 if( frag_len > msg_len - frag_off )
3609 return( -1 );
3610
3611 if( frag_len + 12 > ssl->in_msglen )
3612 return( -1 );
3613
3614 return( 0 );
3615}
3616
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003617/*
3618 * Mark bits in bitmask (used for DTLS HS reassembly)
3619 */
3620static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3621{
3622 unsigned int start_bits, end_bits;
3623
3624 start_bits = 8 - ( offset % 8 );
3625 if( start_bits != 8 )
3626 {
3627 size_t first_byte_idx = offset / 8;
3628
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003629 /* Special case */
3630 if( len <= start_bits )
3631 {
3632 for( ; len != 0; len-- )
3633 mask[first_byte_idx] |= 1 << ( start_bits - len );
3634
3635 /* Avoid potential issues with offset or len becoming invalid */
3636 return;
3637 }
3638
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003639 offset += start_bits; /* Now offset % 8 == 0 */
3640 len -= start_bits;
3641
3642 for( ; start_bits != 0; start_bits-- )
3643 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3644 }
3645
3646 end_bits = len % 8;
3647 if( end_bits != 0 )
3648 {
3649 size_t last_byte_idx = ( offset + len ) / 8;
3650
3651 len -= end_bits; /* Now len % 8 == 0 */
3652
3653 for( ; end_bits != 0; end_bits-- )
3654 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3655 }
3656
3657 memset( mask + offset / 8, 0xFF, len / 8 );
3658}
3659
3660/*
3661 * Check that bitmask is full
3662 */
3663static int ssl_bitmask_check( unsigned char *mask, size_t len )
3664{
3665 size_t i;
3666
3667 for( i = 0; i < len / 8; i++ )
3668 if( mask[i] != 0xFF )
3669 return( -1 );
3670
3671 for( i = 0; i < len % 8; i++ )
3672 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3673 return( -1 );
3674
3675 return( 0 );
3676}
3677
Hanno Becker56e205e2018-08-16 09:06:12 +01003678/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003679static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003680 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003681{
Hanno Becker56e205e2018-08-16 09:06:12 +01003682 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003683
Hanno Becker56e205e2018-08-16 09:06:12 +01003684 alloc_len = 12; /* Handshake header */
3685 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003686
Hanno Beckerd07df862018-08-16 09:14:58 +01003687 if( add_bitmap )
3688 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003689
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003690 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003691}
Hanno Becker56e205e2018-08-16 09:06:12 +01003692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003694
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003695static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003696{
3697 return( ( ssl->in_msg[1] << 16 ) |
3698 ( ssl->in_msg[2] << 8 ) |
3699 ssl->in_msg[3] );
3700}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003701
Simon Butcher99000142016-10-13 17:21:01 +01003702int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003707 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003709 }
3710
Hanno Becker12555c62018-08-16 12:47:53 +01003711 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003714 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003715 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003718 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003719 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003720 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003721 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003722
Hanno Becker44650b72018-08-16 12:51:11 +01003723 if( ssl_check_hs_header( ssl ) != 0 )
3724 {
3725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3726 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3727 }
3728
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003729 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003730 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3731 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3732 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3733 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003734 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003735 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3736 {
3737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3738 recv_msg_seq,
3739 ssl->handshake->in_msg_seq ) );
3740 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3741 }
3742
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003743 /* Retransmit only on last message from previous flight, to avoid
3744 * too many retransmissions.
3745 * Besides, No sane server ever retransmits HelloVerifyRequest */
3746 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003750 "message_seq = %d, start_of_flight = %d",
3751 recv_msg_seq,
3752 ssl->handshake->in_flight_start_seq ) );
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003757 return( ret );
3758 }
3759 }
3760 else
3761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003763 "message_seq = %d, expected = %d",
3764 recv_msg_seq,
3765 ssl->handshake->in_msg_seq ) );
3766 }
3767
Hanno Becker90333da2017-10-10 11:27:13 +01003768 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003769 }
3770 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003771
Hanno Becker6d97ef52018-08-16 13:09:04 +01003772 /* Message reassembly is handled alongside buffering of future
3773 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003774 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003775 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003776 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003779 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003780 }
3781 }
3782 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003784 /* With TLS we don't handle fragmentation (for now) */
3785 if( ssl->in_msglen < ssl->in_hslen )
3786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3788 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003789 }
3790
Simon Butcher99000142016-10-13 17:21:01 +01003791 return( 0 );
3792}
3793
3794void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3795{
Hanno Becker0271f962018-08-16 13:23:47 +01003796 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003797
Hanno Becker0271f962018-08-16 13:23:47 +01003798 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003799 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003800 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003801 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003802
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003803 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003805 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003806 ssl->handshake != NULL )
3807 {
Hanno Becker0271f962018-08-16 13:23:47 +01003808 unsigned offset;
3809 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003810
Hanno Becker0271f962018-08-16 13:23:47 +01003811 /* Increment handshake sequence number */
3812 hs->in_msg_seq++;
3813
3814 /*
3815 * Clear up handshake buffering and reassembly structure.
3816 */
3817
3818 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003819 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003820
3821 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003822 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3823 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003824 offset++, hs_buf++ )
3825 {
3826 *hs_buf = *(hs_buf + 1);
3827 }
3828
3829 /* Create a fresh last entry */
3830 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003831 }
3832#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003833}
3834
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003835/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003836 * DTLS anti-replay: RFC 6347 4.1.2.6
3837 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003838 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3839 * Bit n is set iff record number in_window_top - n has been seen.
3840 *
3841 * Usually, in_window_top is the last record number seen and the lsb of
3842 * in_window is set. The only exception is the initial state (record number 0
3843 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3846static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003847{
3848 ssl->in_window_top = 0;
3849 ssl->in_window = 0;
3850}
3851
3852static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3853{
3854 return( ( (uint64_t) buf[0] << 40 ) |
3855 ( (uint64_t) buf[1] << 32 ) |
3856 ( (uint64_t) buf[2] << 24 ) |
3857 ( (uint64_t) buf[3] << 16 ) |
3858 ( (uint64_t) buf[4] << 8 ) |
3859 ( (uint64_t) buf[5] ) );
3860}
3861
3862/*
3863 * Return 0 if sequence number is acceptable, -1 otherwise
3864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003866{
3867 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3868 uint64_t bit;
3869
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003870 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003871 return( 0 );
3872
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003873 if( rec_seqnum > ssl->in_window_top )
3874 return( 0 );
3875
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003876 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003877
3878 if( bit >= 64 )
3879 return( -1 );
3880
3881 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3882 return( -1 );
3883
3884 return( 0 );
3885}
3886
3887/*
3888 * Update replay window on new validated record
3889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003891{
3892 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3893
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003894 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003895 return;
3896
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003897 if( rec_seqnum > ssl->in_window_top )
3898 {
3899 /* Update window_top and the contents of the window */
3900 uint64_t shift = rec_seqnum - ssl->in_window_top;
3901
3902 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003903 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003904 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003905 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003906 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003907 ssl->in_window |= 1;
3908 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003909
3910 ssl->in_window_top = rec_seqnum;
3911 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003912 else
3913 {
3914 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003915 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003916
3917 if( bit < 64 ) /* Always true, but be extra sure */
3918 ssl->in_window |= (uint64_t) 1 << bit;
3919 }
3920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003922
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003923#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003924/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003925static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3926
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003927/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003928 * Without any SSL context, check if a datagram looks like a ClientHello with
3929 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003930 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003931 *
3932 * - if cookie is valid, return 0
3933 * - if ClientHello looks superficially valid but cookie is not,
3934 * fill obuf and set olen, then
3935 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3936 * - otherwise return a specific error code
3937 */
3938static int ssl_check_dtls_clihlo_cookie(
3939 mbedtls_ssl_cookie_write_t *f_cookie_write,
3940 mbedtls_ssl_cookie_check_t *f_cookie_check,
3941 void *p_cookie,
3942 const unsigned char *cli_id, size_t cli_id_len,
3943 const unsigned char *in, size_t in_len,
3944 unsigned char *obuf, size_t buf_len, size_t *olen )
3945{
3946 size_t sid_len, cookie_len;
3947 unsigned char *p;
3948
3949 if( f_cookie_write == NULL || f_cookie_check == NULL )
3950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3951
3952 /*
3953 * Structure of ClientHello with record and handshake headers,
3954 * and expected values. We don't need to check a lot, more checks will be
3955 * done when actually parsing the ClientHello - skipping those checks
3956 * avoids code duplication and does not make cookie forging any easier.
3957 *
3958 * 0-0 ContentType type; copied, must be handshake
3959 * 1-2 ProtocolVersion version; copied
3960 * 3-4 uint16 epoch; copied, must be 0
3961 * 5-10 uint48 sequence_number; copied
3962 * 11-12 uint16 length; (ignored)
3963 *
3964 * 13-13 HandshakeType msg_type; (ignored)
3965 * 14-16 uint24 length; (ignored)
3966 * 17-18 uint16 message_seq; copied
3967 * 19-21 uint24 fragment_offset; copied, must be 0
3968 * 22-24 uint24 fragment_length; (ignored)
3969 *
3970 * 25-26 ProtocolVersion client_version; (ignored)
3971 * 27-58 Random random; (ignored)
3972 * 59-xx SessionID session_id; 1 byte len + sid_len content
3973 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3974 * ...
3975 *
3976 * Minimum length is 61 bytes.
3977 */
3978 if( in_len < 61 ||
3979 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3980 in[3] != 0 || in[4] != 0 ||
3981 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3982 {
3983 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3984 }
3985
3986 sid_len = in[59];
3987 if( sid_len > in_len - 61 )
3988 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3989
3990 cookie_len = in[60 + sid_len];
3991 if( cookie_len > in_len - 60 )
3992 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3993
3994 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3995 cli_id, cli_id_len ) == 0 )
3996 {
3997 /* Valid cookie */
3998 return( 0 );
3999 }
4000
4001 /*
4002 * If we get here, we've got an invalid cookie, let's prepare HVR.
4003 *
4004 * 0-0 ContentType type; copied
4005 * 1-2 ProtocolVersion version; copied
4006 * 3-4 uint16 epoch; copied
4007 * 5-10 uint48 sequence_number; copied
4008 * 11-12 uint16 length; olen - 13
4009 *
4010 * 13-13 HandshakeType msg_type; hello_verify_request
4011 * 14-16 uint24 length; olen - 25
4012 * 17-18 uint16 message_seq; copied
4013 * 19-21 uint24 fragment_offset; copied
4014 * 22-24 uint24 fragment_length; olen - 25
4015 *
4016 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4017 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4018 *
4019 * Minimum length is 28.
4020 */
4021 if( buf_len < 28 )
4022 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4023
4024 /* Copy most fields and adapt others */
4025 memcpy( obuf, in, 25 );
4026 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4027 obuf[25] = 0xfe;
4028 obuf[26] = 0xff;
4029
4030 /* Generate and write actual cookie */
4031 p = obuf + 28;
4032 if( f_cookie_write( p_cookie,
4033 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4034 {
4035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4036 }
4037
4038 *olen = p - obuf;
4039
4040 /* Go back and fill length fields */
4041 obuf[27] = (unsigned char)( *olen - 28 );
4042
4043 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4044 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4045 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4046
4047 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4048 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4049
4050 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4051}
4052
4053/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004054 * Handle possible client reconnect with the same UDP quadruplet
4055 * (RFC 6347 Section 4.2.8).
4056 *
4057 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4058 * that looks like a ClientHello.
4059 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004060 * - if the input looks like a ClientHello without cookies,
4061 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004062 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004063 * - if the input looks like a ClientHello with a valid cookie,
4064 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004065 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004066 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004067 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004068 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004069 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4070 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004071 */
4072static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4073{
4074 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004075 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004076
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004077 ret = ssl_check_dtls_clihlo_cookie(
4078 ssl->conf->f_cookie_write,
4079 ssl->conf->f_cookie_check,
4080 ssl->conf->p_cookie,
4081 ssl->cli_id, ssl->cli_id_len,
4082 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004083 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004084
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004085 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4086
4087 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004088 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004089 int send_ret;
4090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4091 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4092 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004093 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004094 * If the error is permanent we'll catch it later,
4095 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004096 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4097 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4098 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004099
4100 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004101 }
4102
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004103 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004104 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004106 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4107 {
4108 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4109 return( ret );
4110 }
4111
4112 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004113 }
4114
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004115 return( ret );
4116}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004117#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004118
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004119/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004120 * ContentType type;
4121 * ProtocolVersion version;
4122 * uint16 epoch; // DTLS only
4123 * uint48 sequence_number; // DTLS only
4124 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004125 *
4126 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004127 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004128 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4129 *
4130 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004131 * 1. proceed with the record if this function returns 0
4132 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4133 * 3. return CLIENT_RECONNECT if this function return that value
4134 * 4. drop the whole datagram if this function returns anything else.
4135 * Point 2 is needed when the peer is resending, and we have already received
4136 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004139{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004140 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 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 +02004143
Paul Bakker5121ce52009-01-03 21:22:43 +00004144 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004145 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004146 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004149 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004150 ssl->in_msgtype,
4151 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004152
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004153 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4155 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4156 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4157 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004160
4161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004162 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4163 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004164 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4165#endif /* MBEDTLS_SSL_PROTO_DTLS */
4166 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4167 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004170 }
4171
4172 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004173 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004177 }
4178
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004179 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4182 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004183 }
4184
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004185 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004186 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004187 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4190 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004191 }
4192
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004193 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004194 * DTLS-related tests.
4195 * Check epoch before checking length constraint because
4196 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4197 * message gets duplicated before the corresponding Finished message,
4198 * the second ChangeCipherSpec should be discarded because it belongs
4199 * to an old epoch, but not because its length is shorter than
4200 * the minimum record length for packets using the new record transform.
4201 * Note that these two kinds of failures are handled differently,
4202 * as an unexpected record is silently skipped but an invalid
4203 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004204 */
4205#if defined(MBEDTLS_SSL_PROTO_DTLS)
4206 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4207 {
4208 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4209
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004210 /* Check epoch (and sequence number) with DTLS */
4211 if( rec_epoch != ssl->in_epoch )
4212 {
4213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4214 "expected %d, received %d",
4215 ssl->in_epoch, rec_epoch ) );
4216
4217#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4218 /*
4219 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4220 * access the first byte of record content (handshake type), as we
4221 * have an active transform (possibly iv_len != 0), so use the
4222 * fact that the record header len is 13 instead.
4223 */
4224 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4225 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4226 rec_epoch == 0 &&
4227 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4228 ssl->in_left > 13 &&
4229 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4230 {
4231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4232 "from the same port" ) );
4233 return( ssl_handle_possible_reconnect( ssl ) );
4234 }
4235 else
4236#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004237 {
4238 /* Consider buffering the record. */
4239 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4240 {
4241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4242 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4243 }
4244
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004245 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004246 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004247 }
4248
4249#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4250 /* Replay detection only works for the current epoch */
4251 if( rec_epoch == ssl->in_epoch &&
4252 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4253 {
4254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4255 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4256 }
4257#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004258
Hanno Becker52c6dc62017-05-26 16:07:36 +01004259 /* Drop unexpected ApplicationData records,
4260 * except at the beginning of renegotiations */
4261 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4262 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4263#if defined(MBEDTLS_SSL_RENEGOTIATION)
4264 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4265 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4266#endif
4267 )
4268 {
4269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4270 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4271 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004272 }
4273#endif /* MBEDTLS_SSL_PROTO_DTLS */
4274
Hanno Becker52c6dc62017-05-26 16:07:36 +01004275
4276 /* Check length against bounds of the current transform and version */
4277 if( ssl->transform_in == NULL )
4278 {
4279 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004280 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004281 {
4282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4283 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4284 }
4285 }
4286 else
4287 {
4288 if( ssl->in_msglen < ssl->transform_in->minlen )
4289 {
4290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4291 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4292 }
4293
4294#if defined(MBEDTLS_SSL_PROTO_SSL3)
4295 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004296 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004297 {
4298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4299 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4300 }
4301#endif
4302#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4303 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4304 /*
4305 * TLS encrypted messages can have up to 256 bytes of padding
4306 */
4307 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4308 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004309 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004310 {
4311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4312 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4313 }
4314#endif
4315 }
4316
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004317 return( 0 );
4318}
Paul Bakker5121ce52009-01-03 21:22:43 +00004319
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004320/*
4321 * If applicable, decrypt (and decompress) record content
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004324{
4325 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4328 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4331 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 ret = mbedtls_ssl_hw_record_read( ssl );
4336 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4339 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004340 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004341
4342 if( ret == 0 )
4343 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004344 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004346 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 {
4348 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004351 return( ret );
4352 }
4353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 ssl->in_msg, ssl->in_msglen );
4356
Angus Grattond8213d02016-05-25 20:56:48 +10004357 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4360 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 }
4362 }
4363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004365 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004367 {
4368 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004371 return( ret );
4372 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004377 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004380 }
4381#endif
4382
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004383 return( 0 );
4384}
4385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004387
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004388/*
4389 * Read a record.
4390 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004391 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4392 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4393 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004394 */
Hanno Becker1097b342018-08-15 14:09:41 +01004395
4396/* Helper functions for mbedtls_ssl_read_record(). */
4397static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004398static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4399static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004400
Hanno Becker327c93b2018-08-15 13:56:18 +01004401int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004402 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004403{
4404 int ret;
4405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004407
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004408 if( ssl->keep_current_message == 0 )
4409 {
4410 do {
Simon Butcher99000142016-10-13 17:21:01 +01004411
Hanno Becker26994592018-08-15 14:14:59 +01004412 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004413 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004414 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004415
Hanno Beckere74d5562018-08-15 14:26:08 +01004416 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004417 {
Hanno Becker40f50842018-08-15 14:48:01 +01004418#if defined(MBEDTLS_SSL_PROTO_DTLS)
4419 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004420
Hanno Becker40f50842018-08-15 14:48:01 +01004421 /* We only check for buffered messages if the
4422 * current datagram is fully consumed. */
4423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004424 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004425 {
Hanno Becker40f50842018-08-15 14:48:01 +01004426 if( ssl_load_buffered_message( ssl ) == 0 )
4427 have_buffered = 1;
4428 }
4429
4430 if( have_buffered == 0 )
4431#endif /* MBEDTLS_SSL_PROTO_DTLS */
4432 {
4433 ret = ssl_get_next_record( ssl );
4434 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4435 continue;
4436
4437 if( ret != 0 )
4438 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004439 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004440 return( ret );
4441 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004442 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004443 }
4444
4445 ret = mbedtls_ssl_handle_message_type( ssl );
4446
Hanno Becker40f50842018-08-15 14:48:01 +01004447#if defined(MBEDTLS_SSL_PROTO_DTLS)
4448 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4449 {
4450 /* Buffer future message */
4451 ret = ssl_buffer_message( ssl );
4452 if( ret != 0 )
4453 return( ret );
4454
4455 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4456 }
4457#endif /* MBEDTLS_SSL_PROTO_DTLS */
4458
Hanno Becker90333da2017-10-10 11:27:13 +01004459 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4460 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004461
4462 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004463 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004464 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004465 return( ret );
4466 }
4467
Hanno Becker327c93b2018-08-15 13:56:18 +01004468 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004469 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004470 {
4471 mbedtls_ssl_update_handshake_status( ssl );
4472 }
Simon Butcher99000142016-10-13 17:21:01 +01004473 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004474 else
Simon Butcher99000142016-10-13 17:21:01 +01004475 {
Hanno Becker02f59072018-08-15 14:00:24 +01004476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004477 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004478 }
4479
4480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4481
4482 return( 0 );
4483}
4484
Hanno Becker40f50842018-08-15 14:48:01 +01004485#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004486static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004487{
Hanno Becker40f50842018-08-15 14:48:01 +01004488 if( ssl->in_left > ssl->next_record_offset )
4489 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004490
Hanno Becker40f50842018-08-15 14:48:01 +01004491 return( 0 );
4492}
4493
4494static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4495{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004496 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004497 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004498 int ret = 0;
4499
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004500 if( hs == NULL )
4501 return( -1 );
4502
Hanno Beckere00ae372018-08-20 09:39:42 +01004503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4504
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004505 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4506 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4507 {
4508 /* Check if we have seen a ChangeCipherSpec before.
4509 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004510 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004511 {
4512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4513 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004514 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004515 }
4516
Hanno Becker39b8bc92018-08-28 17:17:13 +01004517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004518 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4519 ssl->in_msglen = 1;
4520 ssl->in_msg[0] = 1;
4521
4522 /* As long as they are equal, the exact value doesn't matter. */
4523 ssl->in_left = 0;
4524 ssl->next_record_offset = 0;
4525
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004526 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004527 goto exit;
4528 }
Hanno Becker37f95322018-08-16 13:55:32 +01004529
Hanno Beckerb8f50142018-08-28 10:01:34 +01004530#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004531 /* Debug only */
4532 {
4533 unsigned offset;
4534 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4535 {
4536 hs_buf = &hs->buffering.hs[offset];
4537 if( hs_buf->is_valid == 1 )
4538 {
4539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4540 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004541 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004542 }
4543 }
4544 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004545#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004546
4547 /* Check if we have buffered and/or fully reassembled the
4548 * next handshake message. */
4549 hs_buf = &hs->buffering.hs[0];
4550 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4551 {
4552 /* Synthesize a record containing the buffered HS message. */
4553 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4554 ( hs_buf->data[2] << 8 ) |
4555 hs_buf->data[3];
4556
4557 /* Double-check that we haven't accidentally buffered
4558 * a message that doesn't fit into the input buffer. */
4559 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4560 {
4561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4563 }
4564
4565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4566 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4567 hs_buf->data, msg_len + 12 );
4568
4569 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4570 ssl->in_hslen = msg_len + 12;
4571 ssl->in_msglen = msg_len + 12;
4572 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4573
4574 ret = 0;
4575 goto exit;
4576 }
4577 else
4578 {
4579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4580 hs->in_msg_seq ) );
4581 }
4582
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004583 ret = -1;
4584
4585exit:
4586
4587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4588 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004589}
4590
Hanno Beckera02b0b42018-08-21 17:20:27 +01004591static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4592 size_t desired )
4593{
4594 int offset;
4595 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4597 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004598
Hanno Becker01315ea2018-08-21 17:22:17 +01004599 /* Get rid of future records epoch first, if such exist. */
4600 ssl_free_buffered_record( ssl );
4601
4602 /* Check if we have enough space available now. */
4603 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4604 hs->buffering.total_bytes_buffered ) )
4605 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004607 return( 0 );
4608 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004609
Hanno Becker4f432ad2018-08-28 10:02:32 +01004610 /* We don't have enough space to buffer the next expected handshake
4611 * message. Remove buffers used for future messages to gain space,
4612 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004613 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4614 offset >= 0; offset-- )
4615 {
4616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4617 offset ) );
4618
Hanno Beckerb309b922018-08-23 13:18:05 +01004619 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004620
4621 /* Check if we have enough space available now. */
4622 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4623 hs->buffering.total_bytes_buffered ) )
4624 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004626 return( 0 );
4627 }
4628 }
4629
4630 return( -1 );
4631}
4632
Hanno Becker40f50842018-08-15 14:48:01 +01004633static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4634{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004635 int ret = 0;
4636 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4637
4638 if( hs == NULL )
4639 return( 0 );
4640
4641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4642
4643 switch( ssl->in_msgtype )
4644 {
4645 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004647
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004648 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004649 break;
4650
4651 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004652 {
4653 unsigned recv_msg_seq_offset;
4654 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4655 mbedtls_ssl_hs_buffer *hs_buf;
4656 size_t msg_len = ssl->in_hslen - 12;
4657
4658 /* We should never receive an old handshake
4659 * message - double-check nonetheless. */
4660 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4661 {
4662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4664 }
4665
4666 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4667 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4668 {
4669 /* Silently ignore -- message too far in the future */
4670 MBEDTLS_SSL_DEBUG_MSG( 2,
4671 ( "Ignore future HS message with sequence number %u, "
4672 "buffering window %u - %u",
4673 recv_msg_seq, ssl->handshake->in_msg_seq,
4674 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4675
4676 goto exit;
4677 }
4678
4679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4680 recv_msg_seq, recv_msg_seq_offset ) );
4681
4682 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4683
4684 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004685 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004686 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004687 size_t reassembly_buf_sz;
4688
Hanno Becker37f95322018-08-16 13:55:32 +01004689 hs_buf->is_fragmented =
4690 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4691
4692 /* We copy the message back into the input buffer
4693 * after reassembly, so check that it's not too large.
4694 * This is an implementation-specific limitation
4695 * and not one from the standard, hence it is not
4696 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004697 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004698 {
4699 /* Ignore message */
4700 goto exit;
4701 }
4702
Hanno Beckere0b150f2018-08-21 15:51:03 +01004703 /* Check if we have enough space to buffer the message. */
4704 if( hs->buffering.total_bytes_buffered >
4705 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4706 {
4707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4709 }
4710
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004711 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4712 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004713
4714 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4715 hs->buffering.total_bytes_buffered ) )
4716 {
4717 if( recv_msg_seq_offset > 0 )
4718 {
4719 /* If we can't buffer a future message because
4720 * of space limitations -- ignore. */
4721 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",
4722 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4723 (unsigned) hs->buffering.total_bytes_buffered ) );
4724 goto exit;
4725 }
Hanno Beckere1801392018-08-21 16:51:05 +01004726 else
4727 {
4728 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",
4729 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4730 (unsigned) hs->buffering.total_bytes_buffered ) );
4731 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004732
Hanno Beckera02b0b42018-08-21 17:20:27 +01004733 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004734 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004735 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",
4736 (unsigned) msg_len,
4737 (unsigned) reassembly_buf_sz,
4738 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004739 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004740 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4741 goto exit;
4742 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004743 }
4744
4745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4746 msg_len ) );
4747
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004748 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4749 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004750 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004751 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004752 goto exit;
4753 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004754 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004755
4756 /* Prepare final header: copy msg_type, length and message_seq,
4757 * then add standardised fragment_offset and fragment_length */
4758 memcpy( hs_buf->data, ssl->in_msg, 6 );
4759 memset( hs_buf->data + 6, 0, 3 );
4760 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4761
4762 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004763
4764 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004765 }
4766 else
4767 {
4768 /* Make sure msg_type and length are consistent */
4769 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4770 {
4771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4772 /* Ignore */
4773 goto exit;
4774 }
4775 }
4776
Hanno Becker4422bbb2018-08-20 09:40:19 +01004777 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004778 {
4779 size_t frag_len, frag_off;
4780 unsigned char * const msg = hs_buf->data + 12;
4781
4782 /*
4783 * Check and copy current fragment
4784 */
4785
4786 /* Validation of header fields already done in
4787 * mbedtls_ssl_prepare_handshake_record(). */
4788 frag_off = ssl_get_hs_frag_off( ssl );
4789 frag_len = ssl_get_hs_frag_len( ssl );
4790
4791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4792 frag_off, frag_len ) );
4793 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4794
4795 if( hs_buf->is_fragmented )
4796 {
4797 unsigned char * const bitmask = msg + msg_len;
4798 ssl_bitmask_set( bitmask, frag_off, frag_len );
4799 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4800 msg_len ) == 0 );
4801 }
4802 else
4803 {
4804 hs_buf->is_complete = 1;
4805 }
4806
4807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4808 hs_buf->is_complete ? "" : "not yet " ) );
4809 }
4810
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004811 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004812 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004813
4814 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004815 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004816 break;
4817 }
4818
4819exit:
4820
4821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4822 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004823}
4824#endif /* MBEDTLS_SSL_PROTO_DTLS */
4825
Hanno Becker1097b342018-08-15 14:09:41 +01004826static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004827{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004828 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004829 * Consume last content-layer message and potentially
4830 * update in_msglen which keeps track of the contents'
4831 * consumption state.
4832 *
4833 * (1) Handshake messages:
4834 * Remove last handshake message, move content
4835 * and adapt in_msglen.
4836 *
4837 * (2) Alert messages:
4838 * Consume whole record content, in_msglen = 0.
4839 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004840 * (3) Change cipher spec:
4841 * Consume whole record content, in_msglen = 0.
4842 *
4843 * (4) Application data:
4844 * Don't do anything - the record layer provides
4845 * the application data as a stream transport
4846 * and consumes through mbedtls_ssl_read only.
4847 *
4848 */
4849
4850 /* Case (1): Handshake messages */
4851 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004852 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004853 /* Hard assertion to be sure that no application data
4854 * is in flight, as corrupting ssl->in_msglen during
4855 * ssl->in_offt != NULL is fatal. */
4856 if( ssl->in_offt != NULL )
4857 {
4858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4860 }
4861
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004862 /*
4863 * Get next Handshake message in the current record
4864 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004865
Hanno Becker4a810fb2017-05-24 16:27:30 +01004866 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004867 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004868 * current handshake content: If DTLS handshake
4869 * fragmentation is used, that's the fragment
4870 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004871 * size here is faulty and should be changed at
4872 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004873 * (2) While it doesn't seem to cause problems, one
4874 * has to be very careful not to assume that in_hslen
4875 * is always <= in_msglen in a sensible communication.
4876 * Again, it's wrong for DTLS handshake fragmentation.
4877 * The following check is therefore mandatory, and
4878 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004879 * Additionally, ssl->in_hslen might be arbitrarily out of
4880 * bounds after handling a DTLS message with an unexpected
4881 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004882 */
4883 if( ssl->in_hslen < ssl->in_msglen )
4884 {
4885 ssl->in_msglen -= ssl->in_hslen;
4886 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4887 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004888
Hanno Becker4a810fb2017-05-24 16:27:30 +01004889 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4890 ssl->in_msg, ssl->in_msglen );
4891 }
4892 else
4893 {
4894 ssl->in_msglen = 0;
4895 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004896
Hanno Becker4a810fb2017-05-24 16:27:30 +01004897 ssl->in_hslen = 0;
4898 }
4899 /* Case (4): Application data */
4900 else if( ssl->in_offt != NULL )
4901 {
4902 return( 0 );
4903 }
4904 /* Everything else (CCS & Alerts) */
4905 else
4906 {
4907 ssl->in_msglen = 0;
4908 }
4909
Hanno Becker1097b342018-08-15 14:09:41 +01004910 return( 0 );
4911}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004912
Hanno Beckere74d5562018-08-15 14:26:08 +01004913static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4914{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004915 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004916 return( 1 );
4917
4918 return( 0 );
4919}
4920
Hanno Becker5f066e72018-08-16 14:56:31 +01004921#if defined(MBEDTLS_SSL_PROTO_DTLS)
4922
4923static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4924{
4925 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4926 if( hs == NULL )
4927 return;
4928
Hanno Becker01315ea2018-08-21 17:22:17 +01004929 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004930 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004931 hs->buffering.total_bytes_buffered -=
4932 hs->buffering.future_record.len;
4933
4934 mbedtls_free( hs->buffering.future_record.data );
4935 hs->buffering.future_record.data = NULL;
4936 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004937}
4938
4939static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4940{
4941 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4942 unsigned char * rec;
4943 size_t rec_len;
4944 unsigned rec_epoch;
4945
4946 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4947 return( 0 );
4948
4949 if( hs == NULL )
4950 return( 0 );
4951
Hanno Becker5f066e72018-08-16 14:56:31 +01004952 rec = hs->buffering.future_record.data;
4953 rec_len = hs->buffering.future_record.len;
4954 rec_epoch = hs->buffering.future_record.epoch;
4955
4956 if( rec == NULL )
4957 return( 0 );
4958
Hanno Becker4cb782d2018-08-20 11:19:05 +01004959 /* Only consider loading future records if the
4960 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004961 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004962 return( 0 );
4963
Hanno Becker5f066e72018-08-16 14:56:31 +01004964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4965
4966 if( rec_epoch != ssl->in_epoch )
4967 {
4968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4969 goto exit;
4970 }
4971
4972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4973
4974 /* Double-check that the record is not too large */
4975 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4976 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4977 {
4978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4980 }
4981
4982 memcpy( ssl->in_hdr, rec, rec_len );
4983 ssl->in_left = rec_len;
4984 ssl->next_record_offset = 0;
4985
4986 ssl_free_buffered_record( ssl );
4987
4988exit:
4989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4990 return( 0 );
4991}
4992
4993static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4994{
4995 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4996 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004997 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004998
4999 /* Don't buffer future records outside handshakes. */
5000 if( hs == NULL )
5001 return( 0 );
5002
5003 /* Only buffer handshake records (we are only interested
5004 * in Finished messages). */
5005 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5006 return( 0 );
5007
5008 /* Don't buffer more than one future epoch record. */
5009 if( hs->buffering.future_record.data != NULL )
5010 return( 0 );
5011
Hanno Becker01315ea2018-08-21 17:22:17 +01005012 /* Don't buffer record if there's not enough buffering space remaining. */
5013 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5014 hs->buffering.total_bytes_buffered ) )
5015 {
5016 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",
5017 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5018 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005019 return( 0 );
5020 }
5021
Hanno Becker5f066e72018-08-16 14:56:31 +01005022 /* Buffer record */
5023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5024 ssl->in_epoch + 1 ) );
5025 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5026 rec_hdr_len + ssl->in_msglen );
5027
5028 /* ssl_parse_record_header() only considers records
5029 * of the next epoch as candidates for buffering. */
5030 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005031 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005032
5033 hs->buffering.future_record.data =
5034 mbedtls_calloc( 1, hs->buffering.future_record.len );
5035 if( hs->buffering.future_record.data == NULL )
5036 {
5037 /* If we run out of RAM trying to buffer a
5038 * record from the next epoch, just ignore. */
5039 return( 0 );
5040 }
5041
Hanno Becker01315ea2018-08-21 17:22:17 +01005042 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005043
Hanno Becker01315ea2018-08-21 17:22:17 +01005044 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005045 return( 0 );
5046}
5047
5048#endif /* MBEDTLS_SSL_PROTO_DTLS */
5049
Hanno Beckere74d5562018-08-15 14:26:08 +01005050static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005051{
5052 int ret;
5053
Hanno Becker5f066e72018-08-16 14:56:31 +01005054#if defined(MBEDTLS_SSL_PROTO_DTLS)
5055 /* We might have buffered a future record; if so,
5056 * and if the epoch matches now, load it.
5057 * On success, this call will set ssl->in_left to
5058 * the length of the buffered record, so that
5059 * the calls to ssl_fetch_input() below will
5060 * essentially be no-ops. */
5061 ret = ssl_load_buffered_record( ssl );
5062 if( ret != 0 )
5063 return( ret );
5064#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005069 return( ret );
5070 }
5071
5072 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005075 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5076 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005077 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005078 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5079 {
5080 ret = ssl_buffer_future_record( ssl );
5081 if( ret != 0 )
5082 return( ret );
5083
5084 /* Fall through to handling of unexpected records */
5085 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5086 }
5087
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005088 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5089 {
5090 /* Skip unexpected record (but not whole datagram) */
5091 ssl->next_record_offset = ssl->in_msglen
5092 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005093
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5095 "(header)" ) );
5096 }
5097 else
5098 {
5099 /* Skip invalid record and the rest of the datagram */
5100 ssl->next_record_offset = 0;
5101 ssl->in_left = 0;
5102
5103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5104 "(header)" ) );
5105 }
5106
5107 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005108 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005109 }
5110#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005111 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005112 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005113
5114 /*
5115 * Read and optionally decrypt the message contents
5116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5118 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005121 return( ret );
5122 }
5123
5124 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005126 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005129 if( ssl->next_record_offset < ssl->in_left )
5130 {
5131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5132 }
5133 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005134 else
5135#endif
5136 ssl->in_left = 0;
5137
5138 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005142 {
5143 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5145 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005146 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005147 /* Except when waiting for Finished as a bad mac here
5148 * probably means something went wrong in the handshake
5149 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5150 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5151 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5152 {
5153#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5154 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5155 {
5156 mbedtls_ssl_send_alert_message( ssl,
5157 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5158 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5159 }
5160#endif
5161 return( ret );
5162 }
5163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005165 if( ssl->conf->badmac_limit != 0 &&
5166 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5169 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005170 }
5171#endif
5172
Hanno Becker4a810fb2017-05-24 16:27:30 +01005173 /* As above, invalid records cause
5174 * dismissal of the whole datagram. */
5175
5176 ssl->next_record_offset = 0;
5177 ssl->in_left = 0;
5178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005180 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005181 }
5182
5183 return( ret );
5184 }
5185 else
5186#endif
5187 {
5188 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5190 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 mbedtls_ssl_send_alert_message( ssl,
5193 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5194 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005195 }
5196#endif
5197 return( ret );
5198 }
5199 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005200
Simon Butcher99000142016-10-13 17:21:01 +01005201 return( 0 );
5202}
5203
5204int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5205{
5206 int ret;
5207
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005208 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005209 * Handle particular types of records
5210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005212 {
Simon Butcher99000142016-10-13 17:21:01 +01005213 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5214 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005215 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005217 }
5218
Hanno Beckere678eaa2018-08-21 14:57:46 +01005219 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005220 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005221 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005222 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5224 ssl->in_msglen ) );
5225 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005226 }
5227
Hanno Beckere678eaa2018-08-21 14:57:46 +01005228 if( ssl->in_msg[0] != 1 )
5229 {
5230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5231 ssl->in_msg[0] ) );
5232 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5233 }
5234
5235#if defined(MBEDTLS_SSL_PROTO_DTLS)
5236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5237 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5238 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5239 {
5240 if( ssl->handshake == NULL )
5241 {
5242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5243 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5244 }
5245
5246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5247 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5248 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005249#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005250 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005253 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005254 if( ssl->in_msglen != 2 )
5255 {
5256 /* Note: Standard allows for more than one 2 byte alert
5257 to be packed in a single message, but Mbed TLS doesn't
5258 currently support this. */
5259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5260 ssl->in_msglen ) );
5261 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5262 }
5263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005265 ssl->in_msg[0], ssl->in_msg[1] ) );
5266
5267 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005268 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005273 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005275 }
5276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5278 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5281 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005283
5284#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5285 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5286 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5287 {
Hanno Becker90333da2017-10-10 11:27:13 +01005288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005289 /* Will be handled when trying to parse ServerHello */
5290 return( 0 );
5291 }
5292#endif
5293
5294#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5295 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5296 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5297 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5298 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5299 {
5300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5301 /* Will be handled in mbedtls_ssl_parse_certificate() */
5302 return( 0 );
5303 }
5304#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5305
5306 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005307 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005308 }
5309
Hanno Beckerc76c6192017-06-06 10:03:17 +01005310#if defined(MBEDTLS_SSL_PROTO_DTLS)
5311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5312 ssl->handshake != NULL &&
5313 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5314 {
5315 ssl_handshake_wrapup_free_hs_transform( ssl );
5316 }
5317#endif
5318
Paul Bakker5121ce52009-01-03 21:22:43 +00005319 return( 0 );
5320}
5321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005323{
5324 int ret;
5325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5328 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005329 {
5330 return( ret );
5331 }
5332
5333 return( 0 );
5334}
5335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005337 unsigned char level,
5338 unsigned char message )
5339{
5340 int ret;
5341
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005342 if( ssl == NULL || ssl->conf == NULL )
5343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005349 ssl->out_msglen = 2;
5350 ssl->out_msg[0] = level;
5351 ssl->out_msg[1] = message;
5352
Hanno Becker67bc7c32018-08-06 11:33:50 +01005353 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005356 return( ret );
5357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005359
5360 return( 0 );
5361}
5362
Paul Bakker5121ce52009-01-03 21:22:43 +00005363/*
5364 * Handshake functions
5365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5367 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5368 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5369 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5370 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5371 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5372 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005373/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5381 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005382 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5383 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005386 ssl->state++;
5387 return( 0 );
5388 }
5389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005392}
5393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5401 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005402 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5403 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005406 ssl->state++;
5407 return( 0 );
5408 }
5409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005412}
Gilles Peskinef9828522017-05-03 12:28:43 +02005413
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005414#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005415/* Some certificate support -> implement write and parse */
5416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005420 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 const mbedtls_x509_crt *crt;
5422 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5427 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005428 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5429 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005432 ssl->state++;
5433 return( 0 );
5434 }
5435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005437 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 {
5439 if( ssl->client_auth == 0 )
5440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 ssl->state++;
5443 return( 0 );
5444 }
5445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005447 /*
5448 * If using SSLv3 and got no cert, send an Alert message
5449 * (otherwise an empty Certificate message will be sent).
5450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5452 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 {
5454 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5456 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5457 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005460 goto write_msg;
5461 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#endif /* MBEDTLS_SSL_CLI_C */
5465#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005466 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5471 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 }
5473 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005477
5478 /*
5479 * 0 . 0 handshake type
5480 * 1 . 3 handshake length
5481 * 4 . 6 length of all certs
5482 * 7 . 9 length of cert. 1
5483 * 10 . n-1 peer certificate
5484 * n . n+2 length of cert. 2
5485 * n+3 . ... upper level cert, etc.
5486 */
5487 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Paul Bakker29087132010-03-21 21:03:34 +00005490 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 {
5492 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005493 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005496 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498 }
5499
5500 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5501 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5502 ssl->out_msg[i + 2] = (unsigned char)( n );
5503
5504 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5505 i += n; crt = crt->next;
5506 }
5507
5508 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5509 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5510 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5511
5512 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5514 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005515
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005516#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005517write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005519
5520 ssl->state++;
5521
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005522 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005523 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 return( ret );
5526 }
5527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005529
Paul Bakkered27a042013-04-18 22:46:23 +02005530 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005531}
5532
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005533/*
5534 * Once the certificate message is read, parse it into a cert chain and
5535 * perform basic checks, but leave actual verification to the caller
5536 */
5537static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005538{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005539 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005540 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005541 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543#if defined(MBEDTLS_SSL_SRV_C)
5544#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 /*
5546 * Check if the client sent an empty certificate
5547 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005548 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005550 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005551 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5553 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5554 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005557
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005558 /* The client was asked for a certificate but didn't send
5559 one. The client should know what's going on, so we
5560 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005561 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005562 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005563 }
5564 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5568 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005569 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5573 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5574 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5575 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005578
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005579 /* The client was asked for a certificate but didn't send
5580 one. The client should know what's going on, so we
5581 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005582 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005583 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005584 }
5585 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5587 MBEDTLS_SSL_PROTO_TLS1_2 */
5588#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005593 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5594 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005596 }
5597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5599 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005605 }
5606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005608
Paul Bakker5121ce52009-01-03 21:22:43 +00005609 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005612 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005613
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005614 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005618 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5619 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 }
5622
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005623 /* In case we tried to reuse a session but it failed */
5624 if( ssl->session_negotiate->peer_cert != NULL )
5625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5627 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005628 }
5629
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005630 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005635 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5636 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005637 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 }
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005642 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005643
5644 while( i < ssl->in_hslen )
5645 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005646 if ( i + 3 > ssl->in_hslen ) {
5647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5648 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5649 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5650 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005652 if( ssl->in_msg[i] != 0 )
5653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005655 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5656 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005658 }
5659
5660 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5661 | (unsigned int) ssl->in_msg[i + 2];
5662 i += 3;
5663
5664 if( n < 128 || i + n > ssl->in_hslen )
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005673 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005674 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005675 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005676 case 0: /*ok*/
5677 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5678 /* Ignore certificate with an unknown algorithm: maybe a
5679 prior certificate was already trusted. */
5680 break;
5681
5682 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5683 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5684 goto crt_parse_der_failed;
5685
5686 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5687 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5688 goto crt_parse_der_failed;
5689
5690 default:
5691 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5692 crt_parse_der_failed:
5693 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005695 return( ret );
5696 }
5697
5698 i += n;
5699 }
5700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005702
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005703 /*
5704 * On client, make sure the server cert doesn't change during renego to
5705 * avoid "triple handshake" attack: https://secure-resumption.com/
5706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005708 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005710 {
5711 if( ssl->session->peer_cert == NULL )
5712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5715 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005717 }
5718
5719 if( ssl->session->peer_cert->raw.len !=
5720 ssl->session_negotiate->peer_cert->raw.len ||
5721 memcmp( ssl->session->peer_cert->raw.p,
5722 ssl->session_negotiate->peer_cert->raw.p,
5723 ssl->session->peer_cert->raw.len ) != 0 )
5724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed 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 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005732
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005733 return( 0 );
5734}
5735
5736int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5737{
5738 int ret;
5739 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5740 ssl->transform_negotiate->ciphersuite_info;
5741#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5742 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5743 ? ssl->handshake->sni_authmode
5744 : ssl->conf->authmode;
5745#else
5746 const int authmode = ssl->conf->authmode;
5747#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005748 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005749
5750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5751
5752 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5753 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5754 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5755 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5756 {
5757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5758 ssl->state++;
5759 return( 0 );
5760 }
5761
5762#if defined(MBEDTLS_SSL_SRV_C)
5763 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5764 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5765 {
5766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5767 ssl->state++;
5768 return( 0 );
5769 }
5770
5771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5772 authmode == MBEDTLS_SSL_VERIFY_NONE )
5773 {
5774 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005776
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005777 ssl->state++;
5778 return( 0 );
5779 }
5780#endif
5781
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005782#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5783 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005784 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005785 {
5786 goto crt_verify;
5787 }
5788#endif
5789
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005790 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005791 {
5792 /* mbedtls_ssl_read_record may have sent an alert already. We
5793 let it decide whether to alert. */
5794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5795 return( ret );
5796 }
5797
5798 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5799 {
5800#if defined(MBEDTLS_SSL_SRV_C)
5801 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5802 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5803 {
5804 ret = 0;
5805 }
5806#endif
5807
5808 ssl->state++;
5809 return( ret );
5810 }
5811
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005812#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5813 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005814 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005815
5816crt_verify:
5817 if( ssl->handshake->ecrs_enabled)
5818 rs_ctx = &ssl->handshake->ecrs_ctx;
5819#endif
5820
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005821 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005822 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005823 mbedtls_x509_crt *ca_chain;
5824 mbedtls_x509_crl *ca_crl;
5825
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005826#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005827 if( ssl->handshake->sni_ca_chain != NULL )
5828 {
5829 ca_chain = ssl->handshake->sni_ca_chain;
5830 ca_crl = ssl->handshake->sni_ca_crl;
5831 }
5832 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005833#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005834 {
5835 ca_chain = ssl->conf->ca_chain;
5836 ca_crl = ssl->conf->ca_crl;
5837 }
5838
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005839 /*
5840 * Main check: verify certificate
5841 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005842 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005843 ssl->session_negotiate->peer_cert,
5844 ca_chain, ca_crl,
5845 ssl->conf->cert_profile,
5846 ssl->hostname,
5847 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005848 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005849
5850 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005853 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005854
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005855#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5856 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005857 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005858#endif
5859
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005860 /*
5861 * Secondary checks: always done, but change 'ret' only if it was 0
5862 */
5863
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005864#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005867
5868 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005870 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005871 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005872 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005875 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005877 }
5878 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005879#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005882 ciphersuite_info,
5883 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005884 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
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é-Gonnard7f2a07d2014-04-09 09:50:57 +02005889 }
5890
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005891 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5892 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5893 * with details encoded in the verification flags. All other kinds
5894 * of error codes, including those from the user provided f_vrfy
5895 * functions, are treated as fatal and lead to a failure of
5896 * ssl_parse_certificate even if verification was optional. */
5897 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5898 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5899 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5900 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005901 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005902 }
5903
5904 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5905 {
5906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5907 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5908 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005909
5910 if( ret != 0 )
5911 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005912 uint8_t alert;
5913
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005914 /* The certificate may have been rejected for several reasons.
5915 Pick one and send the corresponding alert. Which alert to send
5916 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005917 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5918 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5919 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5920 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5921 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5922 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5923 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5924 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5925 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5926 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5927 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5928 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5929 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5930 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5931 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5932 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5933 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5934 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5935 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5936 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005937 else
5938 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005939 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5940 alert );
5941 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005942
Hanno Beckere6706e62017-05-15 16:05:15 +01005943#if defined(MBEDTLS_DEBUG_C)
5944 if( ssl->session_negotiate->verify_result != 0 )
5945 {
5946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5947 ssl->session_negotiate->verify_result ) );
5948 }
5949 else
5950 {
5951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5952 }
5953#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005954 }
5955
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005956 ssl->state++;
5957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005959
5960 return( ret );
5961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5963 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5964 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5965 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5966 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5967 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5968 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005971{
5972 int ret;
5973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005977 ssl->out_msglen = 1;
5978 ssl->out_msg[0] = 1;
5979
Paul Bakker5121ce52009-01-03 21:22:43 +00005980 ssl->state++;
5981
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005982 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005983 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005985 return( ret );
5986 }
5987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005989
5990 return( 0 );
5991}
5992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005994{
5995 int ret;
5996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005998
Hanno Becker327c93b2018-08-15 13:56:18 +01005999 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006002 return( ret );
6003 }
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006008 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6009 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006011 }
6012
Hanno Beckere678eaa2018-08-21 14:57:46 +01006013 /* CCS records are only accepted if they have length 1 and content '1',
6014 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006015
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006016 /*
6017 * Switch to our negotiated transform and session parameters for inbound
6018 * data.
6019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006021 ssl->transform_in = ssl->transform_negotiate;
6022 ssl->session_in = ssl->session_negotiate;
6023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006028 ssl_dtls_replay_reset( ssl );
6029#endif
6030
6031 /* Increment epoch */
6032 if( ++ssl->in_epoch == 0 )
6033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006035 /* This is highly unlikely to happen for legitimate reasons, so
6036 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006038 }
6039 }
6040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006042 memset( ssl->in_ctr, 0, 8 );
6043
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006044 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6047 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006052 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6053 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006055 }
6056 }
6057#endif
6058
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 ssl->state++;
6060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062
6063 return( 0 );
6064}
6065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6067 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006068{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006069 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6072 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6073 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006074 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006075 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6078#if defined(MBEDTLS_SHA512_C)
6079 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006080 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6081 else
6082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083#if defined(MBEDTLS_SHA256_C)
6084 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006085 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006086 else
6087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006091 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006092 }
Paul Bakker380da532012-04-18 16:10:25 +00006093}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6098 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006099 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6100 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6103#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006104 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006107 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006110}
6111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006113 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006114{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6116 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006117 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6118 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6121#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006122 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006125 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006128}
6129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6131 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6132static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006133 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006134{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006135 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6136 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006137}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006138#endif
Paul Bakker380da532012-04-18 16:10:25 +00006139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6141#if defined(MBEDTLS_SHA256_C)
6142static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006143 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006144{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006145 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006146}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006147#endif
Paul Bakker380da532012-04-18 16:10:25 +00006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SHA512_C)
6150static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006151 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006152{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006153 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006154}
Paul Bakker769075d2012-11-24 11:26:46 +01006155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006159static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006161{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006162 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006163 mbedtls_md5_context md5;
6164 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006165
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 unsigned char padbuf[48];
6167 unsigned char md5sum[16];
6168 unsigned char sha1sum[20];
6169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006171 if( !session )
6172 session = ssl->session;
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006175
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006176 mbedtls_md5_init( &md5 );
6177 mbedtls_sha1_init( &sha1 );
6178
6179 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6180 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006181
6182 /*
6183 * SSLv3:
6184 * hash =
6185 * MD5( master + pad2 +
6186 * MD5( handshake + sender + master + pad1 ) )
6187 * + SHA1( master + pad2 +
6188 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 */
6190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006192 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6193 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006194#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006197 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6198 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006202 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006203
Paul Bakker1ef83d62012-04-11 12:09:53 +00006204 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006205
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006206 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6207 mbedtls_md5_update_ret( &md5, session->master, 48 );
6208 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6209 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006211 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6212 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6213 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6214 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006215
Paul Bakker1ef83d62012-04-11 12:09:53 +00006216 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006217
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006218 mbedtls_md5_starts_ret( &md5 );
6219 mbedtls_md5_update_ret( &md5, session->master, 48 );
6220 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6221 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6222 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006223
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006224 mbedtls_sha1_starts_ret( &sha1 );
6225 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6226 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6227 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6228 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006231
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006232 mbedtls_md5_free( &md5 );
6233 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006234
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006235 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6236 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6237 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006244static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006246{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006247 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006248 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006249 mbedtls_md5_context md5;
6250 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006251 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006254 if( !session )
6255 session = ssl->session;
6256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006259 mbedtls_md5_init( &md5 );
6260 mbedtls_sha1_init( &sha1 );
6261
6262 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6263 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006264
Paul Bakker1ef83d62012-04-11 12:09:53 +00006265 /*
6266 * TLSv1:
6267 * hash = PRF( master, finished_label,
6268 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6269 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006272 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6273 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006274#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006277 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6278 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006279#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006282 ? "client finished"
6283 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006284
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006285 mbedtls_md5_finish_ret( &md5, padbuf );
6286 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006287
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006288 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006289 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006292
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006293 mbedtls_md5_free( &md5 );
6294 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006295
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006296 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6303#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006304static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006306{
6307 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006308 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006309 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006310 unsigned char padbuf[32];
6311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006313 if( !session )
6314 session = ssl->session;
6315
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006316 mbedtls_sha256_init( &sha256 );
6317
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006319
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006320 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006321
6322 /*
6323 * TLSv1.2:
6324 * hash = PRF( master, finished_label,
6325 * Hash( handshake ) )[0.11]
6326 */
6327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328#if !defined(MBEDTLS_SHA256_ALT)
6329 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006330 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006331#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006334 ? "client finished"
6335 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006336
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006337 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006338
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006339 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006340 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006343
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006344 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006346 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006349}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006353static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006355{
6356 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006357 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006358 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006359 unsigned char padbuf[48];
6360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006362 if( !session )
6363 session = ssl->session;
6364
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006365 mbedtls_sha512_init( &sha512 );
6366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006368
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006369 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006370
6371 /*
6372 * TLSv1.2:
6373 * hash = PRF( master, finished_label,
6374 * Hash( handshake ) )[0.11]
6375 */
6376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006378 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6379 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006380#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006383 ? "client finished"
6384 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006385
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006386 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006387
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006388 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006389 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006392
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006393 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006394
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006395 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399#endif /* MBEDTLS_SHA512_C */
6400#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006403{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006405
6406 /*
6407 * Free our handshake params
6408 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006409 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006411 ssl->handshake = NULL;
6412
6413 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006414 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006415 */
6416 if( ssl->transform )
6417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418 mbedtls_ssl_transform_free( ssl->transform );
6419 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006420 }
6421 ssl->transform = ssl->transform_negotiate;
6422 ssl->transform_negotiate = NULL;
6423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006425}
6426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006428{
6429 int resume = ssl->handshake->resume;
6430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433#if defined(MBEDTLS_SSL_RENEGOTIATION)
6434 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006437 ssl->renego_records_seen = 0;
6438 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006439#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006440
6441 /*
6442 * Free the previous session and switch in the current one
6443 */
Paul Bakker0a597072012-09-25 21:55:46 +00006444 if( ssl->session )
6445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006447 /* RFC 7366 3.1: keep the EtM state */
6448 ssl->session_negotiate->encrypt_then_mac =
6449 ssl->session->encrypt_then_mac;
6450#endif
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 mbedtls_ssl_session_free( ssl->session );
6453 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006454 }
6455 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006456 ssl->session_negotiate = NULL;
6457
Paul Bakker0a597072012-09-25 21:55:46 +00006458 /*
6459 * Add cache entry
6460 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006461 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006462 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006463 resume == 0 )
6464 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006465 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006467 }
Paul Bakker0a597072012-09-25 21:55:46 +00006468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006470 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006471 ssl->handshake->flight != NULL )
6472 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006473 /* Cancel handshake timer */
6474 ssl_set_timer( ssl, 0 );
6475
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006476 /* Keep last flight around in case we need to resend it:
6477 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006479 }
6480 else
6481#endif
6482 ssl_handshake_wrapup_free_hs_transform( ssl );
6483
Paul Bakker48916f92012-09-16 19:57:18 +00006484 ssl->state++;
6485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006487}
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006490{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006491 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006494
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006495 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006496
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006497 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006498
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006499 /*
6500 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6501 * may define some other value. Currently (early 2016), no defined
6502 * ciphersuite does this (and this is unlikely to change as activity has
6503 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006508 ssl->verify_data_len = hash_len;
6509 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006510#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006511
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6514 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006515
6516 /*
6517 * In case of session resuming, invert the client and server
6518 * ChangeCipherSpec messages order.
6519 */
Paul Bakker0a597072012-09-25 21:55:46 +00006520 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006523 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006527 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006530 }
6531 else
6532 ssl->state++;
6533
Paul Bakker48916f92012-09-16 19:57:18 +00006534 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006535 * Switch to our negotiated transform and session parameters for outbound
6536 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006541 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006542 {
6543 unsigned char i;
6544
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006545 /* Remember current epoch settings for resending */
6546 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006547 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006548
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006549 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006550 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006551
6552 /* Increment epoch */
6553 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006554 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006555 break;
6556
6557 /* The loop goes to its end iff the counter is wrapping */
6558 if( i == 0 )
6559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6561 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006562 }
6563 }
6564 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006566 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006567
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006568 ssl->transform_out = ssl->transform_negotiate;
6569 ssl->session_out = ssl->session_negotiate;
6570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6572 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6577 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006578 }
6579 }
6580#endif
6581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006583 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006585#endif
6586
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006587 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006590 return( ret );
6591 }
6592
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006593#if defined(MBEDTLS_SSL_PROTO_DTLS)
6594 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6595 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6596 {
6597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6598 return( ret );
6599 }
6600#endif
6601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603
6604 return( 0 );
6605}
6606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006608#define SSL_MAX_HASH_LEN 36
6609#else
6610#define SSL_MAX_HASH_LEN 12
6611#endif
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006614{
Paul Bakker23986e52011-04-24 08:57:21 +00006615 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006616 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006617 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006620
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006621 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006622
Hanno Becker327c93b2018-08-15 13:56:18 +01006623 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006626 return( ret );
6627 }
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006632 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6633 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 }
6636
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006637 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#if defined(MBEDTLS_SSL_PROTO_SSL3)
6639 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006640 hash_len = 36;
6641 else
6642#endif
6643 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6646 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6650 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006652 }
6653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006655 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006658 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6659 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 }
6662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006664 ssl->verify_data_len = hash_len;
6665 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006666#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006667
Paul Bakker0a597072012-09-25 21:55:46 +00006668 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006671 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006675 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006677#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006678 }
6679 else
6680 ssl->state++;
6681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006683 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006685#endif
6686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006688
6689 return( 0 );
6690}
6691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6697 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6698 mbedtls_md5_init( &handshake->fin_md5 );
6699 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006700 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6701 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6704#if defined(MBEDTLS_SHA256_C)
6705 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006706 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006707#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#if defined(MBEDTLS_SHA512_C)
6709 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006710 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006713
6714 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006715
6716#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6717 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6718 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6719#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#if defined(MBEDTLS_DHM_C)
6722 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#if defined(MBEDTLS_ECDH_C)
6725 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006726#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006727#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006728 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006729#if defined(MBEDTLS_SSL_CLI_C)
6730 handshake->ecjpake_cache = NULL;
6731 handshake->ecjpake_cache_len = 0;
6732#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006733#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006734
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006735#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006736 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006737#endif
6738
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006739#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6740 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6741#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006742}
6743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6749 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 mbedtls_md_init( &transform->md_ctx_enc );
6752 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006753}
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006758}
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006761{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006762 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006763 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006765 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006767 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006768 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006769
6770 /*
6771 * Either the pointers are now NULL or cleared properly and can be freed.
6772 * Now allocate missing structures.
6773 */
6774 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006775 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006776 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006777 }
Paul Bakker48916f92012-09-16 19:57:18 +00006778
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006779 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006780 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006781 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006782 }
Paul Bakker48916f92012-09-16 19:57:18 +00006783
Paul Bakker82788fb2014-10-20 13:59:19 +02006784 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006785 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006786 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006787 }
Paul Bakker48916f92012-09-16 19:57:18 +00006788
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006789 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006790 if( ssl->handshake == NULL ||
6791 ssl->transform_negotiate == NULL ||
6792 ssl->session_negotiate == NULL )
6793 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 mbedtls_free( ssl->handshake );
6797 mbedtls_free( ssl->transform_negotiate );
6798 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006799
6800 ssl->handshake = NULL;
6801 ssl->transform_negotiate = NULL;
6802 ssl->session_negotiate = NULL;
6803
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006804 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006805 }
6806
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006807 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006809 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006810 ssl_handshake_params_init( ssl->handshake );
6811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006813 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6814 {
6815 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006816
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006817 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6818 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6819 else
6820 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006821
6822 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006823 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006824#endif
6825
Paul Bakker48916f92012-09-16 19:57:18 +00006826 return( 0 );
6827}
6828
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006829#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006830/* Dummy cookie callbacks for defaults */
6831static int ssl_cookie_write_dummy( void *ctx,
6832 unsigned char **p, unsigned char *end,
6833 const unsigned char *cli_id, size_t cli_id_len )
6834{
6835 ((void) ctx);
6836 ((void) p);
6837 ((void) end);
6838 ((void) cli_id);
6839 ((void) cli_id_len);
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006842}
6843
6844static int ssl_cookie_check_dummy( void *ctx,
6845 const unsigned char *cookie, size_t cookie_len,
6846 const unsigned char *cli_id, size_t cli_id_len )
6847{
6848 ((void) ctx);
6849 ((void) cookie);
6850 ((void) cookie_len);
6851 ((void) cli_id);
6852 ((void) cli_id_len);
6853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006855}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006856#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006857
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006858/* Once ssl->out_hdr as the address of the beginning of the
6859 * next outgoing record is set, deduce the other pointers.
6860 *
6861 * Note: For TLS, we save the implicit record sequence number
6862 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6863 * and the caller has to make sure there's space for this.
6864 */
6865
6866static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6867 mbedtls_ssl_transform *transform )
6868{
6869#if defined(MBEDTLS_SSL_PROTO_DTLS)
6870 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6871 {
6872 ssl->out_ctr = ssl->out_hdr + 3;
6873 ssl->out_len = ssl->out_hdr + 11;
6874 ssl->out_iv = ssl->out_hdr + 13;
6875 }
6876 else
6877#endif
6878 {
6879 ssl->out_ctr = ssl->out_hdr - 8;
6880 ssl->out_len = ssl->out_hdr + 3;
6881 ssl->out_iv = ssl->out_hdr + 5;
6882 }
6883
6884 /* Adjust out_msg to make space for explicit IV, if used. */
6885 if( transform != NULL &&
6886 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6887 {
6888 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6889 }
6890 else
6891 ssl->out_msg = ssl->out_iv;
6892}
6893
6894/* Once ssl->in_hdr as the address of the beginning of the
6895 * next incoming record is set, deduce the other pointers.
6896 *
6897 * Note: For TLS, we save the implicit record sequence number
6898 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6899 * and the caller has to make sure there's space for this.
6900 */
6901
6902static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6903 mbedtls_ssl_transform *transform )
6904{
6905#if defined(MBEDTLS_SSL_PROTO_DTLS)
6906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6907 {
6908 ssl->in_ctr = ssl->in_hdr + 3;
6909 ssl->in_len = ssl->in_hdr + 11;
6910 ssl->in_iv = ssl->in_hdr + 13;
6911 }
6912 else
6913#endif
6914 {
6915 ssl->in_ctr = ssl->in_hdr - 8;
6916 ssl->in_len = ssl->in_hdr + 3;
6917 ssl->in_iv = ssl->in_hdr + 5;
6918 }
6919
6920 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6921 if( transform != NULL &&
6922 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6923 {
6924 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6925 }
6926 else
6927 ssl->in_msg = ssl->in_iv;
6928}
6929
Paul Bakker5121ce52009-01-03 21:22:43 +00006930/*
6931 * Initialize an SSL context
6932 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006933void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6934{
6935 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6936}
6937
6938/*
6939 * Setup an SSL context
6940 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006941
6942static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6943{
6944 /* Set the incoming and outgoing record pointers. */
6945#if defined(MBEDTLS_SSL_PROTO_DTLS)
6946 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6947 {
6948 ssl->out_hdr = ssl->out_buf;
6949 ssl->in_hdr = ssl->in_buf;
6950 }
6951 else
6952#endif /* MBEDTLS_SSL_PROTO_DTLS */
6953 {
6954 ssl->out_hdr = ssl->out_buf + 8;
6955 ssl->in_hdr = ssl->in_buf + 8;
6956 }
6957
6958 /* Derive other internal pointers. */
6959 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6960 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6961}
6962
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006963int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006964 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006965{
Paul Bakker48916f92012-09-16 19:57:18 +00006966 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006967
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006968 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006969
6970 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006971 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006972 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006973
6974 /* Set to NULL in case of an error condition */
6975 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006976
Angus Grattond8213d02016-05-25 20:56:48 +10006977 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6978 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006979 {
Angus Grattond8213d02016-05-25 20:56:48 +10006980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006981 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006982 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006983 }
6984
6985 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6986 if( ssl->out_buf == NULL )
6987 {
6988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006989 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006990 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 }
6992
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006993 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006994
Paul Bakker48916f92012-09-16 19:57:18 +00006995 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006996 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
6998 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006999
7000error:
7001 mbedtls_free( ssl->in_buf );
7002 mbedtls_free( ssl->out_buf );
7003
7004 ssl->conf = NULL;
7005
7006 ssl->in_buf = NULL;
7007 ssl->out_buf = NULL;
7008
7009 ssl->in_hdr = NULL;
7010 ssl->in_ctr = NULL;
7011 ssl->in_len = NULL;
7012 ssl->in_iv = NULL;
7013 ssl->in_msg = NULL;
7014
7015 ssl->out_hdr = NULL;
7016 ssl->out_ctr = NULL;
7017 ssl->out_len = NULL;
7018 ssl->out_iv = NULL;
7019 ssl->out_msg = NULL;
7020
k-stachowiak9f7798e2018-07-31 16:52:32 +02007021 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007022}
7023
7024/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007025 * Reset an initialized and used SSL context for re-use while retaining
7026 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007027 *
7028 * If partial is non-zero, keep data in the input buffer and client ID.
7029 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007030 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007031static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007032{
Paul Bakker48916f92012-09-16 19:57:18 +00007033 int ret;
7034
Hanno Becker7e772132018-08-10 12:38:21 +01007035#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7036 !defined(MBEDTLS_SSL_SRV_C)
7037 ((void) partial);
7038#endif
7039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007041
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007042 /* Cancel any possibly running timer */
7043 ssl_set_timer( ssl, 0 );
7044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if defined(MBEDTLS_SSL_RENEGOTIATION)
7046 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007047 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007048
7049 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7051 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007054
Paul Bakker7eb013f2011-10-06 12:37:39 +00007055 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007056 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007057
7058 ssl->in_msgtype = 0;
7059 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007061 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007062 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007065 ssl_dtls_replay_reset( ssl );
7066#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007067
7068 ssl->in_hslen = 0;
7069 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007070
7071 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007072
7073 ssl->out_msgtype = 0;
7074 ssl->out_msglen = 0;
7075 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7077 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007078 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007079#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007080
Hanno Becker19859472018-08-06 09:40:20 +01007081 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7082
Paul Bakker48916f92012-09-16 19:57:18 +00007083 ssl->transform_in = NULL;
7084 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007085
Hanno Becker78640902018-08-13 16:35:15 +01007086 ssl->session_in = NULL;
7087 ssl->session_out = NULL;
7088
Angus Grattond8213d02016-05-25 20:56:48 +10007089 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007090
7091#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007092 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007093#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7094 {
7095 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007096 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007097 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7100 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7103 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7106 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007107 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007108 }
7109#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007110
Paul Bakker48916f92012-09-16 19:57:18 +00007111 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 mbedtls_ssl_transform_free( ssl->transform );
7114 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007115 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007116 }
Paul Bakker48916f92012-09-16 19:57:18 +00007117
Paul Bakkerc0463502013-02-14 11:19:38 +01007118 if( ssl->session )
7119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 mbedtls_ssl_session_free( ssl->session );
7121 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007122 ssl->session = NULL;
7123 }
7124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007126 ssl->alpn_chosen = NULL;
7127#endif
7128
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007129#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007130#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007131 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007132#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007133 {
7134 mbedtls_free( ssl->cli_id );
7135 ssl->cli_id = NULL;
7136 ssl->cli_id_len = 0;
7137 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007138#endif
7139
Paul Bakker48916f92012-09-16 19:57:18 +00007140 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7141 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007142
7143 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007144}
7145
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007146/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007147 * Reset an initialized and used SSL context for re-use while retaining
7148 * all application-set variables, function pointers and data.
7149 */
7150int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7151{
7152 return( ssl_session_reset_int( ssl, 0 ) );
7153}
7154
7155/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007156 * SSL set accessors
7157 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007158void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007159{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007160 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007161}
7162
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007163void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007164{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007165 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007166}
7167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007169void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007171 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007172}
7173#endif
7174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007176void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007177{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007178 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007179}
7180#endif
7181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007183
Hanno Becker1841b0a2018-08-24 11:13:57 +01007184void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7185 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007186{
7187 ssl->disable_datagram_packing = !allow_packing;
7188}
7189
7190void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7191 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007192{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007193 conf->hs_timeout_min = min;
7194 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007195}
7196#endif
7197
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007198void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007199{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007200 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007201}
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007204void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007205 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007206 void *p_vrfy )
7207{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007208 conf->f_vrfy = f_vrfy;
7209 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007212
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007213void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007214 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007215 void *p_rng )
7216{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007217 conf->f_rng = f_rng;
7218 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007219}
7220
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007221void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007222 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007223 void *p_dbg )
7224{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007225 conf->f_dbg = f_dbg;
7226 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007227}
7228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007230 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007231 mbedtls_ssl_send_t *f_send,
7232 mbedtls_ssl_recv_t *f_recv,
7233 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007234{
7235 ssl->p_bio = p_bio;
7236 ssl->f_send = f_send;
7237 ssl->f_recv = f_recv;
7238 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007239}
7240
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007241#if defined(MBEDTLS_SSL_PROTO_DTLS)
7242void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7243{
7244 ssl->mtu = mtu;
7245}
7246#endif
7247
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007248void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007249{
7250 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007251}
7252
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007253void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7254 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007255 mbedtls_ssl_set_timer_t *f_set_timer,
7256 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007257{
7258 ssl->p_timer = p_timer;
7259 ssl->f_set_timer = f_set_timer;
7260 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007261
7262 /* Make sure we start with no timer running */
7263 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007264}
7265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007267void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007268 void *p_cache,
7269 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7270 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007271{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007272 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007273 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007274 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007275}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_CLI_C)
7279int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007280{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007281 int ret;
7282
7283 if( ssl == NULL ||
7284 session == NULL ||
7285 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007286 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007289 }
7290
7291 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7292 return( ret );
7293
Paul Bakker0a597072012-09-25 21:55:46 +00007294 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007295
7296 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007299
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007300void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007301 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007302{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007303 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7304 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7305 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7306 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007307}
7308
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007309void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007310 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007311 int major, int minor )
7312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007314 return;
7315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007317 return;
7318
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007319 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007320}
7321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007323void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007324 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007325{
7326 conf->cert_profile = profile;
7327}
7328
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007329/* Append a new keycert entry to a (possibly empty) list */
7330static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7331 mbedtls_x509_crt *cert,
7332 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007333{
niisato8ee24222018-06-25 19:05:48 +09007334 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007335
niisato8ee24222018-06-25 19:05:48 +09007336 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7337 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007338 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007339
niisato8ee24222018-06-25 19:05:48 +09007340 new_cert->cert = cert;
7341 new_cert->key = key;
7342 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007343
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007344 /* Update head is the list was null, else add to the end */
7345 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007346 {
niisato8ee24222018-06-25 19:05:48 +09007347 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007348 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007349 else
7350 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007351 mbedtls_ssl_key_cert *cur = *head;
7352 while( cur->next != NULL )
7353 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007354 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007355 }
7356
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007357 return( 0 );
7358}
7359
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007360int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007361 mbedtls_x509_crt *own_cert,
7362 mbedtls_pk_context *pk_key )
7363{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007364 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007365}
7366
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007367void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007368 mbedtls_x509_crt *ca_chain,
7369 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007370{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007371 conf->ca_chain = ca_chain;
7372 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007375
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007376#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7377int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7378 mbedtls_x509_crt *own_cert,
7379 mbedtls_pk_context *pk_key )
7380{
7381 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7382 own_cert, pk_key ) );
7383}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007384
7385void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7386 mbedtls_x509_crt *ca_chain,
7387 mbedtls_x509_crl *ca_crl )
7388{
7389 ssl->handshake->sni_ca_chain = ca_chain;
7390 ssl->handshake->sni_ca_crl = ca_crl;
7391}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007392
7393void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7394 int authmode )
7395{
7396 ssl->handshake->sni_authmode = authmode;
7397}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007398#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7399
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007400#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007401/*
7402 * Set EC J-PAKE password for current handshake
7403 */
7404int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7405 const unsigned char *pw,
7406 size_t pw_len )
7407{
7408 mbedtls_ecjpake_role role;
7409
Janos Follath8eb64132016-06-03 15:40:57 +01007410 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7412
7413 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7414 role = MBEDTLS_ECJPAKE_SERVER;
7415 else
7416 role = MBEDTLS_ECJPAKE_CLIENT;
7417
7418 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7419 role,
7420 MBEDTLS_MD_SHA256,
7421 MBEDTLS_ECP_DP_SECP256R1,
7422 pw, pw_len ) );
7423}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007424#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007427int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007428 const unsigned char *psk, size_t psk_len,
7429 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007430{
Paul Bakker6db455e2013-09-18 17:29:31 +02007431 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007436
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007437 /* Identity len will be encoded on two bytes */
7438 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007439 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007440 {
7441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7442 }
7443
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007444 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007445 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007446 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007447
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007448 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007449 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007450 conf->psk_len = 0;
7451 }
7452 if( conf->psk_identity != NULL )
7453 {
7454 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007455 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007456 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007457 }
7458
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007459 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7460 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007461 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007462 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007463 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007464 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007465 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007466 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007467 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007468
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007469 conf->psk_len = psk_len;
7470 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007471
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007472 memcpy( conf->psk, psk, conf->psk_len );
7473 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007474
7475 return( 0 );
7476}
7477
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007478int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7479 const unsigned char *psk, size_t psk_len )
7480{
7481 if( psk == NULL || ssl->handshake == NULL )
7482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7483
7484 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7486
7487 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007488 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007489 mbedtls_platform_zeroize( ssl->handshake->psk,
7490 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007491 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007492 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007493 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007494
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007495 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007496 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007497
7498 ssl->handshake->psk_len = psk_len;
7499 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7500
7501 return( 0 );
7502}
7503
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007504void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007506 size_t),
7507 void *p_psk )
7508{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007509 conf->f_psk = f_psk;
7510 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007513
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007514#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007515
7516#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007517int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007518{
7519 int ret;
7520
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007521 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7522 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7523 {
7524 mbedtls_mpi_free( &conf->dhm_P );
7525 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007526 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007527 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007528
7529 return( 0 );
7530}
Hanno Becker470a8c42017-10-04 15:28:46 +01007531#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007532
Hanno Beckera90658f2017-10-04 15:29:08 +01007533int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7534 const unsigned char *dhm_P, size_t P_len,
7535 const unsigned char *dhm_G, size_t G_len )
7536{
7537 int ret;
7538
7539 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7540 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7541 {
7542 mbedtls_mpi_free( &conf->dhm_P );
7543 mbedtls_mpi_free( &conf->dhm_G );
7544 return( ret );
7545 }
7546
7547 return( 0 );
7548}
Paul Bakker5121ce52009-01-03 21:22:43 +00007549
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007550int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007551{
7552 int ret;
7553
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007554 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7555 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7556 {
7557 mbedtls_mpi_free( &conf->dhm_P );
7558 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007559 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007560 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007561
7562 return( 0 );
7563}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007564#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007565
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007566#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7567/*
7568 * Set the minimum length for Diffie-Hellman parameters
7569 */
7570void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7571 unsigned int bitlen )
7572{
7573 conf->dhm_min_bitlen = bitlen;
7574}
7575#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7576
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007577#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007578/*
7579 * Set allowed/preferred hashes for handshake signatures
7580 */
7581void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7582 const int *hashes )
7583{
7584 conf->sig_hashes = hashes;
7585}
Hanno Becker947194e2017-04-07 13:25:49 +01007586#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007587
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007588#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007589/*
7590 * Set the allowed elliptic curves
7591 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007592void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007593 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007594{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007595 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007596}
Hanno Becker947194e2017-04-07 13:25:49 +01007597#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007598
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007599#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007601{
Hanno Becker947194e2017-04-07 13:25:49 +01007602 /* Initialize to suppress unnecessary compiler warning */
7603 size_t hostname_len = 0;
7604
7605 /* Check if new hostname is valid before
7606 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007607 if( hostname != NULL )
7608 {
7609 hostname_len = strlen( hostname );
7610
7611 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7613 }
7614
7615 /* Now it's clear that we will overwrite the old hostname,
7616 * so we can free it safely */
7617
7618 if( ssl->hostname != NULL )
7619 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007620 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007621 mbedtls_free( ssl->hostname );
7622 }
7623
7624 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007625
Paul Bakker5121ce52009-01-03 21:22:43 +00007626 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007627 {
7628 ssl->hostname = NULL;
7629 }
7630 else
7631 {
7632 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007633 if( ssl->hostname == NULL )
7634 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007635
Hanno Becker947194e2017-04-07 13:25:49 +01007636 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007637
Hanno Becker947194e2017-04-07 13:25:49 +01007638 ssl->hostname[hostname_len] = '\0';
7639 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007640
7641 return( 0 );
7642}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007643#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007644
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007645#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007646void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007648 const unsigned char *, size_t),
7649 void *p_sni )
7650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007651 conf->f_sni = f_sni;
7652 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007657int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007658{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007659 size_t cur_len, tot_len;
7660 const char **p;
7661
7662 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007663 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7664 * MUST NOT be truncated."
7665 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007666 */
7667 tot_len = 0;
7668 for( p = protos; *p != NULL; p++ )
7669 {
7670 cur_len = strlen( *p );
7671 tot_len += cur_len;
7672
Ronald Cron157cffe2020-04-23 16:41:44 +02007673 if( ( cur_len == 0 ) ||
7674 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7675 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007677 }
7678
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007679 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007680
7681 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007682}
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007685{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007686 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007689
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007690void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007691{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007692 conf->max_major_ver = major;
7693 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007694}
7695
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007696void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007697{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007698 conf->min_major_ver = major;
7699 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007700}
7701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007703void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007704{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007705 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007706}
7707#endif
7708
Janos Follath088ce432017-04-10 12:42:31 +01007709#if defined(MBEDTLS_SSL_SRV_C)
7710void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7711 char cert_req_ca_list )
7712{
7713 conf->cert_req_ca_list = cert_req_ca_list;
7714}
7715#endif
7716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007718void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007719{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007720 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007721}
7722#endif
7723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007725void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007726{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007727 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007728}
7729#endif
7730
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007731#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007732void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007734 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007735}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007736#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007739int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007742 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007745 }
7746
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007747 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007748
7749 return( 0 );
7750}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007754void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007755{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007756 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007761void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007762{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007763 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007764}
7765#endif
7766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007767void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007768{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007769 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007770}
7771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007773void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007774{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007775 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007776}
7777
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007778void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007779{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007780 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007781}
7782
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007783void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007784 const unsigned char period[8] )
7785{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007786 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007791#if defined(MBEDTLS_SSL_CLI_C)
7792void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007793{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007794 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007795}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007796#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007797
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007798#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007799void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7800 mbedtls_ssl_ticket_write_t *f_ticket_write,
7801 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7802 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007803{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007804 conf->f_ticket_write = f_ticket_write;
7805 conf->f_ticket_parse = f_ticket_parse;
7806 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007807}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007808#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007810
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007811#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7812void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7813 mbedtls_ssl_export_keys_t *f_export_keys,
7814 void *p_export_keys )
7815{
7816 conf->f_export_keys = f_export_keys;
7817 conf->p_export_keys = p_export_keys;
7818}
7819#endif
7820
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007821#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007822void mbedtls_ssl_conf_async_private_cb(
7823 mbedtls_ssl_config *conf,
7824 mbedtls_ssl_async_sign_t *f_async_sign,
7825 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7826 mbedtls_ssl_async_resume_t *f_async_resume,
7827 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007828 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007829{
7830 conf->f_async_sign_start = f_async_sign;
7831 conf->f_async_decrypt_start = f_async_decrypt;
7832 conf->f_async_resume = f_async_resume;
7833 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007834 conf->p_async_config_data = async_config_data;
7835}
7836
Gilles Peskine8f97af72018-04-26 11:46:10 +02007837void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7838{
7839 return( conf->p_async_config_data );
7840}
7841
Gilles Peskine1febfef2018-04-30 11:54:39 +02007842void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007843{
7844 if( ssl->handshake == NULL )
7845 return( NULL );
7846 else
7847 return( ssl->handshake->user_async_ctx );
7848}
7849
Gilles Peskine1febfef2018-04-30 11:54:39 +02007850void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007851 void *ctx )
7852{
7853 if( ssl->handshake != NULL )
7854 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007855}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007856#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007857
Paul Bakker5121ce52009-01-03 21:22:43 +00007858/*
7859 * SSL get accessors
7860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862{
7863 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7864}
7865
Hanno Becker8b170a02017-10-10 11:51:19 +01007866int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7867{
7868 /*
7869 * Case A: We're currently holding back
7870 * a message for further processing.
7871 */
7872
7873 if( ssl->keep_current_message == 1 )
7874 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007876 return( 1 );
7877 }
7878
7879 /*
7880 * Case B: Further records are pending in the current datagram.
7881 */
7882
7883#if defined(MBEDTLS_SSL_PROTO_DTLS)
7884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7885 ssl->in_left > ssl->next_record_offset )
7886 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007888 return( 1 );
7889 }
7890#endif /* MBEDTLS_SSL_PROTO_DTLS */
7891
7892 /*
7893 * Case C: A handshake message is being processed.
7894 */
7895
Hanno Becker8b170a02017-10-10 11:51:19 +01007896 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7897 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007899 return( 1 );
7900 }
7901
7902 /*
7903 * Case D: An application data message is being processed
7904 */
7905 if( ssl->in_offt != NULL )
7906 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007908 return( 1 );
7909 }
7910
7911 /*
7912 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007913 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007914 * we implement support for multiple alerts in single records.
7915 */
7916
7917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7918 return( 0 );
7919}
7920
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007921uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007922{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007923 if( ssl->session != NULL )
7924 return( ssl->session->verify_result );
7925
7926 if( ssl->session_negotiate != NULL )
7927 return( ssl->session_negotiate->verify_result );
7928
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007929 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007930}
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007933{
Paul Bakker926c8e42013-03-06 10:23:34 +01007934 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007935 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007938}
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007944 {
7945 switch( ssl->minor_ver )
7946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007948 return( "DTLSv1.0" );
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007951 return( "DTLSv1.2" );
7952
7953 default:
7954 return( "unknown (DTLS)" );
7955 }
7956 }
7957#endif
7958
Paul Bakker43ca69c2011-01-15 17:35:19 +00007959 switch( ssl->minor_ver )
7960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007962 return( "SSLv3.0" );
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007965 return( "TLSv1.0" );
7966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007968 return( "TLSv1.1" );
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007971 return( "TLSv1.2" );
7972
Paul Bakker43ca69c2011-01-15 17:35:19 +00007973 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007974 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007975 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007976}
7977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007979{
Hanno Becker3136ede2018-08-17 15:28:19 +01007980 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007982 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007983
Hanno Becker78640902018-08-13 16:35:15 +01007984 if( transform == NULL )
7985 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987#if defined(MBEDTLS_ZLIB_SUPPORT)
7988 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7989 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007990#endif
7991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 case MBEDTLS_MODE_GCM:
7995 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007996 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007998 transform_expansion = transform->minlen;
7999 break;
8000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008002
8003 block_size = mbedtls_cipher_get_block_size(
8004 &transform->cipher_ctx_enc );
8005
Hanno Becker3136ede2018-08-17 15:28:19 +01008006 /* Expansion due to the addition of the MAC. */
8007 transform_expansion += transform->maclen;
8008
8009 /* Expansion due to the addition of CBC padding;
8010 * Theoretically up to 256 bytes, but we never use
8011 * more than the block size of the underlying cipher. */
8012 transform_expansion += block_size;
8013
8014 /* For TLS 1.1 or higher, an explicit IV is added
8015 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008016#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8017 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008018 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008019#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008020
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008021 break;
8022
8023 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008026 }
8027
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008028 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008029}
8030
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008031#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8032size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8033{
8034 size_t max_len;
8035
8036 /*
8037 * Assume mfl_code is correct since it was checked when set
8038 */
Angus Grattond8213d02016-05-25 20:56:48 +10008039 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008040
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008041 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008042 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008043 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008044 {
Angus Grattond8213d02016-05-25 20:56:48 +10008045 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008046 }
8047
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008048 /* During a handshake, use the value being negotiated */
8049 if( ssl->session_negotiate != NULL &&
8050 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8051 {
8052 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8053 }
8054
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008055 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008056}
8057#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8058
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008059#if defined(MBEDTLS_SSL_PROTO_DTLS)
8060static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8061{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008062 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8063 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8064 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8065 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8066 return ( 0 );
8067
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008068 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8069 return( ssl->mtu );
8070
8071 if( ssl->mtu == 0 )
8072 return( ssl->handshake->mtu );
8073
8074 return( ssl->mtu < ssl->handshake->mtu ?
8075 ssl->mtu : ssl->handshake->mtu );
8076}
8077#endif /* MBEDTLS_SSL_PROTO_DTLS */
8078
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008079int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8080{
8081 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8082
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008083#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8084 !defined(MBEDTLS_SSL_PROTO_DTLS)
8085 (void) ssl;
8086#endif
8087
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008088#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8089 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8090
8091 if( max_len > mfl )
8092 max_len = mfl;
8093#endif
8094
8095#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008096 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008097 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008098 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008099 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8100 const size_t overhead = (size_t) ret;
8101
8102 if( ret < 0 )
8103 return( ret );
8104
8105 if( mtu <= overhead )
8106 {
8107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8108 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8109 }
8110
8111 if( max_len > mtu - overhead )
8112 max_len = mtu - overhead;
8113 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008114#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008115
Hanno Becker0defedb2018-08-10 12:35:02 +01008116#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8117 !defined(MBEDTLS_SSL_PROTO_DTLS)
8118 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008119#endif
8120
8121 return( (int) max_len );
8122}
8123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124#if defined(MBEDTLS_X509_CRT_PARSE_C)
8125const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008126{
8127 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008128 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008129
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008130 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#if defined(MBEDTLS_SSL_CLI_C)
8135int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008136{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008137 if( ssl == NULL ||
8138 dst == NULL ||
8139 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008140 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008143 }
8144
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008145 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008148
Paul Bakker5121ce52009-01-03 21:22:43 +00008149/*
Paul Bakker1961b702013-01-25 14:49:24 +01008150 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008155
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008156 if( ssl == NULL || ssl->conf == NULL )
8157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008160 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008164 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008166#endif
8167
Paul Bakker1961b702013-01-25 14:49:24 +01008168 return( ret );
8169}
8170
8171/*
8172 * Perform the SSL handshake
8173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008175{
8176 int ret = 0;
8177
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008178 if( ssl == NULL || ssl->conf == NULL )
8179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008186
8187 if( ret != 0 )
8188 break;
8189 }
8190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008192
8193 return( ret );
8194}
8195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#if defined(MBEDTLS_SSL_RENEGOTIATION)
8197#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008198/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008199 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008202{
8203 int ret;
8204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008206
8207 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8209 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008210
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008211 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008212 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008214 return( ret );
8215 }
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 return( 0 );
8220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008222
8223/*
8224 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 * - any side: calling mbedtls_ssl_renegotiate(),
8226 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8227 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008228 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008229 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008233{
8234 int ret;
8235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008237
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008238 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8239 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008240
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008241 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8242 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008246 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008247 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008248 ssl->handshake->out_msg_seq = 1;
8249 else
8250 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008251 }
8252#endif
8253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8255 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008260 return( ret );
8261 }
8262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008264
8265 return( 0 );
8266}
8267
8268/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008269 * Renegotiate current connection on client,
8270 * or request renegotiation on server
8271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008275
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008276 if( ssl == NULL || ssl->conf == NULL )
8277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008280 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008281 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008287
8288 /* Did we already try/start sending HelloRequest? */
8289 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008291
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008292 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008293 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008297 /*
8298 * On client, either start the renegotiation process or,
8299 * if already in progress, continue the handshake
8300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008305
8306 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008309 return( ret );
8310 }
8311 }
8312 else
8313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008317 return( ret );
8318 }
8319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008321
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008322 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008323}
8324
8325/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008326 * Check record counters and renegotiate if they're above the limit.
8327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008329{
Andres AG2196c7f2016-12-15 17:01:16 +00008330 size_t ep_len = ssl_ep_len( ssl );
8331 int in_ctr_cmp;
8332 int out_ctr_cmp;
8333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8335 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008336 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008337 {
8338 return( 0 );
8339 }
8340
Andres AG2196c7f2016-12-15 17:01:16 +00008341 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8342 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008343 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008344 ssl->conf->renego_period + ep_len, 8 - ep_len );
8345
8346 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008347 {
8348 return( 0 );
8349 }
8350
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008355
8356/*
8357 * Receive application data decrypted from the SSL layer
8358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008360{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008361 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008362 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008363
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008364 if( ssl == NULL || ssl->conf == NULL )
8365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008370 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008373 return( ret );
8374
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008375 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008377 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008378 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008379 return( ret );
8380 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008381 }
8382#endif
8383
Hanno Becker4a810fb2017-05-24 16:27:30 +01008384 /*
8385 * Check if renegotiation is necessary and/or handshake is
8386 * in process. If yes, perform/continue, and fall through
8387 * if an unexpected packet is received while the client
8388 * is waiting for the ServerHello.
8389 *
8390 * (There is no equivalent to the last condition on
8391 * the server-side as it is not treated as within
8392 * a handshake while waiting for the ClientHello
8393 * after a renegotiation request.)
8394 */
8395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008397 ret = ssl_check_ctr_renegotiate( ssl );
8398 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8399 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008402 return( ret );
8403 }
8404#endif
8405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008409 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8410 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008413 return( ret );
8414 }
8415 }
8416
Hanno Beckere41158b2017-10-23 13:30:32 +01008417 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008418 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008419 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008420 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008421 if( ssl->f_get_timer != NULL &&
8422 ssl->f_get_timer( ssl->p_timer ) == -1 )
8423 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008424 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008425 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008426
Hanno Becker327c93b2018-08-15 13:56:18 +01008427 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008428 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008429 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8430 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008431
Hanno Becker4a810fb2017-05-24 16:27:30 +01008432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8433 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008434 }
8435
8436 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008438 {
8439 /*
8440 * OpenSSL sends empty messages to randomize the IV
8441 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008442 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008445 return( 0 );
8446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008448 return( ret );
8449 }
8450 }
8451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008455
Hanno Becker4a810fb2017-05-24 16:27:30 +01008456 /*
8457 * - For client-side, expect SERVER_HELLO_REQUEST.
8458 * - For server-side, expect CLIENT_HELLO.
8459 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8460 */
8461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008463 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008465 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008468
8469 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008471 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008472 {
8473 continue;
8474 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008477 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008478#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008479
Hanno Becker4a810fb2017-05-24 16:27:30 +01008480#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008481 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008485
8486 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008489 {
8490 continue;
8491 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008494 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008495#endif /* MBEDTLS_SSL_SRV_C */
8496
Hanno Becker21df7f92017-10-17 11:03:26 +01008497#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008498 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008499 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8500 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8501 ssl->conf->allow_legacy_renegotiation ==
8502 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8503 {
8504 /*
8505 * Accept renegotiation request
8506 */
Paul Bakker48916f92012-09-16 19:57:18 +00008507
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008508 /* DTLS clients need to know renego is server-initiated */
8509#if defined(MBEDTLS_SSL_PROTO_DTLS)
8510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8511 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8512 {
8513 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8514 }
8515#endif
8516 ret = ssl_start_renegotiation( ssl );
8517 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8518 ret != 0 )
8519 {
8520 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8521 return( ret );
8522 }
8523 }
8524 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008525#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008526 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008527 /*
8528 * Refuse renegotiation
8529 */
8530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533#if defined(MBEDTLS_SSL_PROTO_SSL3)
8534 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008535 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008536 /* SSLv3 does not have a "no_renegotiation" warning, so
8537 we send a fatal alert and abort the connection. */
8538 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8539 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8540 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008541 }
8542 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8544#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8545 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8546 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8549 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8550 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008551 {
8552 return( ret );
8553 }
Paul Bakker48916f92012-09-16 19:57:18 +00008554 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008555 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8557 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008561 }
Paul Bakker48916f92012-09-16 19:57:18 +00008562 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008563
Hanno Becker90333da2017-10-10 11:27:13 +01008564 /* At this point, we don't know whether the renegotiation has been
8565 * completed or not. The cases to consider are the following:
8566 * 1) The renegotiation is complete. In this case, no new record
8567 * has been read yet.
8568 * 2) The renegotiation is incomplete because the client received
8569 * an application data record while awaiting the ServerHello.
8570 * 3) The renegotiation is incomplete because the client received
8571 * a non-handshake, non-application data message while awaiting
8572 * the ServerHello.
8573 * In each of these case, looping will be the proper action:
8574 * - For 1), the next iteration will read a new record and check
8575 * if it's application data.
8576 * - For 2), the loop condition isn't satisfied as application data
8577 * is present, hence continue is the same as break
8578 * - For 3), the loop condition is satisfied and read_record
8579 * will re-deliver the message that was held back by the client
8580 * when expecting the ServerHello.
8581 */
8582 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008583 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008584#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008586 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008587 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008588 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008589 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008592 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008594 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008595 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8600 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008603 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008604 }
8605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8609 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008610 }
8611
8612 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008613
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008614 /* We're going to return something now, cancel timer,
8615 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008617 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008618
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008620 /* If we requested renego but received AppData, resend HelloRequest.
8621 * Do it now, after setting in_offt, to avoid taking this branch
8622 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008624 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008626 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008627 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008630 return( ret );
8631 }
8632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008634#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008635 }
8636
8637 n = ( len < ssl->in_msglen )
8638 ? len : ssl->in_msglen;
8639
8640 memcpy( buf, ssl->in_offt, n );
8641 ssl->in_msglen -= n;
8642
8643 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008644 {
8645 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008646 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008647 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008649 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008650 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008651 /* more data available */
8652 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008656
Paul Bakker23986e52011-04-24 08:57:21 +00008657 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008658}
8659
8660/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008661 * Send application data to be encrypted by the SSL layer, taking care of max
8662 * fragment length and buffer size.
8663 *
8664 * According to RFC 5246 Section 6.2.1:
8665 *
8666 * Zero-length fragments of Application data MAY be sent as they are
8667 * potentially useful as a traffic analysis countermeasure.
8668 *
8669 * Therefore, it is possible that the input message length is 0 and the
8670 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008671 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008672static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008673 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008674{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008675 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8676 const size_t max_len = (size_t) ret;
8677
8678 if( ret < 0 )
8679 {
8680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8681 return( ret );
8682 }
8683
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008684 if( len > max_len )
8685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008687 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008690 "maximum fragment length: %d > %d",
8691 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008693 }
8694 else
8695#endif
8696 len = max_len;
8697 }
Paul Bakker887bd502011-06-08 13:10:54 +00008698
Paul Bakker5121ce52009-01-03 21:22:43 +00008699 if( ssl->out_left != 0 )
8700 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008701 /*
8702 * The user has previously tried to send the data and
8703 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8704 * written. In this case, we expect the high-level write function
8705 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008710 return( ret );
8711 }
8712 }
Paul Bakker887bd502011-06-08 13:10:54 +00008713 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008714 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008715 /*
8716 * The user is trying to send a message the first time, so we need to
8717 * copy the data into the internal buffers and setup the data structure
8718 * to keep track of partial writes
8719 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008720 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008722 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008723
Hanno Becker67bc7c32018-08-06 11:33:50 +01008724 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008727 return( ret );
8728 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008729 }
8730
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008731 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008732}
8733
8734/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008735 * Write application data, doing 1/n-1 splitting if necessary.
8736 *
8737 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008738 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008739 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008742static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008743 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008744{
8745 int ret;
8746
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008747 if( ssl->conf->cbc_record_splitting ==
8748 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008749 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8751 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8752 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008753 {
8754 return( ssl_write_real( ssl, buf, len ) );
8755 }
8756
8757 if( ssl->split_done == 0 )
8758 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008759 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008760 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008761 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008762 }
8763
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008764 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8765 return( ret );
8766 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008767
8768 return( ret + 1 );
8769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008771
8772/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008773 * Write application data (public-facing wrapper)
8774 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008775int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008776{
8777 int ret;
8778
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008780
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008781 if( ssl == NULL || ssl->conf == NULL )
8782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8783
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008784#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008785 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8786 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008787 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008788 return( ret );
8789 }
8790#endif
8791
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008792 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008793 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008794 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008795 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008797 return( ret );
8798 }
8799 }
8800
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008801#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008802 ret = ssl_write_split( ssl, buf, len );
8803#else
8804 ret = ssl_write_real( ssl, buf, len );
8805#endif
8806
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008808
8809 return( ret );
8810}
8811
8812/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008813 * Notify the peer that the connection is being closed
8814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008816{
8817 int ret;
8818
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008819 if( ssl == NULL || ssl->conf == NULL )
8820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008823
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008824 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8830 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8831 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008834 return( ret );
8835 }
8836 }
8837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008839
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008840 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008841}
8842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008844{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008845 if( transform == NULL )
8846 return;
8847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008849 deflateEnd( &transform->ctx_deflate );
8850 inflateEnd( &transform->ctx_inflate );
8851#endif
8852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8854 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856 mbedtls_md_free( &transform->md_ctx_enc );
8857 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008858
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008859 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008860}
8861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862#if defined(MBEDTLS_X509_CRT_PARSE_C)
8863static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008866
8867 while( cur != NULL )
8868 {
8869 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008871 cur = next;
8872 }
8873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008875
Hanno Becker0271f962018-08-16 13:23:47 +01008876#if defined(MBEDTLS_SSL_PROTO_DTLS)
8877
8878static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8879{
8880 unsigned offset;
8881 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8882
8883 if( hs == NULL )
8884 return;
8885
Hanno Becker283f5ef2018-08-24 09:34:47 +01008886 ssl_free_buffered_record( ssl );
8887
Hanno Becker0271f962018-08-16 13:23:47 +01008888 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008889 ssl_buffering_free_slot( ssl, offset );
8890}
8891
8892static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8893 uint8_t slot )
8894{
8895 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8896 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008897
8898 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8899 return;
8900
Hanno Beckere605b192018-08-21 15:59:07 +01008901 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008902 {
Hanno Beckere605b192018-08-21 15:59:07 +01008903 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008904 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008905 mbedtls_free( hs_buf->data );
8906 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008907 }
8908}
8909
8910#endif /* MBEDTLS_SSL_PROTO_DTLS */
8911
Gilles Peskine9b562d52018-04-25 20:32:43 +02008912void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008913{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008914 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8915
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008916 if( handshake == NULL )
8917 return;
8918
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008919#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8920 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8921 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008922 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008923 handshake->async_in_progress = 0;
8924 }
8925#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8926
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008927#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8928 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8929 mbedtls_md5_free( &handshake->fin_md5 );
8930 mbedtls_sha1_free( &handshake->fin_sha1 );
8931#endif
8932#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8933#if defined(MBEDTLS_SHA256_C)
8934 mbedtls_sha256_free( &handshake->fin_sha256 );
8935#endif
8936#if defined(MBEDTLS_SHA512_C)
8937 mbedtls_sha512_free( &handshake->fin_sha512 );
8938#endif
8939#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941#if defined(MBEDTLS_DHM_C)
8942 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944#if defined(MBEDTLS_ECDH_C)
8945 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008946#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008947#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008948 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008949#if defined(MBEDTLS_SSL_CLI_C)
8950 mbedtls_free( handshake->ecjpake_cache );
8951 handshake->ecjpake_cache = NULL;
8952 handshake->ecjpake_cache_len = 0;
8953#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008954#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008955
Janos Follath4ae5c292016-02-10 11:27:43 +00008956#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8957 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008958 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008960#endif
8961
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008962#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8963 if( handshake->psk != NULL )
8964 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008965 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008966 mbedtls_free( handshake->psk );
8967 }
8968#endif
8969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8971 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008972 /*
8973 * Free only the linked list wrapper, not the keys themselves
8974 * since the belong to the SNI callback
8975 */
8976 if( handshake->sni_key_cert != NULL )
8977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008979
8980 while( cur != NULL )
8981 {
8982 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008983 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008984 cur = next;
8985 }
8986 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008988
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008989#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008990 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008991#endif
8992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#if defined(MBEDTLS_SSL_PROTO_DTLS)
8994 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008995 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008996 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008997#endif
8998
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008999 mbedtls_platform_zeroize( handshake,
9000 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009001}
9002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009004{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009005 if( session == NULL )
9006 return;
9007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009009 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 mbedtls_x509_crt_free( session->peer_cert );
9012 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009013 }
Paul Bakkered27a042013-04-18 22:46:23 +02009014#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009015
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009016#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009018#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009019
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009020 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009021}
9022
Paul Bakker5121ce52009-01-03 21:22:43 +00009023/*
9024 * Free an SSL context
9025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009027{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009028 if( ssl == NULL )
9029 return;
9030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009032
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009033 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009034 {
Angus Grattond8213d02016-05-25 20:56:48 +10009035 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009037 }
9038
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009039 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009040 {
Angus Grattond8213d02016-05-25 20:56:48 +10009041 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009043 }
9044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009046 if( ssl->compress_buf != NULL )
9047 {
Angus Grattond8213d02016-05-25 20:56:48 +10009048 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009050 }
9051#endif
9052
Paul Bakker48916f92012-09-16 19:57:18 +00009053 if( ssl->transform )
9054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055 mbedtls_ssl_transform_free( ssl->transform );
9056 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009057 }
9058
9059 if( ssl->handshake )
9060 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009061 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9063 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065 mbedtls_free( ssl->handshake );
9066 mbedtls_free( ssl->transform_negotiate );
9067 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009068 }
9069
Paul Bakkerc0463502013-02-14 11:19:38 +01009070 if( ssl->session )
9071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 mbedtls_ssl_session_free( ssl->session );
9073 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009074 }
9075
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009076#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009077 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009078 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009079 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009081 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9085 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9088 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009089 }
9090#endif
9091
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009092#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009094#endif
9095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009097
Paul Bakker86f04f42013-02-14 11:20:09 +01009098 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009099 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009100}
9101
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009102/*
9103 * Initialze mbedtls_ssl_config
9104 */
9105void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9106{
9107 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9108}
9109
Simon Butcherc97b6972015-12-27 23:48:17 +00009110#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009111static int ssl_preset_default_hashes[] = {
9112#if defined(MBEDTLS_SHA512_C)
9113 MBEDTLS_MD_SHA512,
9114 MBEDTLS_MD_SHA384,
9115#endif
9116#if defined(MBEDTLS_SHA256_C)
9117 MBEDTLS_MD_SHA256,
9118 MBEDTLS_MD_SHA224,
9119#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009120#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009121 MBEDTLS_MD_SHA1,
9122#endif
9123 MBEDTLS_MD_NONE
9124};
Simon Butcherc97b6972015-12-27 23:48:17 +00009125#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009126
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009127static int ssl_preset_suiteb_ciphersuites[] = {
9128 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9129 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9130 0
9131};
9132
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009133#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009134static int ssl_preset_suiteb_hashes[] = {
9135 MBEDTLS_MD_SHA256,
9136 MBEDTLS_MD_SHA384,
9137 MBEDTLS_MD_NONE
9138};
9139#endif
9140
9141#if defined(MBEDTLS_ECP_C)
9142static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009143#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009144 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009145#endif
9146#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009147 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009148#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009149 MBEDTLS_ECP_DP_NONE
9150};
9151#endif
9152
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009153/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009154 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009155 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009156int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009157 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009158{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009159#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009160 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009161#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009162
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009163 /* Use the functions here so that they are covered in tests,
9164 * but otherwise access member directly for efficiency */
9165 mbedtls_ssl_conf_endpoint( conf, endpoint );
9166 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009167
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009168 /*
9169 * Things that are common to all presets
9170 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009171#if defined(MBEDTLS_SSL_CLI_C)
9172 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9173 {
9174 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9175#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9176 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9177#endif
9178 }
9179#endif
9180
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009181#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009182 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009183#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009184
9185#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9186 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9187#endif
9188
9189#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9190 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9191#endif
9192
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009193#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9194 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9195#endif
9196
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009197#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009198 conf->f_cookie_write = ssl_cookie_write_dummy;
9199 conf->f_cookie_check = ssl_cookie_check_dummy;
9200#endif
9201
9202#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9203 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9204#endif
9205
Janos Follath088ce432017-04-10 12:42:31 +01009206#if defined(MBEDTLS_SSL_SRV_C)
9207 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9208#endif
9209
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009210#if defined(MBEDTLS_SSL_PROTO_DTLS)
9211 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9212 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9213#endif
9214
9215#if defined(MBEDTLS_SSL_RENEGOTIATION)
9216 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009217 memset( conf->renego_period, 0x00, 2 );
9218 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009219#endif
9220
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009221#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9222 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9223 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009224 const unsigned char dhm_p[] =
9225 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9226 const unsigned char dhm_g[] =
9227 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9228
Hanno Beckera90658f2017-10-04 15:29:08 +01009229 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9230 dhm_p, sizeof( dhm_p ),
9231 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009232 {
9233 return( ret );
9234 }
9235 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009236#endif
9237
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009238 /*
9239 * Preset-specific defaults
9240 */
9241 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009242 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009243 /*
9244 * NSA Suite B
9245 */
9246 case MBEDTLS_SSL_PRESET_SUITEB:
9247 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9248 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9249 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9250 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9251
9252 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9253 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9254 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9255 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9256 ssl_preset_suiteb_ciphersuites;
9257
9258#if defined(MBEDTLS_X509_CRT_PARSE_C)
9259 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009260#endif
9261
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009262#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009263 conf->sig_hashes = ssl_preset_suiteb_hashes;
9264#endif
9265
9266#if defined(MBEDTLS_ECP_C)
9267 conf->curve_list = ssl_preset_suiteb_curves;
9268#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009269 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009270
9271 /*
9272 * Default
9273 */
9274 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009275 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9276 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9277 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9278 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9279 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9280 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9281 MBEDTLS_SSL_MIN_MINOR_VERSION :
9282 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009283 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9284 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9285
9286#if defined(MBEDTLS_SSL_PROTO_DTLS)
9287 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9288 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9289#endif
9290
9291 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9292 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9293 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9294 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9295 mbedtls_ssl_list_ciphersuites();
9296
9297#if defined(MBEDTLS_X509_CRT_PARSE_C)
9298 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9299#endif
9300
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009301#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009302 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009303#endif
9304
9305#if defined(MBEDTLS_ECP_C)
9306 conf->curve_list = mbedtls_ecp_grp_id_list();
9307#endif
9308
9309#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9310 conf->dhm_min_bitlen = 1024;
9311#endif
9312 }
9313
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009314 return( 0 );
9315}
9316
9317/*
9318 * Free mbedtls_ssl_config
9319 */
9320void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9321{
9322#if defined(MBEDTLS_DHM_C)
9323 mbedtls_mpi_free( &conf->dhm_P );
9324 mbedtls_mpi_free( &conf->dhm_G );
9325#endif
9326
9327#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9328 if( conf->psk != NULL )
9329 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009330 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009331 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009332 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009333 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009334 }
9335
9336 if( conf->psk_identity != NULL )
9337 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009338 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009339 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009340 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009341 conf->psk_identity_len = 0;
9342 }
9343#endif
9344
9345#if defined(MBEDTLS_X509_CRT_PARSE_C)
9346 ssl_key_cert_free( conf->key_cert );
9347#endif
9348
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009349 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009350}
9351
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009352#if defined(MBEDTLS_PK_C) && \
9353 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009354/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359#if defined(MBEDTLS_RSA_C)
9360 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9361 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363#if defined(MBEDTLS_ECDSA_C)
9364 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9365 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009368}
9369
Hanno Becker7e5437a2017-04-28 17:15:26 +01009370unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9371{
9372 switch( type ) {
9373 case MBEDTLS_PK_RSA:
9374 return( MBEDTLS_SSL_SIG_RSA );
9375 case MBEDTLS_PK_ECDSA:
9376 case MBEDTLS_PK_ECKEY:
9377 return( MBEDTLS_SSL_SIG_ECDSA );
9378 default:
9379 return( MBEDTLS_SSL_SIG_ANON );
9380 }
9381}
9382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009384{
9385 switch( sig )
9386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387#if defined(MBEDTLS_RSA_C)
9388 case MBEDTLS_SSL_SIG_RSA:
9389 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391#if defined(MBEDTLS_ECDSA_C)
9392 case MBEDTLS_SSL_SIG_ECDSA:
9393 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009394#endif
9395 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009397 }
9398}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009399#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009400
Hanno Becker7e5437a2017-04-28 17:15:26 +01009401#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9402 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9403
9404/* Find an entry in a signature-hash set matching a given hash algorithm. */
9405mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9406 mbedtls_pk_type_t sig_alg )
9407{
9408 switch( sig_alg )
9409 {
9410 case MBEDTLS_PK_RSA:
9411 return( set->rsa );
9412 case MBEDTLS_PK_ECDSA:
9413 return( set->ecdsa );
9414 default:
9415 return( MBEDTLS_MD_NONE );
9416 }
9417}
9418
9419/* Add a signature-hash-pair to a signature-hash set */
9420void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9421 mbedtls_pk_type_t sig_alg,
9422 mbedtls_md_type_t md_alg )
9423{
9424 switch( sig_alg )
9425 {
9426 case MBEDTLS_PK_RSA:
9427 if( set->rsa == MBEDTLS_MD_NONE )
9428 set->rsa = md_alg;
9429 break;
9430
9431 case MBEDTLS_PK_ECDSA:
9432 if( set->ecdsa == MBEDTLS_MD_NONE )
9433 set->ecdsa = md_alg;
9434 break;
9435
9436 default:
9437 break;
9438 }
9439}
9440
9441/* Allow exactly one hash algorithm for each signature. */
9442void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9443 mbedtls_md_type_t md_alg )
9444{
9445 set->rsa = md_alg;
9446 set->ecdsa = md_alg;
9447}
9448
9449#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9450 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9451
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009452/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009453 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009456{
9457 switch( hash )
9458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459#if defined(MBEDTLS_MD5_C)
9460 case MBEDTLS_SSL_HASH_MD5:
9461 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009462#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463#if defined(MBEDTLS_SHA1_C)
9464 case MBEDTLS_SSL_HASH_SHA1:
9465 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467#if defined(MBEDTLS_SHA256_C)
9468 case MBEDTLS_SSL_HASH_SHA224:
9469 return( MBEDTLS_MD_SHA224 );
9470 case MBEDTLS_SSL_HASH_SHA256:
9471 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009472#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473#if defined(MBEDTLS_SHA512_C)
9474 case MBEDTLS_SSL_HASH_SHA384:
9475 return( MBEDTLS_MD_SHA384 );
9476 case MBEDTLS_SSL_HASH_SHA512:
9477 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009478#endif
9479 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009481 }
9482}
9483
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009484/*
9485 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9486 */
9487unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9488{
9489 switch( md )
9490 {
9491#if defined(MBEDTLS_MD5_C)
9492 case MBEDTLS_MD_MD5:
9493 return( MBEDTLS_SSL_HASH_MD5 );
9494#endif
9495#if defined(MBEDTLS_SHA1_C)
9496 case MBEDTLS_MD_SHA1:
9497 return( MBEDTLS_SSL_HASH_SHA1 );
9498#endif
9499#if defined(MBEDTLS_SHA256_C)
9500 case MBEDTLS_MD_SHA224:
9501 return( MBEDTLS_SSL_HASH_SHA224 );
9502 case MBEDTLS_MD_SHA256:
9503 return( MBEDTLS_SSL_HASH_SHA256 );
9504#endif
9505#if defined(MBEDTLS_SHA512_C)
9506 case MBEDTLS_MD_SHA384:
9507 return( MBEDTLS_SSL_HASH_SHA384 );
9508 case MBEDTLS_MD_SHA512:
9509 return( MBEDTLS_SSL_HASH_SHA512 );
9510#endif
9511 default:
9512 return( MBEDTLS_SSL_HASH_NONE );
9513 }
9514}
9515
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009516#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009517/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009518 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009519 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009520 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009521int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009524
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009525 if( ssl->conf->curve_list == NULL )
9526 return( -1 );
9527
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009528 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009529 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009530 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009531
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009532 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009533}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009534#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009535
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009536#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009537/*
9538 * Check if a hash proposed by the peer is in our list.
9539 * Return 0 if we're willing to use it, -1 otherwise.
9540 */
9541int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9542 mbedtls_md_type_t md )
9543{
9544 const int *cur;
9545
9546 if( ssl->conf->sig_hashes == NULL )
9547 return( -1 );
9548
9549 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9550 if( *cur == (int) md )
9551 return( 0 );
9552
9553 return( -1 );
9554}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009555#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557#if defined(MBEDTLS_X509_CRT_PARSE_C)
9558int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9559 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009560 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009561 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009562{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009563 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009565 int usage = 0;
9566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009568 const char *ext_oid;
9569 size_t ext_len;
9570#endif
9571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9573 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009574 ((void) cert);
9575 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009576 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009577#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9580 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009581 {
9582 /* Server part of the key exchange */
9583 switch( ciphersuite->key_exchange )
9584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 case MBEDTLS_KEY_EXCHANGE_RSA:
9586 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009587 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009588 break;
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9591 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9592 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9593 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009594 break;
9595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9597 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009598 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009599 break;
9600
9601 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 case MBEDTLS_KEY_EXCHANGE_NONE:
9603 case MBEDTLS_KEY_EXCHANGE_PSK:
9604 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9605 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009606 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009607 usage = 0;
9608 }
9609 }
9610 else
9611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9613 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009614 }
9615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009617 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009618 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009619 ret = -1;
9620 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009621#else
9622 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9626 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9629 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009630 }
9631 else
9632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9634 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009635 }
9636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009638 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009639 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009640 ret = -1;
9641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009643
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009644 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009647
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009648/*
9649 * Convert version numbers to/from wire format
9650 * and, for DTLS, to/from TLS equivalent.
9651 *
9652 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009653 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009654 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9655 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009658 unsigned char ver[2] )
9659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660#if defined(MBEDTLS_SSL_PROTO_DTLS)
9661 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009664 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9665
9666 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9667 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9668 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009669 else
9670#else
9671 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009672#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009673 {
9674 ver[0] = (unsigned char) major;
9675 ver[1] = (unsigned char) minor;
9676 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009677}
9678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009680 const unsigned char ver[2] )
9681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682#if defined(MBEDTLS_SSL_PROTO_DTLS)
9683 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009684 {
9685 *major = 255 - ver[0] + 2;
9686 *minor = 255 - ver[1] + 1;
9687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009689 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9690 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009691 else
9692#else
9693 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009694#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009695 {
9696 *major = ver[0];
9697 *minor = ver[1];
9698 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009699}
9700
Simon Butcher99000142016-10-13 17:21:01 +01009701int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9702{
9703#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9704 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9705 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9706
9707 switch( md )
9708 {
9709#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9710#if defined(MBEDTLS_MD5_C)
9711 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009712 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009713#endif
9714#if defined(MBEDTLS_SHA1_C)
9715 case MBEDTLS_SSL_HASH_SHA1:
9716 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9717 break;
9718#endif
9719#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9720#if defined(MBEDTLS_SHA512_C)
9721 case MBEDTLS_SSL_HASH_SHA384:
9722 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9723 break;
9724#endif
9725#if defined(MBEDTLS_SHA256_C)
9726 case MBEDTLS_SSL_HASH_SHA256:
9727 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9728 break;
9729#endif
9730 default:
9731 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9732 }
9733
9734 return 0;
9735#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9736 (void) ssl;
9737 (void) md;
9738
9739 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9740#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9741}
9742
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009743#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9744 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9745int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9746 unsigned char *output,
9747 unsigned char *data, size_t data_len )
9748{
9749 int ret = 0;
9750 mbedtls_md5_context mbedtls_md5;
9751 mbedtls_sha1_context mbedtls_sha1;
9752
9753 mbedtls_md5_init( &mbedtls_md5 );
9754 mbedtls_sha1_init( &mbedtls_sha1 );
9755
9756 /*
9757 * digitally-signed struct {
9758 * opaque md5_hash[16];
9759 * opaque sha_hash[20];
9760 * };
9761 *
9762 * md5_hash
9763 * MD5(ClientHello.random + ServerHello.random
9764 * + ServerParams);
9765 * sha_hash
9766 * SHA(ClientHello.random + ServerHello.random
9767 * + ServerParams);
9768 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009769 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009770 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009772 goto exit;
9773 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009774 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009775 ssl->handshake->randbytes, 64 ) ) != 0 )
9776 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009778 goto exit;
9779 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009780 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009781 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009783 goto exit;
9784 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009785 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009786 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009788 goto exit;
9789 }
9790
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009791 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009792 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009794 goto exit;
9795 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009796 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009797 ssl->handshake->randbytes, 64 ) ) != 0 )
9798 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009800 goto exit;
9801 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009802 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009803 data_len ) ) != 0 )
9804 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_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_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009809 output + 16 ) ) != 0 )
9810 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009812 goto exit;
9813 }
9814
9815exit:
9816 mbedtls_md5_free( &mbedtls_md5 );
9817 mbedtls_sha1_free( &mbedtls_sha1 );
9818
9819 if( ret != 0 )
9820 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9821 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9822
9823 return( ret );
9824
9825}
9826#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9827 MBEDTLS_SSL_PROTO_TLS1_1 */
9828
9829#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9830 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9831int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009832 unsigned char *hash, size_t *hashlen,
9833 unsigned char *data, size_t data_len,
9834 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009835{
9836 int ret = 0;
9837 mbedtls_md_context_t ctx;
9838 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009839 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009840
9841 mbedtls_md_init( &ctx );
9842
9843 /*
9844 * digitally-signed struct {
9845 * opaque client_random[32];
9846 * opaque server_random[32];
9847 * ServerDHParams params;
9848 * };
9849 */
9850 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9851 {
9852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9853 goto exit;
9854 }
9855 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9856 {
9857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9858 goto exit;
9859 }
9860 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9861 {
9862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9863 goto exit;
9864 }
9865 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9866 {
9867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9868 goto exit;
9869 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009870 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009871 {
9872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9873 goto exit;
9874 }
9875
9876exit:
9877 mbedtls_md_free( &ctx );
9878
9879 if( ret != 0 )
9880 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9881 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9882
9883 return( ret );
9884}
9885#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9886 MBEDTLS_SSL_PROTO_TLS1_2 */
9887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888#endif /* MBEDTLS_SSL_TLS_C */