blob: b82dec9b85dab4670fecf67ef38511962fa4e3e6 [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) || \
1436 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001437 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001438#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001439#endif
1440
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001441/* The function below is only used in the Lucky 13 counter-measure in
1442 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1443#if defined(SSL_SOME_MODES_USE_MAC) && \
1444 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1445 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1446 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1447/* This function makes sure every byte in the memory region is accessed
1448 * (in ascending addresses order) */
1449static void ssl_read_memory( unsigned char *p, size_t len )
1450{
1451 unsigned char acc = 0;
1452 volatile unsigned char force;
1453
1454 for( ; len != 0; p++, len-- )
1455 acc ^= *p;
1456
1457 force = acc;
1458 (void) force;
1459}
1460#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1461
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001462/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001466{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001468 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001472 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001476 }
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001481 ssl->out_msg, ssl->out_msglen );
1482
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001484 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001486#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 if( mode == MBEDTLS_MODE_STREAM ||
1488 ( mode == MBEDTLS_MODE_CBC
1489#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1490 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001491#endif
1492 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_SSL_PROTO_SSL3)
1495 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001496 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001497 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001498
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001499 ssl_mac( &ssl->transform_out->md_ctx_enc,
1500 ssl->transform_out->mac_enc,
1501 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001502 ssl->out_ctr, ssl->out_msgtype,
1503 mac );
1504
1505 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001506 }
1507 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1510 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1511 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001512 {
Hanno Becker992b6872017-11-09 18:57:39 +00001513 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1516 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1517 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1518 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001519 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001520 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001522
1523 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001524 }
1525 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001526#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001530 }
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001533 ssl->out_msg + ssl->out_msglen,
1534 ssl->transform_out->maclen );
1535
1536 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001537 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001538 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001539#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001541 /*
1542 * Encrypt
1543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1545 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001547 int ret;
1548 size_t olen = 0;
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 "including %d bytes of padding",
1552 ssl->out_msglen, 0 ) );
1553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001555 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001556 ssl->transform_out->ivlen,
1557 ssl->out_msg, ssl->out_msglen,
1558 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001561 return( ret );
1562 }
1563
1564 if( ssl->out_msglen != olen )
1565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
Paul Bakker68884e32013-01-07 18:20:04 +01001570 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001572#if defined(MBEDTLS_GCM_C) || \
1573 defined(MBEDTLS_CCM_C) || \
1574 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001576 mode == MBEDTLS_MODE_CCM ||
1577 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001578 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001579 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001580 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001581 unsigned char *enc_msg;
1582 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001583 unsigned char iv[12];
1584 mbedtls_ssl_transform *transform = ssl->transform_out;
1585 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001587 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001588
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001589 /*
1590 * Prepare additional authenticated data
1591 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001592 memcpy( add_data, ssl->out_ctr, 8 );
1593 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001595 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001596 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1597 add_data[12] = ssl->out_msglen & 0xFF;
1598
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001599 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001600
Paul Bakker68884e32013-01-07 18:20:04 +01001601 /*
1602 * Generate IV
1603 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001604 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1605 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001606 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001607 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1608 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1609 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1610
1611 }
1612 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1613 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001614 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001615 unsigned char i;
1616
1617 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1618
1619 for( i = 0; i < 8; i++ )
1620 iv[i+4] ^= ssl->out_ctr[i];
1621 }
1622 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001623 {
1624 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001627 }
1628
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001629 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1630 iv, transform->ivlen );
1631 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1632 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001633
Paul Bakker68884e32013-01-07 18:20:04 +01001634 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001635 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001636 */
1637 enc_msg = ssl->out_msg;
1638 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001639 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001642 "including 0 bytes of padding",
1643 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001644
Paul Bakker68884e32013-01-07 18:20:04 +01001645 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001646 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001647 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001648 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1649 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001650 add_data, 13,
1651 enc_msg, enc_msglen,
1652 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001653 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001656 return( ret );
1657 }
1658
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001659 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001663 }
1664
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001665 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001666 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1672#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001673 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001676 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001677 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001678 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001679
Paul Bakker48916f92012-09-16 19:57:18 +00001680 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1681 ssl->transform_out->ivlen;
1682 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 padlen = 0;
1684
1685 for( i = 0; i <= padlen; i++ )
1686 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1687
1688 ssl->out_msglen += padlen + 1;
1689
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001690 enc_msglen = ssl->out_msglen;
1691 enc_msg = ssl->out_msg;
1692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001694 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001695 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1696 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001699 {
1700 /*
1701 * Generate IV
1702 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001703 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001704 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001705 if( ret != 0 )
1706 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001707
Paul Bakker92be97b2013-01-02 17:30:03 +01001708 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001709 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001710
1711 /*
1712 * Fix pointer positions and message length with added IV
1713 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001714 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001716 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001721 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001722 ssl->out_msglen, ssl->transform_out->ivlen,
1723 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001726 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001727 ssl->transform_out->ivlen,
1728 enc_msg, enc_msglen,
1729 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001732 return( ret );
1733 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001734
Paul Bakkercca5b812013-08-31 17:40:26 +02001735 if( enc_msglen != olen )
1736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1738 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001739 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1742 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001743 {
1744 /*
1745 * Save IV in SSL3 and TLS1
1746 */
1747 memcpy( ssl->transform_out->iv_enc,
1748 ssl->transform_out->cipher_ctx_enc.iv,
1749 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001751#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001754 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001755 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001756 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1757
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001758 /*
1759 * MAC(MAC_write_key, seq_num +
1760 * TLSCipherText.type +
1761 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001762 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001763 * IV + // except for TLS 1.0
1764 * ENC(content + padding + padding_length));
1765 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001766 unsigned char pseudo_hdr[13];
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001769
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001770 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1771 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001772 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1773 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1778 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001779 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001780 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782
Hanno Becker3d8c9072018-01-05 16:24:22 +00001783 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1784 ssl->transform_out->maclen );
1785
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001786 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001787 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001791 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001793 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001797 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001799 /* Make extra sure authentication was performed, exactly once */
1800 if( auth_done != 1 )
1801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001804 }
1805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001807
1808 return( 0 );
1809}
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001814 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001815#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001816 size_t padlen = 0, correct = 1;
1817#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001821 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001825 }
1826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001828
Paul Bakker48916f92012-09-16 19:57:18 +00001829 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001832 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 }
1835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1837 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001838 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001839 int ret;
1840 size_t olen = 0;
1841
Paul Bakker68884e32013-01-07 18:20:04 +01001842 padlen = 0;
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001845 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001846 ssl->transform_in->ivlen,
1847 ssl->in_msg, ssl->in_msglen,
1848 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001851 return( ret );
1852 }
1853
1854 if( ssl->in_msglen != olen )
1855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001858 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 }
Paul Bakker68884e32013-01-07 18:20:04 +01001860 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862#if defined(MBEDTLS_GCM_C) || \
1863 defined(MBEDTLS_CCM_C) || \
1864 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001866 mode == MBEDTLS_MODE_CCM ||
1867 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001868 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001869 int ret;
1870 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001871 unsigned char *dec_msg;
1872 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001873 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001874 unsigned char iv[12];
1875 mbedtls_ssl_transform *transform = ssl->transform_in;
1876 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001878 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001879
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001880 /*
1881 * Compute and update sizes
1882 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001883 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001886 "+ taglen (%d)", ssl->in_msglen,
1887 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001889 }
1890 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1891
Paul Bakker68884e32013-01-07 18:20:04 +01001892 dec_msg = ssl->in_msg;
1893 dec_msg_result = ssl->in_msg;
1894 ssl->in_msglen = dec_msglen;
1895
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001896 /*
1897 * Prepare additional authenticated data
1898 */
Paul Bakker68884e32013-01-07 18:20:04 +01001899 memcpy( add_data, ssl->in_ctr, 8 );
1900 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001902 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001903 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1904 add_data[12] = ssl->in_msglen & 0xFF;
1905
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001906 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001907
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001908 /*
1909 * Prepare IV
1910 */
1911 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1912 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001913 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001914 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1915 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001916
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001917 }
1918 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1919 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001920 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001921 unsigned char i;
1922
1923 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1924
1925 for( i = 0; i < 8; i++ )
1926 iv[i+4] ^= ssl->in_ctr[i];
1927 }
1928 else
1929 {
1930 /* Reminder if we ever add an AEAD mode with a different size */
1931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1933 }
1934
1935 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001937
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001938 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001939 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001942 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001943 add_data, 13,
1944 dec_msg, dec_msglen,
1945 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001946 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1951 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001952
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001953 return( ret );
1954 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001955 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001956
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001957 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001961 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1965#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001966 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 {
Paul Bakker45829992013-01-03 14:52:21 +01001969 /*
1970 * Decrypt and check the padding
1971 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001972 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001973 unsigned char *dec_msg;
1974 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001975 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001976 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001977 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001978
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 /*
Paul Bakker45829992013-01-03 14:52:21 +01001980 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1983 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001984 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001985#endif
Paul Bakker45829992013-01-03 14:52:21 +01001986
1987 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1988 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001991 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1992 ssl->transform_in->ivlen,
1993 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001995 }
1996
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001997 dec_msglen = ssl->in_msglen;
1998 dec_msg = ssl->in_msg;
1999 dec_msg_result = ssl->in_msg;
2000
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002001 /*
2002 * Authenticate before decrypt if enabled
2003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2005 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002006 {
Hanno Becker992b6872017-11-09 18:57:39 +00002007 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002008 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002011
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002012 dec_msglen -= ssl->transform_in->maclen;
2013 ssl->in_msglen -= ssl->transform_in->maclen;
2014
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002015 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2016 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2017 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2018 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2023 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002024 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002025 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002029 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002030 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002031 ssl->transform_in->maclen );
2032
Hanno Becker992b6872017-11-09 18:57:39 +00002033 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2034 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002039 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002040 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002041 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002043
2044 /*
2045 * Check length sanity
2046 */
2047 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002050 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002052 }
2053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002055 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002056 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002059 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002060 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002061 dec_msglen -= ssl->transform_in->ivlen;
2062 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002063
Paul Bakker48916f92012-09-16 19:57:18 +00002064 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002065 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002066 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002070 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002071 ssl->transform_in->ivlen,
2072 dec_msg, dec_msglen,
2073 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002076 return( ret );
2077 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002078
Paul Bakkercca5b812013-08-31 17:40:26 +02002079 if( dec_msglen != olen )
2080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002083 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2086 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002087 {
2088 /*
2089 * Save IV in SSL3 and TLS1
2090 */
2091 memcpy( ssl->transform_in->iv_dec,
2092 ssl->transform_in->cipher_ctx_dec.iv,
2093 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
2097 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002098
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002099 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002100 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#if defined(MBEDTLS_SSL_DEBUG_ALL)
2103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002104 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002105#endif
Paul Bakker45829992013-01-03 14:52:21 +01002106 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002107 correct = 0;
2108 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#if defined(MBEDTLS_SSL_PROTO_SSL3)
2111 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 {
Paul Bakker48916f92012-09-16 19:57:18 +00002113 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_SSL_DEBUG_ALL)
2116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002118 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002119#endif
Paul Bakker45829992013-01-03 14:52:21 +01002120 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 }
2122 }
2123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2125#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2126 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2127 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 {
2129 /*
Paul Bakker45829992013-01-03 14:52:21 +01002130 * TLSv1+: always check the padding up to the first failure
2131 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002133 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002134 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002135 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002136
Paul Bakker956c9e02013-12-19 14:42:28 +01002137 /*
2138 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002139 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002140 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002141 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002142 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002143 *
2144 * In both cases we reset padding_idx to a safe value (0) to
2145 * prevent out-of-buffer reads.
2146 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002147 correct &= ( padlen <= ssl->in_msglen );
2148 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002149 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002150
2151 padding_idx *= correct;
2152
Angus Grattonb512bc12018-06-19 15:57:50 +10002153 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002154 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002155 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002156 pad_count += real_count *
2157 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2158 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002159
2160 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002163 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002165#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002166 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002168 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2170 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002174 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002175
2176 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002180 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002186#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002189#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
2191 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002192 * Authenticate if not done yet.
2193 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002195#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002196 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002197 {
Hanno Becker992b6872017-11-09 18:57:39 +00002198 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002199
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002200 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002202 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2203 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#if defined(MBEDTLS_SSL_PROTO_SSL3)
2206 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002207 {
2208 ssl_mac( &ssl->transform_in->md_ctx_dec,
2209 ssl->transform_in->mac_dec,
2210 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002211 ssl->in_ctr, ssl->in_msgtype,
2212 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002213 }
2214 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2216#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2217 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2218 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002219 {
2220 /*
2221 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002222 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002223 *
2224 * Known timing attacks:
2225 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2226 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002227 * To compensate for different timings for the MAC calculation
2228 * depending on how much padding was removed (which is determined
2229 * by padlen), process extra_run more blocks through the hash
2230 * function.
2231 *
2232 * The formula in the paper is
2233 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2234 * where L1 is the size of the header plus the decrypted message
2235 * plus CBC padding and L2 is the size of the header plus the
2236 * decrypted message. This is for an underlying hash function
2237 * with 64-byte blocks.
2238 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2239 * correctly. We round down instead of up, so -56 is the correct
2240 * value for our calculations instead of -55.
2241 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002242 * Repeat the formula rather than defining a block_size variable.
2243 * This avoids requiring division by a variable at runtime
2244 * (which would be marginally less efficient and would require
2245 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002246 */
2247 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002248
2249 /*
2250 * The next two sizes are the minimum and maximum values of
2251 * in_msglen over all padlen values.
2252 *
2253 * They're independent of padlen, since we previously did
2254 * in_msglen -= padlen.
2255 *
2256 * Note that max_len + maclen is never more than the buffer
2257 * length, as we previously did in_msglen -= maclen too.
2258 */
2259 const size_t max_len = ssl->in_msglen + padlen;
2260 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2261
Gilles Peskine20b44082018-05-29 14:06:49 +02002262 switch( ssl->transform_in->ciphersuite_info->mac )
2263 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002264#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2265 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002266 case MBEDTLS_MD_MD5:
2267 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002268 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002269 /* 8 bytes of message size, 64-byte compression blocks */
2270 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2271 ( 13 + ssl->in_msglen + 8 ) / 64;
2272 break;
2273#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002274#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002275 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002276 /* 16 bytes of message size, 128-byte compression blocks */
2277 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2278 ( 13 + ssl->in_msglen + 16 ) / 128;
2279 break;
2280#endif
2281 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2284 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002285
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002286 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2289 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2290 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2291 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002292 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002293 /* Make sure we access everything even when padlen > 0. This
2294 * makes the synchronisation requirements for just-in-time
2295 * Prime+Probe attacks much tighter and hopefully impractical. */
2296 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002297 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002298
Manuel Pégourié-Gonnard20cd85c2020-06-18 11:30:40 +02002299 /* Dummy calls to compression function.
2300 * Call mbedtls_md_process at least once due to cache attacks
2301 * that observe whether md_process() was called of not.
2302 * Respect the usual start-(process|update)-finish sequence for
2303 * the sake of hardware accelerators that might require it. */
2304 mbedtls_md_starts( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002305 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Manuel Pégourié-Gonnard20cd85c2020-06-18 11:30:40 +02002307 {
2308 /* The switch statement above already checks that we're using
2309 * one of MD-5, SHA-1, SHA-256 or SHA-384. */
2310 unsigned char tmp[384 / 8];
2311 mbedtls_md_finish( &ssl->transform_in->md_ctx_dec, tmp );
2312 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002315
2316 /* Make sure we access all the memory that could contain the MAC,
2317 * before we check it in the next code block. This makes the
2318 * synchronisation requirements for just-in-time Prime+Probe
2319 * attacks much tighter and hopefully impractical. */
2320 ssl_read_memory( ssl->in_msg + min_len,
2321 max_len - min_len + ssl->transform_in->maclen );
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_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2325 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002331#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002332 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2333 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2334 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002335#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Hanno Becker992b6872017-11-09 18:57:39 +00002337 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2338 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_SSL_DEBUG_ALL)
2341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002342#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002343 correct = 0;
2344 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002345 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002346 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002347
2348 /*
2349 * Finally check the correct flag
2350 */
2351 if( correct == 0 )
2352 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002353#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002354
2355 /* Make extra sure authentication was performed, exactly once */
2356 if( auth_done != 1 )
2357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002361
2362 if( ssl->in_msglen == 0 )
2363 {
Angus Gratton34817922018-06-19 15:58:22 +10002364#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2365 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2366 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2367 {
2368 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2370 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2371 }
2372#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2373
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 ssl->nb_zero++;
2375
2376 /*
2377 * Three or more empty messages may be a DoS attack
2378 * (excessive CPU consumption).
2379 */
2380 if( ssl->nb_zero > 3 )
2381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386 }
2387 else
2388 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002392 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002393 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002394 }
2395 else
2396#endif
2397 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002398 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002399 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2400 if( ++ssl->in_ctr[i - 1] != 0 )
2401 break;
2402
2403 /* The loop goes to its end iff the counter is wrapping */
2404 if( i == ssl_ep_len( ssl ) )
2405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2407 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002408 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002409 }
2410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 return( 0 );
2414}
2415
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002416#undef MAC_NONE
2417#undef MAC_PLAINTEXT
2418#undef MAC_CIPHERTEXT
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421/*
2422 * Compression/decompression functions
2423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002425{
2426 int ret;
2427 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002428 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002429 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002430 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002433
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002434 if( len_pre == 0 )
2435 return( 0 );
2436
Paul Bakker2770fbd2012-07-03 13:30:23 +00002437 memcpy( msg_pre, ssl->out_msg, len_pre );
2438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440 ssl->out_msglen ) );
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002443 ssl->out_msg, ssl->out_msglen );
2444
Paul Bakker48916f92012-09-16 19:57:18 +00002445 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2446 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2447 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002448 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002449
Paul Bakker48916f92012-09-16 19:57:18 +00002450 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002451 if( ret != Z_OK )
2452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2454 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002455 }
2456
Angus Grattond8213d02016-05-25 20:56:48 +10002457 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002458 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002461 ssl->out_msglen ) );
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002464 ssl->out_msg, ssl->out_msglen );
2465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002467
2468 return( 0 );
2469}
2470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002472{
2473 int ret;
2474 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002475 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002476 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002477 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002480
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002481 if( len_pre == 0 )
2482 return( 0 );
2483
Paul Bakker2770fbd2012-07-03 13:30:23 +00002484 memcpy( msg_pre, ssl->in_msg, len_pre );
2485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002487 ssl->in_msglen ) );
2488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002490 ssl->in_msg, ssl->in_msglen );
2491
Paul Bakker48916f92012-09-16 19:57:18 +00002492 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2493 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2494 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002495 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002496 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002497
Paul Bakker48916f92012-09-16 19:57:18 +00002498 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002499 if( ret != Z_OK )
2500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2502 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002503 }
2504
Angus Grattond8213d02016-05-25 20:56:48 +10002505 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002506 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002509 ssl->in_msglen ) );
2510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002512 ssl->in_msg, ssl->in_msglen );
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002515
2516 return( 0 );
2517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2521static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_PROTO_DTLS)
2524static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002525{
2526 /* If renegotiation is not enforced, retransmit until we would reach max
2527 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002528 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002529 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002530 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002531 unsigned char doublings = 1;
2532
2533 while( ratio != 0 )
2534 {
2535 ++doublings;
2536 ratio >>= 1;
2537 }
2538
2539 if( ++ssl->renego_records_seen > doublings )
2540 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002542 return( 0 );
2543 }
2544 }
2545
2546 return( ssl_write_hello_request( ssl ) );
2547}
2548#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002550
Paul Bakker5121ce52009-01-03 21:22:43 +00002551/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002552 * Fill the input message buffer by appending data to it.
2553 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002554 *
2555 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2556 * available (from this read and/or a previous one). Otherwise, an error code
2557 * is returned (possibly EOF or WANT_READ).
2558 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002559 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2560 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2561 * since we always read a whole datagram at once.
2562 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002563 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002564 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002567{
Paul Bakker23986e52011-04-24 08:57:21 +00002568 int ret;
2569 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002573 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002576 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002578 }
2579
Angus Grattond8213d02016-05-25 20:56:48 +10002580 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002584 }
2585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002589 uint32_t timeout;
2590
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002591 /* Just to be sure */
2592 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2593 {
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2595 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2597 }
2598
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002599 /*
2600 * The point is, we need to always read a full datagram at once, so we
2601 * sometimes read more then requested, and handle the additional data.
2602 * It could be the rest of the current record (while fetching the
2603 * header) and/or some other records in the same datagram.
2604 */
2605
2606 /*
2607 * Move to the next record in the already read datagram if applicable
2608 */
2609 if( ssl->next_record_offset != 0 )
2610 {
2611 if( ssl->in_left < ssl->next_record_offset )
2612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002615 }
2616
2617 ssl->in_left -= ssl->next_record_offset;
2618
2619 if( ssl->in_left != 0 )
2620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002622 ssl->next_record_offset ) );
2623 memmove( ssl->in_hdr,
2624 ssl->in_hdr + ssl->next_record_offset,
2625 ssl->in_left );
2626 }
2627
2628 ssl->next_record_offset = 0;
2629 }
2630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002633
2634 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002635 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002636 */
2637 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002640 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002641 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002642
2643 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002644 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002645 * are not at the beginning of a new record, the caller did something
2646 * wrong.
2647 */
2648 if( ssl->in_left != 0 )
2649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002652 }
2653
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002654 /*
2655 * Don't even try to read if time's out already.
2656 * This avoids by-passing the timer when repeatedly receiving messages
2657 * that will end up being dropped.
2658 */
2659 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002660 {
2661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002662 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002663 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002664 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002665 {
Angus Grattond8213d02016-05-25 20:56:48 +10002666 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002669 timeout = ssl->handshake->retransmit_timeout;
2670 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002671 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002674
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002675 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002676 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2677 timeout );
2678 else
2679 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002682
2683 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002685 }
2686
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002687 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002690 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002693 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002694 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002697 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002698 }
2699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002703 return( ret );
2704 }
2705
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002706 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002709 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002711 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002712 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002715 return( ret );
2716 }
2717
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002718 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002721 }
2722
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 if( ret < 0 )
2724 return( ret );
2725
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002726 ssl->in_left = ret;
2727 }
2728 else
2729#endif
2730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002732 ssl->in_left, nb_want ) );
2733
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002734 while( ssl->in_left < nb_want )
2735 {
2736 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002737
2738 if( ssl_check_timer( ssl ) != 0 )
2739 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2740 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002741 {
2742 if( ssl->f_recv_timeout != NULL )
2743 {
2744 ret = ssl->f_recv_timeout( ssl->p_bio,
2745 ssl->in_hdr + ssl->in_left, len,
2746 ssl->conf->read_timeout );
2747 }
2748 else
2749 {
2750 ret = ssl->f_recv( ssl->p_bio,
2751 ssl->in_hdr + ssl->in_left, len );
2752 }
2753 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002756 ssl->in_left, nb_want ) );
2757 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002758
2759 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002761
2762 if( ret < 0 )
2763 return( ret );
2764
makise-homura50f6a192020-08-23 00:39:15 +03002765 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && (size_t)ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002766 {
Darryl Green11999bb2018-03-13 15:22:58 +00002767 MBEDTLS_SSL_DEBUG_MSG( 1,
2768 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002769 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2771 }
2772
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002773 ssl->in_left += ret;
2774 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 }
2776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
2779 return( 0 );
2780}
2781
2782/*
2783 * Flush any data not yet written
2784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002787 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002788 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002792 if( ssl->f_send == NULL )
2793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002795 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002797 }
2798
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002799 /* Avoid incrementing counter if data is flushed */
2800 if( ssl->out_left == 0 )
2801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002803 return( 0 );
2804 }
2805
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 while( ssl->out_left > 0 )
2807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2809 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Hanno Becker2b1e3542018-08-06 11:19:13 +01002811 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002812 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
2816 if( ret <= 0 )
2817 return( ret );
2818
makise-homura50f6a192020-08-23 00:39:15 +03002819 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && (size_t)ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002820 {
Darryl Green11999bb2018-03-13 15:22:58 +00002821 MBEDTLS_SSL_DEBUG_MSG( 1,
2822 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002823 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2825 }
2826
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 ssl->out_left -= ret;
2828 }
2829
Hanno Becker2b1e3542018-08-06 11:19:13 +01002830#if defined(MBEDTLS_SSL_PROTO_DTLS)
2831 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002832 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002833 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002834 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002835 else
2836#endif
2837 {
2838 ssl->out_hdr = ssl->out_buf + 8;
2839 }
2840 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
2844 return( 0 );
2845}
2846
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002847/*
2848 * Functions to handle the DTLS retransmission state machine
2849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002851/*
2852 * Append current handshake message to current outgoing flight
2853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2858 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2859 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002860
2861 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002862 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002863 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002866 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002867 }
2868
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002869 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002870 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002873 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002874 }
2875
2876 /* Copy current handshake message with headers */
2877 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2878 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002879 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002880 msg->next = NULL;
2881
2882 /* Append to the current flight */
2883 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002884 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002885 else
2886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002888 while( cur->next != NULL )
2889 cur = cur->next;
2890 cur->next = msg;
2891 }
2892
Hanno Becker3b235902018-08-06 09:54:53 +01002893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002894 return( 0 );
2895}
2896
2897/*
2898 * Free the current flight of handshake messages
2899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 mbedtls_ssl_flight_item *cur = flight;
2903 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002904
2905 while( cur != NULL )
2906 {
2907 next = cur->next;
2908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 mbedtls_free( cur->p );
2910 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002911
2912 cur = next;
2913 }
2914}
2915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2917static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002918#endif
2919
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002920/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002921 * Swap transform_out and out_ctr with the alternative ones
2922 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002923static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002926 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002927#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2928 int ret;
2929#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002930
2931 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002934 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002935 }
2936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002938
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002939 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002940 tmp_transform = ssl->transform_out;
2941 ssl->transform_out = ssl->handshake->alt_transform_out;
2942 ssl->handshake->alt_transform_out = tmp_transform;
2943
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002944 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002945 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2946 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002947 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002948
2949 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002950 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2953 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2958 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002959 }
2960 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002961#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2962
2963 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002964}
2965
2966/*
2967 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002968 */
2969int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2970{
2971 int ret = 0;
2972
2973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2974
2975 ret = mbedtls_ssl_flight_transmit( ssl );
2976
2977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2978
2979 return( ret );
2980}
2981
2982/*
2983 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002984 *
2985 * Need to remember the current message in case flush_output returns
2986 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002987 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002988 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002989int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002990{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002991 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002995 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002997
2998 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002999 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003000 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3001 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003004 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003005
3006 while( ssl->handshake->cur_msg != NULL )
3007 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003008 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003009 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003010
Hanno Beckere1dcb032018-08-17 16:47:58 +01003011 int const is_finished =
3012 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3013 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3014
Hanno Becker04da1892018-08-14 13:22:10 +01003015 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3016 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3017
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003018 /* Swap epochs before sending Finished: we can't do it after
3019 * sending ChangeCipherSpec, in case write returns WANT_READ.
3020 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003021 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003022 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003024 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3025 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003026 }
3027
Hanno Becker67bc7c32018-08-06 11:33:50 +01003028 ret = ssl_get_remaining_payload_in_datagram( ssl );
3029 if( ret < 0 )
3030 return( ret );
3031 max_frag_len = (size_t) ret;
3032
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003033 /* CCS is copied as is, while HS messages may need fragmentation */
3034 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3035 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003036 if( max_frag_len == 0 )
3037 {
3038 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3039 return( ret );
3040
3041 continue;
3042 }
3043
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003044 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003045 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003046 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003047
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003048 /* Update position inside current message */
3049 ssl->handshake->cur_msg_p += cur->len;
3050 }
3051 else
3052 {
3053 const unsigned char * const p = ssl->handshake->cur_msg_p;
3054 const size_t hs_len = cur->len - 12;
3055 const size_t frag_off = p - ( cur->p + 12 );
3056 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003057 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003058
Hanno Beckere1dcb032018-08-17 16:47:58 +01003059 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003060 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003061 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003062 {
3063 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3064 return( ret );
3065 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003066
Hanno Becker67bc7c32018-08-06 11:33:50 +01003067 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3068 return( ret );
3069
3070 continue;
3071 }
3072 max_hs_frag_len = max_frag_len - 12;
3073
3074 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3075 max_hs_frag_len : rem_len;
3076
3077 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003078 {
3079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003080 (unsigned) cur_hs_frag_len,
3081 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003082 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003083
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003084 /* Messages are stored with handshake headers as if not fragmented,
3085 * copy beginning of headers then fill fragmentation fields.
3086 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3087 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003088
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003089 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3090 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3091 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3092
Hanno Becker67bc7c32018-08-06 11:33:50 +01003093 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3094 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3095 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003096
3097 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3098
Hanno Becker3f7b9732018-08-28 09:53:25 +01003099 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003100 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3101 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003102 ssl->out_msgtype = cur->type;
3103
3104 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003105 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003106 }
3107
3108 /* If done with the current message move to the next one if any */
3109 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3110 {
3111 if( cur->next != NULL )
3112 {
3113 ssl->handshake->cur_msg = cur->next;
3114 ssl->handshake->cur_msg_p = cur->next->p + 12;
3115 }
3116 else
3117 {
3118 ssl->handshake->cur_msg = NULL;
3119 ssl->handshake->cur_msg_p = NULL;
3120 }
3121 }
3122
3123 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003124 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003127 return( ret );
3128 }
3129 }
3130
Hanno Becker67bc7c32018-08-06 11:33:50 +01003131 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3132 return( ret );
3133
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003134 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3136 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003137 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003140 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3141 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003142
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003144
3145 return( 0 );
3146}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003147
3148/*
3149 * To be called when the last message of an incoming flight is received.
3150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003152{
3153 /* We won't need to resend that one any more */
3154 ssl_flight_free( ssl->handshake->flight );
3155 ssl->handshake->flight = NULL;
3156 ssl->handshake->cur_msg = NULL;
3157
3158 /* The next incoming flight will start with this msg_seq */
3159 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3160
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003161 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003162 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003163
Hanno Becker0271f962018-08-16 13:23:47 +01003164 /* Clear future message buffering structure. */
3165 ssl_buffering_free( ssl );
3166
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003167 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003168 ssl_set_timer( ssl, 0 );
3169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3171 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003174 }
3175 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003177}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003178
3179/*
3180 * To be called when the last message of an outgoing flight is send.
3181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003183{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003184 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003185 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3188 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003191 }
3192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003196
Paul Bakker5121ce52009-01-03 21:22:43 +00003197/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003198 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200
3201/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003202 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003203 *
3204 * - fill in handshake headers
3205 * - update handshake checksum
3206 * - DTLS: save message for resending
3207 * - then pass to the record layer
3208 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003209 * DTLS: except for HelloRequest, messages are only queued, and will only be
3210 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003211 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003212 * Inputs:
3213 * - ssl->out_msglen: 4 + actual handshake message len
3214 * (4 is the size of handshake headers for TLS)
3215 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3216 * - ssl->out_msg + 4: the handshake message body
3217 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003218 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003219 * - ssl->out_msglen: the length of the record contents
3220 * (including handshake headers but excluding record headers)
3221 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003222 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003223int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003224{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003225 int ret;
3226 const size_t hs_len = ssl->out_msglen - 4;
3227 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3230
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003231 /*
3232 * Sanity checks
3233 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003234 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003235 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3236 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003237 /* In SSLv3, the client might send a NoCertificate alert. */
3238#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3239 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3240 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3241 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3242#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3243 {
3244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3246 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003249 /* Whenever we send anything different from a
3250 * HelloRequest we should be in a handshake - double check. */
3251 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3252 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003253 ssl->handshake == NULL )
3254 {
3255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3257 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003261 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003263 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003266 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003267#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003268
Hanno Beckerb50a2532018-08-06 11:52:54 +01003269 /* Double-check that we did not exceed the bounds
3270 * of the outgoing record buffer.
3271 * This should never fail as the various message
3272 * writing functions must obey the bounds of the
3273 * outgoing record buffer, but better be safe.
3274 *
3275 * Note: We deliberately do not check for the MTU or MFL here.
3276 */
3277 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3278 {
3279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3280 "size %u, maximum %u",
3281 (unsigned) ssl->out_msglen,
3282 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3284 }
3285
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003286 /*
3287 * Fill handshake headers
3288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003291 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3292 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3293 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003295 /*
3296 * DTLS has additional fields in the Handshake layer,
3297 * between the length field and the actual payload:
3298 * uint16 message_seq;
3299 * uint24 fragment_offset;
3300 * uint24 fragment_length;
3301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003303 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003304 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003305 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003306 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003307 {
3308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3309 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003310 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003311 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3313 }
3314
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003315 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003316 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003317
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003318 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003319 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003320 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003321 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3322 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3323 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003324 }
3325 else
3326 {
3327 ssl->out_msg[4] = 0;
3328 ssl->out_msg[5] = 0;
3329 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003330
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003331 /* Handshake hashes are computed without fragmentation,
3332 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003333 memset( ssl->out_msg + 6, 0x00, 3 );
3334 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003337
Hanno Becker0207e532018-08-28 10:28:28 +01003338 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003339 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3340 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 }
3342
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003343 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003346 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3347 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348 {
3349 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 return( ret );
3353 }
3354 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003355 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003356#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003357 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003358 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003359 {
3360 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3361 return( ret );
3362 }
3363 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003364
3365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3366
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003367 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003368}
3369
3370/*
3371 * Record layer functions
3372 */
3373
3374/*
3375 * Write current record.
3376 *
3377 * Uses:
3378 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3379 * - ssl->out_msglen: length of the record content (excl headers)
3380 * - ssl->out_msg: record content
3381 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003382int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003383{
3384 int ret, done = 0;
3385 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003386 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003387
3388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003391 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003393 {
3394 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003397 return( ret );
3398 }
3399
3400 len = ssl->out_msglen;
3401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3405 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 ret = mbedtls_ssl_hw_record_write( ssl );
3410 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3413 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003414 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003415
3416 if( ret == 0 )
3417 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003418 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003420 if( !done )
3421 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003422 unsigned i;
3423 size_t protected_record_size;
3424
Paul Bakker05ef8352012-05-08 09:17:57 +00003425 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003427 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003428
Hanno Becker19859472018-08-06 09:40:20 +01003429 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003430 ssl->out_len[0] = (unsigned char)( len >> 8 );
3431 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003432
Paul Bakker48916f92012-09-16 19:57:18 +00003433 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003434 {
3435 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003438 return( ret );
3439 }
3440
3441 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003442 ssl->out_len[0] = (unsigned char)( len >> 8 );
3443 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003444 }
3445
Hanno Becker2b1e3542018-08-06 11:19:13 +01003446 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3447
3448#if defined(MBEDTLS_SSL_PROTO_DTLS)
3449 /* In case of DTLS, double-check that we don't exceed
3450 * the remaining space in the datagram. */
3451 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3452 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003453 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003454 if( ret < 0 )
3455 return( ret );
3456
3457 if( protected_record_size > (size_t) ret )
3458 {
3459 /* Should never happen */
3460 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3461 }
3462 }
3463#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003466 "version = [%d:%d], msglen = %d",
3467 ssl->out_hdr[0], ssl->out_hdr[1],
3468 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003471 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003472
3473 ssl->out_left += protected_record_size;
3474 ssl->out_hdr += protected_record_size;
3475 ssl_update_out_pointers( ssl, ssl->transform_out );
3476
Hanno Becker04484622018-08-06 09:49:38 +01003477 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3478 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3479 break;
3480
3481 /* The loop goes to its end iff the counter is wrapping */
3482 if( i == ssl_ep_len( ssl ) )
3483 {
3484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3485 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 }
3488
Hanno Becker67bc7c32018-08-06 11:33:50 +01003489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3491 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003492 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003493 size_t remaining;
3494 ret = ssl_get_remaining_payload_in_datagram( ssl );
3495 if( ret < 0 )
3496 {
3497 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3498 ret );
3499 return( ret );
3500 }
3501
3502 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003503 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003504 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003506 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003507 else
3508 {
Hanno Becker513815a2018-08-20 11:56:09 +01003509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003510 }
3511 }
3512#endif /* MBEDTLS_SSL_PROTO_DTLS */
3513
3514 if( ( flush == SSL_FORCE_FLUSH ) &&
3515 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 return( ret );
3519 }
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
3523 return( 0 );
3524}
3525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003527
3528static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3529{
3530 if( ssl->in_msglen < ssl->in_hslen ||
3531 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3532 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3533 {
3534 return( 1 );
3535 }
3536 return( 0 );
3537}
Hanno Becker44650b72018-08-16 12:51:11 +01003538
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003539static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003540{
3541 return( ( ssl->in_msg[9] << 16 ) |
3542 ( ssl->in_msg[10] << 8 ) |
3543 ssl->in_msg[11] );
3544}
3545
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003546static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003547{
3548 return( ( ssl->in_msg[6] << 16 ) |
3549 ( ssl->in_msg[7] << 8 ) |
3550 ssl->in_msg[8] );
3551}
3552
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003553static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003554{
3555 uint32_t msg_len, frag_off, frag_len;
3556
3557 msg_len = ssl_get_hs_total_len( ssl );
3558 frag_off = ssl_get_hs_frag_off( ssl );
3559 frag_len = ssl_get_hs_frag_len( ssl );
3560
3561 if( frag_off > msg_len )
3562 return( -1 );
3563
3564 if( frag_len > msg_len - frag_off )
3565 return( -1 );
3566
3567 if( frag_len + 12 > ssl->in_msglen )
3568 return( -1 );
3569
3570 return( 0 );
3571}
3572
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003573/*
3574 * Mark bits in bitmask (used for DTLS HS reassembly)
3575 */
3576static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3577{
3578 unsigned int start_bits, end_bits;
3579
3580 start_bits = 8 - ( offset % 8 );
3581 if( start_bits != 8 )
3582 {
3583 size_t first_byte_idx = offset / 8;
3584
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003585 /* Special case */
3586 if( len <= start_bits )
3587 {
3588 for( ; len != 0; len-- )
3589 mask[first_byte_idx] |= 1 << ( start_bits - len );
3590
3591 /* Avoid potential issues with offset or len becoming invalid */
3592 return;
3593 }
3594
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003595 offset += start_bits; /* Now offset % 8 == 0 */
3596 len -= start_bits;
3597
3598 for( ; start_bits != 0; start_bits-- )
3599 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3600 }
3601
3602 end_bits = len % 8;
3603 if( end_bits != 0 )
3604 {
3605 size_t last_byte_idx = ( offset + len ) / 8;
3606
3607 len -= end_bits; /* Now len % 8 == 0 */
3608
3609 for( ; end_bits != 0; end_bits-- )
3610 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3611 }
3612
3613 memset( mask + offset / 8, 0xFF, len / 8 );
3614}
3615
3616/*
3617 * Check that bitmask is full
3618 */
3619static int ssl_bitmask_check( unsigned char *mask, size_t len )
3620{
3621 size_t i;
3622
3623 for( i = 0; i < len / 8; i++ )
3624 if( mask[i] != 0xFF )
3625 return( -1 );
3626
3627 for( i = 0; i < len % 8; i++ )
3628 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3629 return( -1 );
3630
3631 return( 0 );
3632}
3633
Hanno Becker56e205e2018-08-16 09:06:12 +01003634/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003635static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003636 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003637{
Hanno Becker56e205e2018-08-16 09:06:12 +01003638 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003639
Hanno Becker56e205e2018-08-16 09:06:12 +01003640 alloc_len = 12; /* Handshake header */
3641 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003642
Hanno Beckerd07df862018-08-16 09:14:58 +01003643 if( add_bitmap )
3644 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003645
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003646 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003647}
Hanno Becker56e205e2018-08-16 09:06:12 +01003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003650
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003651static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003652{
3653 return( ( ssl->in_msg[1] << 16 ) |
3654 ( ssl->in_msg[2] << 8 ) |
3655 ssl->in_msg[3] );
3656}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003657
Simon Butcher99000142016-10-13 17:21:01 +01003658int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003663 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003665 }
3666
Hanno Becker12555c62018-08-16 12:47:53 +01003667 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003670 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003671 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003674 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003675 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003676 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003677 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003678
Hanno Becker44650b72018-08-16 12:51:11 +01003679 if( ssl_check_hs_header( ssl ) != 0 )
3680 {
3681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3682 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3683 }
3684
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003685 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003686 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3687 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3688 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3689 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003690 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003691 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3692 {
3693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3694 recv_msg_seq,
3695 ssl->handshake->in_msg_seq ) );
3696 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3697 }
3698
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003699 /* Retransmit only on last message from previous flight, to avoid
3700 * too many retransmissions.
3701 * Besides, No sane server ever retransmits HelloVerifyRequest */
3702 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003706 "message_seq = %d, start_of_flight = %d",
3707 recv_msg_seq,
3708 ssl->handshake->in_flight_start_seq ) );
3709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003713 return( ret );
3714 }
3715 }
3716 else
3717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003719 "message_seq = %d, expected = %d",
3720 recv_msg_seq,
3721 ssl->handshake->in_msg_seq ) );
3722 }
3723
Hanno Becker90333da2017-10-10 11:27:13 +01003724 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003725 }
3726 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003727
Hanno Becker6d97ef52018-08-16 13:09:04 +01003728 /* Message reassembly is handled alongside buffering of future
3729 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003730 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003731 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003732 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003735 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003736 }
3737 }
3738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003740 /* With TLS we don't handle fragmentation (for now) */
3741 if( ssl->in_msglen < ssl->in_hslen )
3742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3744 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003745 }
3746
Simon Butcher99000142016-10-13 17:21:01 +01003747 return( 0 );
3748}
3749
3750void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3751{
Hanno Becker0271f962018-08-16 13:23:47 +01003752 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003753
Hanno Becker0271f962018-08-16 13:23:47 +01003754 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003755 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003756 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003757 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003758
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003759 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003762 ssl->handshake != NULL )
3763 {
Hanno Becker0271f962018-08-16 13:23:47 +01003764 unsigned offset;
3765 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003766
Hanno Becker0271f962018-08-16 13:23:47 +01003767 /* Increment handshake sequence number */
3768 hs->in_msg_seq++;
3769
3770 /*
3771 * Clear up handshake buffering and reassembly structure.
3772 */
3773
3774 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003775 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003776
3777 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003778 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3779 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003780 offset++, hs_buf++ )
3781 {
3782 *hs_buf = *(hs_buf + 1);
3783 }
3784
3785 /* Create a fresh last entry */
3786 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003787 }
3788#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003789}
3790
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003791/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003792 * DTLS anti-replay: RFC 6347 4.1.2.6
3793 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003794 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3795 * Bit n is set iff record number in_window_top - n has been seen.
3796 *
3797 * Usually, in_window_top is the last record number seen and the lsb of
3798 * in_window is set. The only exception is the initial state (record number 0
3799 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3802static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003803{
3804 ssl->in_window_top = 0;
3805 ssl->in_window = 0;
3806}
3807
3808static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3809{
3810 return( ( (uint64_t) buf[0] << 40 ) |
3811 ( (uint64_t) buf[1] << 32 ) |
3812 ( (uint64_t) buf[2] << 24 ) |
3813 ( (uint64_t) buf[3] << 16 ) |
3814 ( (uint64_t) buf[4] << 8 ) |
3815 ( (uint64_t) buf[5] ) );
3816}
3817
3818/*
3819 * Return 0 if sequence number is acceptable, -1 otherwise
3820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003822{
3823 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3824 uint64_t bit;
3825
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003826 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003827 return( 0 );
3828
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003829 if( rec_seqnum > ssl->in_window_top )
3830 return( 0 );
3831
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003832 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003833
3834 if( bit >= 64 )
3835 return( -1 );
3836
3837 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3838 return( -1 );
3839
3840 return( 0 );
3841}
3842
3843/*
3844 * Update replay window on new validated record
3845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003847{
3848 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3849
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003850 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003851 return;
3852
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003853 if( rec_seqnum > ssl->in_window_top )
3854 {
3855 /* Update window_top and the contents of the window */
3856 uint64_t shift = rec_seqnum - ssl->in_window_top;
3857
3858 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003859 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003860 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003861 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003862 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003863 ssl->in_window |= 1;
3864 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003865
3866 ssl->in_window_top = rec_seqnum;
3867 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003868 else
3869 {
3870 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003871 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003872
3873 if( bit < 64 ) /* Always true, but be extra sure */
3874 ssl->in_window |= (uint64_t) 1 << bit;
3875 }
3876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003878
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003879#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003880/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003881static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3882
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003883/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003884 * Without any SSL context, check if a datagram looks like a ClientHello with
3885 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003886 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003887 *
3888 * - if cookie is valid, return 0
3889 * - if ClientHello looks superficially valid but cookie is not,
3890 * fill obuf and set olen, then
3891 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3892 * - otherwise return a specific error code
3893 */
3894static int ssl_check_dtls_clihlo_cookie(
3895 mbedtls_ssl_cookie_write_t *f_cookie_write,
3896 mbedtls_ssl_cookie_check_t *f_cookie_check,
3897 void *p_cookie,
3898 const unsigned char *cli_id, size_t cli_id_len,
3899 const unsigned char *in, size_t in_len,
3900 unsigned char *obuf, size_t buf_len, size_t *olen )
3901{
3902 size_t sid_len, cookie_len;
3903 unsigned char *p;
3904
3905 if( f_cookie_write == NULL || f_cookie_check == NULL )
3906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3907
3908 /*
3909 * Structure of ClientHello with record and handshake headers,
3910 * and expected values. We don't need to check a lot, more checks will be
3911 * done when actually parsing the ClientHello - skipping those checks
3912 * avoids code duplication and does not make cookie forging any easier.
3913 *
3914 * 0-0 ContentType type; copied, must be handshake
3915 * 1-2 ProtocolVersion version; copied
3916 * 3-4 uint16 epoch; copied, must be 0
3917 * 5-10 uint48 sequence_number; copied
3918 * 11-12 uint16 length; (ignored)
3919 *
3920 * 13-13 HandshakeType msg_type; (ignored)
3921 * 14-16 uint24 length; (ignored)
3922 * 17-18 uint16 message_seq; copied
3923 * 19-21 uint24 fragment_offset; copied, must be 0
3924 * 22-24 uint24 fragment_length; (ignored)
3925 *
3926 * 25-26 ProtocolVersion client_version; (ignored)
3927 * 27-58 Random random; (ignored)
3928 * 59-xx SessionID session_id; 1 byte len + sid_len content
3929 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3930 * ...
3931 *
3932 * Minimum length is 61 bytes.
3933 */
3934 if( in_len < 61 ||
3935 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3936 in[3] != 0 || in[4] != 0 ||
3937 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3938 {
3939 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3940 }
3941
3942 sid_len = in[59];
3943 if( sid_len > in_len - 61 )
3944 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3945
3946 cookie_len = in[60 + sid_len];
3947 if( cookie_len > in_len - 60 )
3948 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3949
3950 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3951 cli_id, cli_id_len ) == 0 )
3952 {
3953 /* Valid cookie */
3954 return( 0 );
3955 }
3956
3957 /*
3958 * If we get here, we've got an invalid cookie, let's prepare HVR.
3959 *
3960 * 0-0 ContentType type; copied
3961 * 1-2 ProtocolVersion version; copied
3962 * 3-4 uint16 epoch; copied
3963 * 5-10 uint48 sequence_number; copied
3964 * 11-12 uint16 length; olen - 13
3965 *
3966 * 13-13 HandshakeType msg_type; hello_verify_request
3967 * 14-16 uint24 length; olen - 25
3968 * 17-18 uint16 message_seq; copied
3969 * 19-21 uint24 fragment_offset; copied
3970 * 22-24 uint24 fragment_length; olen - 25
3971 *
3972 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3973 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3974 *
3975 * Minimum length is 28.
3976 */
3977 if( buf_len < 28 )
3978 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3979
3980 /* Copy most fields and adapt others */
3981 memcpy( obuf, in, 25 );
3982 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3983 obuf[25] = 0xfe;
3984 obuf[26] = 0xff;
3985
3986 /* Generate and write actual cookie */
3987 p = obuf + 28;
3988 if( f_cookie_write( p_cookie,
3989 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3990 {
3991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3992 }
3993
3994 *olen = p - obuf;
3995
3996 /* Go back and fill length fields */
3997 obuf[27] = (unsigned char)( *olen - 28 );
3998
3999 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4000 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4001 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4002
4003 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4004 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4005
4006 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4007}
4008
4009/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004010 * Handle possible client reconnect with the same UDP quadruplet
4011 * (RFC 6347 Section 4.2.8).
4012 *
4013 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4014 * that looks like a ClientHello.
4015 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004016 * - if the input looks like a ClientHello without cookies,
4017 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004018 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004019 * - if the input looks like a ClientHello with a valid cookie,
4020 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004021 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004022 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004023 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004024 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004025 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4026 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004027 */
4028static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4029{
4030 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004031 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004032
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004033 ret = ssl_check_dtls_clihlo_cookie(
4034 ssl->conf->f_cookie_write,
4035 ssl->conf->f_cookie_check,
4036 ssl->conf->p_cookie,
4037 ssl->cli_id, ssl->cli_id_len,
4038 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004039 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004040
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004041 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4042
4043 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004044 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004045 int send_ret;
4046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4047 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4048 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004049 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004050 * If the error is permanent we'll catch it later,
4051 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004052 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4053 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4054 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004055
4056 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004057 }
4058
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004059 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004060 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004062 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4063 {
4064 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4065 return( ret );
4066 }
4067
4068 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004069 }
4070
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004071 return( ret );
4072}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004073#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004074
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004075/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004076 * ContentType type;
4077 * ProtocolVersion version;
4078 * uint16 epoch; // DTLS only
4079 * uint48 sequence_number; // DTLS only
4080 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004081 *
4082 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004083 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004084 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4085 *
4086 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004087 * 1. proceed with the record if this function returns 0
4088 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4089 * 3. return CLIENT_RECONNECT if this function return that value
4090 * 4. drop the whole datagram if this function returns anything else.
4091 * Point 2 is needed when the peer is resending, and we have already received
4092 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004095{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004096 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 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 +02004099
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004101 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004102 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004105 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004106 ssl->in_msgtype,
4107 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004108
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004109 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4111 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4112 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4113 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004116
4117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004118 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4119 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004120 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4121#endif /* MBEDTLS_SSL_PROTO_DTLS */
4122 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4123 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004126 }
4127
4128 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004129 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4132 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 }
4134
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004135 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4138 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004139 }
4140
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004141 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004142 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004143 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4146 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004147 }
4148
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004149 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004150 * DTLS-related tests.
4151 * Check epoch before checking length constraint because
4152 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4153 * message gets duplicated before the corresponding Finished message,
4154 * the second ChangeCipherSpec should be discarded because it belongs
4155 * to an old epoch, but not because its length is shorter than
4156 * the minimum record length for packets using the new record transform.
4157 * Note that these two kinds of failures are handled differently,
4158 * as an unexpected record is silently skipped but an invalid
4159 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004160 */
4161#if defined(MBEDTLS_SSL_PROTO_DTLS)
4162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4163 {
4164 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4165
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004166 /* Check epoch (and sequence number) with DTLS */
4167 if( rec_epoch != ssl->in_epoch )
4168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4170 "expected %d, received %d",
4171 ssl->in_epoch, rec_epoch ) );
4172
4173#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4174 /*
4175 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4176 * access the first byte of record content (handshake type), as we
4177 * have an active transform (possibly iv_len != 0), so use the
4178 * fact that the record header len is 13 instead.
4179 */
4180 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4181 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4182 rec_epoch == 0 &&
4183 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4184 ssl->in_left > 13 &&
4185 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4186 {
4187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4188 "from the same port" ) );
4189 return( ssl_handle_possible_reconnect( ssl ) );
4190 }
4191 else
4192#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004193 {
4194 /* Consider buffering the record. */
4195 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4196 {
4197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4198 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4199 }
4200
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004201 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004202 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004203 }
4204
4205#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4206 /* Replay detection only works for the current epoch */
4207 if( rec_epoch == ssl->in_epoch &&
4208 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4209 {
4210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4211 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4212 }
4213#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004214
Hanno Becker52c6dc62017-05-26 16:07:36 +01004215 /* Drop unexpected ApplicationData records,
4216 * except at the beginning of renegotiations */
4217 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4218 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4219#if defined(MBEDTLS_SSL_RENEGOTIATION)
4220 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4221 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4222#endif
4223 )
4224 {
4225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4226 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4227 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004228 }
4229#endif /* MBEDTLS_SSL_PROTO_DTLS */
4230
Hanno Becker52c6dc62017-05-26 16:07:36 +01004231
4232 /* Check length against bounds of the current transform and version */
4233 if( ssl->transform_in == NULL )
4234 {
4235 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004236 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004237 {
4238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4239 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4240 }
4241 }
4242 else
4243 {
4244 if( ssl->in_msglen < ssl->transform_in->minlen )
4245 {
4246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4247 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4248 }
4249
4250#if defined(MBEDTLS_SSL_PROTO_SSL3)
4251 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004252 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004253 {
4254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4255 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4256 }
4257#endif
4258#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4259 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4260 /*
4261 * TLS encrypted messages can have up to 256 bytes of padding
4262 */
4263 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4264 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004265 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004266 {
4267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4268 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4269 }
4270#endif
4271 }
4272
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004273 return( 0 );
4274}
Paul Bakker5121ce52009-01-03 21:22:43 +00004275
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004276/*
4277 * If applicable, decrypt (and decompress) record content
4278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004280{
4281 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4284 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4287 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 ret = mbedtls_ssl_hw_record_read( ssl );
4292 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4295 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004296 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004297
4298 if( ret == 0 )
4299 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004302 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004303 {
4304 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004307 return( ret );
4308 }
4309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 ssl->in_msg, ssl->in_msglen );
4312
Angus Grattond8213d02016-05-25 20:56:48 +10004313 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4316 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 }
4318 }
4319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004321 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004323 {
4324 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004327 return( ret );
4328 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004336 }
4337#endif
4338
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004339 return( 0 );
4340}
4341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004343
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004344/*
4345 * Read a record.
4346 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004347 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4348 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4349 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004350 */
Hanno Becker1097b342018-08-15 14:09:41 +01004351
4352/* Helper functions for mbedtls_ssl_read_record(). */
4353static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004354static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4355static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004356
Hanno Becker327c93b2018-08-15 13:56:18 +01004357int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004358 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004359{
4360 int ret;
4361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004363
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004364 if( ssl->keep_current_message == 0 )
4365 {
4366 do {
Simon Butcher99000142016-10-13 17:21:01 +01004367
Hanno Becker26994592018-08-15 14:14:59 +01004368 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004369 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004370 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004371
Hanno Beckere74d5562018-08-15 14:26:08 +01004372 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004373 {
Hanno Becker40f50842018-08-15 14:48:01 +01004374#if defined(MBEDTLS_SSL_PROTO_DTLS)
4375 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004376
Hanno Becker40f50842018-08-15 14:48:01 +01004377 /* We only check for buffered messages if the
4378 * current datagram is fully consumed. */
4379 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004380 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004381 {
Hanno Becker40f50842018-08-15 14:48:01 +01004382 if( ssl_load_buffered_message( ssl ) == 0 )
4383 have_buffered = 1;
4384 }
4385
4386 if( have_buffered == 0 )
4387#endif /* MBEDTLS_SSL_PROTO_DTLS */
4388 {
4389 ret = ssl_get_next_record( ssl );
4390 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4391 continue;
4392
4393 if( ret != 0 )
4394 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004395 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004396 return( ret );
4397 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004398 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004399 }
4400
4401 ret = mbedtls_ssl_handle_message_type( ssl );
4402
Hanno Becker40f50842018-08-15 14:48:01 +01004403#if defined(MBEDTLS_SSL_PROTO_DTLS)
4404 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4405 {
4406 /* Buffer future message */
4407 ret = ssl_buffer_message( ssl );
4408 if( ret != 0 )
4409 return( ret );
4410
4411 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4412 }
4413#endif /* MBEDTLS_SSL_PROTO_DTLS */
4414
Hanno Becker90333da2017-10-10 11:27:13 +01004415 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4416 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004417
4418 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004419 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004420 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004421 return( ret );
4422 }
4423
Hanno Becker327c93b2018-08-15 13:56:18 +01004424 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004425 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004426 {
4427 mbedtls_ssl_update_handshake_status( ssl );
4428 }
Simon Butcher99000142016-10-13 17:21:01 +01004429 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004430 else
Simon Butcher99000142016-10-13 17:21:01 +01004431 {
Hanno Becker02f59072018-08-15 14:00:24 +01004432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004433 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004434 }
4435
4436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4437
4438 return( 0 );
4439}
4440
Hanno Becker40f50842018-08-15 14:48:01 +01004441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004442static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004443{
Hanno Becker40f50842018-08-15 14:48:01 +01004444 if( ssl->in_left > ssl->next_record_offset )
4445 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004446
Hanno Becker40f50842018-08-15 14:48:01 +01004447 return( 0 );
4448}
4449
4450static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4451{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004452 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004453 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004454 int ret = 0;
4455
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004456 if( hs == NULL )
4457 return( -1 );
4458
Hanno Beckere00ae372018-08-20 09:39:42 +01004459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4460
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004461 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4462 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4463 {
4464 /* Check if we have seen a ChangeCipherSpec before.
4465 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004466 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004467 {
4468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4469 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004470 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004471 }
4472
Hanno Becker39b8bc92018-08-28 17:17:13 +01004473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004474 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4475 ssl->in_msglen = 1;
4476 ssl->in_msg[0] = 1;
4477
4478 /* As long as they are equal, the exact value doesn't matter. */
4479 ssl->in_left = 0;
4480 ssl->next_record_offset = 0;
4481
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004482 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004483 goto exit;
4484 }
Hanno Becker37f95322018-08-16 13:55:32 +01004485
Hanno Beckerb8f50142018-08-28 10:01:34 +01004486#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004487 /* Debug only */
4488 {
4489 unsigned offset;
4490 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4491 {
4492 hs_buf = &hs->buffering.hs[offset];
4493 if( hs_buf->is_valid == 1 )
4494 {
4495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4496 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004497 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004498 }
4499 }
4500 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004501#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004502
4503 /* Check if we have buffered and/or fully reassembled the
4504 * next handshake message. */
4505 hs_buf = &hs->buffering.hs[0];
4506 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4507 {
4508 /* Synthesize a record containing the buffered HS message. */
4509 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4510 ( hs_buf->data[2] << 8 ) |
4511 hs_buf->data[3];
4512
4513 /* Double-check that we haven't accidentally buffered
4514 * a message that doesn't fit into the input buffer. */
4515 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4516 {
4517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4519 }
4520
4521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4522 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4523 hs_buf->data, msg_len + 12 );
4524
4525 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4526 ssl->in_hslen = msg_len + 12;
4527 ssl->in_msglen = msg_len + 12;
4528 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4529
4530 ret = 0;
4531 goto exit;
4532 }
4533 else
4534 {
4535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4536 hs->in_msg_seq ) );
4537 }
4538
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004539 ret = -1;
4540
4541exit:
4542
4543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4544 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004545}
4546
Hanno Beckera02b0b42018-08-21 17:20:27 +01004547static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4548 size_t desired )
4549{
4550 int offset;
4551 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4553 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004554
Hanno Becker01315ea2018-08-21 17:22:17 +01004555 /* Get rid of future records epoch first, if such exist. */
4556 ssl_free_buffered_record( ssl );
4557
4558 /* Check if we have enough space available now. */
4559 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4560 hs->buffering.total_bytes_buffered ) )
4561 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004563 return( 0 );
4564 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004565
Hanno Becker4f432ad2018-08-28 10:02:32 +01004566 /* We don't have enough space to buffer the next expected handshake
4567 * message. Remove buffers used for future messages to gain space,
4568 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004569 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4570 offset >= 0; offset-- )
4571 {
4572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4573 offset ) );
4574
Hanno Beckerb309b922018-08-23 13:18:05 +01004575 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004576
4577 /* Check if we have enough space available now. */
4578 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4579 hs->buffering.total_bytes_buffered ) )
4580 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004582 return( 0 );
4583 }
4584 }
4585
4586 return( -1 );
4587}
4588
Hanno Becker40f50842018-08-15 14:48:01 +01004589static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4590{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004591 int ret = 0;
4592 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4593
4594 if( hs == NULL )
4595 return( 0 );
4596
4597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4598
4599 switch( ssl->in_msgtype )
4600 {
4601 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004603
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004604 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004605 break;
4606
4607 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004608 {
4609 unsigned recv_msg_seq_offset;
4610 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4611 mbedtls_ssl_hs_buffer *hs_buf;
4612 size_t msg_len = ssl->in_hslen - 12;
4613
4614 /* We should never receive an old handshake
4615 * message - double-check nonetheless. */
4616 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4617 {
4618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4619 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4620 }
4621
4622 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4623 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4624 {
4625 /* Silently ignore -- message too far in the future */
4626 MBEDTLS_SSL_DEBUG_MSG( 2,
4627 ( "Ignore future HS message with sequence number %u, "
4628 "buffering window %u - %u",
4629 recv_msg_seq, ssl->handshake->in_msg_seq,
4630 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4631
4632 goto exit;
4633 }
4634
4635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4636 recv_msg_seq, recv_msg_seq_offset ) );
4637
4638 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4639
4640 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004641 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004642 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004643 size_t reassembly_buf_sz;
4644
Hanno Becker37f95322018-08-16 13:55:32 +01004645 hs_buf->is_fragmented =
4646 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4647
4648 /* We copy the message back into the input buffer
4649 * after reassembly, so check that it's not too large.
4650 * This is an implementation-specific limitation
4651 * and not one from the standard, hence it is not
4652 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004653 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004654 {
4655 /* Ignore message */
4656 goto exit;
4657 }
4658
Hanno Beckere0b150f2018-08-21 15:51:03 +01004659 /* Check if we have enough space to buffer the message. */
4660 if( hs->buffering.total_bytes_buffered >
4661 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4662 {
4663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4665 }
4666
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004667 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4668 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004669
4670 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4671 hs->buffering.total_bytes_buffered ) )
4672 {
4673 if( recv_msg_seq_offset > 0 )
4674 {
4675 /* If we can't buffer a future message because
4676 * of space limitations -- ignore. */
4677 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",
4678 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4679 (unsigned) hs->buffering.total_bytes_buffered ) );
4680 goto exit;
4681 }
Hanno Beckere1801392018-08-21 16:51:05 +01004682 else
4683 {
4684 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",
4685 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4686 (unsigned) hs->buffering.total_bytes_buffered ) );
4687 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004688
Hanno Beckera02b0b42018-08-21 17:20:27 +01004689 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004690 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004691 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",
4692 (unsigned) msg_len,
4693 (unsigned) reassembly_buf_sz,
4694 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004695 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004696 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4697 goto exit;
4698 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004699 }
4700
4701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4702 msg_len ) );
4703
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004704 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4705 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004706 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004707 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004708 goto exit;
4709 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004710 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004711
4712 /* Prepare final header: copy msg_type, length and message_seq,
4713 * then add standardised fragment_offset and fragment_length */
4714 memcpy( hs_buf->data, ssl->in_msg, 6 );
4715 memset( hs_buf->data + 6, 0, 3 );
4716 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4717
4718 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004719
4720 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004721 }
4722 else
4723 {
4724 /* Make sure msg_type and length are consistent */
4725 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4728 /* Ignore */
4729 goto exit;
4730 }
4731 }
4732
Hanno Becker4422bbb2018-08-20 09:40:19 +01004733 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004734 {
4735 size_t frag_len, frag_off;
4736 unsigned char * const msg = hs_buf->data + 12;
4737
4738 /*
4739 * Check and copy current fragment
4740 */
4741
4742 /* Validation of header fields already done in
4743 * mbedtls_ssl_prepare_handshake_record(). */
4744 frag_off = ssl_get_hs_frag_off( ssl );
4745 frag_len = ssl_get_hs_frag_len( ssl );
4746
4747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4748 frag_off, frag_len ) );
4749 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4750
4751 if( hs_buf->is_fragmented )
4752 {
4753 unsigned char * const bitmask = msg + msg_len;
4754 ssl_bitmask_set( bitmask, frag_off, frag_len );
4755 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4756 msg_len ) == 0 );
4757 }
4758 else
4759 {
4760 hs_buf->is_complete = 1;
4761 }
4762
4763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4764 hs_buf->is_complete ? "" : "not yet " ) );
4765 }
4766
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004767 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004768 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004769
4770 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004771 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004772 break;
4773 }
4774
4775exit:
4776
4777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4778 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004779}
4780#endif /* MBEDTLS_SSL_PROTO_DTLS */
4781
Hanno Becker1097b342018-08-15 14:09:41 +01004782static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004783{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004784 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004785 * Consume last content-layer message and potentially
4786 * update in_msglen which keeps track of the contents'
4787 * consumption state.
4788 *
4789 * (1) Handshake messages:
4790 * Remove last handshake message, move content
4791 * and adapt in_msglen.
4792 *
4793 * (2) Alert messages:
4794 * Consume whole record content, in_msglen = 0.
4795 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004796 * (3) Change cipher spec:
4797 * Consume whole record content, in_msglen = 0.
4798 *
4799 * (4) Application data:
4800 * Don't do anything - the record layer provides
4801 * the application data as a stream transport
4802 * and consumes through mbedtls_ssl_read only.
4803 *
4804 */
4805
4806 /* Case (1): Handshake messages */
4807 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004809 /* Hard assertion to be sure that no application data
4810 * is in flight, as corrupting ssl->in_msglen during
4811 * ssl->in_offt != NULL is fatal. */
4812 if( ssl->in_offt != NULL )
4813 {
4814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4815 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4816 }
4817
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004818 /*
4819 * Get next Handshake message in the current record
4820 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004821
Hanno Becker4a810fb2017-05-24 16:27:30 +01004822 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004823 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004824 * current handshake content: If DTLS handshake
4825 * fragmentation is used, that's the fragment
4826 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004827 * size here is faulty and should be changed at
4828 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004829 * (2) While it doesn't seem to cause problems, one
4830 * has to be very careful not to assume that in_hslen
4831 * is always <= in_msglen in a sensible communication.
4832 * Again, it's wrong for DTLS handshake fragmentation.
4833 * The following check is therefore mandatory, and
4834 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004835 * Additionally, ssl->in_hslen might be arbitrarily out of
4836 * bounds after handling a DTLS message with an unexpected
4837 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004838 */
4839 if( ssl->in_hslen < ssl->in_msglen )
4840 {
4841 ssl->in_msglen -= ssl->in_hslen;
4842 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4843 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004844
Hanno Becker4a810fb2017-05-24 16:27:30 +01004845 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4846 ssl->in_msg, ssl->in_msglen );
4847 }
4848 else
4849 {
4850 ssl->in_msglen = 0;
4851 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004852
Hanno Becker4a810fb2017-05-24 16:27:30 +01004853 ssl->in_hslen = 0;
4854 }
4855 /* Case (4): Application data */
4856 else if( ssl->in_offt != NULL )
4857 {
4858 return( 0 );
4859 }
4860 /* Everything else (CCS & Alerts) */
4861 else
4862 {
4863 ssl->in_msglen = 0;
4864 }
4865
Hanno Becker1097b342018-08-15 14:09:41 +01004866 return( 0 );
4867}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004868
Hanno Beckere74d5562018-08-15 14:26:08 +01004869static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4870{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004871 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004872 return( 1 );
4873
4874 return( 0 );
4875}
4876
Hanno Becker5f066e72018-08-16 14:56:31 +01004877#if defined(MBEDTLS_SSL_PROTO_DTLS)
4878
4879static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4880{
4881 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4882 if( hs == NULL )
4883 return;
4884
Hanno Becker01315ea2018-08-21 17:22:17 +01004885 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004886 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004887 hs->buffering.total_bytes_buffered -=
4888 hs->buffering.future_record.len;
4889
4890 mbedtls_free( hs->buffering.future_record.data );
4891 hs->buffering.future_record.data = NULL;
4892 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004893}
4894
4895static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4896{
4897 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4898 unsigned char * rec;
4899 size_t rec_len;
4900 unsigned rec_epoch;
4901
4902 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4903 return( 0 );
4904
4905 if( hs == NULL )
4906 return( 0 );
4907
Hanno Becker5f066e72018-08-16 14:56:31 +01004908 rec = hs->buffering.future_record.data;
4909 rec_len = hs->buffering.future_record.len;
4910 rec_epoch = hs->buffering.future_record.epoch;
4911
4912 if( rec == NULL )
4913 return( 0 );
4914
Hanno Becker4cb782d2018-08-20 11:19:05 +01004915 /* Only consider loading future records if the
4916 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004917 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004918 return( 0 );
4919
Hanno Becker5f066e72018-08-16 14:56:31 +01004920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4921
4922 if( rec_epoch != ssl->in_epoch )
4923 {
4924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4925 goto exit;
4926 }
4927
4928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4929
4930 /* Double-check that the record is not too large */
4931 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4932 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4933 {
4934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4936 }
4937
4938 memcpy( ssl->in_hdr, rec, rec_len );
4939 ssl->in_left = rec_len;
4940 ssl->next_record_offset = 0;
4941
4942 ssl_free_buffered_record( ssl );
4943
4944exit:
4945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4946 return( 0 );
4947}
4948
4949static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4950{
4951 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4952 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004953 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004954
4955 /* Don't buffer future records outside handshakes. */
4956 if( hs == NULL )
4957 return( 0 );
4958
4959 /* Only buffer handshake records (we are only interested
4960 * in Finished messages). */
4961 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4962 return( 0 );
4963
4964 /* Don't buffer more than one future epoch record. */
4965 if( hs->buffering.future_record.data != NULL )
4966 return( 0 );
4967
Hanno Becker01315ea2018-08-21 17:22:17 +01004968 /* Don't buffer record if there's not enough buffering space remaining. */
4969 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4970 hs->buffering.total_bytes_buffered ) )
4971 {
4972 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",
4973 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4974 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004975 return( 0 );
4976 }
4977
Hanno Becker5f066e72018-08-16 14:56:31 +01004978 /* Buffer record */
4979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4980 ssl->in_epoch + 1 ) );
4981 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4982 rec_hdr_len + ssl->in_msglen );
4983
4984 /* ssl_parse_record_header() only considers records
4985 * of the next epoch as candidates for buffering. */
4986 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004987 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004988
4989 hs->buffering.future_record.data =
4990 mbedtls_calloc( 1, hs->buffering.future_record.len );
4991 if( hs->buffering.future_record.data == NULL )
4992 {
4993 /* If we run out of RAM trying to buffer a
4994 * record from the next epoch, just ignore. */
4995 return( 0 );
4996 }
4997
Hanno Becker01315ea2018-08-21 17:22:17 +01004998 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004999
Hanno Becker01315ea2018-08-21 17:22:17 +01005000 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005001 return( 0 );
5002}
5003
5004#endif /* MBEDTLS_SSL_PROTO_DTLS */
5005
Hanno Beckere74d5562018-08-15 14:26:08 +01005006static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005007{
5008 int ret;
5009
Hanno Becker5f066e72018-08-16 14:56:31 +01005010#if defined(MBEDTLS_SSL_PROTO_DTLS)
5011 /* We might have buffered a future record; if so,
5012 * and if the epoch matches now, load it.
5013 * On success, this call will set ssl->in_left to
5014 * the length of the buffered record, so that
5015 * the calls to ssl_fetch_input() below will
5016 * essentially be no-ops. */
5017 ret = ssl_load_buffered_record( ssl );
5018 if( ret != 0 )
5019 return( ret );
5020#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005025 return( ret );
5026 }
5027
5028 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5032 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005033 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005034 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5035 {
5036 ret = ssl_buffer_future_record( ssl );
5037 if( ret != 0 )
5038 return( ret );
5039
5040 /* Fall through to handling of unexpected records */
5041 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5042 }
5043
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005044 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5045 {
5046 /* Skip unexpected record (but not whole datagram) */
5047 ssl->next_record_offset = ssl->in_msglen
5048 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005049
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5051 "(header)" ) );
5052 }
5053 else
5054 {
5055 /* Skip invalid record and the rest of the datagram */
5056 ssl->next_record_offset = 0;
5057 ssl->in_left = 0;
5058
5059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5060 "(header)" ) );
5061 }
5062
5063 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005064 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005065 }
5066#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005067 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005068 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005069
5070 /*
5071 * Read and optionally decrypt the message contents
5072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5074 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005077 return( ret );
5078 }
5079
5080 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005085 if( ssl->next_record_offset < ssl->in_left )
5086 {
5087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5088 }
5089 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005090 else
5091#endif
5092 ssl->in_left = 0;
5093
5094 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005098 {
5099 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005100 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5101 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005102 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005103 /* Except when waiting for Finished as a bad mac here
5104 * probably means something went wrong in the handshake
5105 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5106 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5107 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5108 {
5109#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5110 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5111 {
5112 mbedtls_ssl_send_alert_message( ssl,
5113 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5114 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5115 }
5116#endif
5117 return( ret );
5118 }
5119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005121 if( ssl->conf->badmac_limit != 0 &&
5122 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5125 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005126 }
5127#endif
5128
Hanno Becker4a810fb2017-05-24 16:27:30 +01005129 /* As above, invalid records cause
5130 * dismissal of the whole datagram. */
5131
5132 ssl->next_record_offset = 0;
5133 ssl->in_left = 0;
5134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005136 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005137 }
5138
5139 return( ret );
5140 }
5141 else
5142#endif
5143 {
5144 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5146 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 mbedtls_ssl_send_alert_message( ssl,
5149 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5150 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005151 }
5152#endif
5153 return( ret );
5154 }
5155 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005156
Simon Butcher99000142016-10-13 17:21:01 +01005157 return( 0 );
5158}
5159
5160int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5161{
5162 int ret;
5163
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005164 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005165 * Handle particular types of records
5166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005168 {
Simon Butcher99000142016-10-13 17:21:01 +01005169 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5170 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005171 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005173 }
5174
Hanno Beckere678eaa2018-08-21 14:57:46 +01005175 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005176 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005177 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005178 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5180 ssl->in_msglen ) );
5181 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005182 }
5183
Hanno Beckere678eaa2018-08-21 14:57:46 +01005184 if( ssl->in_msg[0] != 1 )
5185 {
5186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5187 ssl->in_msg[0] ) );
5188 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5189 }
5190
5191#if defined(MBEDTLS_SSL_PROTO_DTLS)
5192 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5193 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5194 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5195 {
5196 if( ssl->handshake == NULL )
5197 {
5198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5199 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5200 }
5201
5202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5203 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5204 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005205#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005206 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005209 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005210 if( ssl->in_msglen != 2 )
5211 {
5212 /* Note: Standard allows for more than one 2 byte alert
5213 to be packed in a single message, but Mbed TLS doesn't
5214 currently support this. */
5215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5216 ssl->in_msglen ) );
5217 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5218 }
5219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005221 ssl->in_msg[0], ssl->in_msg[1] ) );
5222
5223 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005224 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005229 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005231 }
5232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5234 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5237 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005238 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005239
5240#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5241 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5242 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5243 {
Hanno Becker90333da2017-10-10 11:27:13 +01005244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005245 /* Will be handled when trying to parse ServerHello */
5246 return( 0 );
5247 }
5248#endif
5249
5250#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5251 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5252 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5253 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5254 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5255 {
5256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5257 /* Will be handled in mbedtls_ssl_parse_certificate() */
5258 return( 0 );
5259 }
5260#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5261
5262 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005263 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005264 }
5265
Hanno Beckerc76c6192017-06-06 10:03:17 +01005266#if defined(MBEDTLS_SSL_PROTO_DTLS)
5267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5268 ssl->handshake != NULL &&
5269 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5270 {
5271 ssl_handshake_wrapup_free_hs_transform( ssl );
5272 }
5273#endif
5274
Paul Bakker5121ce52009-01-03 21:22:43 +00005275 return( 0 );
5276}
5277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005279{
5280 int ret;
5281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5283 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5284 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005285 {
5286 return( ret );
5287 }
5288
5289 return( 0 );
5290}
5291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005293 unsigned char level,
5294 unsigned char message )
5295{
5296 int ret;
5297
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005298 if( ssl == NULL || ssl->conf == NULL )
5299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005305 ssl->out_msglen = 2;
5306 ssl->out_msg[0] = level;
5307 ssl->out_msg[1] = message;
5308
Hanno Becker67bc7c32018-08-06 11:33:50 +01005309 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005312 return( ret );
5313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005315
5316 return( 0 );
5317}
5318
Paul Bakker5121ce52009-01-03 21:22:43 +00005319/*
5320 * Handshake functions
5321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5323 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5324 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5325 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5326 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5327 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5328 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005329/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5337 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005338 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5339 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005342 ssl->state++;
5343 return( 0 );
5344 }
5345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5347 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005348}
5349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5357 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005358 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5359 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005362 ssl->state++;
5363 return( 0 );
5364 }
5365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5367 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005368}
Gilles Peskinef9828522017-05-03 12:28:43 +02005369
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005370#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005371/* Some certificate support -> implement write and parse */
5372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005374{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005376 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 const mbedtls_x509_crt *crt;
5378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5383 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005384 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5385 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005388 ssl->state++;
5389 return( 0 );
5390 }
5391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005393 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005394 {
5395 if( ssl->client_auth == 0 )
5396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 ssl->state++;
5399 return( 0 );
5400 }
5401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005403 /*
5404 * If using SSLv3 and got no cert, send an Alert message
5405 * (otherwise an empty Certificate message will be sent).
5406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5408 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005409 {
5410 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5412 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5413 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005416 goto write_msg;
5417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420#endif /* MBEDTLS_SSL_CLI_C */
5421#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005422 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5427 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005428 }
5429 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005430#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005433
5434 /*
5435 * 0 . 0 handshake type
5436 * 1 . 3 handshake length
5437 * 4 . 6 length of all certs
5438 * 7 . 9 length of cert. 1
5439 * 10 . n-1 peer certificate
5440 * n . n+2 length of cert. 2
5441 * n+3 . ... upper level cert, etc.
5442 */
5443 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005445
Paul Bakker29087132010-03-21 21:03:34 +00005446 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005447 {
5448 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005449 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005452 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 }
5455
5456 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5457 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5458 ssl->out_msg[i + 2] = (unsigned char)( n );
5459
5460 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5461 i += n; crt = crt->next;
5462 }
5463
5464 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5465 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5466 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5467
5468 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5470 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005471
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005472#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005473write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005475
5476 ssl->state++;
5477
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005478 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 return( ret );
5482 }
5483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005485
Paul Bakkered27a042013-04-18 22:46:23 +02005486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005487}
5488
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005489/*
5490 * Once the certificate message is read, parse it into a cert chain and
5491 * perform basic checks, but leave actual verification to the caller
5492 */
5493static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005494{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005495 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005496 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005497 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499#if defined(MBEDTLS_SSL_SRV_C)
5500#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 /*
5502 * Check if the client sent an empty certificate
5503 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005504 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005507 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5509 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5510 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005514 /* The client was asked for a certificate but didn't send
5515 one. The client should know what's going on, so we
5516 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005517 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005518 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 }
5520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5524 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005525 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5529 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5530 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5531 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005534
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005535 /* The client was asked for a certificate but didn't send
5536 one. The client should know what's going on, so we
5537 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005538 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005539 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005540 }
5541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5543 MBEDTLS_SSL_PROTO_TLS1_2 */
5544#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005549 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5550 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005552 }
5553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5555 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005558 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5559 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005561 }
5562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005564
Paul Bakker5121ce52009-01-03 21:22:43 +00005565 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005568 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005569
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005570 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005574 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5575 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 }
5578
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005579 /* In case we tried to reuse a session but it failed */
5580 if( ssl->session_negotiate->peer_cert != NULL )
5581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5583 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005584 }
5585
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005586 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005588 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5592 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005593 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 }
5595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005598 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005599
5600 while( i < ssl->in_hslen )
5601 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005602 if ( i + 3 > ssl->in_hslen ) {
5603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5604 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5605 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5606 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 if( ssl->in_msg[i] != 0 )
5609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005611 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5612 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005614 }
5615
5616 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5617 | (unsigned int) ssl->in_msg[i + 2];
5618 i += 3;
5619
5620 if( n < 128 || i + n > ssl->in_hslen )
5621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005623 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5624 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 }
5627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005629 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005630 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005631 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005632 case 0: /*ok*/
5633 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5634 /* Ignore certificate with an unknown algorithm: maybe a
5635 prior certificate was already trusted. */
5636 break;
5637
5638 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5639 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5640 goto crt_parse_der_failed;
5641
5642 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5643 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5644 goto crt_parse_der_failed;
5645
5646 default:
5647 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5648 crt_parse_der_failed:
5649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005651 return( ret );
5652 }
5653
5654 i += n;
5655 }
5656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005658
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005659 /*
5660 * On client, make sure the server cert doesn't change during renego to
5661 * avoid "triple handshake" attack: https://secure-resumption.com/
5662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005664 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005666 {
5667 if( ssl->session->peer_cert == NULL )
5668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005670 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5671 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005673 }
5674
5675 if( ssl->session->peer_cert->raw.len !=
5676 ssl->session_negotiate->peer_cert->raw.len ||
5677 memcmp( ssl->session->peer_cert->raw.p,
5678 ssl->session_negotiate->peer_cert->raw.p,
5679 ssl->session->peer_cert->raw.len ) != 0 )
5680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005682 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5683 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005685 }
5686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005688
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005689 return( 0 );
5690}
5691
5692int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5693{
5694 int ret;
5695 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5696 ssl->transform_negotiate->ciphersuite_info;
5697#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5698 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5699 ? ssl->handshake->sni_authmode
5700 : ssl->conf->authmode;
5701#else
5702 const int authmode = ssl->conf->authmode;
5703#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005704 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005705
5706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5707
5708 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5709 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5710 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5711 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5712 {
5713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5714 ssl->state++;
5715 return( 0 );
5716 }
5717
5718#if defined(MBEDTLS_SSL_SRV_C)
5719 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5720 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5721 {
5722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5723 ssl->state++;
5724 return( 0 );
5725 }
5726
5727 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5728 authmode == MBEDTLS_SSL_VERIFY_NONE )
5729 {
5730 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005732
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005733 ssl->state++;
5734 return( 0 );
5735 }
5736#endif
5737
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005738#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5739 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005740 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005741 {
5742 goto crt_verify;
5743 }
5744#endif
5745
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005746 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005747 {
5748 /* mbedtls_ssl_read_record may have sent an alert already. We
5749 let it decide whether to alert. */
5750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5751 return( ret );
5752 }
5753
5754 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5755 {
5756#if defined(MBEDTLS_SSL_SRV_C)
5757 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5758 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5759 {
5760 ret = 0;
5761 }
5762#endif
5763
5764 ssl->state++;
5765 return( ret );
5766 }
5767
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005768#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5769 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005770 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005771
5772crt_verify:
5773 if( ssl->handshake->ecrs_enabled)
5774 rs_ctx = &ssl->handshake->ecrs_ctx;
5775#endif
5776
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005777 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005778 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005779 mbedtls_x509_crt *ca_chain;
5780 mbedtls_x509_crl *ca_crl;
5781
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005782#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005783 if( ssl->handshake->sni_ca_chain != NULL )
5784 {
5785 ca_chain = ssl->handshake->sni_ca_chain;
5786 ca_crl = ssl->handshake->sni_ca_crl;
5787 }
5788 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005789#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005790 {
5791 ca_chain = ssl->conf->ca_chain;
5792 ca_crl = ssl->conf->ca_crl;
5793 }
5794
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005795 /*
5796 * Main check: verify certificate
5797 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005798 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005799 ssl->session_negotiate->peer_cert,
5800 ca_chain, ca_crl,
5801 ssl->conf->cert_profile,
5802 ssl->hostname,
5803 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005804 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005805
5806 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005809 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005810
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005811#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5812 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005813 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005814#endif
5815
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005816 /*
5817 * Secondary checks: always done, but change 'ret' only if it was 0
5818 */
5819
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005820#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005823
5824 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005826 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005827 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005828 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005831 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005833 }
5834 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005835#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005838 ciphersuite_info,
5839 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005840 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005843 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005845 }
5846
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005847 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5848 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5849 * with details encoded in the verification flags. All other kinds
5850 * of error codes, including those from the user provided f_vrfy
5851 * functions, are treated as fatal and lead to a failure of
5852 * ssl_parse_certificate even if verification was optional. */
5853 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5854 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5855 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5856 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005857 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005858 }
5859
5860 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5861 {
5862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5863 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5864 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005865
5866 if( ret != 0 )
5867 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005868 uint8_t alert;
5869
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005870 /* The certificate may have been rejected for several reasons.
5871 Pick one and send the corresponding alert. Which alert to send
5872 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005873 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5874 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5875 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5876 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5877 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5878 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5879 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5880 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5881 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5882 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5883 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5884 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5885 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5886 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5887 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5888 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5889 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5890 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5891 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5892 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005893 else
5894 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5896 alert );
5897 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005898
Hanno Beckere6706e62017-05-15 16:05:15 +01005899#if defined(MBEDTLS_DEBUG_C)
5900 if( ssl->session_negotiate->verify_result != 0 )
5901 {
5902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5903 ssl->session_negotiate->verify_result ) );
5904 }
5905 else
5906 {
5907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5908 }
5909#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005910 }
5911
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005912 ssl->state++;
5913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005915
5916 return( ret );
5917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5919 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5920 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5921 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5922 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5923 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5924 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005927{
5928 int ret;
5929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 ssl->out_msglen = 1;
5934 ssl->out_msg[0] = 1;
5935
Paul Bakker5121ce52009-01-03 21:22:43 +00005936 ssl->state++;
5937
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005938 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005939 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005941 return( ret );
5942 }
5943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005945
5946 return( 0 );
5947}
5948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005950{
5951 int ret;
5952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005954
Hanno Becker327c93b2018-08-15 13:56:18 +01005955 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005958 return( ret );
5959 }
5960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005964 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5965 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005967 }
5968
Hanno Beckere678eaa2018-08-21 14:57:46 +01005969 /* CCS records are only accepted if they have length 1 and content '1',
5970 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005971
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005972 /*
5973 * Switch to our negotiated transform and session parameters for inbound
5974 * data.
5975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005977 ssl->transform_in = ssl->transform_negotiate;
5978 ssl->session_in = ssl->session_negotiate;
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005984 ssl_dtls_replay_reset( ssl );
5985#endif
5986
5987 /* Increment epoch */
5988 if( ++ssl->in_epoch == 0 )
5989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005991 /* This is highly unlikely to happen for legitimate reasons, so
5992 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005994 }
5995 }
5996 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005998 memset( ssl->in_ctr, 0, 8 );
5999
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006000 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6003 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006008 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6009 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006011 }
6012 }
6013#endif
6014
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 ssl->state++;
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006018
6019 return( 0 );
6020}
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6023 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006024{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006025 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6028 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6029 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006030 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006031 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6034#if defined(MBEDTLS_SHA512_C)
6035 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006036 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6037 else
6038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#if defined(MBEDTLS_SHA256_C)
6040 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006041 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006042 else
6043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006047 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006048 }
Paul Bakker380da532012-04-18 16:10:25 +00006049}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6054 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006055 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6056 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6059#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006060 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006063 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006066}
6067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006069 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006070{
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)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006073 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6074 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6077#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006078 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006081 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006084}
6085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6087 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6088static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006089 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006090{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006091 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6092 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006093}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006094#endif
Paul Bakker380da532012-04-18 16:10:25 +00006095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6097#if defined(MBEDTLS_SHA256_C)
6098static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006099 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006100{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006101 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006102}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006103#endif
Paul Bakker380da532012-04-18 16:10:25 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105#if defined(MBEDTLS_SHA512_C)
6106static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006107 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006108{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006109 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006110}
Paul Bakker769075d2012-11-24 11:26:46 +01006111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006115static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006117{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006118 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006119 mbedtls_md5_context md5;
6120 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006121
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 unsigned char padbuf[48];
6123 unsigned char md5sum[16];
6124 unsigned char sha1sum[20];
6125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006127 if( !session )
6128 session = ssl->session;
6129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006131
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006132 mbedtls_md5_init( &md5 );
6133 mbedtls_sha1_init( &sha1 );
6134
6135 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6136 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
6138 /*
6139 * SSLv3:
6140 * hash =
6141 * MD5( master + pad2 +
6142 * MD5( handshake + sender + master + pad1 ) )
6143 * + SHA1( master + pad2 +
6144 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 */
6146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006148 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6149 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006153 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6154 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006155#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006158 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006159
Paul Bakker1ef83d62012-04-11 12:09:53 +00006160 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006162 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6163 mbedtls_md5_update_ret( &md5, session->master, 48 );
6164 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6165 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006166
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006167 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6168 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6169 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6170 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Paul Bakker1ef83d62012-04-11 12:09:53 +00006172 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006173
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006174 mbedtls_md5_starts_ret( &md5 );
6175 mbedtls_md5_update_ret( &md5, session->master, 48 );
6176 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6177 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6178 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006179
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006180 mbedtls_sha1_starts_ret( &sha1 );
6181 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6182 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6183 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6184 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006187
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006188 mbedtls_md5_free( &md5 );
6189 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006190
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006191 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6192 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6193 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006200static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006202{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006203 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006204 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006205 mbedtls_md5_context md5;
6206 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006207 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006210 if( !session )
6211 session = ssl->session;
6212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006214
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006215 mbedtls_md5_init( &md5 );
6216 mbedtls_sha1_init( &sha1 );
6217
6218 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6219 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006220
Paul Bakker1ef83d62012-04-11 12:09:53 +00006221 /*
6222 * TLSv1:
6223 * hash = PRF( master, finished_label,
6224 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6225 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006228 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6229 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006230#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006233 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6234 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006235#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006238 ? "client finished"
6239 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006240
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006241 mbedtls_md5_finish_ret( &md5, padbuf );
6242 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006243
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006244 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006245 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006248
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006249 mbedtls_md5_free( &md5 );
6250 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006251
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006252 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6259#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006260static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006262{
6263 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006264 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006265 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006266 unsigned char padbuf[32];
6267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006269 if( !session )
6270 session = ssl->session;
6271
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006272 mbedtls_sha256_init( &sha256 );
6273
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006275
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006276 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006277
6278 /*
6279 * TLSv1.2:
6280 * hash = PRF( master, finished_label,
6281 * Hash( handshake ) )[0.11]
6282 */
6283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284#if !defined(MBEDTLS_SHA256_ALT)
6285 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006286 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006287#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006290 ? "client finished"
6291 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006292
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006293 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006294
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006295 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006296 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006299
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006300 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006301
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006302 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006305}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006309static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006311{
6312 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006313 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006314 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006315 unsigned char padbuf[48];
6316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006318 if( !session )
6319 session = ssl->session;
6320
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006321 mbedtls_sha512_init( &sha512 );
6322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006324
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006325 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006326
6327 /*
6328 * TLSv1.2:
6329 * hash = PRF( master, finished_label,
6330 * Hash( handshake ) )[0.11]
6331 */
6332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006334 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6335 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006336#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006339 ? "client finished"
6340 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006341
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006342 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006343
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006344 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006345 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006348
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006349 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006350
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006351 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355#endif /* MBEDTLS_SHA512_C */
6356#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006361
6362 /*
6363 * Free our handshake params
6364 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006365 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006367 ssl->handshake = NULL;
6368
6369 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006370 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006371 */
6372 if( ssl->transform )
6373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 mbedtls_ssl_transform_free( ssl->transform );
6375 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006376 }
6377 ssl->transform = ssl->transform_negotiate;
6378 ssl->transform_negotiate = NULL;
6379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006381}
6382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006384{
6385 int resume = ssl->handshake->resume;
6386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389#if defined(MBEDTLS_SSL_RENEGOTIATION)
6390 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006393 ssl->renego_records_seen = 0;
6394 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006395#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006396
6397 /*
6398 * Free the previous session and switch in the current one
6399 */
Paul Bakker0a597072012-09-25 21:55:46 +00006400 if( ssl->session )
6401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006403 /* RFC 7366 3.1: keep the EtM state */
6404 ssl->session_negotiate->encrypt_then_mac =
6405 ssl->session->encrypt_then_mac;
6406#endif
6407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 mbedtls_ssl_session_free( ssl->session );
6409 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006410 }
6411 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006412 ssl->session_negotiate = NULL;
6413
Paul Bakker0a597072012-09-25 21:55:46 +00006414 /*
6415 * Add cache entry
6416 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006417 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006418 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006419 resume == 0 )
6420 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006421 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006423 }
Paul Bakker0a597072012-09-25 21:55:46 +00006424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006426 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006427 ssl->handshake->flight != NULL )
6428 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006429 /* Cancel handshake timer */
6430 ssl_set_timer( ssl, 0 );
6431
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006432 /* Keep last flight around in case we need to resend it:
6433 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006435 }
6436 else
6437#endif
6438 ssl_handshake_wrapup_free_hs_transform( ssl );
6439
Paul Bakker48916f92012-09-16 19:57:18 +00006440 ssl->state++;
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006443}
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006446{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006447 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006450
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006451 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006452
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006453 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006454
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006455 /*
6456 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6457 * may define some other value. Currently (early 2016), no defined
6458 * ciphersuite does this (and this is unlikely to change as activity has
6459 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006464 ssl->verify_data_len = hash_len;
6465 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006466#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006467
Paul Bakker5121ce52009-01-03 21:22:43 +00006468 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6470 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006471
6472 /*
6473 * In case of session resuming, invert the client and server
6474 * ChangeCipherSpec messages order.
6475 */
Paul Bakker0a597072012-09-25 21:55:46 +00006476 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006479 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006481#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006483 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 }
6487 else
6488 ssl->state++;
6489
Paul Bakker48916f92012-09-16 19:57:18 +00006490 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006491 * Switch to our negotiated transform and session parameters for outbound
6492 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006497 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006498 {
6499 unsigned char i;
6500
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006501 /* Remember current epoch settings for resending */
6502 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006503 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006504
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006505 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006506 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006507
6508 /* Increment epoch */
6509 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006510 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006511 break;
6512
6513 /* The loop goes to its end iff the counter is wrapping */
6514 if( i == 0 )
6515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6517 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006518 }
6519 }
6520 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006522 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006523
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006524 ssl->transform_out = ssl->transform_negotiate;
6525 ssl->session_out = ssl->session_negotiate;
6526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6528 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6533 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006534 }
6535 }
6536#endif
6537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006539 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006541#endif
6542
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006543 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006545 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 return( ret );
6547 }
6548
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006549#if defined(MBEDTLS_SSL_PROTO_DTLS)
6550 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6551 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6552 {
6553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6554 return( ret );
6555 }
6556#endif
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006559
6560 return( 0 );
6561}
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006564#define SSL_MAX_HASH_LEN 36
6565#else
6566#define SSL_MAX_HASH_LEN 12
6567#endif
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006570{
Paul Bakker23986e52011-04-24 08:57:21 +00006571 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006572 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006573 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006576
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006577 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006578
Hanno Becker327c93b2018-08-15 13:56:18 +01006579 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 return( ret );
6583 }
6584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006588 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6589 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 }
6592
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006593 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#if defined(MBEDTLS_SSL_PROTO_SSL3)
6595 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006596 hash_len = 36;
6597 else
6598#endif
6599 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6602 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006605 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6606 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 }
6609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006611 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006614 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6615 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 }
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006620 ssl->verify_data_len = hash_len;
6621 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006622#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006623
Paul Bakker0a597072012-09-25 21:55:46 +00006624 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006627 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006633#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006634 }
6635 else
6636 ssl->state++;
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006639 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006641#endif
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006644
6645 return( 0 );
6646}
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6653 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6654 mbedtls_md5_init( &handshake->fin_md5 );
6655 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006656 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6657 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006658#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6660#if defined(MBEDTLS_SHA256_C)
6661 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006662 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664#if defined(MBEDTLS_SHA512_C)
6665 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006666 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006667#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006669
6670 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006671
6672#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6673 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6674 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6675#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677#if defined(MBEDTLS_DHM_C)
6678 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006679#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#if defined(MBEDTLS_ECDH_C)
6681 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006682#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006683#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006684 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006685#if defined(MBEDTLS_SSL_CLI_C)
6686 handshake->ecjpake_cache = NULL;
6687 handshake->ecjpake_cache_len = 0;
6688#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006689#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006690
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006691#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006692 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006693#endif
6694
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006695#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6696 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6697#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006698}
6699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6705 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 mbedtls_md_init( &transform->md_ctx_enc );
6708 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006709}
6710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006714}
6715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006717{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006718 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006719 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006721 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006723 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006724 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006725
6726 /*
6727 * Either the pointers are now NULL or cleared properly and can be freed.
6728 * Now allocate missing structures.
6729 */
6730 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006731 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006732 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006733 }
Paul Bakker48916f92012-09-16 19:57:18 +00006734
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006735 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006736 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006737 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006738 }
Paul Bakker48916f92012-09-16 19:57:18 +00006739
Paul Bakker82788fb2014-10-20 13:59:19 +02006740 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006742 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006743 }
Paul Bakker48916f92012-09-16 19:57:18 +00006744
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006745 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006746 if( ssl->handshake == NULL ||
6747 ssl->transform_negotiate == NULL ||
6748 ssl->session_negotiate == NULL )
6749 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 mbedtls_free( ssl->handshake );
6753 mbedtls_free( ssl->transform_negotiate );
6754 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006755
6756 ssl->handshake = NULL;
6757 ssl->transform_negotiate = NULL;
6758 ssl->session_negotiate = NULL;
6759
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006760 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006761 }
6762
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006763 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006765 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006766 ssl_handshake_params_init( ssl->handshake );
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006769 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6770 {
6771 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006772
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006773 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6774 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6775 else
6776 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006777
6778 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006779 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006780#endif
6781
Paul Bakker48916f92012-09-16 19:57:18 +00006782 return( 0 );
6783}
6784
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006785#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006786/* Dummy cookie callbacks for defaults */
6787static int ssl_cookie_write_dummy( void *ctx,
6788 unsigned char **p, unsigned char *end,
6789 const unsigned char *cli_id, size_t cli_id_len )
6790{
6791 ((void) ctx);
6792 ((void) p);
6793 ((void) end);
6794 ((void) cli_id);
6795 ((void) cli_id_len);
6796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006798}
6799
6800static int ssl_cookie_check_dummy( void *ctx,
6801 const unsigned char *cookie, size_t cookie_len,
6802 const unsigned char *cli_id, size_t cli_id_len )
6803{
6804 ((void) ctx);
6805 ((void) cookie);
6806 ((void) cookie_len);
6807 ((void) cli_id);
6808 ((void) cli_id_len);
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006811}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006812#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006813
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006814/* Once ssl->out_hdr as the address of the beginning of the
6815 * next outgoing record is set, deduce the other pointers.
6816 *
6817 * Note: For TLS, we save the implicit record sequence number
6818 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6819 * and the caller has to make sure there's space for this.
6820 */
6821
6822static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6823 mbedtls_ssl_transform *transform )
6824{
6825#if defined(MBEDTLS_SSL_PROTO_DTLS)
6826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6827 {
6828 ssl->out_ctr = ssl->out_hdr + 3;
6829 ssl->out_len = ssl->out_hdr + 11;
6830 ssl->out_iv = ssl->out_hdr + 13;
6831 }
6832 else
6833#endif
6834 {
6835 ssl->out_ctr = ssl->out_hdr - 8;
6836 ssl->out_len = ssl->out_hdr + 3;
6837 ssl->out_iv = ssl->out_hdr + 5;
6838 }
6839
6840 /* Adjust out_msg to make space for explicit IV, if used. */
6841 if( transform != NULL &&
6842 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6843 {
6844 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6845 }
6846 else
6847 ssl->out_msg = ssl->out_iv;
6848}
6849
6850/* Once ssl->in_hdr as the address of the beginning of the
6851 * next incoming record is set, deduce the other pointers.
6852 *
6853 * Note: For TLS, we save the implicit record sequence number
6854 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6855 * and the caller has to make sure there's space for this.
6856 */
6857
6858static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6859 mbedtls_ssl_transform *transform )
6860{
6861#if defined(MBEDTLS_SSL_PROTO_DTLS)
6862 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6863 {
6864 ssl->in_ctr = ssl->in_hdr + 3;
6865 ssl->in_len = ssl->in_hdr + 11;
6866 ssl->in_iv = ssl->in_hdr + 13;
6867 }
6868 else
6869#endif
6870 {
6871 ssl->in_ctr = ssl->in_hdr - 8;
6872 ssl->in_len = ssl->in_hdr + 3;
6873 ssl->in_iv = ssl->in_hdr + 5;
6874 }
6875
6876 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6877 if( transform != NULL &&
6878 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6879 {
6880 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6881 }
6882 else
6883 ssl->in_msg = ssl->in_iv;
6884}
6885
Paul Bakker5121ce52009-01-03 21:22:43 +00006886/*
6887 * Initialize an SSL context
6888 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006889void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6890{
6891 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6892}
6893
6894/*
6895 * Setup an SSL context
6896 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006897
6898static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6899{
6900 /* Set the incoming and outgoing record pointers. */
6901#if defined(MBEDTLS_SSL_PROTO_DTLS)
6902 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6903 {
6904 ssl->out_hdr = ssl->out_buf;
6905 ssl->in_hdr = ssl->in_buf;
6906 }
6907 else
6908#endif /* MBEDTLS_SSL_PROTO_DTLS */
6909 {
6910 ssl->out_hdr = ssl->out_buf + 8;
6911 ssl->in_hdr = ssl->in_buf + 8;
6912 }
6913
6914 /* Derive other internal pointers. */
6915 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6916 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6917}
6918
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006919int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006920 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006921{
Paul Bakker48916f92012-09-16 19:57:18 +00006922 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006923
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006924 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006925
6926 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006927 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006928 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006929
6930 /* Set to NULL in case of an error condition */
6931 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006932
Angus Grattond8213d02016-05-25 20:56:48 +10006933 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6934 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006935 {
Angus Grattond8213d02016-05-25 20:56:48 +10006936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006937 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006938 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006939 }
6940
6941 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6942 if( ssl->out_buf == NULL )
6943 {
6944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006945 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006946 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 }
6948
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006949 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006950
Paul Bakker48916f92012-09-16 19:57:18 +00006951 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006952 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006953
6954 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006955
6956error:
6957 mbedtls_free( ssl->in_buf );
6958 mbedtls_free( ssl->out_buf );
6959
6960 ssl->conf = NULL;
6961
6962 ssl->in_buf = NULL;
6963 ssl->out_buf = NULL;
6964
6965 ssl->in_hdr = NULL;
6966 ssl->in_ctr = NULL;
6967 ssl->in_len = NULL;
6968 ssl->in_iv = NULL;
6969 ssl->in_msg = NULL;
6970
6971 ssl->out_hdr = NULL;
6972 ssl->out_ctr = NULL;
6973 ssl->out_len = NULL;
6974 ssl->out_iv = NULL;
6975 ssl->out_msg = NULL;
6976
k-stachowiak9f7798e2018-07-31 16:52:32 +02006977 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006978}
6979
6980/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006981 * Reset an initialized and used SSL context for re-use while retaining
6982 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006983 *
6984 * If partial is non-zero, keep data in the input buffer and client ID.
6985 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006986 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006987static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006988{
Paul Bakker48916f92012-09-16 19:57:18 +00006989 int ret;
6990
Hanno Becker7e772132018-08-10 12:38:21 +01006991#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6992 !defined(MBEDTLS_SSL_SRV_C)
6993 ((void) partial);
6994#endif
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006997
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006998 /* Cancel any possibly running timer */
6999 ssl_set_timer( ssl, 0 );
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001#if defined(MBEDTLS_SSL_RENEGOTIATION)
7002 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007003 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007004
7005 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7007 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007010
Paul Bakker7eb013f2011-10-06 12:37:39 +00007011 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007012 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007013
7014 ssl->in_msgtype = 0;
7015 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007017 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007018 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007021 ssl_dtls_replay_reset( ssl );
7022#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007023
7024 ssl->in_hslen = 0;
7025 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007026
7027 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007028
7029 ssl->out_msgtype = 0;
7030 ssl->out_msglen = 0;
7031 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7033 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007034 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007035#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007036
Hanno Becker19859472018-08-06 09:40:20 +01007037 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7038
Paul Bakker48916f92012-09-16 19:57:18 +00007039 ssl->transform_in = NULL;
7040 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007041
Hanno Becker78640902018-08-13 16:35:15 +01007042 ssl->session_in = NULL;
7043 ssl->session_out = NULL;
7044
Angus Grattond8213d02016-05-25 20:56:48 +10007045 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007046
7047#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007048 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007049#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7050 {
7051 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007052 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007053 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7056 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7059 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7062 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007063 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007064 }
7065#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007066
Paul Bakker48916f92012-09-16 19:57:18 +00007067 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069 mbedtls_ssl_transform_free( ssl->transform );
7070 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007071 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007072 }
Paul Bakker48916f92012-09-16 19:57:18 +00007073
Paul Bakkerc0463502013-02-14 11:19:38 +01007074 if( ssl->session )
7075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076 mbedtls_ssl_session_free( ssl->session );
7077 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007078 ssl->session = NULL;
7079 }
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007082 ssl->alpn_chosen = NULL;
7083#endif
7084
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007085#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007086#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007087 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007088#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007089 {
7090 mbedtls_free( ssl->cli_id );
7091 ssl->cli_id = NULL;
7092 ssl->cli_id_len = 0;
7093 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007094#endif
7095
Paul Bakker48916f92012-09-16 19:57:18 +00007096 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7097 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007098
7099 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007100}
7101
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007102/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007103 * Reset an initialized and used SSL context for re-use while retaining
7104 * all application-set variables, function pointers and data.
7105 */
7106int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7107{
7108 return( ssl_session_reset_int( ssl, 0 ) );
7109}
7110
7111/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007112 * SSL set accessors
7113 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007114void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007116 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007117}
7118
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007119void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007121 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007122}
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007125void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007127 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007128}
7129#endif
7130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007132void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007133{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007134 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007135}
7136#endif
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007139
Hanno Becker1841b0a2018-08-24 11:13:57 +01007140void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7141 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007142{
7143 ssl->disable_datagram_packing = !allow_packing;
7144}
7145
7146void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7147 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007148{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007149 conf->hs_timeout_min = min;
7150 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007151}
7152#endif
7153
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007154void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007155{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007156 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007157}
7158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007160void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007161 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007162 void *p_vrfy )
7163{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007164 conf->f_vrfy = f_vrfy;
7165 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007169void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007170 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007171 void *p_rng )
7172{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007173 conf->f_rng = f_rng;
7174 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007175}
7176
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007177void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007178 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007179 void *p_dbg )
7180{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007181 conf->f_dbg = f_dbg;
7182 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007183}
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007186 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007187 mbedtls_ssl_send_t *f_send,
7188 mbedtls_ssl_recv_t *f_recv,
7189 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007190{
7191 ssl->p_bio = p_bio;
7192 ssl->f_send = f_send;
7193 ssl->f_recv = f_recv;
7194 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007195}
7196
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007197#if defined(MBEDTLS_SSL_PROTO_DTLS)
7198void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7199{
7200 ssl->mtu = mtu;
7201}
7202#endif
7203
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007204void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007205{
7206 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007207}
7208
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007209void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7210 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007211 mbedtls_ssl_set_timer_t *f_set_timer,
7212 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007213{
7214 ssl->p_timer = p_timer;
7215 ssl->f_set_timer = f_set_timer;
7216 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007217
7218 /* Make sure we start with no timer running */
7219 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007220}
7221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007223void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007224 void *p_cache,
7225 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7226 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007227{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007228 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007229 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007230 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007231}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234#if defined(MBEDTLS_SSL_CLI_C)
7235int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007236{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007237 int ret;
7238
7239 if( ssl == NULL ||
7240 session == NULL ||
7241 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007242 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007245 }
7246
7247 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7248 return( ret );
7249
Paul Bakker0a597072012-09-25 21:55:46 +00007250 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007251
7252 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007255
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007256void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007257 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007259 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7260 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7261 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7262 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007263}
7264
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007265void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007266 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007267 int major, int minor )
7268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007270 return;
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007273 return;
7274
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007275 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007276}
7277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007279void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007280 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007281{
7282 conf->cert_profile = profile;
7283}
7284
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007285/* Append a new keycert entry to a (possibly empty) list */
7286static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7287 mbedtls_x509_crt *cert,
7288 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007289{
niisato8ee24222018-06-25 19:05:48 +09007290 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007291
niisato8ee24222018-06-25 19:05:48 +09007292 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7293 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007295
niisato8ee24222018-06-25 19:05:48 +09007296 new_cert->cert = cert;
7297 new_cert->key = key;
7298 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007299
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007300 /* Update head is the list was null, else add to the end */
7301 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007302 {
niisato8ee24222018-06-25 19:05:48 +09007303 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007304 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007305 else
7306 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007307 mbedtls_ssl_key_cert *cur = *head;
7308 while( cur->next != NULL )
7309 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007310 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007311 }
7312
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007313 return( 0 );
7314}
7315
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007316int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007317 mbedtls_x509_crt *own_cert,
7318 mbedtls_pk_context *pk_key )
7319{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007320 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007321}
7322
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007323void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007324 mbedtls_x509_crt *ca_chain,
7325 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007326{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007327 conf->ca_chain = ca_chain;
7328 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007331
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007332#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7333int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7334 mbedtls_x509_crt *own_cert,
7335 mbedtls_pk_context *pk_key )
7336{
7337 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7338 own_cert, pk_key ) );
7339}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007340
7341void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7342 mbedtls_x509_crt *ca_chain,
7343 mbedtls_x509_crl *ca_crl )
7344{
7345 ssl->handshake->sni_ca_chain = ca_chain;
7346 ssl->handshake->sni_ca_crl = ca_crl;
7347}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007348
7349void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7350 int authmode )
7351{
7352 ssl->handshake->sni_authmode = authmode;
7353}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007354#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7355
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007356#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007357/*
7358 * Set EC J-PAKE password for current handshake
7359 */
7360int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7361 const unsigned char *pw,
7362 size_t pw_len )
7363{
7364 mbedtls_ecjpake_role role;
7365
Janos Follath8eb64132016-06-03 15:40:57 +01007366 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7368
7369 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7370 role = MBEDTLS_ECJPAKE_SERVER;
7371 else
7372 role = MBEDTLS_ECJPAKE_CLIENT;
7373
7374 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7375 role,
7376 MBEDTLS_MD_SHA256,
7377 MBEDTLS_ECP_DP_SECP256R1,
7378 pw, pw_len ) );
7379}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007380#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007383int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007384 const unsigned char *psk, size_t psk_len,
7385 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007386{
Paul Bakker6db455e2013-09-18 17:29:31 +02007387 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007392
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007393 /* Identity len will be encoded on two bytes */
7394 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007395 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007396 {
7397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7398 }
7399
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007400 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007401 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007402 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007403
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007404 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007405 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007406 conf->psk_len = 0;
7407 }
7408 if( conf->psk_identity != NULL )
7409 {
7410 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007411 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007412 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007413 }
7414
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007415 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7416 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007417 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007418 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007419 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007420 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007421 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007422 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007423 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007424
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007425 conf->psk_len = psk_len;
7426 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007427
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007428 memcpy( conf->psk, psk, conf->psk_len );
7429 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007430
7431 return( 0 );
7432}
7433
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007434int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7435 const unsigned char *psk, size_t psk_len )
7436{
7437 if( psk == NULL || ssl->handshake == NULL )
7438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7439
7440 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7442
7443 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007444 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007445 mbedtls_platform_zeroize( ssl->handshake->psk,
7446 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007447 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007448 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007449 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007450
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007451 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007452 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007453
7454 ssl->handshake->psk_len = psk_len;
7455 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7456
7457 return( 0 );
7458}
7459
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007460void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007462 size_t),
7463 void *p_psk )
7464{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007465 conf->f_psk = f_psk;
7466 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007469
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007470#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007471
7472#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007473int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007474{
7475 int ret;
7476
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007477 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7478 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7479 {
7480 mbedtls_mpi_free( &conf->dhm_P );
7481 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007482 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007484
7485 return( 0 );
7486}
Hanno Becker470a8c42017-10-04 15:28:46 +01007487#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007488
Hanno Beckera90658f2017-10-04 15:29:08 +01007489int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7490 const unsigned char *dhm_P, size_t P_len,
7491 const unsigned char *dhm_G, size_t G_len )
7492{
7493 int ret;
7494
7495 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7496 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7497 {
7498 mbedtls_mpi_free( &conf->dhm_P );
7499 mbedtls_mpi_free( &conf->dhm_G );
7500 return( ret );
7501 }
7502
7503 return( 0 );
7504}
Paul Bakker5121ce52009-01-03 21:22:43 +00007505
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007506int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007507{
7508 int ret;
7509
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007510 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7511 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7512 {
7513 mbedtls_mpi_free( &conf->dhm_P );
7514 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007515 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007516 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007517
7518 return( 0 );
7519}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007520#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007521
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007522#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7523/*
7524 * Set the minimum length for Diffie-Hellman parameters
7525 */
7526void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7527 unsigned int bitlen )
7528{
7529 conf->dhm_min_bitlen = bitlen;
7530}
7531#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7532
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007533#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007534/*
7535 * Set allowed/preferred hashes for handshake signatures
7536 */
7537void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7538 const int *hashes )
7539{
7540 conf->sig_hashes = hashes;
7541}
Hanno Becker947194e2017-04-07 13:25:49 +01007542#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007543
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007544#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007545/*
7546 * Set the allowed elliptic curves
7547 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007548void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007549 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007551 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007552}
Hanno Becker947194e2017-04-07 13:25:49 +01007553#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007554
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007555#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007557{
Hanno Becker947194e2017-04-07 13:25:49 +01007558 /* Initialize to suppress unnecessary compiler warning */
7559 size_t hostname_len = 0;
7560
7561 /* Check if new hostname is valid before
7562 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007563 if( hostname != NULL )
7564 {
7565 hostname_len = strlen( hostname );
7566
7567 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7569 }
7570
7571 /* Now it's clear that we will overwrite the old hostname,
7572 * so we can free it safely */
7573
7574 if( ssl->hostname != NULL )
7575 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007576 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007577 mbedtls_free( ssl->hostname );
7578 }
7579
7580 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007581
Paul Bakker5121ce52009-01-03 21:22:43 +00007582 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007583 {
7584 ssl->hostname = NULL;
7585 }
7586 else
7587 {
7588 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007589 if( ssl->hostname == NULL )
7590 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007591
Hanno Becker947194e2017-04-07 13:25:49 +01007592 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007593
Hanno Becker947194e2017-04-07 13:25:49 +01007594 ssl->hostname[hostname_len] = '\0';
7595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007596
7597 return( 0 );
7598}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007599#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007600
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007601#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007602void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007604 const unsigned char *, size_t),
7605 void *p_sni )
7606{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007607 conf->f_sni = f_sni;
7608 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007613int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007614{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007615 size_t cur_len, tot_len;
7616 const char **p;
7617
7618 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007619 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7620 * MUST NOT be truncated."
7621 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007622 */
7623 tot_len = 0;
7624 for( p = protos; *p != NULL; p++ )
7625 {
7626 cur_len = strlen( *p );
7627 tot_len += cur_len;
7628
Ronald Cron157cffe2020-04-23 16:41:44 +02007629 if( ( cur_len == 0 ) ||
7630 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7631 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007633 }
7634
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007635 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007636
7637 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007638}
7639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007641{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007642 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007645
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007646void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007648 conf->max_major_ver = major;
7649 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007650}
7651
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007652void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007653{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007654 conf->min_major_ver = major;
7655 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007656}
7657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007659void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007660{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007661 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007662}
7663#endif
7664
Janos Follath088ce432017-04-10 12:42:31 +01007665#if defined(MBEDTLS_SSL_SRV_C)
7666void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7667 char cert_req_ca_list )
7668{
7669 conf->cert_req_ca_list = cert_req_ca_list;
7670}
7671#endif
7672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007674void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007676 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007677}
7678#endif
7679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007681void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007682{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007683 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007684}
7685#endif
7686
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007687#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007688void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007689{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007690 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007691}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007692#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007695int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007698 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007701 }
7702
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007703 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007704
7705 return( 0 );
7706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007710void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007711{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007712 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007717void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007718{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007719 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007720}
7721#endif
7722
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007723void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007724{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007725 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007726}
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007729void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007730{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007731 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007732}
7733
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007734void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007735{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007736 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007737}
7738
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007739void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007740 const unsigned char period[8] )
7741{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007742 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007747#if defined(MBEDTLS_SSL_CLI_C)
7748void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007749{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007750 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007751}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007752#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007753
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007754#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007755void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7756 mbedtls_ssl_ticket_write_t *f_ticket_write,
7757 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7758 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007759{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007760 conf->f_ticket_write = f_ticket_write;
7761 conf->f_ticket_parse = f_ticket_parse;
7762 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007763}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007764#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007766
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007767#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7768void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7769 mbedtls_ssl_export_keys_t *f_export_keys,
7770 void *p_export_keys )
7771{
7772 conf->f_export_keys = f_export_keys;
7773 conf->p_export_keys = p_export_keys;
7774}
7775#endif
7776
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007777#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007778void mbedtls_ssl_conf_async_private_cb(
7779 mbedtls_ssl_config *conf,
7780 mbedtls_ssl_async_sign_t *f_async_sign,
7781 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7782 mbedtls_ssl_async_resume_t *f_async_resume,
7783 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007784 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007785{
7786 conf->f_async_sign_start = f_async_sign;
7787 conf->f_async_decrypt_start = f_async_decrypt;
7788 conf->f_async_resume = f_async_resume;
7789 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007790 conf->p_async_config_data = async_config_data;
7791}
7792
Gilles Peskine8f97af72018-04-26 11:46:10 +02007793void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7794{
7795 return( conf->p_async_config_data );
7796}
7797
Gilles Peskine1febfef2018-04-30 11:54:39 +02007798void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007799{
7800 if( ssl->handshake == NULL )
7801 return( NULL );
7802 else
7803 return( ssl->handshake->user_async_ctx );
7804}
7805
Gilles Peskine1febfef2018-04-30 11:54:39 +02007806void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007807 void *ctx )
7808{
7809 if( ssl->handshake != NULL )
7810 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007811}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007812#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007813
Paul Bakker5121ce52009-01-03 21:22:43 +00007814/*
7815 * SSL get accessors
7816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007818{
7819 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7820}
7821
Hanno Becker8b170a02017-10-10 11:51:19 +01007822int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7823{
7824 /*
7825 * Case A: We're currently holding back
7826 * a message for further processing.
7827 */
7828
7829 if( ssl->keep_current_message == 1 )
7830 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007832 return( 1 );
7833 }
7834
7835 /*
7836 * Case B: Further records are pending in the current datagram.
7837 */
7838
7839#if defined(MBEDTLS_SSL_PROTO_DTLS)
7840 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7841 ssl->in_left > ssl->next_record_offset )
7842 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007843 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007844 return( 1 );
7845 }
7846#endif /* MBEDTLS_SSL_PROTO_DTLS */
7847
7848 /*
7849 * Case C: A handshake message is being processed.
7850 */
7851
Hanno Becker8b170a02017-10-10 11:51:19 +01007852 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7853 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007855 return( 1 );
7856 }
7857
7858 /*
7859 * Case D: An application data message is being processed
7860 */
7861 if( ssl->in_offt != NULL )
7862 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007864 return( 1 );
7865 }
7866
7867 /*
7868 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007869 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007870 * we implement support for multiple alerts in single records.
7871 */
7872
7873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7874 return( 0 );
7875}
7876
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007877uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007879 if( ssl->session != NULL )
7880 return( ssl->session->verify_result );
7881
7882 if( ssl->session_negotiate != NULL )
7883 return( ssl->session_negotiate->verify_result );
7884
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007885 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007886}
7887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007889{
Paul Bakker926c8e42013-03-06 10:23:34 +01007890 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007891 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007894}
7895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007900 {
7901 switch( ssl->minor_ver )
7902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007904 return( "DTLSv1.0" );
7905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007907 return( "DTLSv1.2" );
7908
7909 default:
7910 return( "unknown (DTLS)" );
7911 }
7912 }
7913#endif
7914
Paul Bakker43ca69c2011-01-15 17:35:19 +00007915 switch( ssl->minor_ver )
7916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007918 return( "SSLv3.0" );
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007921 return( "TLSv1.0" );
7922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007924 return( "TLSv1.1" );
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007927 return( "TLSv1.2" );
7928
Paul Bakker43ca69c2011-01-15 17:35:19 +00007929 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007930 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007931 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007932}
7933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007935{
Hanno Becker3136ede2018-08-17 15:28:19 +01007936 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007938 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007939
Hanno Becker78640902018-08-13 16:35:15 +01007940 if( transform == NULL )
7941 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_ZLIB_SUPPORT)
7944 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7945 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007946#endif
7947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 case MBEDTLS_MODE_GCM:
7951 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007952 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007954 transform_expansion = transform->minlen;
7955 break;
7956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007958
7959 block_size = mbedtls_cipher_get_block_size(
7960 &transform->cipher_ctx_enc );
7961
Hanno Becker3136ede2018-08-17 15:28:19 +01007962 /* Expansion due to the addition of the MAC. */
7963 transform_expansion += transform->maclen;
7964
7965 /* Expansion due to the addition of CBC padding;
7966 * Theoretically up to 256 bytes, but we never use
7967 * more than the block size of the underlying cipher. */
7968 transform_expansion += block_size;
7969
7970 /* For TLS 1.1 or higher, an explicit IV is added
7971 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007972#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7973 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007974 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007975#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007976
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007977 break;
7978
7979 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007982 }
7983
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007984 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007985}
7986
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007987#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7988size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7989{
7990 size_t max_len;
7991
7992 /*
7993 * Assume mfl_code is correct since it was checked when set
7994 */
Angus Grattond8213d02016-05-25 20:56:48 +10007995 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007996
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007997 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007998 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007999 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008000 {
Angus Grattond8213d02016-05-25 20:56:48 +10008001 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008002 }
8003
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008004 /* During a handshake, use the value being negotiated */
8005 if( ssl->session_negotiate != NULL &&
8006 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8007 {
8008 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8009 }
8010
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008011 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008012}
8013#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8014
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008015#if defined(MBEDTLS_SSL_PROTO_DTLS)
8016static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8017{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008018 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8019 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8020 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8021 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8022 return ( 0 );
8023
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008024 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8025 return( ssl->mtu );
8026
8027 if( ssl->mtu == 0 )
8028 return( ssl->handshake->mtu );
8029
8030 return( ssl->mtu < ssl->handshake->mtu ?
8031 ssl->mtu : ssl->handshake->mtu );
8032}
8033#endif /* MBEDTLS_SSL_PROTO_DTLS */
8034
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008035int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8036{
8037 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8038
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008039#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8040 !defined(MBEDTLS_SSL_PROTO_DTLS)
8041 (void) ssl;
8042#endif
8043
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008044#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8045 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8046
8047 if( max_len > mfl )
8048 max_len = mfl;
8049#endif
8050
8051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008052 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008053 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008054 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008055 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8056 const size_t overhead = (size_t) ret;
8057
8058 if( ret < 0 )
8059 return( ret );
8060
8061 if( mtu <= overhead )
8062 {
8063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8064 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8065 }
8066
8067 if( max_len > mtu - overhead )
8068 max_len = mtu - overhead;
8069 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008070#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008071
Hanno Becker0defedb2018-08-10 12:35:02 +01008072#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8073 !defined(MBEDTLS_SSL_PROTO_DTLS)
8074 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008075#endif
8076
8077 return( (int) max_len );
8078}
8079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#if defined(MBEDTLS_X509_CRT_PARSE_C)
8081const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008082{
8083 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008084 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008085
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008086 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090#if defined(MBEDTLS_SSL_CLI_C)
8091int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008092{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008093 if( ssl == NULL ||
8094 dst == NULL ||
8095 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008096 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008099 }
8100
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008101 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008104
Paul Bakker5121ce52009-01-03 21:22:43 +00008105/*
Paul Bakker1961b702013-01-25 14:49:24 +01008106 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008111
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008112 if( ssl == NULL || ssl->conf == NULL )
8113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008116 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008120 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008122#endif
8123
Paul Bakker1961b702013-01-25 14:49:24 +01008124 return( ret );
8125}
8126
8127/*
8128 * Perform the SSL handshake
8129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008131{
8132 int ret = 0;
8133
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008134 if( ssl == NULL || ssl->conf == NULL )
8135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008142
8143 if( ret != 0 )
8144 break;
8145 }
8146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008148
8149 return( ret );
8150}
8151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#if defined(MBEDTLS_SSL_RENEGOTIATION)
8153#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008154/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008155 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008158{
8159 int ret;
8160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008162
8163 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8165 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008166
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008167 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008168 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008170 return( ret );
8171 }
8172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008174
8175 return( 0 );
8176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008178
8179/*
8180 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 * - any side: calling mbedtls_ssl_renegotiate(),
8182 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8183 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008184 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008185 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008189{
8190 int ret;
8191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008193
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008194 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8195 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008196
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008197 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8198 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008200 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008202 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008203 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008204 ssl->handshake->out_msg_seq = 1;
8205 else
8206 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008207 }
8208#endif
8209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8211 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008216 return( ret );
8217 }
8218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008220
8221 return( 0 );
8222}
8223
8224/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008225 * Renegotiate current connection on client,
8226 * or request renegotiation on server
8227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008231
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008232 if( ssl == NULL || ssl->conf == NULL )
8233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008236 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008237 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008243
8244 /* Did we already try/start sending HelloRequest? */
8245 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008247
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008248 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008253 /*
8254 * On client, either start the renegotiation process or,
8255 * if already in progress, continue the handshake
8256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008261
8262 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008265 return( ret );
8266 }
8267 }
8268 else
8269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008273 return( ret );
8274 }
8275 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008277
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008278 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008279}
8280
8281/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008282 * Check record counters and renegotiate if they're above the limit.
8283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008285{
Andres AG2196c7f2016-12-15 17:01:16 +00008286 size_t ep_len = ssl_ep_len( ssl );
8287 int in_ctr_cmp;
8288 int out_ctr_cmp;
8289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8291 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008292 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008293 {
8294 return( 0 );
8295 }
8296
Andres AG2196c7f2016-12-15 17:01:16 +00008297 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8298 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008299 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008300 ssl->conf->renego_period + ep_len, 8 - ep_len );
8301
8302 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008303 {
8304 return( 0 );
8305 }
8306
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008311
8312/*
8313 * Receive application data decrypted from the SSL layer
8314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008316{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008317 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008318 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008319
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008320 if( ssl == NULL || ssl->conf == NULL )
8321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008326 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008329 return( ret );
8330
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008331 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008333 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008334 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008335 return( ret );
8336 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008337 }
8338#endif
8339
Hanno Becker4a810fb2017-05-24 16:27:30 +01008340 /*
8341 * Check if renegotiation is necessary and/or handshake is
8342 * in process. If yes, perform/continue, and fall through
8343 * if an unexpected packet is received while the client
8344 * is waiting for the ServerHello.
8345 *
8346 * (There is no equivalent to the last condition on
8347 * the server-side as it is not treated as within
8348 * a handshake while waiting for the ClientHello
8349 * after a renegotiation request.)
8350 */
8351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008353 ret = ssl_check_ctr_renegotiate( ssl );
8354 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8355 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008358 return( ret );
8359 }
8360#endif
8361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008365 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8366 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008369 return( ret );
8370 }
8371 }
8372
Hanno Beckere41158b2017-10-23 13:30:32 +01008373 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008374 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008375 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008376 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008377 if( ssl->f_get_timer != NULL &&
8378 ssl->f_get_timer( ssl->p_timer ) == -1 )
8379 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008380 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008381 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008382
Hanno Becker327c93b2018-08-15 13:56:18 +01008383 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008384 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008385 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8386 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008387
Hanno Becker4a810fb2017-05-24 16:27:30 +01008388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8389 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008390 }
8391
8392 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008394 {
8395 /*
8396 * OpenSSL sends empty messages to randomize the IV
8397 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008398 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008401 return( 0 );
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008404 return( ret );
8405 }
8406 }
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008411
Hanno Becker4a810fb2017-05-24 16:27:30 +01008412 /*
8413 * - For client-side, expect SERVER_HELLO_REQUEST.
8414 * - For server-side, expect CLIENT_HELLO.
8415 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8416 */
8417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008419 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008421 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008424
8425 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008427 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008428 {
8429 continue;
8430 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008433 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008434#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008435
Hanno Becker4a810fb2017-05-24 16:27:30 +01008436#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008437 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008441
8442 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008444 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008445 {
8446 continue;
8447 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008448#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008450 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008451#endif /* MBEDTLS_SSL_SRV_C */
8452
Hanno Becker21df7f92017-10-17 11:03:26 +01008453#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008454 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008455 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8456 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8457 ssl->conf->allow_legacy_renegotiation ==
8458 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8459 {
8460 /*
8461 * Accept renegotiation request
8462 */
Paul Bakker48916f92012-09-16 19:57:18 +00008463
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008464 /* DTLS clients need to know renego is server-initiated */
8465#if defined(MBEDTLS_SSL_PROTO_DTLS)
8466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8467 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8468 {
8469 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8470 }
8471#endif
8472 ret = ssl_start_renegotiation( ssl );
8473 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8474 ret != 0 )
8475 {
8476 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8477 return( ret );
8478 }
8479 }
8480 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008481#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008482 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008483 /*
8484 * Refuse renegotiation
8485 */
8486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489#if defined(MBEDTLS_SSL_PROTO_SSL3)
8490 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008491 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008492 /* SSLv3 does not have a "no_renegotiation" warning, so
8493 we send a fatal alert and abort the connection. */
8494 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8495 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8496 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008497 }
8498 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8500#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8501 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8502 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8505 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8506 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008507 {
8508 return( ret );
8509 }
Paul Bakker48916f92012-09-16 19:57:18 +00008510 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8513 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008517 }
Paul Bakker48916f92012-09-16 19:57:18 +00008518 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008519
Hanno Becker90333da2017-10-10 11:27:13 +01008520 /* At this point, we don't know whether the renegotiation has been
8521 * completed or not. The cases to consider are the following:
8522 * 1) The renegotiation is complete. In this case, no new record
8523 * has been read yet.
8524 * 2) The renegotiation is incomplete because the client received
8525 * an application data record while awaiting the ServerHello.
8526 * 3) The renegotiation is incomplete because the client received
8527 * a non-handshake, non-application data message while awaiting
8528 * the ServerHello.
8529 * In each of these case, looping will be the proper action:
8530 * - For 1), the next iteration will read a new record and check
8531 * if it's application data.
8532 * - For 2), the loop condition isn't satisfied as application data
8533 * is present, hence continue is the same as break
8534 * - For 3), the loop condition is satisfied and read_record
8535 * will re-deliver the message that was held back by the client
8536 * when expecting the ServerHello.
8537 */
8538 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008539 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008540#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008541 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008542 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008543 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008544 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008545 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008548 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008550 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008551 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008552 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8556 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008559 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008560 }
8561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8565 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008566 }
8567
8568 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008569
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008570 /* We're going to return something now, cancel timer,
8571 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008573 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008574
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008576 /* If we requested renego but received AppData, resend HelloRequest.
8577 * Do it now, after setting in_offt, to avoid taking this branch
8578 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008580 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008582 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008583 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008586 return( ret );
8587 }
8588 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008590#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008591 }
8592
8593 n = ( len < ssl->in_msglen )
8594 ? len : ssl->in_msglen;
8595
8596 memcpy( buf, ssl->in_offt, n );
8597 ssl->in_msglen -= n;
8598
8599 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008600 {
8601 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008602 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008603 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008605 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008606 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008607 /* more data available */
8608 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008609 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008612
Paul Bakker23986e52011-04-24 08:57:21 +00008613 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008614}
8615
8616/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008617 * Send application data to be encrypted by the SSL layer, taking care of max
8618 * fragment length and buffer size.
8619 *
8620 * According to RFC 5246 Section 6.2.1:
8621 *
8622 * Zero-length fragments of Application data MAY be sent as they are
8623 * potentially useful as a traffic analysis countermeasure.
8624 *
8625 * Therefore, it is possible that the input message length is 0 and the
8626 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008627 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008628static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008629 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008630{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008631 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8632 const size_t max_len = (size_t) ret;
8633
8634 if( ret < 0 )
8635 {
8636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8637 return( ret );
8638 }
8639
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008640 if( len > max_len )
8641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008643 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008646 "maximum fragment length: %d > %d",
8647 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008649 }
8650 else
8651#endif
8652 len = max_len;
8653 }
Paul Bakker887bd502011-06-08 13:10:54 +00008654
Paul Bakker5121ce52009-01-03 21:22:43 +00008655 if( ssl->out_left != 0 )
8656 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008657 /*
8658 * The user has previously tried to send the data and
8659 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8660 * written. In this case, we expect the high-level write function
8661 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008666 return( ret );
8667 }
8668 }
Paul Bakker887bd502011-06-08 13:10:54 +00008669 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008670 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008671 /*
8672 * The user is trying to send a message the first time, so we need to
8673 * copy the data into the internal buffers and setup the data structure
8674 * to keep track of partial writes
8675 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008676 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008678 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008679
Hanno Becker67bc7c32018-08-06 11:33:50 +01008680 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008683 return( ret );
8684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008685 }
8686
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008687 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008688}
8689
8690/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008691 * Write application data, doing 1/n-1 splitting if necessary.
8692 *
8693 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008694 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008695 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008698static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008699 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008700{
8701 int ret;
8702
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008703 if( ssl->conf->cbc_record_splitting ==
8704 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008705 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8707 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8708 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008709 {
8710 return( ssl_write_real( ssl, buf, len ) );
8711 }
8712
8713 if( ssl->split_done == 0 )
8714 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008715 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008716 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008717 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008718 }
8719
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008720 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8721 return( ret );
8722 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008723
8724 return( ret + 1 );
8725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008727
8728/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008729 * Write application data (public-facing wrapper)
8730 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008731int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008732{
8733 int ret;
8734
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008736
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008737 if( ssl == NULL || ssl->conf == NULL )
8738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8739
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008740#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008741 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8742 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008743 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008744 return( ret );
8745 }
8746#endif
8747
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008748 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008749 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008750 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008751 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008753 return( ret );
8754 }
8755 }
8756
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008757#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008758 ret = ssl_write_split( ssl, buf, len );
8759#else
8760 ret = ssl_write_real( ssl, buf, len );
8761#endif
8762
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008764
8765 return( ret );
8766}
8767
8768/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008769 * Notify the peer that the connection is being closed
8770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008772{
8773 int ret;
8774
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008775 if( ssl == NULL || ssl->conf == NULL )
8776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008779
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008780 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8786 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8787 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008790 return( ret );
8791 }
8792 }
8793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008795
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008796 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008797}
8798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008800{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008801 if( transform == NULL )
8802 return;
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008805 deflateEnd( &transform->ctx_deflate );
8806 inflateEnd( &transform->ctx_inflate );
8807#endif
8808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8810 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 mbedtls_md_free( &transform->md_ctx_enc );
8813 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008814
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008815 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008816}
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818#if defined(MBEDTLS_X509_CRT_PARSE_C)
8819static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008822
8823 while( cur != NULL )
8824 {
8825 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008827 cur = next;
8828 }
8829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008831
Hanno Becker0271f962018-08-16 13:23:47 +01008832#if defined(MBEDTLS_SSL_PROTO_DTLS)
8833
8834static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8835{
8836 unsigned offset;
8837 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8838
8839 if( hs == NULL )
8840 return;
8841
Hanno Becker283f5ef2018-08-24 09:34:47 +01008842 ssl_free_buffered_record( ssl );
8843
Hanno Becker0271f962018-08-16 13:23:47 +01008844 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008845 ssl_buffering_free_slot( ssl, offset );
8846}
8847
8848static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8849 uint8_t slot )
8850{
8851 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8852 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008853
8854 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8855 return;
8856
Hanno Beckere605b192018-08-21 15:59:07 +01008857 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008858 {
Hanno Beckere605b192018-08-21 15:59:07 +01008859 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008860 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008861 mbedtls_free( hs_buf->data );
8862 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008863 }
8864}
8865
8866#endif /* MBEDTLS_SSL_PROTO_DTLS */
8867
Gilles Peskine9b562d52018-04-25 20:32:43 +02008868void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008869{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008870 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8871
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008872 if( handshake == NULL )
8873 return;
8874
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008875#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8876 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8877 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008878 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008879 handshake->async_in_progress = 0;
8880 }
8881#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8882
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008883#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8884 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8885 mbedtls_md5_free( &handshake->fin_md5 );
8886 mbedtls_sha1_free( &handshake->fin_sha1 );
8887#endif
8888#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8889#if defined(MBEDTLS_SHA256_C)
8890 mbedtls_sha256_free( &handshake->fin_sha256 );
8891#endif
8892#if defined(MBEDTLS_SHA512_C)
8893 mbedtls_sha512_free( &handshake->fin_sha512 );
8894#endif
8895#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897#if defined(MBEDTLS_DHM_C)
8898 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900#if defined(MBEDTLS_ECDH_C)
8901 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008902#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008903#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008904 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008905#if defined(MBEDTLS_SSL_CLI_C)
8906 mbedtls_free( handshake->ecjpake_cache );
8907 handshake->ecjpake_cache = NULL;
8908 handshake->ecjpake_cache_len = 0;
8909#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008910#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008911
Janos Follath4ae5c292016-02-10 11:27:43 +00008912#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8913 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008914 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008916#endif
8917
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008918#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8919 if( handshake->psk != NULL )
8920 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008921 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008922 mbedtls_free( handshake->psk );
8923 }
8924#endif
8925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8927 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008928 /*
8929 * Free only the linked list wrapper, not the keys themselves
8930 * since the belong to the SNI callback
8931 */
8932 if( handshake->sni_key_cert != NULL )
8933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008935
8936 while( cur != NULL )
8937 {
8938 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008940 cur = next;
8941 }
8942 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008944
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008945#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008946 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008947#endif
8948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949#if defined(MBEDTLS_SSL_PROTO_DTLS)
8950 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008951 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008952 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008953#endif
8954
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008955 mbedtls_platform_zeroize( handshake,
8956 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008957}
8958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008960{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008961 if( session == NULL )
8962 return;
8963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008965 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967 mbedtls_x509_crt_free( session->peer_cert );
8968 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008969 }
Paul Bakkered27a042013-04-18 22:46:23 +02008970#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008971
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008972#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008974#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008975
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008976 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008977}
8978
Paul Bakker5121ce52009-01-03 21:22:43 +00008979/*
8980 * Free an SSL context
8981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008983{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008984 if( ssl == NULL )
8985 return;
8986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008988
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008989 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008990 {
Angus Grattond8213d02016-05-25 20:56:48 +10008991 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008993 }
8994
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008995 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008996 {
Angus Grattond8213d02016-05-25 20:56:48 +10008997 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008999 }
9000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009002 if( ssl->compress_buf != NULL )
9003 {
Angus Grattond8213d02016-05-25 20:56:48 +10009004 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009006 }
9007#endif
9008
Paul Bakker48916f92012-09-16 19:57:18 +00009009 if( ssl->transform )
9010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 mbedtls_ssl_transform_free( ssl->transform );
9012 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009013 }
9014
9015 if( ssl->handshake )
9016 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009017 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9019 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 mbedtls_free( ssl->handshake );
9022 mbedtls_free( ssl->transform_negotiate );
9023 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009024 }
9025
Paul Bakkerc0463502013-02-14 11:19:38 +01009026 if( ssl->session )
9027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 mbedtls_ssl_session_free( ssl->session );
9029 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009030 }
9031
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009032#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009033 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009034 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009035 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009037 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9041 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9044 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009045 }
9046#endif
9047
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009048#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009050#endif
9051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009053
Paul Bakker86f04f42013-02-14 11:20:09 +01009054 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009055 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009056}
9057
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009058/*
9059 * Initialze mbedtls_ssl_config
9060 */
9061void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9062{
9063 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9064}
9065
Simon Butcherc97b6972015-12-27 23:48:17 +00009066#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009067static int ssl_preset_default_hashes[] = {
9068#if defined(MBEDTLS_SHA512_C)
9069 MBEDTLS_MD_SHA512,
9070 MBEDTLS_MD_SHA384,
9071#endif
9072#if defined(MBEDTLS_SHA256_C)
9073 MBEDTLS_MD_SHA256,
9074 MBEDTLS_MD_SHA224,
9075#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009076#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009077 MBEDTLS_MD_SHA1,
9078#endif
9079 MBEDTLS_MD_NONE
9080};
Simon Butcherc97b6972015-12-27 23:48:17 +00009081#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009082
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009083static int ssl_preset_suiteb_ciphersuites[] = {
9084 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9085 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9086 0
9087};
9088
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009089#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009090static int ssl_preset_suiteb_hashes[] = {
9091 MBEDTLS_MD_SHA256,
9092 MBEDTLS_MD_SHA384,
9093 MBEDTLS_MD_NONE
9094};
9095#endif
9096
9097#if defined(MBEDTLS_ECP_C)
9098static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009099#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009100 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009101#endif
9102#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009103 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009104#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009105 MBEDTLS_ECP_DP_NONE
9106};
9107#endif
9108
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009109/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009110 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009111 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009112int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009113 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009114{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009115#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009116 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009117#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009118
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009119 /* Use the functions here so that they are covered in tests,
9120 * but otherwise access member directly for efficiency */
9121 mbedtls_ssl_conf_endpoint( conf, endpoint );
9122 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009123
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009124 /*
9125 * Things that are common to all presets
9126 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009127#if defined(MBEDTLS_SSL_CLI_C)
9128 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9129 {
9130 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9131#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9132 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9133#endif
9134 }
9135#endif
9136
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009137#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009138 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009139#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009140
9141#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9142 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9143#endif
9144
9145#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9146 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9147#endif
9148
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009149#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9150 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9151#endif
9152
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009153#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009154 conf->f_cookie_write = ssl_cookie_write_dummy;
9155 conf->f_cookie_check = ssl_cookie_check_dummy;
9156#endif
9157
9158#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9159 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9160#endif
9161
Janos Follath088ce432017-04-10 12:42:31 +01009162#if defined(MBEDTLS_SSL_SRV_C)
9163 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9164#endif
9165
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009166#if defined(MBEDTLS_SSL_PROTO_DTLS)
9167 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9168 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9169#endif
9170
9171#if defined(MBEDTLS_SSL_RENEGOTIATION)
9172 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009173 memset( conf->renego_period, 0x00, 2 );
9174 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009175#endif
9176
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009177#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9178 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9179 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009180 const unsigned char dhm_p[] =
9181 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9182 const unsigned char dhm_g[] =
9183 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9184
Hanno Beckera90658f2017-10-04 15:29:08 +01009185 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9186 dhm_p, sizeof( dhm_p ),
9187 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009188 {
9189 return( ret );
9190 }
9191 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009192#endif
9193
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009194 /*
9195 * Preset-specific defaults
9196 */
9197 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009198 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009199 /*
9200 * NSA Suite B
9201 */
9202 case MBEDTLS_SSL_PRESET_SUITEB:
9203 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9204 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9205 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9206 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9207
9208 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9209 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9210 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9211 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9212 ssl_preset_suiteb_ciphersuites;
9213
9214#if defined(MBEDTLS_X509_CRT_PARSE_C)
9215 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009216#endif
9217
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009218#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009219 conf->sig_hashes = ssl_preset_suiteb_hashes;
9220#endif
9221
9222#if defined(MBEDTLS_ECP_C)
9223 conf->curve_list = ssl_preset_suiteb_curves;
9224#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009225 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009226
9227 /*
9228 * Default
9229 */
9230 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009231 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9232 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9233 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9234 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9235 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9236 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9237 MBEDTLS_SSL_MIN_MINOR_VERSION :
9238 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009239 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9240 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9241
9242#if defined(MBEDTLS_SSL_PROTO_DTLS)
9243 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9244 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9245#endif
9246
9247 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9248 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9249 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9250 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9251 mbedtls_ssl_list_ciphersuites();
9252
9253#if defined(MBEDTLS_X509_CRT_PARSE_C)
9254 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9255#endif
9256
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009257#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009258 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009259#endif
9260
9261#if defined(MBEDTLS_ECP_C)
9262 conf->curve_list = mbedtls_ecp_grp_id_list();
9263#endif
9264
9265#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9266 conf->dhm_min_bitlen = 1024;
9267#endif
9268 }
9269
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009270 return( 0 );
9271}
9272
9273/*
9274 * Free mbedtls_ssl_config
9275 */
9276void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9277{
9278#if defined(MBEDTLS_DHM_C)
9279 mbedtls_mpi_free( &conf->dhm_P );
9280 mbedtls_mpi_free( &conf->dhm_G );
9281#endif
9282
9283#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9284 if( conf->psk != NULL )
9285 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009286 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009287 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009288 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009289 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009290 }
9291
9292 if( conf->psk_identity != NULL )
9293 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009294 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009295 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009296 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009297 conf->psk_identity_len = 0;
9298 }
9299#endif
9300
9301#if defined(MBEDTLS_X509_CRT_PARSE_C)
9302 ssl_key_cert_free( conf->key_cert );
9303#endif
9304
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009305 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009306}
9307
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009308#if defined(MBEDTLS_PK_C) && \
9309 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009310/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#if defined(MBEDTLS_RSA_C)
9316 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9317 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319#if defined(MBEDTLS_ECDSA_C)
9320 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9321 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009324}
9325
Hanno Becker7e5437a2017-04-28 17:15:26 +01009326unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9327{
9328 switch( type ) {
9329 case MBEDTLS_PK_RSA:
9330 return( MBEDTLS_SSL_SIG_RSA );
9331 case MBEDTLS_PK_ECDSA:
9332 case MBEDTLS_PK_ECKEY:
9333 return( MBEDTLS_SSL_SIG_ECDSA );
9334 default:
9335 return( MBEDTLS_SSL_SIG_ANON );
9336 }
9337}
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009340{
9341 switch( sig )
9342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343#if defined(MBEDTLS_RSA_C)
9344 case MBEDTLS_SSL_SIG_RSA:
9345 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347#if defined(MBEDTLS_ECDSA_C)
9348 case MBEDTLS_SSL_SIG_ECDSA:
9349 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009350#endif
9351 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009353 }
9354}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009355#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009356
Hanno Becker7e5437a2017-04-28 17:15:26 +01009357#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9358 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9359
9360/* Find an entry in a signature-hash set matching a given hash algorithm. */
9361mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9362 mbedtls_pk_type_t sig_alg )
9363{
9364 switch( sig_alg )
9365 {
9366 case MBEDTLS_PK_RSA:
9367 return( set->rsa );
9368 case MBEDTLS_PK_ECDSA:
9369 return( set->ecdsa );
9370 default:
9371 return( MBEDTLS_MD_NONE );
9372 }
9373}
9374
9375/* Add a signature-hash-pair to a signature-hash set */
9376void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9377 mbedtls_pk_type_t sig_alg,
9378 mbedtls_md_type_t md_alg )
9379{
9380 switch( sig_alg )
9381 {
9382 case MBEDTLS_PK_RSA:
9383 if( set->rsa == MBEDTLS_MD_NONE )
9384 set->rsa = md_alg;
9385 break;
9386
9387 case MBEDTLS_PK_ECDSA:
9388 if( set->ecdsa == MBEDTLS_MD_NONE )
9389 set->ecdsa = md_alg;
9390 break;
9391
9392 default:
9393 break;
9394 }
9395}
9396
9397/* Allow exactly one hash algorithm for each signature. */
9398void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9399 mbedtls_md_type_t md_alg )
9400{
9401 set->rsa = md_alg;
9402 set->ecdsa = md_alg;
9403}
9404
9405#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9406 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9407
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009408/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009409 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009412{
9413 switch( hash )
9414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415#if defined(MBEDTLS_MD5_C)
9416 case MBEDTLS_SSL_HASH_MD5:
9417 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419#if defined(MBEDTLS_SHA1_C)
9420 case MBEDTLS_SSL_HASH_SHA1:
9421 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009422#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423#if defined(MBEDTLS_SHA256_C)
9424 case MBEDTLS_SSL_HASH_SHA224:
9425 return( MBEDTLS_MD_SHA224 );
9426 case MBEDTLS_SSL_HASH_SHA256:
9427 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429#if defined(MBEDTLS_SHA512_C)
9430 case MBEDTLS_SSL_HASH_SHA384:
9431 return( MBEDTLS_MD_SHA384 );
9432 case MBEDTLS_SSL_HASH_SHA512:
9433 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009434#endif
9435 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009437 }
9438}
9439
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009440/*
9441 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9442 */
9443unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9444{
9445 switch( md )
9446 {
9447#if defined(MBEDTLS_MD5_C)
9448 case MBEDTLS_MD_MD5:
9449 return( MBEDTLS_SSL_HASH_MD5 );
9450#endif
9451#if defined(MBEDTLS_SHA1_C)
9452 case MBEDTLS_MD_SHA1:
9453 return( MBEDTLS_SSL_HASH_SHA1 );
9454#endif
9455#if defined(MBEDTLS_SHA256_C)
9456 case MBEDTLS_MD_SHA224:
9457 return( MBEDTLS_SSL_HASH_SHA224 );
9458 case MBEDTLS_MD_SHA256:
9459 return( MBEDTLS_SSL_HASH_SHA256 );
9460#endif
9461#if defined(MBEDTLS_SHA512_C)
9462 case MBEDTLS_MD_SHA384:
9463 return( MBEDTLS_SSL_HASH_SHA384 );
9464 case MBEDTLS_MD_SHA512:
9465 return( MBEDTLS_SSL_HASH_SHA512 );
9466#endif
9467 default:
9468 return( MBEDTLS_SSL_HASH_NONE );
9469 }
9470}
9471
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009472#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009473/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009474 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009475 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009476 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009477int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009480
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009481 if( ssl->conf->curve_list == NULL )
9482 return( -1 );
9483
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009484 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009485 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009486 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009487
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009488 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009489}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009490#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009491
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009492#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009493/*
9494 * Check if a hash proposed by the peer is in our list.
9495 * Return 0 if we're willing to use it, -1 otherwise.
9496 */
9497int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9498 mbedtls_md_type_t md )
9499{
9500 const int *cur;
9501
9502 if( ssl->conf->sig_hashes == NULL )
9503 return( -1 );
9504
9505 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9506 if( *cur == (int) md )
9507 return( 0 );
9508
9509 return( -1 );
9510}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009511#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513#if defined(MBEDTLS_X509_CRT_PARSE_C)
9514int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9515 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009516 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009517 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009518{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009519 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009521 int usage = 0;
9522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009524 const char *ext_oid;
9525 size_t ext_len;
9526#endif
9527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9529 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009530 ((void) cert);
9531 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009532 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009533#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9536 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009537 {
9538 /* Server part of the key exchange */
9539 switch( ciphersuite->key_exchange )
9540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 case MBEDTLS_KEY_EXCHANGE_RSA:
9542 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009543 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009544 break;
9545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9547 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9548 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9549 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009550 break;
9551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9553 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009554 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009555 break;
9556
9557 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 case MBEDTLS_KEY_EXCHANGE_NONE:
9559 case MBEDTLS_KEY_EXCHANGE_PSK:
9560 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9561 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009562 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009563 usage = 0;
9564 }
9565 }
9566 else
9567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9569 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009570 }
9571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009573 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009574 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009575 ret = -1;
9576 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009577#else
9578 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9582 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9585 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009586 }
9587 else
9588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9590 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009591 }
9592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009594 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009595 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009596 ret = -1;
9597 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009599
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009600 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009603
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009604/*
9605 * Convert version numbers to/from wire format
9606 * and, for DTLS, to/from TLS equivalent.
9607 *
9608 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009609 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009610 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9611 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009614 unsigned char ver[2] )
9615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#if defined(MBEDTLS_SSL_PROTO_DTLS)
9617 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009620 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9621
9622 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9623 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9624 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009625 else
9626#else
9627 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009628#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009629 {
9630 ver[0] = (unsigned char) major;
9631 ver[1] = (unsigned char) minor;
9632 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009633}
9634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009636 const unsigned char ver[2] )
9637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638#if defined(MBEDTLS_SSL_PROTO_DTLS)
9639 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009640 {
9641 *major = 255 - ver[0] + 2;
9642 *minor = 255 - ver[1] + 1;
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009645 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9646 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009647 else
9648#else
9649 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009650#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009651 {
9652 *major = ver[0];
9653 *minor = ver[1];
9654 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009655}
9656
Simon Butcher99000142016-10-13 17:21:01 +01009657int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9658{
9659#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9660 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9661 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9662
9663 switch( md )
9664 {
9665#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9666#if defined(MBEDTLS_MD5_C)
9667 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009668 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009669#endif
9670#if defined(MBEDTLS_SHA1_C)
9671 case MBEDTLS_SSL_HASH_SHA1:
9672 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9673 break;
9674#endif
9675#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9676#if defined(MBEDTLS_SHA512_C)
9677 case MBEDTLS_SSL_HASH_SHA384:
9678 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9679 break;
9680#endif
9681#if defined(MBEDTLS_SHA256_C)
9682 case MBEDTLS_SSL_HASH_SHA256:
9683 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9684 break;
9685#endif
9686 default:
9687 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9688 }
9689
9690 return 0;
9691#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9692 (void) ssl;
9693 (void) md;
9694
9695 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9696#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9697}
9698
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009699#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9700 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9701int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9702 unsigned char *output,
9703 unsigned char *data, size_t data_len )
9704{
9705 int ret = 0;
9706 mbedtls_md5_context mbedtls_md5;
9707 mbedtls_sha1_context mbedtls_sha1;
9708
9709 mbedtls_md5_init( &mbedtls_md5 );
9710 mbedtls_sha1_init( &mbedtls_sha1 );
9711
9712 /*
9713 * digitally-signed struct {
9714 * opaque md5_hash[16];
9715 * opaque sha_hash[20];
9716 * };
9717 *
9718 * md5_hash
9719 * MD5(ClientHello.random + ServerHello.random
9720 * + ServerParams);
9721 * sha_hash
9722 * SHA(ClientHello.random + ServerHello.random
9723 * + ServerParams);
9724 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009725 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009726 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009728 goto exit;
9729 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009730 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009731 ssl->handshake->randbytes, 64 ) ) != 0 )
9732 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009734 goto exit;
9735 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009736 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009737 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009738 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009739 goto exit;
9740 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009741 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009742 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009744 goto exit;
9745 }
9746
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009747 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009748 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009750 goto exit;
9751 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009752 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009753 ssl->handshake->randbytes, 64 ) ) != 0 )
9754 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009756 goto exit;
9757 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009758 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009759 data_len ) ) != 0 )
9760 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009762 goto exit;
9763 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009764 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009765 output + 16 ) ) != 0 )
9766 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009768 goto exit;
9769 }
9770
9771exit:
9772 mbedtls_md5_free( &mbedtls_md5 );
9773 mbedtls_sha1_free( &mbedtls_sha1 );
9774
9775 if( ret != 0 )
9776 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9777 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9778
9779 return( ret );
9780
9781}
9782#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9783 MBEDTLS_SSL_PROTO_TLS1_1 */
9784
9785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9786 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9787int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009788 unsigned char *hash, size_t *hashlen,
9789 unsigned char *data, size_t data_len,
9790 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009791{
9792 int ret = 0;
9793 mbedtls_md_context_t ctx;
9794 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009795 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009796
9797 mbedtls_md_init( &ctx );
9798
9799 /*
9800 * digitally-signed struct {
9801 * opaque client_random[32];
9802 * opaque server_random[32];
9803 * ServerDHParams params;
9804 * };
9805 */
9806 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9807 {
9808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9809 goto exit;
9810 }
9811 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9812 {
9813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9814 goto exit;
9815 }
9816 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9817 {
9818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9819 goto exit;
9820 }
9821 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9822 {
9823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9824 goto exit;
9825 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009826 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009827 {
9828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9829 goto exit;
9830 }
9831
9832exit:
9833 mbedtls_md_free( &ctx );
9834
9835 if( ret != 0 )
9836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9837 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9838
9839 return( ret );
9840}
9841#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9842 MBEDTLS_SSL_PROTO_TLS1_2 */
9843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844#endif /* MBEDTLS_SSL_TLS_C */