blob: 34485d2cef2843f4408aafe2d4ff0ba6b234cbd7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48/*
49 * The SSL 3.0 specification was drafted by Netscape in 1996,
50 * and became an IETF standard in 1999.
51 *
52 * http://wp.netscape.com/eng/ssl3/
53 * http://www.ietf.org/rfc/rfc2246.txt
54 * http://www.ietf.org/rfc/rfc4346.txt
55 */
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000064
SimonBd5800b72016-04-26 07:43:27 +010065#if defined(MBEDTLS_PLATFORM_C)
66#include "mbedtls/platform.h"
67#else
68#include <stdlib.h>
69#define mbedtls_calloc calloc
70#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010071#endif
72
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/debug.h"
74#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020075#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050076#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020077
Rich Evans00ab4702015-02-06 13:43:58 +000078#include <string.h>
79
Janos Follath23bdca02016-10-07 14:47:14 +010080#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020082#endif
83
Hanno Becker2a43f6f2018-08-10 11:12:52 +010084static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010085static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010086
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010087/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020091 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010092 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010093#else
94 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010095#endif
96 return( 0 );
97}
98
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020099/*
100 * Start a timer.
101 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200105 if( ssl->f_set_timer == NULL )
106 return;
107
108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
109 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110}
111
112/*
113 * Return -1 is timer is expired, 0 if it isn't.
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200116{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200117 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200118 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200119
120 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200121 {
122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200123 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200124 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200125
126 return( 0 );
127}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200128
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100129static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
130 mbedtls_ssl_transform *transform );
131static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
132 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100133
134#define SSL_DONT_FORCE_FLUSH 0
135#define SSL_FORCE_FLUSH 1
136
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200137#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100138
Hanno Beckerd5847772018-08-28 10:09:23 +0100139/* Forward declarations for functions related to message buffering. */
140static void ssl_buffering_free( mbedtls_ssl_context *ssl );
141static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
142 uint8_t slot );
143static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
144static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
145static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
146static int ssl_buffer_message( mbedtls_ssl_context *ssl );
147static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100148static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100149
Hanno Beckera67dee22018-08-22 10:05:20 +0100150static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100151static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100152{
Hanno Becker11682cc2018-08-22 14:41:02 +0100153 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100154
155 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100156 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100157
158 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
159}
160
Hanno Becker67bc7c32018-08-06 11:33:50 +0100161static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
162{
Hanno Becker11682cc2018-08-22 14:41:02 +0100163 size_t const bytes_written = ssl->out_left;
164 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
166 /* Double-check that the write-index hasn't gone
167 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100168 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100169 {
170 /* Should never happen... */
171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
172 }
173
174 return( (int) ( mtu - bytes_written ) );
175}
176
177static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
178{
179 int ret;
180 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400181 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100182
183#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
184 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
185
186 if( max_len > mfl )
187 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100188
189 /* By the standard (RFC 6066 Sect. 4), the MFL extension
190 * only limits the maximum record payload size, so in theory
191 * we would be allowed to pack multiple records of payload size
192 * MFL into a single datagram. However, this would mean that there's
193 * no way to explicitly communicate MTU restrictions to the peer.
194 *
195 * The following reduction of max_len makes sure that we never
196 * write datagrams larger than MFL + Record Expansion Overhead.
197 */
198 if( max_len <= ssl->out_left )
199 return( 0 );
200
201 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100202#endif
203
204 ret = ssl_get_remaining_space_in_datagram( ssl );
205 if( ret < 0 )
206 return( ret );
207 remaining = (size_t) ret;
208
209 ret = mbedtls_ssl_get_record_expansion( ssl );
210 if( ret < 0 )
211 return( ret );
212 expansion = (size_t) ret;
213
214 if( remaining <= expansion )
215 return( 0 );
216
217 remaining -= expansion;
218 if( remaining >= max_len )
219 remaining = max_len;
220
221 return( (int) remaining );
222}
223
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200224/*
225 * Double the retransmit timeout value, within the allowed range,
226 * returning -1 if the maximum value has already been reached.
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229{
230 uint32_t new_timeout;
231
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200232 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200233 return( -1 );
234
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200235 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
236 * in the following way: after the initial transmission and a first
237 * retransmission, back off to a temporary estimated MTU of 508 bytes.
238 * This value is guaranteed to be deliverable (if not guaranteed to be
239 * delivered) of any compliant IPv4 (and IPv6) network, and should work
240 * on most non-IP stacks too. */
241 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400242 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200243 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
245 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200246
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200247 new_timeout = 2 * ssl->handshake->retransmit_timeout;
248
249 /* Avoid arithmetic overflow and range overflow */
250 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200251 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200253 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200254 }
255
256 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200258 ssl->handshake->retransmit_timeout ) );
259
260 return( 0 );
261}
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200264{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200265 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200267 ssl->handshake->retransmit_timeout ) );
268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200272/*
273 * Convert max_fragment_length codes to length.
274 * RFC 6066 says:
275 * enum{
276 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
277 * } MaxFragmentLength;
278 * and we add 0 -> extension unused
279 */
Angus Grattond8213d02016-05-25 20:56:48 +1000280static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281{
Angus Grattond8213d02016-05-25 20:56:48 +1000282 switch( mfl )
283 {
284 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
285 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
286 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
287 return 512;
288 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
289 return 1024;
290 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
291 return 2048;
292 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
293 return 4096;
294 default:
295 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
296 }
297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200299
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200300#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_ssl_session_free( dst );
304 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200307 if( src->peer_cert != NULL )
308 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200309 int ret;
310
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200311 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200312 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200313 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200318 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200321 dst->peer_cert = NULL;
322 return( ret );
323 }
324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200326
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200327#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200328 if( src->ticket != NULL )
329 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200330 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200331 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200332 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200333
334 memcpy( dst->ticket, src->ticket, src->ticket_len );
335 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200336#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200337
338 return( 0 );
339}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200340#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
343int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200344 const unsigned char *key_enc, const unsigned char *key_dec,
345 size_t keylen,
346 const unsigned char *iv_enc, const unsigned char *iv_dec,
347 size_t ivlen,
348 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200349 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
351int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
353int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
354int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
355#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000356
Paul Bakker5121ce52009-01-03 21:22:43 +0000357/*
358 * Key material generation
359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200361static int ssl3_prf( const unsigned char *secret, size_t slen,
362 const char *label,
363 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000364 unsigned char *dstbuf, size_t dlen )
365{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100366 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000367 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200368 mbedtls_md5_context md5;
369 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000370 unsigned char padding[16];
371 unsigned char sha1sum[20];
372 ((void)label);
373
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200374 mbedtls_md5_init( &md5 );
375 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200376
Paul Bakker5f70b252012-09-13 14:23:06 +0000377 /*
378 * SSLv3:
379 * block =
380 * MD5( secret + SHA1( 'A' + secret + random ) ) +
381 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
382 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
383 * ...
384 */
385 for( i = 0; i < dlen / 16; i++ )
386 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200387 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000388
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100398 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100401 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100406 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100407 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000408 }
409
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100410exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200411 mbedtls_md5_free( &md5 );
412 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000413
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500414 mbedtls_platform_zeroize( padding, sizeof( padding ) );
415 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100417 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200422static int tls1_prf( const unsigned char *secret, size_t slen,
423 const char *label,
424 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000425 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000426{
Paul Bakker23986e52011-04-24 08:57:21 +0000427 size_t nb, hs;
428 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200429 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 unsigned char tmp[128];
431 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 const mbedtls_md_info_t *md_info;
433 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100434 int ret;
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441 hs = ( slen + 1 ) / 2;
442 S1 = secret;
443 S2 = secret + slen - hs;
444
445 nb = strlen( label );
446 memcpy( tmp + 20, label, nb );
447 memcpy( tmp + 20 + nb, random, rlen );
448 nb += rlen;
449
450 /*
451 * First compute P_md5(secret,label+random)[0..dlen]
452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100457 return( ret );
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
460 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
461 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 for( i = 0; i < dlen; i += 16 )
464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_md_hmac_reset ( &md_ctx );
466 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
467 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_md_hmac_reset ( &md_ctx );
470 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
471 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
474
475 for( j = 0; j < k; j++ )
476 dstbuf[i + j] = h_i[j];
477 }
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100480
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 /*
482 * XOR out with P_sha1(secret,label+random)[0..dlen]
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100488 return( ret );
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
491 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
492 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 for( i = 0; i < dlen; i += 20 )
495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_md_hmac_reset ( &md_ctx );
497 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
498 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_md_hmac_reset ( &md_ctx );
501 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
502 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
505
506 for( j = 0; j < k; j++ )
507 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
508 }
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100511
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500512 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
513 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515 return( 0 );
516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
520static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100521 const unsigned char *secret, size_t slen,
522 const char *label,
523 const unsigned char *random, size_t rlen,
524 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000525{
526 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100527 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000528 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
530 const mbedtls_md_info_t *md_info;
531 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100532 int ret;
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100540
541 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000543
544 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100545 memcpy( tmp + md_len, label, nb );
546 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000547 nb += rlen;
548
549 /*
550 * Compute P_<hash>(secret, label + random)[0..dlen]
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100553 return( ret );
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
556 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
557 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100558
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100559 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_md_hmac_reset ( &md_ctx );
562 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
563 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_hmac_reset ( &md_ctx );
566 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
567 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000568
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100569 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000570
571 for( j = 0; j < k; j++ )
572 dstbuf[i + j] = h_i[j];
573 }
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100576
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500577 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
578 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000579
580 return( 0 );
581}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100584static int tls_prf_sha256( const unsigned char *secret, size_t slen,
585 const char *label,
586 const unsigned char *random, size_t rlen,
587 unsigned char *dstbuf, size_t dlen )
588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100590 label, random, rlen, dstbuf, dlen ) );
591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200595static int tls_prf_sha384( const unsigned char *secret, size_t slen,
596 const char *label,
597 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000598 unsigned char *dstbuf, size_t dlen )
599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100601 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_SHA512_C */
604#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
609 defined(MBEDTLS_SSL_PROTO_TLS1_1)
610static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200611#endif
Paul Bakker380da532012-04-18 16:10:25 +0000612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_SSL_PROTO_SSL3)
614static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
615static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200616#endif
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
619static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
620static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200621#endif
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
624#if defined(MBEDTLS_SHA256_C)
625static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
626static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
627static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200628#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#if defined(MBEDTLS_SHA512_C)
631static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
632static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
633static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100634#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200639 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 unsigned char keyblk[256];
642 unsigned char *key1;
643 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100644 unsigned char *mac_enc;
645 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000646 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200647 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 const mbedtls_cipher_info_t *cipher_info;
649 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_ssl_session *session = ssl->session_negotiate;
652 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
653 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100658 if( cipher_info == NULL )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100661 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100663 }
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100666 if( md_info == NULL )
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100669 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100671 }
672
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000674 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_SSL_PROTO_SSL3)
677 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000678 {
Paul Bakker48916f92012-09-16 19:57:18 +0000679 handshake->tls_prf = ssl3_prf;
680 handshake->calc_verify = ssl_calc_verify_ssl;
681 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200683 else
684#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
686 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000687 {
Paul Bakker48916f92012-09-16 19:57:18 +0000688 handshake->tls_prf = tls1_prf;
689 handshake->calc_verify = ssl_calc_verify_tls;
690 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200692 else
693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
695#if defined(MBEDTLS_SHA512_C)
696 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
697 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000698 {
Paul Bakker48916f92012-09-16 19:57:18 +0000699 handshake->tls_prf = tls_prf_sha384;
700 handshake->calc_verify = ssl_calc_verify_tls_sha384;
701 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000702 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SHA256_C)
706 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000707 {
Paul Bakker48916f92012-09-16 19:57:18 +0000708 handshake->tls_prf = tls_prf_sha256;
709 handshake->calc_verify = ssl_calc_verify_tls_sha256;
710 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200712 else
713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200718 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000719
720 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 * SSLv3:
722 * master =
723 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
724 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
725 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200726 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200727 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 * master = PRF( premaster, "master secret", randbytes )[0..47]
729 */
Paul Bakker0a597072012-09-25 21:55:46 +0000730 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000733 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
736 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200737 {
738 unsigned char session_hash[48];
739 size_t hash_len;
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200742
743 ssl->handshake->calc_verify( ssl, session_hash );
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
746 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200749 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200751 {
752 hash_len = 48;
753 }
754 else
755#endif
756 hash_len = 32;
757 }
758 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200760 hash_len = 36;
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200763
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100764 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
765 "extended master secret",
766 session_hash, hash_len,
767 session->master, 48 );
768 if( ret != 0 )
769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100771 return( ret );
772 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200773
774 }
775 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200776#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100777 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
778 "master secret",
779 handshake->randbytes, 64,
780 session->master, 48 );
781 if( ret != 0 )
782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100784 return( ret );
785 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200786
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500787 mbedtls_platform_zeroize( handshake->premaster,
788 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 }
790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793 /*
794 * Swap the client and server random values.
795 */
Paul Bakker48916f92012-09-16 19:57:18 +0000796 memcpy( tmp, handshake->randbytes, 64 );
797 memcpy( handshake->randbytes, tmp + 32, 32 );
798 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500799 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000800
801 /*
802 * SSLv3:
803 * key block =
804 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
805 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
806 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
807 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
808 * ...
809 *
810 * TLSv1:
811 * key block = PRF( master, "key expansion", randbytes )
812 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100813 ret = handshake->tls_prf( session->master, 48, "key expansion",
814 handshake->randbytes, 64, keyblk, 256 );
815 if( ret != 0 )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100818 return( ret );
819 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
822 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
823 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
824 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
825 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500827 mbedtls_platform_zeroize( handshake->randbytes,
828 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
830 /*
831 * Determine the appropriate key, IV and MAC length.
832 */
Paul Bakker68884e32013-01-07 18:20:04 +0100833
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200834 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200837 cipher_info->mode == MBEDTLS_MODE_CCM ||
838 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200840 size_t taglen, explicit_ivlen;
841
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200842 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000843 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200844
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200845 /* All modes haves 96-bit IVs;
846 * GCM and CCM has 4 implicit and 8 explicit bytes
847 * ChachaPoly has all 12 bytes implicit
848 */
Paul Bakker68884e32013-01-07 18:20:04 +0100849 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200850 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
851 transform->fixed_ivlen = 12;
852 else
853 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200854
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200855 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
856 taglen = transform->ciphersuite_info->flags &
857 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
858
859
860 /* Minimum length of encrypted record */
861 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
862 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100863 }
864 else
865 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
868 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200871 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100872 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000873
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200874 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000875 mac_key_len = mbedtls_md_get_size( md_info );
876 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200879 /*
880 * If HMAC is to be truncated, we shall keep the leftmost bytes,
881 * (rfc 6066 page 13 or rfc 2104 section 4),
882 * so we only need to adjust the length here.
883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000887
888#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
889 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000890 * HMAC implementation which also truncates the key
891 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000892 mac_key_len = transform->maclen;
893#endif
894 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200896
897 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100898 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200900 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200902 transform->minlen = transform->maclen;
903 else
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200905 /*
906 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100907 * 1. if EtM is in use: one block plus MAC
908 * otherwise: * first multiple of blocklen greater than maclen
909 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
912 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100913 {
914 transform->minlen = transform->maclen
915 + cipher_info->block_size;
916 }
917 else
918#endif
919 {
920 transform->minlen = transform->maclen
921 + cipher_info->block_size
922 - transform->maclen % cipher_info->block_size;
923 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
926 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
927 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200928 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100929 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
932 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
933 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200934 {
935 transform->minlen += transform->ivlen;
936 }
937 else
938#endif
939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200942 }
Paul Bakker68884e32013-01-07 18:20:04 +0100943 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 }
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000947 transform->keylen, transform->minlen, transform->ivlen,
948 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
950 /*
951 * Finally setup the cipher contexts, IVs and MAC secrets.
952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000956 key1 = keyblk + mac_key_len * 2;
957 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakker68884e32013-01-07 18:20:04 +0100959 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000960 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000962 /*
963 * This is not used in TLS v1.1.
964 */
Paul Bakker48916f92012-09-16 19:57:18 +0000965 iv_copy_len = ( transform->fixed_ivlen ) ?
966 transform->fixed_ivlen : transform->ivlen;
967 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
968 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000969 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 }
971 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_SSL_CLI_C */
973#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000976 key1 = keyblk + mac_key_len * 2 + transform->keylen;
977 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Hanno Becker81c7b182017-11-09 18:39:33 +0000979 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100980 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000982 /*
983 * This is not used in TLS v1.1.
984 */
Paul Bakker48916f92012-09-16 19:57:18 +0000985 iv_copy_len = ( transform->fixed_ivlen ) ?
986 transform->fixed_ivlen : transform->ivlen;
987 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
988 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000989 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100996 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#if defined(MBEDTLS_SSL_PROTO_SSL3)
999 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001000 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001005 }
1006
Hanno Becker81c7b182017-11-09 18:39:33 +00001007 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1008 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001009 }
1010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1012#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1013 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1014 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001015 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001016 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1017 For AEAD-based ciphersuites, there is nothing to do here. */
1018 if( mac_key_len != 0 )
1019 {
1020 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1021 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1022 }
Paul Bakker68884e32013-01-07 18:20:04 +01001023 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001024 else
1025#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1028 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001029 }
Paul Bakker68884e32013-01-07 18:20:04 +01001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1032 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001037 transform->iv_enc, transform->iv_dec,
1038 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001039 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001040 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1043 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001044 }
1045 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001047
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001048#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1049 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001050 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001051 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1052 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001053 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001054 iv_copy_len );
1055 }
1056#endif
1057
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001058 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001059 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001062 return( ret );
1063 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001064
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001065 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001066 cipher_info ) ) != 0 )
1067 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001069 return( ret );
1070 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001073 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001077 return( ret );
1078 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001081 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001085 return( ret );
1086 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_CIPHER_MODE_CBC)
1089 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1092 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001096 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1099 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001102 return( ret );
1103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001107 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001110 // Initialize compression
1111 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001113 {
Paul Bakker16770332013-10-11 09:59:44 +02001114 if( ssl->compress_buf == NULL )
1115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001117 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001118 if( ssl->compress_buf == NULL )
1119 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001121 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001122 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001123 }
1124 }
1125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001127
Paul Bakker48916f92012-09-16 19:57:18 +00001128 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1129 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001130
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001131 if( deflateInit( &transform->ctx_deflate,
1132 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001133 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1136 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001137 }
1138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
1143 return( 0 );
1144}
1145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_SSL_PROTO_SSL3)
1147void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001148{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001149 mbedtls_md5_context md5;
1150 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 unsigned char pad_1[48];
1152 unsigned char pad_2[48];
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001156 mbedtls_md5_init( &md5 );
1157 mbedtls_sha1_init( &sha1 );
1158
1159 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1160 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Paul Bakker380da532012-04-18 16:10:25 +00001162 memset( pad_1, 0x36, 48 );
1163 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001165 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1166 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1167 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001169 mbedtls_md5_starts_ret( &md5 );
1170 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1171 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1172 mbedtls_md5_update_ret( &md5, hash, 16 );
1173 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001175 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1176 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1177 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001178
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001179 mbedtls_sha1_starts_ret( &sha1 );
1180 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1181 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1182 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1183 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001187
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001188 mbedtls_md5_free( &md5 );
1189 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001190
Paul Bakker380da532012-04-18 16:10:25 +00001191 return;
1192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1196void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001197{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001198 mbedtls_md5_context md5;
1199 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001202
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001203 mbedtls_md5_init( &md5 );
1204 mbedtls_sha1_init( &sha1 );
1205
1206 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1207 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001208
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001209 mbedtls_md5_finish_ret( &md5, hash );
1210 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001214
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001215 mbedtls_md5_free( &md5 );
1216 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001217
Paul Bakker380da532012-04-18 16:10:25 +00001218 return;
1219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1223#if defined(MBEDTLS_SHA256_C)
1224void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001225{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001226 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001227
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001228 mbedtls_sha256_init( &sha256 );
1229
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001231
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001232 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001233 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001237
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001238 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001239
Paul Bakker380da532012-04-18 16:10:25 +00001240 return;
1241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_SHA512_C)
1245void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001246{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001247 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001248
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001249 mbedtls_sha512_init( &sha512 );
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001252
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001253 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001254 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001259 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001260
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 return;
1262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SHA512_C */
1264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1267int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001268{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001269 unsigned char *p = ssl->handshake->premaster;
1270 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001271 const unsigned char *psk = ssl->conf->psk;
1272 size_t psk_len = ssl->conf->psk_len;
1273
1274 /* If the psk callback was called, use its result */
1275 if( ssl->handshake->psk != NULL )
1276 {
1277 psk = ssl->handshake->psk;
1278 psk_len = ssl->handshake->psk_len;
1279 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001280
1281 /*
1282 * PMS = struct {
1283 * opaque other_secret<0..2^16-1>;
1284 * opaque psk<0..2^16-1>;
1285 * };
1286 * with "other_secret" depending on the particular key exchange
1287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1289 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001290 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001291 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001293
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001294 *(p++) = (unsigned char)( psk_len >> 8 );
1295 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001296
1297 if( end < p || (size_t)( end - p ) < psk_len )
1298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1299
1300 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001301 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001302 }
1303 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1305#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1306 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001307 {
1308 /*
1309 * other_secret already set by the ClientKeyExchange message,
1310 * and is 48 bytes long
1311 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001312 if( end - p < 2 )
1313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1314
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001315 *p++ = 0;
1316 *p++ = 48;
1317 p += 48;
1318 }
1319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1321#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1322 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001323 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001324 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001325 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001326
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001327 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001329 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001330 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001333 return( ret );
1334 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001335 *(p++) = (unsigned char)( len >> 8 );
1336 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001337 p += len;
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001340 }
1341 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1343#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1344 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001345 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001346 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001347 size_t zlen;
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001350 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001351 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001354 return( ret );
1355 }
1356
1357 *(p++) = (unsigned char)( zlen >> 8 );
1358 *(p++) = (unsigned char)( zlen );
1359 p += zlen;
1360
Janos Follath3fbdada2018-08-15 10:26:53 +01001361 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1362 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001363 }
1364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001369 }
1370
1371 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001372 if( end - p < 2 )
1373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001374
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001375 *(p++) = (unsigned char)( psk_len >> 8 );
1376 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001377
1378 if( end < p || (size_t)( end - p ) < psk_len )
1379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1380
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001381 memcpy( p, psk, psk_len );
1382 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001383
1384 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1385
1386 return( 0 );
1387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001391/*
1392 * SSLv3.0 MAC functions
1393 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001394#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001395static void ssl_mac( mbedtls_md_context_t *md_ctx,
1396 const unsigned char *secret,
1397 const unsigned char *buf, size_t len,
1398 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001399 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400{
1401 unsigned char header[11];
1402 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001403 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1405 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001406
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001407 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001409 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001410 else
Paul Bakker68884e32013-01-07 18:20:04 +01001411 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
1413 memcpy( header, ctr, 8 );
1414 header[ 8] = (unsigned char) type;
1415 header[ 9] = (unsigned char)( len >> 8 );
1416 header[10] = (unsigned char)( len );
1417
Paul Bakker68884e32013-01-07 18:20:04 +01001418 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_md_starts( md_ctx );
1420 mbedtls_md_update( md_ctx, secret, md_size );
1421 mbedtls_md_update( md_ctx, padding, padlen );
1422 mbedtls_md_update( md_ctx, header, 11 );
1423 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001424 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Paul Bakker68884e32013-01-07 18:20:04 +01001426 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_md_starts( md_ctx );
1428 mbedtls_md_update( md_ctx, secret, md_size );
1429 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001430 mbedtls_md_update( md_ctx, out, md_size );
1431 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001436 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001437#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001438#endif
1439
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001440/* The function below is only used in the Lucky 13 counter-measure in
1441 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1442#if defined(SSL_SOME_MODES_USE_MAC) && \
1443 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1444 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1445 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1446/* This function makes sure every byte in the memory region is accessed
1447 * (in ascending addresses order) */
1448static void ssl_read_memory( unsigned char *p, size_t len )
1449{
1450 unsigned char acc = 0;
1451 volatile unsigned char force;
1452
1453 for( ; len != 0; p++, len-- )
1454 acc ^= *p;
1455
1456 force = acc;
1457 (void) force;
1458}
1459#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1460
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001461/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001467 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001471 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001475 }
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001480 ssl->out_msg, ssl->out_msglen );
1481
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001483 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001485#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 if( mode == MBEDTLS_MODE_STREAM ||
1487 ( mode == MBEDTLS_MODE_CBC
1488#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1489 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001490#endif
1491 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_SSL_PROTO_SSL3)
1494 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001495 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001496 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001497
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001498 ssl_mac( &ssl->transform_out->md_ctx_enc,
1499 ssl->transform_out->mac_enc,
1500 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001501 ssl->out_ctr, ssl->out_msgtype,
1502 mac );
1503
1504 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001505 }
1506 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1509 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1510 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001511 {
Hanno Becker992b6872017-11-09 18:57:39 +00001512 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1515 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1516 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1517 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001518 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001519 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001521
1522 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001523 }
1524 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001525#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001529 }
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001532 ssl->out_msg + ssl->out_msglen,
1533 ssl->transform_out->maclen );
1534
1535 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001536 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001537 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001538#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001540 /*
1541 * Encrypt
1542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1544 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001546 int ret;
1547 size_t olen = 0;
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 "including %d bytes of padding",
1551 ssl->out_msglen, 0 ) );
1552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001554 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001555 ssl->transform_out->ivlen,
1556 ssl->out_msg, ssl->out_msglen,
1557 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001560 return( ret );
1561 }
1562
1563 if( ssl->out_msglen != olen )
1564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 }
Paul Bakker68884e32013-01-07 18:20:04 +01001569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001571#if defined(MBEDTLS_GCM_C) || \
1572 defined(MBEDTLS_CCM_C) || \
1573 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001575 mode == MBEDTLS_MODE_CCM ||
1576 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001577 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001578 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001579 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001580 unsigned char *enc_msg;
1581 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001582 unsigned char iv[12];
1583 mbedtls_ssl_transform *transform = ssl->transform_out;
1584 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001587
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001588 /*
1589 * Prepare additional authenticated data
1590 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001591 memcpy( add_data, ssl->out_ctr, 8 );
1592 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001594 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001595 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1596 add_data[12] = ssl->out_msglen & 0xFF;
1597
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001598 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001599
Paul Bakker68884e32013-01-07 18:20:04 +01001600 /*
1601 * Generate IV
1602 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001603 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1604 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001605 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001606 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1607 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1608 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1609
1610 }
1611 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1612 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001613 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001614 unsigned char i;
1615
1616 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1617
1618 for( i = 0; i < 8; i++ )
1619 iv[i+4] ^= ssl->out_ctr[i];
1620 }
1621 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001622 {
1623 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001626 }
1627
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001628 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1629 iv, transform->ivlen );
1630 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1631 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001632
Paul Bakker68884e32013-01-07 18:20:04 +01001633 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001634 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001635 */
1636 enc_msg = ssl->out_msg;
1637 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001638 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001641 "including 0 bytes of padding",
1642 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001643
Paul Bakker68884e32013-01-07 18:20:04 +01001644 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001645 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001646 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001647 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1648 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001649 add_data, 13,
1650 enc_msg, enc_msglen,
1651 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001652 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001655 return( ret );
1656 }
1657
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001658 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001662 }
1663
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001664 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001665 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001671#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001674 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001675 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001676 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001677
Paul Bakker48916f92012-09-16 19:57:18 +00001678 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1679 ssl->transform_out->ivlen;
1680 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 padlen = 0;
1682
1683 for( i = 0; i <= padlen; i++ )
1684 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1685
1686 ssl->out_msglen += padlen + 1;
1687
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001688 enc_msglen = ssl->out_msglen;
1689 enc_msg = ssl->out_msg;
1690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001692 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001693 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1694 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001697 {
1698 /*
1699 * Generate IV
1700 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001701 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001702 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001703 if( ret != 0 )
1704 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001705
Paul Bakker92be97b2013-01-02 17:30:03 +01001706 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001707 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001708
1709 /*
1710 * Fix pointer positions and message length with added IV
1711 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001712 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001713 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001714 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001719 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001720 ssl->out_msglen, ssl->transform_out->ivlen,
1721 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001724 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001725 ssl->transform_out->ivlen,
1726 enc_msg, enc_msglen,
1727 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001730 return( ret );
1731 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001732
Paul Bakkercca5b812013-08-31 17:40:26 +02001733 if( enc_msglen != olen )
1734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1740 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001741 {
1742 /*
1743 * Save IV in SSL3 and TLS1
1744 */
1745 memcpy( ssl->transform_out->iv_enc,
1746 ssl->transform_out->cipher_ctx_enc.iv,
1747 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001749#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001752 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001753 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001754 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1755
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001756 /*
1757 * MAC(MAC_write_key, seq_num +
1758 * TLSCipherText.type +
1759 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001760 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001761 * IV + // except for TLS 1.0
1762 * ENC(content + padding + padding_length));
1763 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001764 unsigned char pseudo_hdr[13];
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001767
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1769 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001770 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1771 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1776 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001778 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001780
Hanno Becker3d8c9072018-01-05 16:24:22 +00001781 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1782 ssl->transform_out->maclen );
1783
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001784 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001789 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001790#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001796 /* Make extra sure authentication was performed, exactly once */
1797 if( auth_done != 1 )
1798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001801 }
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
1805 return( 0 );
1806}
1807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001809{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001811 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001812#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001813 size_t padlen = 0, correct = 1;
1814#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001818 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001822 }
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001825
Paul Bakker48916f92012-09-16 19:57:18 +00001826 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001829 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 }
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1834 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001835 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001836 int ret;
1837 size_t olen = 0;
1838
Paul Bakker68884e32013-01-07 18:20:04 +01001839 padlen = 0;
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001842 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001843 ssl->transform_in->ivlen,
1844 ssl->in_msg, ssl->in_msglen,
1845 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001848 return( ret );
1849 }
1850
1851 if( ssl->in_msglen != olen )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 }
Paul Bakker68884e32013-01-07 18:20:04 +01001857 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001859#if defined(MBEDTLS_GCM_C) || \
1860 defined(MBEDTLS_CCM_C) || \
1861 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001863 mode == MBEDTLS_MODE_CCM ||
1864 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001865 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001866 int ret;
1867 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001868 unsigned char *dec_msg;
1869 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001870 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001871 unsigned char iv[12];
1872 mbedtls_ssl_transform *transform = ssl->transform_in;
1873 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001875 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001877 /*
1878 * Compute and update sizes
1879 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001880 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001883 "+ taglen (%d)", ssl->in_msglen,
1884 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001886 }
1887 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1888
Paul Bakker68884e32013-01-07 18:20:04 +01001889 dec_msg = ssl->in_msg;
1890 dec_msg_result = ssl->in_msg;
1891 ssl->in_msglen = dec_msglen;
1892
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001893 /*
1894 * Prepare additional authenticated data
1895 */
Paul Bakker68884e32013-01-07 18:20:04 +01001896 memcpy( add_data, ssl->in_ctr, 8 );
1897 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001899 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001900 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1901 add_data[12] = ssl->in_msglen & 0xFF;
1902
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001903 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001904
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001905 /*
1906 * Prepare IV
1907 */
1908 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1909 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001910 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001911 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1912 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001913
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001914 }
1915 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1916 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001917 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 unsigned char i;
1919
1920 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1921
1922 for( i = 0; i < 8; i++ )
1923 iv[i+4] ^= ssl->in_ctr[i];
1924 }
1925 else
1926 {
1927 /* Reminder if we ever add an AEAD mode with a different size */
1928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1930 }
1931
1932 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001934
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001935 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001936 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001939 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001940 add_data, 13,
1941 dec_msg, dec_msglen,
1942 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001943 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1948 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001949
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001950 return( ret );
1951 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001952 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001953
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001954 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001958 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001962#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 {
Paul Bakker45829992013-01-03 14:52:21 +01001965 /*
1966 * Decrypt and check the padding
1967 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001968 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001969 unsigned char *dec_msg;
1970 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001971 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001972 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001973 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001974
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 /*
Paul Bakker45829992013-01-03 14:52:21 +01001976 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1979 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001980 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001981#endif
Paul Bakker45829992013-01-03 14:52:21 +01001982
1983 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1984 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001987 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1988 ssl->transform_in->ivlen,
1989 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001991 }
1992
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001993 dec_msglen = ssl->in_msglen;
1994 dec_msg = ssl->in_msg;
1995 dec_msg_result = ssl->in_msg;
1996
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001997 /*
1998 * Authenticate before decrypt if enabled
1999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2001 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002002 {
Hanno Becker992b6872017-11-09 18:57:39 +00002003 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002004 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002007
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002008 dec_msglen -= ssl->transform_in->maclen;
2009 ssl->in_msglen -= ssl->transform_in->maclen;
2010
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002011 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2012 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2013 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2014 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2019 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002020 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002021 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002025 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002026 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002027 ssl->transform_in->maclen );
2028
Hanno Becker992b6872017-11-09 18:57:39 +00002029 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2030 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002035 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002036 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002039
2040 /*
2041 * Check length sanity
2042 */
2043 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002046 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002048 }
2049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002051 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002052 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002055 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002056 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002057 dec_msglen -= ssl->transform_in->ivlen;
2058 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002059
Paul Bakker48916f92012-09-16 19:57:18 +00002060 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002061 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002062 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002066 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002067 ssl->transform_in->ivlen,
2068 dec_msg, dec_msglen,
2069 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002072 return( ret );
2073 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002074
Paul Bakkercca5b812013-08-31 17:40:26 +02002075 if( dec_msglen != olen )
2076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002079 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2082 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002083 {
2084 /*
2085 * Save IV in SSL3 and TLS1
2086 */
2087 memcpy( ssl->transform_in->iv_dec,
2088 ssl->transform_in->cipher_ctx_dec.iv,
2089 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002092
2093 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002094
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002095 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002096 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#if defined(MBEDTLS_SSL_DEBUG_ALL)
2099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002100 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002101#endif
Paul Bakker45829992013-01-03 14:52:21 +01002102 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002103 correct = 0;
2104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_SSL_PROTO_SSL3)
2107 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 {
Paul Bakker48916f92012-09-16 19:57:18 +00002109 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#if defined(MBEDTLS_SSL_DEBUG_ALL)
2112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002114 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002115#endif
Paul Bakker45829992013-01-03 14:52:21 +01002116 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 }
2118 }
2119 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2121#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2122 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2123 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 {
2125 /*
Paul Bakker45829992013-01-03 14:52:21 +01002126 * TLSv1+: always check the padding up to the first failure
2127 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002129 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002130 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002131 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002132
Paul Bakker956c9e02013-12-19 14:42:28 +01002133 /*
2134 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002135 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002136 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002137 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002138 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002139 *
2140 * In both cases we reset padding_idx to a safe value (0) to
2141 * prevent out-of-buffer reads.
2142 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002143 correct &= ( padlen <= ssl->in_msglen );
2144 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002145 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002146
2147 padding_idx *= correct;
2148
Angus Grattonb512bc12018-06-19 15:57:50 +10002149 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002150 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002151 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002152 pad_count += real_count *
2153 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2154 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002155
2156 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002159 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002161#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002162 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2166 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002170 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002171
2172 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002174 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002175#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002179 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002180
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002181#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
2186 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002187 * Authenticate if not done yet.
2188 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002190#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002191 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002192 {
Hanno Becker992b6872017-11-09 18:57:39 +00002193 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002194
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002195 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002197 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2198 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_PROTO_SSL3)
2201 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002202 {
2203 ssl_mac( &ssl->transform_in->md_ctx_dec,
2204 ssl->transform_in->mac_dec,
2205 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002206 ssl->in_ctr, ssl->in_msgtype,
2207 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002208 }
2209 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2211#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2212 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2213 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002214 {
2215 /*
2216 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002217 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002218 *
2219 * Known timing attacks:
2220 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2221 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002222 * To compensate for different timings for the MAC calculation
2223 * depending on how much padding was removed (which is determined
2224 * by padlen), process extra_run more blocks through the hash
2225 * function.
2226 *
2227 * The formula in the paper is
2228 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2229 * where L1 is the size of the header plus the decrypted message
2230 * plus CBC padding and L2 is the size of the header plus the
2231 * decrypted message. This is for an underlying hash function
2232 * with 64-byte blocks.
2233 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2234 * correctly. We round down instead of up, so -56 is the correct
2235 * value for our calculations instead of -55.
2236 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002237 * Repeat the formula rather than defining a block_size variable.
2238 * This avoids requiring division by a variable at runtime
2239 * (which would be marginally less efficient and would require
2240 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 */
2242 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002243
2244 /*
2245 * The next two sizes are the minimum and maximum values of
2246 * in_msglen over all padlen values.
2247 *
2248 * They're independent of padlen, since we previously did
2249 * in_msglen -= padlen.
2250 *
2251 * Note that max_len + maclen is never more than the buffer
2252 * length, as we previously did in_msglen -= maclen too.
2253 */
2254 const size_t max_len = ssl->in_msglen + padlen;
2255 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2256
Gilles Peskine20b44082018-05-29 14:06:49 +02002257 switch( ssl->transform_in->ciphersuite_info->mac )
2258 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002259#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2260 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002261 case MBEDTLS_MD_MD5:
2262 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002263 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002264 /* 8 bytes of message size, 64-byte compression blocks */
2265 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2266 ( 13 + ssl->in_msglen + 8 ) / 64;
2267 break;
2268#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002269#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002270 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002271 /* 16 bytes of message size, 128-byte compression blocks */
2272 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2273 ( 13 + ssl->in_msglen + 16 ) / 128;
2274 break;
2275#endif
2276 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2279 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002280
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002281 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2284 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2285 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2286 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002287 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002288 /* Make sure we access everything even when padlen > 0. This
2289 * makes the synchronisation requirements for just-in-time
2290 * Prime+Probe attacks much tighter and hopefully impractical. */
2291 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002292 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002293
Manuel Pégourié-Gonnard20cd85c2020-06-18 11:30:40 +02002294 /* Dummy calls to compression function.
2295 * Call mbedtls_md_process at least once due to cache attacks
2296 * that observe whether md_process() was called of not.
2297 * Respect the usual start-(process|update)-finish sequence for
2298 * the sake of hardware accelerators that might require it. */
2299 mbedtls_md_starts( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002300 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Manuel Pégourié-Gonnard20cd85c2020-06-18 11:30:40 +02002302 {
2303 /* The switch statement above already checks that we're using
2304 * one of MD-5, SHA-1, SHA-256 or SHA-384. */
2305 unsigned char tmp[384 / 8];
2306 mbedtls_md_finish( &ssl->transform_in->md_ctx_dec, tmp );
2307 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002310
2311 /* Make sure we access all the memory that could contain the MAC,
2312 * before we check it in the next code block. This makes the
2313 * synchronisation requirements for just-in-time Prime+Probe
2314 * attacks much tighter and hopefully impractical. */
2315 ssl_read_memory( ssl->in_msg + min_len,
2316 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002317 }
2318 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2320 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002326#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002327 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2328 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2329 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Hanno Becker992b6872017-11-09 18:57:39 +00002332 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2333 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335#if defined(MBEDTLS_SSL_DEBUG_ALL)
2336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002337#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002338 correct = 0;
2339 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002340 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002341 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002342
2343 /*
2344 * Finally check the correct flag
2345 */
2346 if( correct == 0 )
2347 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002348#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002349
2350 /* Make extra sure authentication was performed, exactly once */
2351 if( auth_done != 1 )
2352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002355 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
2357 if( ssl->in_msglen == 0 )
2358 {
Angus Gratton34817922018-06-19 15:58:22 +10002359#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2360 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2361 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2362 {
2363 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2365 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2366 }
2367#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2368
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 ssl->nb_zero++;
2370
2371 /*
2372 * Three or more empty messages may be a DoS attack
2373 * (excessive CPU consumption).
2374 */
2375 if( ssl->nb_zero > 3 )
2376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 }
2381 }
2382 else
2383 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002386 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002387 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002388 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002389 }
2390 else
2391#endif
2392 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002393 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002394 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2395 if( ++ssl->in_ctr[i - 1] != 0 )
2396 break;
2397
2398 /* The loop goes to its end iff the counter is wrapping */
2399 if( i == ssl_ep_len( ssl ) )
2400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2402 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002403 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002404 }
2405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
2408 return( 0 );
2409}
2410
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002411#undef MAC_NONE
2412#undef MAC_PLAINTEXT
2413#undef MAC_CIPHERTEXT
2414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416/*
2417 * Compression/decompression functions
2418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002420{
2421 int ret;
2422 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002423 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002425 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002428
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002429 if( len_pre == 0 )
2430 return( 0 );
2431
Paul Bakker2770fbd2012-07-03 13:30:23 +00002432 memcpy( msg_pre, ssl->out_msg, len_pre );
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002435 ssl->out_msglen ) );
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438 ssl->out_msg, ssl->out_msglen );
2439
Paul Bakker48916f92012-09-16 19:57:18 +00002440 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2441 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2442 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002443 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002444
Paul Bakker48916f92012-09-16 19:57:18 +00002445 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446 if( ret != Z_OK )
2447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2449 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002450 }
2451
Angus Grattond8213d02016-05-25 20:56:48 +10002452 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002453 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002456 ssl->out_msglen ) );
2457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002459 ssl->out_msg, ssl->out_msglen );
2460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002462
2463 return( 0 );
2464}
2465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002467{
2468 int ret;
2469 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002470 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002471 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002472 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002475
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002476 if( len_pre == 0 )
2477 return( 0 );
2478
Paul Bakker2770fbd2012-07-03 13:30:23 +00002479 memcpy( msg_pre, ssl->in_msg, len_pre );
2480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002482 ssl->in_msglen ) );
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002485 ssl->in_msg, ssl->in_msglen );
2486
Paul Bakker48916f92012-09-16 19:57:18 +00002487 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2488 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2489 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002490 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002491 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002492
Paul Bakker48916f92012-09-16 19:57:18 +00002493 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002494 if( ret != Z_OK )
2495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2497 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002498 }
2499
Angus Grattond8213d02016-05-25 20:56:48 +10002500 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002501 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002504 ssl->in_msglen ) );
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002507 ssl->in_msg, ssl->in_msglen );
2508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002510
2511 return( 0 );
2512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2516static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_SSL_PROTO_DTLS)
2519static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002520{
2521 /* If renegotiation is not enforced, retransmit until we would reach max
2522 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002523 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002524 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002525 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002526 unsigned char doublings = 1;
2527
2528 while( ratio != 0 )
2529 {
2530 ++doublings;
2531 ratio >>= 1;
2532 }
2533
2534 if( ++ssl->renego_records_seen > doublings )
2535 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002537 return( 0 );
2538 }
2539 }
2540
2541 return( ssl_write_hello_request( ssl ) );
2542}
2543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002545
Paul Bakker5121ce52009-01-03 21:22:43 +00002546/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002547 * Fill the input message buffer by appending data to it.
2548 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002549 *
2550 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2551 * available (from this read and/or a previous one). Otherwise, an error code
2552 * is returned (possibly EOF or WANT_READ).
2553 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002554 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2555 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2556 * since we always read a whole datagram at once.
2557 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002558 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002559 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002562{
Paul Bakker23986e52011-04-24 08:57:21 +00002563 int ret;
2564 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002568 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002571 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002573 }
2574
Angus Grattond8213d02016-05-25 20:56:48 +10002575 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002579 }
2580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002582 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002584 uint32_t timeout;
2585
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002586 /* Just to be sure */
2587 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2588 {
2589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2590 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2592 }
2593
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002594 /*
2595 * The point is, we need to always read a full datagram at once, so we
2596 * sometimes read more then requested, and handle the additional data.
2597 * It could be the rest of the current record (while fetching the
2598 * header) and/or some other records in the same datagram.
2599 */
2600
2601 /*
2602 * Move to the next record in the already read datagram if applicable
2603 */
2604 if( ssl->next_record_offset != 0 )
2605 {
2606 if( ssl->in_left < ssl->next_record_offset )
2607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2609 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002610 }
2611
2612 ssl->in_left -= ssl->next_record_offset;
2613
2614 if( ssl->in_left != 0 )
2615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002617 ssl->next_record_offset ) );
2618 memmove( ssl->in_hdr,
2619 ssl->in_hdr + ssl->next_record_offset,
2620 ssl->in_left );
2621 }
2622
2623 ssl->next_record_offset = 0;
2624 }
2625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002628
2629 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002630 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002631 */
2632 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002635 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002636 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002637
2638 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002639 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002640 * are not at the beginning of a new record, the caller did something
2641 * wrong.
2642 */
2643 if( ssl->in_left != 0 )
2644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002647 }
2648
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002649 /*
2650 * Don't even try to read if time's out already.
2651 * This avoids by-passing the timer when repeatedly receiving messages
2652 * that will end up being dropped.
2653 */
2654 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002655 {
2656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002657 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002658 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002659 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002660 {
Angus Grattond8213d02016-05-25 20:56:48 +10002661 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002664 timeout = ssl->handshake->retransmit_timeout;
2665 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002666 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002669
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002670 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002671 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2672 timeout );
2673 else
2674 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002677
2678 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002680 }
2681
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002682 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002685 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002688 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002689 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002692 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002693 }
2694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002698 return( ret );
2699 }
2700
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002701 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002702 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002704 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002706 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002707 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002710 return( ret );
2711 }
2712
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002713 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002716 }
2717
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 if( ret < 0 )
2719 return( ret );
2720
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002721 ssl->in_left = ret;
2722 }
2723 else
2724#endif
2725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002727 ssl->in_left, nb_want ) );
2728
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002729 while( ssl->in_left < nb_want )
2730 {
2731 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002732
2733 if( ssl_check_timer( ssl ) != 0 )
2734 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2735 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002736 {
2737 if( ssl->f_recv_timeout != NULL )
2738 {
2739 ret = ssl->f_recv_timeout( ssl->p_bio,
2740 ssl->in_hdr + ssl->in_left, len,
2741 ssl->conf->read_timeout );
2742 }
2743 else
2744 {
2745 ret = ssl->f_recv( ssl->p_bio,
2746 ssl->in_hdr + ssl->in_left, len );
2747 }
2748 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002751 ssl->in_left, nb_want ) );
2752 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002753
2754 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002756
2757 if( ret < 0 )
2758 return( ret );
2759
mohammad160352aecb92018-03-28 23:41:40 -07002760 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002761 {
Darryl Green11999bb2018-03-13 15:22:58 +00002762 MBEDTLS_SSL_DEBUG_MSG( 1,
2763 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002764 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2766 }
2767
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002768 ssl->in_left += ret;
2769 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 }
2771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
2774 return( 0 );
2775}
2776
2777/*
2778 * Flush any data not yet written
2779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002781{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002782 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002783 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002787 if( ssl->f_send == NULL )
2788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002790 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002792 }
2793
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002794 /* Avoid incrementing counter if data is flushed */
2795 if( ssl->out_left == 0 )
2796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002798 return( 0 );
2799 }
2800
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 while( ssl->out_left > 0 )
2802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2804 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Hanno Becker2b1e3542018-08-06 11:19:13 +01002806 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002807 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 if( ret <= 0 )
2812 return( ret );
2813
mohammad160352aecb92018-03-28 23:41:40 -07002814 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002815 {
Darryl Green11999bb2018-03-13 15:22:58 +00002816 MBEDTLS_SSL_DEBUG_MSG( 1,
2817 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002818 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002819 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2820 }
2821
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 ssl->out_left -= ret;
2823 }
2824
Hanno Becker2b1e3542018-08-06 11:19:13 +01002825#if defined(MBEDTLS_SSL_PROTO_DTLS)
2826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002827 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002828 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002829 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002830 else
2831#endif
2832 {
2833 ssl->out_hdr = ssl->out_buf + 8;
2834 }
2835 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
2839 return( 0 );
2840}
2841
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002842/*
2843 * Functions to handle the DTLS retransmission state machine
2844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002846/*
2847 * Append current handshake message to current outgoing flight
2848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2853 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2854 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002855
2856 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002857 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002861 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002862 }
2863
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002864 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002865 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002868 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002869 }
2870
2871 /* Copy current handshake message with headers */
2872 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2873 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002874 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002875 msg->next = NULL;
2876
2877 /* Append to the current flight */
2878 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002879 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002880 else
2881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002883 while( cur->next != NULL )
2884 cur = cur->next;
2885 cur->next = msg;
2886 }
2887
Hanno Becker3b235902018-08-06 09:54:53 +01002888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002889 return( 0 );
2890}
2891
2892/*
2893 * Free the current flight of handshake messages
2894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 mbedtls_ssl_flight_item *cur = flight;
2898 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002899
2900 while( cur != NULL )
2901 {
2902 next = cur->next;
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 mbedtls_free( cur->p );
2905 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002906
2907 cur = next;
2908 }
2909}
2910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2912static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002913#endif
2914
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002915/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002916 * Swap transform_out and out_ctr with the alternative ones
2917 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002918static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002919{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002921 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002922#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2923 int ret;
2924#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002925
2926 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002929 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002930 }
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002933
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002934 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002935 tmp_transform = ssl->transform_out;
2936 ssl->transform_out = ssl->handshake->alt_transform_out;
2937 ssl->handshake->alt_transform_out = tmp_transform;
2938
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002939 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002940 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2941 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002942 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002943
2944 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002945 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2948 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2953 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002954 }
2955 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002956#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2957
2958 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002959}
2960
2961/*
2962 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002963 */
2964int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2965{
2966 int ret = 0;
2967
2968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2969
2970 ret = mbedtls_ssl_flight_transmit( ssl );
2971
2972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2973
2974 return( ret );
2975}
2976
2977/*
2978 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002979 *
2980 * Need to remember the current message in case flush_output returns
2981 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002982 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002983 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002984int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002985{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002986 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002990 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002992
2993 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002994 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002995 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2996 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002999 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003000
3001 while( ssl->handshake->cur_msg != NULL )
3002 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003003 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003004 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003005
Hanno Beckere1dcb032018-08-17 16:47:58 +01003006 int const is_finished =
3007 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3008 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3009
Hanno Becker04da1892018-08-14 13:22:10 +01003010 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3011 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3012
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003013 /* Swap epochs before sending Finished: we can't do it after
3014 * sending ChangeCipherSpec, in case write returns WANT_READ.
3015 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003016 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003017 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003019 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3020 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003021 }
3022
Hanno Becker67bc7c32018-08-06 11:33:50 +01003023 ret = ssl_get_remaining_payload_in_datagram( ssl );
3024 if( ret < 0 )
3025 return( ret );
3026 max_frag_len = (size_t) ret;
3027
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003028 /* CCS is copied as is, while HS messages may need fragmentation */
3029 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3030 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003031 if( max_frag_len == 0 )
3032 {
3033 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3034 return( ret );
3035
3036 continue;
3037 }
3038
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003039 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003040 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003041 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003042
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003043 /* Update position inside current message */
3044 ssl->handshake->cur_msg_p += cur->len;
3045 }
3046 else
3047 {
3048 const unsigned char * const p = ssl->handshake->cur_msg_p;
3049 const size_t hs_len = cur->len - 12;
3050 const size_t frag_off = p - ( cur->p + 12 );
3051 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003052 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003053
Hanno Beckere1dcb032018-08-17 16:47:58 +01003054 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003055 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003056 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003057 {
3058 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3059 return( ret );
3060 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061
Hanno Becker67bc7c32018-08-06 11:33:50 +01003062 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3063 return( ret );
3064
3065 continue;
3066 }
3067 max_hs_frag_len = max_frag_len - 12;
3068
3069 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3070 max_hs_frag_len : rem_len;
3071
3072 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003073 {
3074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003075 (unsigned) cur_hs_frag_len,
3076 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003077 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003078
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003079 /* Messages are stored with handshake headers as if not fragmented,
3080 * copy beginning of headers then fill fragmentation fields.
3081 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3082 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003083
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003084 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3085 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3086 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3087
Hanno Becker67bc7c32018-08-06 11:33:50 +01003088 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3089 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3090 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003091
3092 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3093
Hanno Becker3f7b9732018-08-28 09:53:25 +01003094 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003095 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3096 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003097 ssl->out_msgtype = cur->type;
3098
3099 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003100 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003101 }
3102
3103 /* If done with the current message move to the next one if any */
3104 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3105 {
3106 if( cur->next != NULL )
3107 {
3108 ssl->handshake->cur_msg = cur->next;
3109 ssl->handshake->cur_msg_p = cur->next->p + 12;
3110 }
3111 else
3112 {
3113 ssl->handshake->cur_msg = NULL;
3114 ssl->handshake->cur_msg_p = NULL;
3115 }
3116 }
3117
3118 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003119 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003122 return( ret );
3123 }
3124 }
3125
Hanno Becker67bc7c32018-08-06 11:33:50 +01003126 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3127 return( ret );
3128
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003129 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3131 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003132 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3136 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003137
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139
3140 return( 0 );
3141}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003142
3143/*
3144 * To be called when the last message of an incoming flight is received.
3145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003147{
3148 /* We won't need to resend that one any more */
3149 ssl_flight_free( ssl->handshake->flight );
3150 ssl->handshake->flight = NULL;
3151 ssl->handshake->cur_msg = NULL;
3152
3153 /* The next incoming flight will start with this msg_seq */
3154 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3155
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003156 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003157 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003158
Hanno Becker0271f962018-08-16 13:23:47 +01003159 /* Clear future message buffering structure. */
3160 ssl_buffering_free( ssl );
3161
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003162 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003163 ssl_set_timer( ssl, 0 );
3164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3166 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003169 }
3170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003172}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003173
3174/*
3175 * To be called when the last message of an outgoing flight is send.
3176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003178{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003179 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003180 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3183 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003186 }
3187 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003191
Paul Bakker5121ce52009-01-03 21:22:43 +00003192/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003193 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003195
3196/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003197 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003198 *
3199 * - fill in handshake headers
3200 * - update handshake checksum
3201 * - DTLS: save message for resending
3202 * - then pass to the record layer
3203 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003204 * DTLS: except for HelloRequest, messages are only queued, and will only be
3205 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003206 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003207 * Inputs:
3208 * - ssl->out_msglen: 4 + actual handshake message len
3209 * (4 is the size of handshake headers for TLS)
3210 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3211 * - ssl->out_msg + 4: the handshake message body
3212 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003213 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003214 * - ssl->out_msglen: the length of the record contents
3215 * (including handshake headers but excluding record headers)
3216 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003217 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003218int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003220 int ret;
3221 const size_t hs_len = ssl->out_msglen - 4;
3222 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3225
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003226 /*
3227 * Sanity checks
3228 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003229 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003230 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3231 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003232 /* In SSLv3, the client might send a NoCertificate alert. */
3233#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3234 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3235 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3236 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3237#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3238 {
3239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3241 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003244 /* Whenever we send anything different from a
3245 * HelloRequest we should be in a handshake - double check. */
3246 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3247 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003248 ssl->handshake == NULL )
3249 {
3250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003256 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003258 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003261 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003262#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003263
Hanno Beckerb50a2532018-08-06 11:52:54 +01003264 /* Double-check that we did not exceed the bounds
3265 * of the outgoing record buffer.
3266 * This should never fail as the various message
3267 * writing functions must obey the bounds of the
3268 * outgoing record buffer, but better be safe.
3269 *
3270 * Note: We deliberately do not check for the MTU or MFL here.
3271 */
3272 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3273 {
3274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3275 "size %u, maximum %u",
3276 (unsigned) ssl->out_msglen,
3277 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3279 }
3280
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003281 /*
3282 * Fill handshake headers
3283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003286 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3287 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3288 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003289
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003290 /*
3291 * DTLS has additional fields in the Handshake layer,
3292 * between the length field and the actual payload:
3293 * uint16 message_seq;
3294 * uint24 fragment_offset;
3295 * uint24 fragment_length;
3296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003298 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003299 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003300 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003301 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003302 {
3303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3304 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003305 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003306 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3308 }
3309
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003310 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003311 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003312
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003313 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003314 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003315 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003316 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3317 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3318 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003319 }
3320 else
3321 {
3322 ssl->out_msg[4] = 0;
3323 ssl->out_msg[5] = 0;
3324 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003325
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003326 /* Handshake hashes are computed without fragmentation,
3327 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003328 memset( ssl->out_msg + 6, 0x00, 3 );
3329 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003332
Hanno Becker0207e532018-08-28 10:28:28 +01003333 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003334 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3335 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 }
3337
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003338 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003341 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3342 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343 {
3344 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003347 return( ret );
3348 }
3349 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003350 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003352 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003353 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003354 {
3355 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3356 return( ret );
3357 }
3358 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003359
3360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3361
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003362 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003363}
3364
3365/*
3366 * Record layer functions
3367 */
3368
3369/*
3370 * Write current record.
3371 *
3372 * Uses:
3373 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3374 * - ssl->out_msglen: length of the record content (excl headers)
3375 * - ssl->out_msg: record content
3376 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003377int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003378{
3379 int ret, done = 0;
3380 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003381 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003382
3383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003386 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003388 {
3389 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003392 return( ret );
3393 }
3394
3395 len = ssl->out_msglen;
3396 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3400 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 ret = mbedtls_ssl_hw_record_write( ssl );
3405 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3408 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003409 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003410
3411 if( ret == 0 )
3412 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003415 if( !done )
3416 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003417 unsigned i;
3418 size_t protected_record_size;
3419
Paul Bakker05ef8352012-05-08 09:17:57 +00003420 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003422 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003423
Hanno Becker19859472018-08-06 09:40:20 +01003424 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003425 ssl->out_len[0] = (unsigned char)( len >> 8 );
3426 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003427
Paul Bakker48916f92012-09-16 19:57:18 +00003428 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003429 {
3430 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003433 return( ret );
3434 }
3435
3436 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003437 ssl->out_len[0] = (unsigned char)( len >> 8 );
3438 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003439 }
3440
Hanno Becker2b1e3542018-08-06 11:19:13 +01003441 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3442
3443#if defined(MBEDTLS_SSL_PROTO_DTLS)
3444 /* In case of DTLS, double-check that we don't exceed
3445 * the remaining space in the datagram. */
3446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3447 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003448 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003449 if( ret < 0 )
3450 return( ret );
3451
3452 if( protected_record_size > (size_t) ret )
3453 {
3454 /* Should never happen */
3455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3456 }
3457 }
3458#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003461 "version = [%d:%d], msglen = %d",
3462 ssl->out_hdr[0], ssl->out_hdr[1],
3463 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003466 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003467
3468 ssl->out_left += protected_record_size;
3469 ssl->out_hdr += protected_record_size;
3470 ssl_update_out_pointers( ssl, ssl->transform_out );
3471
Hanno Becker04484622018-08-06 09:49:38 +01003472 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3473 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3474 break;
3475
3476 /* The loop goes to its end iff the counter is wrapping */
3477 if( i == ssl_ep_len( ssl ) )
3478 {
3479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3480 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 }
3483
Hanno Becker67bc7c32018-08-06 11:33:50 +01003484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3486 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003487 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003488 size_t remaining;
3489 ret = ssl_get_remaining_payload_in_datagram( ssl );
3490 if( ret < 0 )
3491 {
3492 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3493 ret );
3494 return( ret );
3495 }
3496
3497 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003499 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003500 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003501 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003502 else
3503 {
Hanno Becker513815a2018-08-20 11:56:09 +01003504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 }
3506 }
3507#endif /* MBEDTLS_SSL_PROTO_DTLS */
3508
3509 if( ( flush == SSL_FORCE_FLUSH ) &&
3510 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 return( ret );
3514 }
3515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003517
3518 return( 0 );
3519}
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003522
3523static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3524{
3525 if( ssl->in_msglen < ssl->in_hslen ||
3526 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3527 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3528 {
3529 return( 1 );
3530 }
3531 return( 0 );
3532}
Hanno Becker44650b72018-08-16 12:51:11 +01003533
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003534static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003535{
3536 return( ( ssl->in_msg[9] << 16 ) |
3537 ( ssl->in_msg[10] << 8 ) |
3538 ssl->in_msg[11] );
3539}
3540
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003541static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003542{
3543 return( ( ssl->in_msg[6] << 16 ) |
3544 ( ssl->in_msg[7] << 8 ) |
3545 ssl->in_msg[8] );
3546}
3547
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003548static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003549{
3550 uint32_t msg_len, frag_off, frag_len;
3551
3552 msg_len = ssl_get_hs_total_len( ssl );
3553 frag_off = ssl_get_hs_frag_off( ssl );
3554 frag_len = ssl_get_hs_frag_len( ssl );
3555
3556 if( frag_off > msg_len )
3557 return( -1 );
3558
3559 if( frag_len > msg_len - frag_off )
3560 return( -1 );
3561
3562 if( frag_len + 12 > ssl->in_msglen )
3563 return( -1 );
3564
3565 return( 0 );
3566}
3567
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003568/*
3569 * Mark bits in bitmask (used for DTLS HS reassembly)
3570 */
3571static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3572{
3573 unsigned int start_bits, end_bits;
3574
3575 start_bits = 8 - ( offset % 8 );
3576 if( start_bits != 8 )
3577 {
3578 size_t first_byte_idx = offset / 8;
3579
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003580 /* Special case */
3581 if( len <= start_bits )
3582 {
3583 for( ; len != 0; len-- )
3584 mask[first_byte_idx] |= 1 << ( start_bits - len );
3585
3586 /* Avoid potential issues with offset or len becoming invalid */
3587 return;
3588 }
3589
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003590 offset += start_bits; /* Now offset % 8 == 0 */
3591 len -= start_bits;
3592
3593 for( ; start_bits != 0; start_bits-- )
3594 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3595 }
3596
3597 end_bits = len % 8;
3598 if( end_bits != 0 )
3599 {
3600 size_t last_byte_idx = ( offset + len ) / 8;
3601
3602 len -= end_bits; /* Now len % 8 == 0 */
3603
3604 for( ; end_bits != 0; end_bits-- )
3605 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3606 }
3607
3608 memset( mask + offset / 8, 0xFF, len / 8 );
3609}
3610
3611/*
3612 * Check that bitmask is full
3613 */
3614static int ssl_bitmask_check( unsigned char *mask, size_t len )
3615{
3616 size_t i;
3617
3618 for( i = 0; i < len / 8; i++ )
3619 if( mask[i] != 0xFF )
3620 return( -1 );
3621
3622 for( i = 0; i < len % 8; i++ )
3623 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3624 return( -1 );
3625
3626 return( 0 );
3627}
3628
Hanno Becker56e205e2018-08-16 09:06:12 +01003629/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003630static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003631 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003632{
Hanno Becker56e205e2018-08-16 09:06:12 +01003633 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003634
Hanno Becker56e205e2018-08-16 09:06:12 +01003635 alloc_len = 12; /* Handshake header */
3636 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003637
Hanno Beckerd07df862018-08-16 09:14:58 +01003638 if( add_bitmap )
3639 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003640
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003641 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003642}
Hanno Becker56e205e2018-08-16 09:06:12 +01003643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003645
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003646static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003647{
3648 return( ( ssl->in_msg[1] << 16 ) |
3649 ( ssl->in_msg[2] << 8 ) |
3650 ssl->in_msg[3] );
3651}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003652
Simon Butcher99000142016-10-13 17:21:01 +01003653int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003658 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003660 }
3661
Hanno Becker12555c62018-08-16 12:47:53 +01003662 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003665 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003666 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003670 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003671 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003672 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003673
Hanno Becker44650b72018-08-16 12:51:11 +01003674 if( ssl_check_hs_header( ssl ) != 0 )
3675 {
3676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3677 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3678 }
3679
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003680 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003681 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3682 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3683 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3684 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003685 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003686 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3687 {
3688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3689 recv_msg_seq,
3690 ssl->handshake->in_msg_seq ) );
3691 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3692 }
3693
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003694 /* Retransmit only on last message from previous flight, to avoid
3695 * too many retransmissions.
3696 * Besides, No sane server ever retransmits HelloVerifyRequest */
3697 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003701 "message_seq = %d, start_of_flight = %d",
3702 recv_msg_seq,
3703 ssl->handshake->in_flight_start_seq ) );
3704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003708 return( ret );
3709 }
3710 }
3711 else
3712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003714 "message_seq = %d, expected = %d",
3715 recv_msg_seq,
3716 ssl->handshake->in_msg_seq ) );
3717 }
3718
Hanno Becker90333da2017-10-10 11:27:13 +01003719 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003720 }
3721 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003722
Hanno Becker6d97ef52018-08-16 13:09:04 +01003723 /* Message reassembly is handled alongside buffering of future
3724 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003725 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003726 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003727 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003730 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003731 }
3732 }
3733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003735 /* With TLS we don't handle fragmentation (for now) */
3736 if( ssl->in_msglen < ssl->in_hslen )
3737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3739 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003740 }
3741
Simon Butcher99000142016-10-13 17:21:01 +01003742 return( 0 );
3743}
3744
3745void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3746{
Hanno Becker0271f962018-08-16 13:23:47 +01003747 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003748
Hanno Becker0271f962018-08-16 13:23:47 +01003749 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003750 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003751 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003752 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003753
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003754 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003757 ssl->handshake != NULL )
3758 {
Hanno Becker0271f962018-08-16 13:23:47 +01003759 unsigned offset;
3760 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003761
Hanno Becker0271f962018-08-16 13:23:47 +01003762 /* Increment handshake sequence number */
3763 hs->in_msg_seq++;
3764
3765 /*
3766 * Clear up handshake buffering and reassembly structure.
3767 */
3768
3769 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003770 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003771
3772 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003773 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3774 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003775 offset++, hs_buf++ )
3776 {
3777 *hs_buf = *(hs_buf + 1);
3778 }
3779
3780 /* Create a fresh last entry */
3781 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003782 }
3783#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003784}
3785
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003786/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003787 * DTLS anti-replay: RFC 6347 4.1.2.6
3788 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003789 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3790 * Bit n is set iff record number in_window_top - n has been seen.
3791 *
3792 * Usually, in_window_top is the last record number seen and the lsb of
3793 * in_window is set. The only exception is the initial state (record number 0
3794 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3797static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003798{
3799 ssl->in_window_top = 0;
3800 ssl->in_window = 0;
3801}
3802
3803static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3804{
3805 return( ( (uint64_t) buf[0] << 40 ) |
3806 ( (uint64_t) buf[1] << 32 ) |
3807 ( (uint64_t) buf[2] << 24 ) |
3808 ( (uint64_t) buf[3] << 16 ) |
3809 ( (uint64_t) buf[4] << 8 ) |
3810 ( (uint64_t) buf[5] ) );
3811}
3812
3813/*
3814 * Return 0 if sequence number is acceptable, -1 otherwise
3815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003817{
3818 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3819 uint64_t bit;
3820
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003821 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003822 return( 0 );
3823
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003824 if( rec_seqnum > ssl->in_window_top )
3825 return( 0 );
3826
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003827 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003828
3829 if( bit >= 64 )
3830 return( -1 );
3831
3832 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3833 return( -1 );
3834
3835 return( 0 );
3836}
3837
3838/*
3839 * Update replay window on new validated record
3840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003842{
3843 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3844
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003845 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003846 return;
3847
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003848 if( rec_seqnum > ssl->in_window_top )
3849 {
3850 /* Update window_top and the contents of the window */
3851 uint64_t shift = rec_seqnum - ssl->in_window_top;
3852
3853 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003854 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003855 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003856 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003857 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003858 ssl->in_window |= 1;
3859 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003860
3861 ssl->in_window_top = rec_seqnum;
3862 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003863 else
3864 {
3865 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003866 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003867
3868 if( bit < 64 ) /* Always true, but be extra sure */
3869 ssl->in_window |= (uint64_t) 1 << bit;
3870 }
3871}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003873
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003874#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003875/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003876static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3877
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003878/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003879 * Without any SSL context, check if a datagram looks like a ClientHello with
3880 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003881 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003882 *
3883 * - if cookie is valid, return 0
3884 * - if ClientHello looks superficially valid but cookie is not,
3885 * fill obuf and set olen, then
3886 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3887 * - otherwise return a specific error code
3888 */
3889static int ssl_check_dtls_clihlo_cookie(
3890 mbedtls_ssl_cookie_write_t *f_cookie_write,
3891 mbedtls_ssl_cookie_check_t *f_cookie_check,
3892 void *p_cookie,
3893 const unsigned char *cli_id, size_t cli_id_len,
3894 const unsigned char *in, size_t in_len,
3895 unsigned char *obuf, size_t buf_len, size_t *olen )
3896{
3897 size_t sid_len, cookie_len;
3898 unsigned char *p;
3899
3900 if( f_cookie_write == NULL || f_cookie_check == NULL )
3901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3902
3903 /*
3904 * Structure of ClientHello with record and handshake headers,
3905 * and expected values. We don't need to check a lot, more checks will be
3906 * done when actually parsing the ClientHello - skipping those checks
3907 * avoids code duplication and does not make cookie forging any easier.
3908 *
3909 * 0-0 ContentType type; copied, must be handshake
3910 * 1-2 ProtocolVersion version; copied
3911 * 3-4 uint16 epoch; copied, must be 0
3912 * 5-10 uint48 sequence_number; copied
3913 * 11-12 uint16 length; (ignored)
3914 *
3915 * 13-13 HandshakeType msg_type; (ignored)
3916 * 14-16 uint24 length; (ignored)
3917 * 17-18 uint16 message_seq; copied
3918 * 19-21 uint24 fragment_offset; copied, must be 0
3919 * 22-24 uint24 fragment_length; (ignored)
3920 *
3921 * 25-26 ProtocolVersion client_version; (ignored)
3922 * 27-58 Random random; (ignored)
3923 * 59-xx SessionID session_id; 1 byte len + sid_len content
3924 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3925 * ...
3926 *
3927 * Minimum length is 61 bytes.
3928 */
3929 if( in_len < 61 ||
3930 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3931 in[3] != 0 || in[4] != 0 ||
3932 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3933 {
3934 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3935 }
3936
3937 sid_len = in[59];
3938 if( sid_len > in_len - 61 )
3939 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3940
3941 cookie_len = in[60 + sid_len];
3942 if( cookie_len > in_len - 60 )
3943 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3944
3945 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3946 cli_id, cli_id_len ) == 0 )
3947 {
3948 /* Valid cookie */
3949 return( 0 );
3950 }
3951
3952 /*
3953 * If we get here, we've got an invalid cookie, let's prepare HVR.
3954 *
3955 * 0-0 ContentType type; copied
3956 * 1-2 ProtocolVersion version; copied
3957 * 3-4 uint16 epoch; copied
3958 * 5-10 uint48 sequence_number; copied
3959 * 11-12 uint16 length; olen - 13
3960 *
3961 * 13-13 HandshakeType msg_type; hello_verify_request
3962 * 14-16 uint24 length; olen - 25
3963 * 17-18 uint16 message_seq; copied
3964 * 19-21 uint24 fragment_offset; copied
3965 * 22-24 uint24 fragment_length; olen - 25
3966 *
3967 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3968 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3969 *
3970 * Minimum length is 28.
3971 */
3972 if( buf_len < 28 )
3973 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3974
3975 /* Copy most fields and adapt others */
3976 memcpy( obuf, in, 25 );
3977 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3978 obuf[25] = 0xfe;
3979 obuf[26] = 0xff;
3980
3981 /* Generate and write actual cookie */
3982 p = obuf + 28;
3983 if( f_cookie_write( p_cookie,
3984 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3985 {
3986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3987 }
3988
3989 *olen = p - obuf;
3990
3991 /* Go back and fill length fields */
3992 obuf[27] = (unsigned char)( *olen - 28 );
3993
3994 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3995 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3996 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3997
3998 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3999 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4000
4001 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4002}
4003
4004/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004005 * Handle possible client reconnect with the same UDP quadruplet
4006 * (RFC 6347 Section 4.2.8).
4007 *
4008 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4009 * that looks like a ClientHello.
4010 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004011 * - if the input looks like a ClientHello without cookies,
4012 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004013 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004014 * - if the input looks like a ClientHello with a valid cookie,
4015 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004016 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004017 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004018 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004019 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004020 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4021 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004022 */
4023static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4024{
4025 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004026 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004027
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004028 ret = ssl_check_dtls_clihlo_cookie(
4029 ssl->conf->f_cookie_write,
4030 ssl->conf->f_cookie_check,
4031 ssl->conf->p_cookie,
4032 ssl->cli_id, ssl->cli_id_len,
4033 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004034 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004035
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004036 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4037
4038 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004039 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004040 int send_ret;
4041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4042 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4043 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004044 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004045 * If the error is permanent we'll catch it later,
4046 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004047 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4048 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4049 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004050
4051 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004052 }
4053
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004054 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004055 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004057 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4058 {
4059 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4060 return( ret );
4061 }
4062
4063 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004064 }
4065
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004066 return( ret );
4067}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004068#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004069
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004070/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004071 * ContentType type;
4072 * ProtocolVersion version;
4073 * uint16 epoch; // DTLS only
4074 * uint48 sequence_number; // DTLS only
4075 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004076 *
4077 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004078 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004079 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4080 *
4081 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004082 * 1. proceed with the record if this function returns 0
4083 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4084 * 3. return CLIENT_RECONNECT if this function return that value
4085 * 4. drop the whole datagram if this function returns anything else.
4086 * Point 2 is needed when the peer is resending, and we have already received
4087 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004090{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004091 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 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 +02004094
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004096 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004097 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004101 ssl->in_msgtype,
4102 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004103
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004104 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4106 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4107 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4108 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004111
4112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004113 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4114 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4116#endif /* MBEDTLS_SSL_PROTO_DTLS */
4117 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4118 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004121 }
4122
4123 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004124 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004128 }
4129
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004130 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4133 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 }
4135
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004136 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004137 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004138 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4141 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004142 }
4143
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004144 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004145 * DTLS-related tests.
4146 * Check epoch before checking length constraint because
4147 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4148 * message gets duplicated before the corresponding Finished message,
4149 * the second ChangeCipherSpec should be discarded because it belongs
4150 * to an old epoch, but not because its length is shorter than
4151 * the minimum record length for packets using the new record transform.
4152 * Note that these two kinds of failures are handled differently,
4153 * as an unexpected record is silently skipped but an invalid
4154 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004155 */
4156#if defined(MBEDTLS_SSL_PROTO_DTLS)
4157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4158 {
4159 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4160
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004161 /* Check epoch (and sequence number) with DTLS */
4162 if( rec_epoch != ssl->in_epoch )
4163 {
4164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4165 "expected %d, received %d",
4166 ssl->in_epoch, rec_epoch ) );
4167
4168#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4169 /*
4170 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4171 * access the first byte of record content (handshake type), as we
4172 * have an active transform (possibly iv_len != 0), so use the
4173 * fact that the record header len is 13 instead.
4174 */
4175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4176 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4177 rec_epoch == 0 &&
4178 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4179 ssl->in_left > 13 &&
4180 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4181 {
4182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4183 "from the same port" ) );
4184 return( ssl_handle_possible_reconnect( ssl ) );
4185 }
4186 else
4187#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004188 {
4189 /* Consider buffering the record. */
4190 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4191 {
4192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4193 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4194 }
4195
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004196 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004197 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004198 }
4199
4200#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4201 /* Replay detection only works for the current epoch */
4202 if( rec_epoch == ssl->in_epoch &&
4203 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4204 {
4205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4206 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4207 }
4208#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004209
Hanno Becker52c6dc62017-05-26 16:07:36 +01004210 /* Drop unexpected ApplicationData records,
4211 * except at the beginning of renegotiations */
4212 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4213 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4214#if defined(MBEDTLS_SSL_RENEGOTIATION)
4215 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4216 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4217#endif
4218 )
4219 {
4220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4221 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4222 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004223 }
4224#endif /* MBEDTLS_SSL_PROTO_DTLS */
4225
Hanno Becker52c6dc62017-05-26 16:07:36 +01004226
4227 /* Check length against bounds of the current transform and version */
4228 if( ssl->transform_in == NULL )
4229 {
4230 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004231 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4234 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4235 }
4236 }
4237 else
4238 {
4239 if( ssl->in_msglen < ssl->transform_in->minlen )
4240 {
4241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4242 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4243 }
4244
4245#if defined(MBEDTLS_SSL_PROTO_SSL3)
4246 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004247 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004248 {
4249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4250 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4251 }
4252#endif
4253#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4254 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4255 /*
4256 * TLS encrypted messages can have up to 256 bytes of padding
4257 */
4258 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4259 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004260 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004261 {
4262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4263 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4264 }
4265#endif
4266 }
4267
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004268 return( 0 );
4269}
Paul Bakker5121ce52009-01-03 21:22:43 +00004270
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004271/*
4272 * If applicable, decrypt (and decompress) record content
4273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004275{
4276 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4279 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4282 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 ret = mbedtls_ssl_hw_record_read( ssl );
4287 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4290 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004291 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004292
4293 if( ret == 0 )
4294 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004295 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004297 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004298 {
4299 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004302 return( ret );
4303 }
4304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004306 ssl->in_msg, ssl->in_msglen );
4307
Angus Grattond8213d02016-05-25 20:56:48 +10004308 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4311 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 }
4313 }
4314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004316 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004318 {
4319 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004322 return( ret );
4323 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004328 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004331 }
4332#endif
4333
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004334 return( 0 );
4335}
4336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004338
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004339/*
4340 * Read a record.
4341 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004342 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4343 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4344 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004345 */
Hanno Becker1097b342018-08-15 14:09:41 +01004346
4347/* Helper functions for mbedtls_ssl_read_record(). */
4348static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004349static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4350static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004351
Hanno Becker327c93b2018-08-15 13:56:18 +01004352int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004353 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004354{
4355 int ret;
4356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004358
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004359 if( ssl->keep_current_message == 0 )
4360 {
4361 do {
Simon Butcher99000142016-10-13 17:21:01 +01004362
Hanno Becker26994592018-08-15 14:14:59 +01004363 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004364 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004365 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004366
Hanno Beckere74d5562018-08-15 14:26:08 +01004367 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004368 {
Hanno Becker40f50842018-08-15 14:48:01 +01004369#if defined(MBEDTLS_SSL_PROTO_DTLS)
4370 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004371
Hanno Becker40f50842018-08-15 14:48:01 +01004372 /* We only check for buffered messages if the
4373 * current datagram is fully consumed. */
4374 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004375 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004376 {
Hanno Becker40f50842018-08-15 14:48:01 +01004377 if( ssl_load_buffered_message( ssl ) == 0 )
4378 have_buffered = 1;
4379 }
4380
4381 if( have_buffered == 0 )
4382#endif /* MBEDTLS_SSL_PROTO_DTLS */
4383 {
4384 ret = ssl_get_next_record( ssl );
4385 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4386 continue;
4387
4388 if( ret != 0 )
4389 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004390 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004391 return( ret );
4392 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004393 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004394 }
4395
4396 ret = mbedtls_ssl_handle_message_type( ssl );
4397
Hanno Becker40f50842018-08-15 14:48:01 +01004398#if defined(MBEDTLS_SSL_PROTO_DTLS)
4399 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4400 {
4401 /* Buffer future message */
4402 ret = ssl_buffer_message( ssl );
4403 if( ret != 0 )
4404 return( ret );
4405
4406 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4407 }
4408#endif /* MBEDTLS_SSL_PROTO_DTLS */
4409
Hanno Becker90333da2017-10-10 11:27:13 +01004410 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4411 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004412
4413 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004414 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004415 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004416 return( ret );
4417 }
4418
Hanno Becker327c93b2018-08-15 13:56:18 +01004419 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004420 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004421 {
4422 mbedtls_ssl_update_handshake_status( ssl );
4423 }
Simon Butcher99000142016-10-13 17:21:01 +01004424 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004425 else
Simon Butcher99000142016-10-13 17:21:01 +01004426 {
Hanno Becker02f59072018-08-15 14:00:24 +01004427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004428 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004429 }
4430
4431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4432
4433 return( 0 );
4434}
4435
Hanno Becker40f50842018-08-15 14:48:01 +01004436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004437static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004438{
Hanno Becker40f50842018-08-15 14:48:01 +01004439 if( ssl->in_left > ssl->next_record_offset )
4440 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004441
Hanno Becker40f50842018-08-15 14:48:01 +01004442 return( 0 );
4443}
4444
4445static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4446{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004447 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004448 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004449 int ret = 0;
4450
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004451 if( hs == NULL )
4452 return( -1 );
4453
Hanno Beckere00ae372018-08-20 09:39:42 +01004454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4455
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004456 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4457 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4458 {
4459 /* Check if we have seen a ChangeCipherSpec before.
4460 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004461 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004462 {
4463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4464 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004465 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004466 }
4467
Hanno Becker39b8bc92018-08-28 17:17:13 +01004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004469 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4470 ssl->in_msglen = 1;
4471 ssl->in_msg[0] = 1;
4472
4473 /* As long as they are equal, the exact value doesn't matter. */
4474 ssl->in_left = 0;
4475 ssl->next_record_offset = 0;
4476
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004477 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004478 goto exit;
4479 }
Hanno Becker37f95322018-08-16 13:55:32 +01004480
Hanno Beckerb8f50142018-08-28 10:01:34 +01004481#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004482 /* Debug only */
4483 {
4484 unsigned offset;
4485 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4486 {
4487 hs_buf = &hs->buffering.hs[offset];
4488 if( hs_buf->is_valid == 1 )
4489 {
4490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4491 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004492 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004493 }
4494 }
4495 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004496#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004497
4498 /* Check if we have buffered and/or fully reassembled the
4499 * next handshake message. */
4500 hs_buf = &hs->buffering.hs[0];
4501 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4502 {
4503 /* Synthesize a record containing the buffered HS message. */
4504 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4505 ( hs_buf->data[2] << 8 ) |
4506 hs_buf->data[3];
4507
4508 /* Double-check that we haven't accidentally buffered
4509 * a message that doesn't fit into the input buffer. */
4510 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4511 {
4512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4514 }
4515
4516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4517 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4518 hs_buf->data, msg_len + 12 );
4519
4520 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4521 ssl->in_hslen = msg_len + 12;
4522 ssl->in_msglen = msg_len + 12;
4523 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4524
4525 ret = 0;
4526 goto exit;
4527 }
4528 else
4529 {
4530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4531 hs->in_msg_seq ) );
4532 }
4533
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004534 ret = -1;
4535
4536exit:
4537
4538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4539 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004540}
4541
Hanno Beckera02b0b42018-08-21 17:20:27 +01004542static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4543 size_t desired )
4544{
4545 int offset;
4546 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4548 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004549
Hanno Becker01315ea2018-08-21 17:22:17 +01004550 /* Get rid of future records epoch first, if such exist. */
4551 ssl_free_buffered_record( ssl );
4552
4553 /* Check if we have enough space available now. */
4554 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4555 hs->buffering.total_bytes_buffered ) )
4556 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004558 return( 0 );
4559 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004560
Hanno Becker4f432ad2018-08-28 10:02:32 +01004561 /* We don't have enough space to buffer the next expected handshake
4562 * message. Remove buffers used for future messages to gain space,
4563 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004564 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4565 offset >= 0; offset-- )
4566 {
4567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4568 offset ) );
4569
Hanno Beckerb309b922018-08-23 13:18:05 +01004570 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004571
4572 /* Check if we have enough space available now. */
4573 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4574 hs->buffering.total_bytes_buffered ) )
4575 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004577 return( 0 );
4578 }
4579 }
4580
4581 return( -1 );
4582}
4583
Hanno Becker40f50842018-08-15 14:48:01 +01004584static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4585{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004586 int ret = 0;
4587 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4588
4589 if( hs == NULL )
4590 return( 0 );
4591
4592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4593
4594 switch( ssl->in_msgtype )
4595 {
4596 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004598
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004599 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004600 break;
4601
4602 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004603 {
4604 unsigned recv_msg_seq_offset;
4605 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4606 mbedtls_ssl_hs_buffer *hs_buf;
4607 size_t msg_len = ssl->in_hslen - 12;
4608
4609 /* We should never receive an old handshake
4610 * message - double-check nonetheless. */
4611 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4612 {
4613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4615 }
4616
4617 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4618 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4619 {
4620 /* Silently ignore -- message too far in the future */
4621 MBEDTLS_SSL_DEBUG_MSG( 2,
4622 ( "Ignore future HS message with sequence number %u, "
4623 "buffering window %u - %u",
4624 recv_msg_seq, ssl->handshake->in_msg_seq,
4625 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4626
4627 goto exit;
4628 }
4629
4630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4631 recv_msg_seq, recv_msg_seq_offset ) );
4632
4633 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4634
4635 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004636 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004637 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004638 size_t reassembly_buf_sz;
4639
Hanno Becker37f95322018-08-16 13:55:32 +01004640 hs_buf->is_fragmented =
4641 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4642
4643 /* We copy the message back into the input buffer
4644 * after reassembly, so check that it's not too large.
4645 * This is an implementation-specific limitation
4646 * and not one from the standard, hence it is not
4647 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004648 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004649 {
4650 /* Ignore message */
4651 goto exit;
4652 }
4653
Hanno Beckere0b150f2018-08-21 15:51:03 +01004654 /* Check if we have enough space to buffer the message. */
4655 if( hs->buffering.total_bytes_buffered >
4656 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4657 {
4658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4660 }
4661
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004662 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4663 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004664
4665 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4666 hs->buffering.total_bytes_buffered ) )
4667 {
4668 if( recv_msg_seq_offset > 0 )
4669 {
4670 /* If we can't buffer a future message because
4671 * of space limitations -- ignore. */
4672 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",
4673 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4674 (unsigned) hs->buffering.total_bytes_buffered ) );
4675 goto exit;
4676 }
Hanno Beckere1801392018-08-21 16:51:05 +01004677 else
4678 {
4679 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",
4680 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4681 (unsigned) hs->buffering.total_bytes_buffered ) );
4682 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004683
Hanno Beckera02b0b42018-08-21 17:20:27 +01004684 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004685 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004686 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",
4687 (unsigned) msg_len,
4688 (unsigned) reassembly_buf_sz,
4689 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004690 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004691 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4692 goto exit;
4693 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004694 }
4695
4696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4697 msg_len ) );
4698
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004699 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4700 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004701 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004702 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004703 goto exit;
4704 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004705 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004706
4707 /* Prepare final header: copy msg_type, length and message_seq,
4708 * then add standardised fragment_offset and fragment_length */
4709 memcpy( hs_buf->data, ssl->in_msg, 6 );
4710 memset( hs_buf->data + 6, 0, 3 );
4711 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4712
4713 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004714
4715 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004716 }
4717 else
4718 {
4719 /* Make sure msg_type and length are consistent */
4720 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4723 /* Ignore */
4724 goto exit;
4725 }
4726 }
4727
Hanno Becker4422bbb2018-08-20 09:40:19 +01004728 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004729 {
4730 size_t frag_len, frag_off;
4731 unsigned char * const msg = hs_buf->data + 12;
4732
4733 /*
4734 * Check and copy current fragment
4735 */
4736
4737 /* Validation of header fields already done in
4738 * mbedtls_ssl_prepare_handshake_record(). */
4739 frag_off = ssl_get_hs_frag_off( ssl );
4740 frag_len = ssl_get_hs_frag_len( ssl );
4741
4742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4743 frag_off, frag_len ) );
4744 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4745
4746 if( hs_buf->is_fragmented )
4747 {
4748 unsigned char * const bitmask = msg + msg_len;
4749 ssl_bitmask_set( bitmask, frag_off, frag_len );
4750 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4751 msg_len ) == 0 );
4752 }
4753 else
4754 {
4755 hs_buf->is_complete = 1;
4756 }
4757
4758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4759 hs_buf->is_complete ? "" : "not yet " ) );
4760 }
4761
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004762 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004763 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004764
4765 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004766 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004767 break;
4768 }
4769
4770exit:
4771
4772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4773 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004774}
4775#endif /* MBEDTLS_SSL_PROTO_DTLS */
4776
Hanno Becker1097b342018-08-15 14:09:41 +01004777static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004778{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004779 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004780 * Consume last content-layer message and potentially
4781 * update in_msglen which keeps track of the contents'
4782 * consumption state.
4783 *
4784 * (1) Handshake messages:
4785 * Remove last handshake message, move content
4786 * and adapt in_msglen.
4787 *
4788 * (2) Alert messages:
4789 * Consume whole record content, in_msglen = 0.
4790 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004791 * (3) Change cipher spec:
4792 * Consume whole record content, in_msglen = 0.
4793 *
4794 * (4) Application data:
4795 * Don't do anything - the record layer provides
4796 * the application data as a stream transport
4797 * and consumes through mbedtls_ssl_read only.
4798 *
4799 */
4800
4801 /* Case (1): Handshake messages */
4802 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004803 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004804 /* Hard assertion to be sure that no application data
4805 * is in flight, as corrupting ssl->in_msglen during
4806 * ssl->in_offt != NULL is fatal. */
4807 if( ssl->in_offt != NULL )
4808 {
4809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4811 }
4812
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004813 /*
4814 * Get next Handshake message in the current record
4815 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004816
Hanno Becker4a810fb2017-05-24 16:27:30 +01004817 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004818 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004819 * current handshake content: If DTLS handshake
4820 * fragmentation is used, that's the fragment
4821 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004822 * size here is faulty and should be changed at
4823 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004824 * (2) While it doesn't seem to cause problems, one
4825 * has to be very careful not to assume that in_hslen
4826 * is always <= in_msglen in a sensible communication.
4827 * Again, it's wrong for DTLS handshake fragmentation.
4828 * The following check is therefore mandatory, and
4829 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004830 * Additionally, ssl->in_hslen might be arbitrarily out of
4831 * bounds after handling a DTLS message with an unexpected
4832 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004833 */
4834 if( ssl->in_hslen < ssl->in_msglen )
4835 {
4836 ssl->in_msglen -= ssl->in_hslen;
4837 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4838 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004839
Hanno Becker4a810fb2017-05-24 16:27:30 +01004840 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4841 ssl->in_msg, ssl->in_msglen );
4842 }
4843 else
4844 {
4845 ssl->in_msglen = 0;
4846 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004847
Hanno Becker4a810fb2017-05-24 16:27:30 +01004848 ssl->in_hslen = 0;
4849 }
4850 /* Case (4): Application data */
4851 else if( ssl->in_offt != NULL )
4852 {
4853 return( 0 );
4854 }
4855 /* Everything else (CCS & Alerts) */
4856 else
4857 {
4858 ssl->in_msglen = 0;
4859 }
4860
Hanno Becker1097b342018-08-15 14:09:41 +01004861 return( 0 );
4862}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004863
Hanno Beckere74d5562018-08-15 14:26:08 +01004864static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4865{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004866 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004867 return( 1 );
4868
4869 return( 0 );
4870}
4871
Hanno Becker5f066e72018-08-16 14:56:31 +01004872#if defined(MBEDTLS_SSL_PROTO_DTLS)
4873
4874static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4875{
4876 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4877 if( hs == NULL )
4878 return;
4879
Hanno Becker01315ea2018-08-21 17:22:17 +01004880 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004881 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004882 hs->buffering.total_bytes_buffered -=
4883 hs->buffering.future_record.len;
4884
4885 mbedtls_free( hs->buffering.future_record.data );
4886 hs->buffering.future_record.data = NULL;
4887 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004888}
4889
4890static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4891{
4892 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4893 unsigned char * rec;
4894 size_t rec_len;
4895 unsigned rec_epoch;
4896
4897 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4898 return( 0 );
4899
4900 if( hs == NULL )
4901 return( 0 );
4902
Hanno Becker5f066e72018-08-16 14:56:31 +01004903 rec = hs->buffering.future_record.data;
4904 rec_len = hs->buffering.future_record.len;
4905 rec_epoch = hs->buffering.future_record.epoch;
4906
4907 if( rec == NULL )
4908 return( 0 );
4909
Hanno Becker4cb782d2018-08-20 11:19:05 +01004910 /* Only consider loading future records if the
4911 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004912 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004913 return( 0 );
4914
Hanno Becker5f066e72018-08-16 14:56:31 +01004915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4916
4917 if( rec_epoch != ssl->in_epoch )
4918 {
4919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4920 goto exit;
4921 }
4922
4923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4924
4925 /* Double-check that the record is not too large */
4926 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4927 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4928 {
4929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4931 }
4932
4933 memcpy( ssl->in_hdr, rec, rec_len );
4934 ssl->in_left = rec_len;
4935 ssl->next_record_offset = 0;
4936
4937 ssl_free_buffered_record( ssl );
4938
4939exit:
4940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4941 return( 0 );
4942}
4943
4944static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4945{
4946 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4947 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004948 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004949
4950 /* Don't buffer future records outside handshakes. */
4951 if( hs == NULL )
4952 return( 0 );
4953
4954 /* Only buffer handshake records (we are only interested
4955 * in Finished messages). */
4956 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4957 return( 0 );
4958
4959 /* Don't buffer more than one future epoch record. */
4960 if( hs->buffering.future_record.data != NULL )
4961 return( 0 );
4962
Hanno Becker01315ea2018-08-21 17:22:17 +01004963 /* Don't buffer record if there's not enough buffering space remaining. */
4964 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4965 hs->buffering.total_bytes_buffered ) )
4966 {
4967 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",
4968 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4969 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004970 return( 0 );
4971 }
4972
Hanno Becker5f066e72018-08-16 14:56:31 +01004973 /* Buffer record */
4974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4975 ssl->in_epoch + 1 ) );
4976 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4977 rec_hdr_len + ssl->in_msglen );
4978
4979 /* ssl_parse_record_header() only considers records
4980 * of the next epoch as candidates for buffering. */
4981 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004982 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004983
4984 hs->buffering.future_record.data =
4985 mbedtls_calloc( 1, hs->buffering.future_record.len );
4986 if( hs->buffering.future_record.data == NULL )
4987 {
4988 /* If we run out of RAM trying to buffer a
4989 * record from the next epoch, just ignore. */
4990 return( 0 );
4991 }
4992
Hanno Becker01315ea2018-08-21 17:22:17 +01004993 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004994
Hanno Becker01315ea2018-08-21 17:22:17 +01004995 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004996 return( 0 );
4997}
4998
4999#endif /* MBEDTLS_SSL_PROTO_DTLS */
5000
Hanno Beckere74d5562018-08-15 14:26:08 +01005001static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005002{
5003 int ret;
5004
Hanno Becker5f066e72018-08-16 14:56:31 +01005005#if defined(MBEDTLS_SSL_PROTO_DTLS)
5006 /* We might have buffered a future record; if so,
5007 * and if the epoch matches now, load it.
5008 * On success, this call will set ssl->in_left to
5009 * the length of the buffered record, so that
5010 * the calls to ssl_fetch_input() below will
5011 * essentially be no-ops. */
5012 ret = ssl_load_buffered_record( ssl );
5013 if( ret != 0 )
5014 return( ret );
5015#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005020 return( ret );
5021 }
5022
5023 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5027 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005028 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005029 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5030 {
5031 ret = ssl_buffer_future_record( ssl );
5032 if( ret != 0 )
5033 return( ret );
5034
5035 /* Fall through to handling of unexpected records */
5036 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5037 }
5038
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005039 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5040 {
5041 /* Skip unexpected record (but not whole datagram) */
5042 ssl->next_record_offset = ssl->in_msglen
5043 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005044
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5046 "(header)" ) );
5047 }
5048 else
5049 {
5050 /* Skip invalid record and the rest of the datagram */
5051 ssl->next_record_offset = 0;
5052 ssl->in_left = 0;
5053
5054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5055 "(header)" ) );
5056 }
5057
5058 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005059 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005060 }
5061#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005062 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005063 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005064
5065 /*
5066 * Read and optionally decrypt the message contents
5067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5069 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005072 return( ret );
5073 }
5074
5075 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005080 if( ssl->next_record_offset < ssl->in_left )
5081 {
5082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5083 }
5084 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005085 else
5086#endif
5087 ssl->in_left = 0;
5088
5089 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005093 {
5094 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5096 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005097 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005098 /* Except when waiting for Finished as a bad mac here
5099 * probably means something went wrong in the handshake
5100 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5101 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5102 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5103 {
5104#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5105 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5106 {
5107 mbedtls_ssl_send_alert_message( ssl,
5108 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5109 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5110 }
5111#endif
5112 return( ret );
5113 }
5114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005116 if( ssl->conf->badmac_limit != 0 &&
5117 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5120 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005121 }
5122#endif
5123
Hanno Becker4a810fb2017-05-24 16:27:30 +01005124 /* As above, invalid records cause
5125 * dismissal of the whole datagram. */
5126
5127 ssl->next_record_offset = 0;
5128 ssl->in_left = 0;
5129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005131 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005132 }
5133
5134 return( ret );
5135 }
5136 else
5137#endif
5138 {
5139 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5141 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 mbedtls_ssl_send_alert_message( ssl,
5144 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5145 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005146 }
5147#endif
5148 return( ret );
5149 }
5150 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005151
Simon Butcher99000142016-10-13 17:21:01 +01005152 return( 0 );
5153}
5154
5155int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5156{
5157 int ret;
5158
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005159 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005160 * Handle particular types of records
5161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005163 {
Simon Butcher99000142016-10-13 17:21:01 +01005164 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5165 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005166 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005168 }
5169
Hanno Beckere678eaa2018-08-21 14:57:46 +01005170 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005171 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005172 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005173 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5175 ssl->in_msglen ) );
5176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005177 }
5178
Hanno Beckere678eaa2018-08-21 14:57:46 +01005179 if( ssl->in_msg[0] != 1 )
5180 {
5181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5182 ssl->in_msg[0] ) );
5183 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5184 }
5185
5186#if defined(MBEDTLS_SSL_PROTO_DTLS)
5187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5188 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5189 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5190 {
5191 if( ssl->handshake == NULL )
5192 {
5193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5194 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5195 }
5196
5197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5198 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5199 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005200#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005201 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005204 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005205 if( ssl->in_msglen != 2 )
5206 {
5207 /* Note: Standard allows for more than one 2 byte alert
5208 to be packed in a single message, but Mbed TLS doesn't
5209 currently support this. */
5210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5211 ssl->in_msglen ) );
5212 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5213 }
5214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005216 ssl->in_msg[0], ssl->in_msg[1] ) );
5217
5218 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005219 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005224 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005226 }
5227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5229 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5232 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005233 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005234
5235#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5236 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5237 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5238 {
Hanno Becker90333da2017-10-10 11:27:13 +01005239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005240 /* Will be handled when trying to parse ServerHello */
5241 return( 0 );
5242 }
5243#endif
5244
5245#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5246 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5247 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5248 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5249 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5250 {
5251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5252 /* Will be handled in mbedtls_ssl_parse_certificate() */
5253 return( 0 );
5254 }
5255#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5256
5257 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005258 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005259 }
5260
Hanno Beckerc76c6192017-06-06 10:03:17 +01005261#if defined(MBEDTLS_SSL_PROTO_DTLS)
5262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5263 ssl->handshake != NULL &&
5264 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5265 {
5266 ssl_handshake_wrapup_free_hs_transform( ssl );
5267 }
5268#endif
5269
Paul Bakker5121ce52009-01-03 21:22:43 +00005270 return( 0 );
5271}
5272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005274{
5275 int ret;
5276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5278 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5279 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005280 {
5281 return( ret );
5282 }
5283
5284 return( 0 );
5285}
5286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005288 unsigned char level,
5289 unsigned char message )
5290{
5291 int ret;
5292
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005293 if( ssl == NULL || ssl->conf == NULL )
5294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005300 ssl->out_msglen = 2;
5301 ssl->out_msg[0] = level;
5302 ssl->out_msg[1] = message;
5303
Hanno Becker67bc7c32018-08-06 11:33:50 +01005304 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005307 return( ret );
5308 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005310
5311 return( 0 );
5312}
5313
Paul Bakker5121ce52009-01-03 21:22:43 +00005314/*
5315 * Handshake functions
5316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5318 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5319 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5320 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5321 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5322 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5323 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005324/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5332 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005333 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5334 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005337 ssl->state++;
5338 return( 0 );
5339 }
5340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005343}
5344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5352 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005353 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5354 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005357 ssl->state++;
5358 return( 0 );
5359 }
5360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005363}
Gilles Peskinef9828522017-05-03 12:28:43 +02005364
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005365#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005366/* Some certificate support -> implement write and parse */
5367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005371 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 const mbedtls_x509_crt *crt;
5373 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5378 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005379 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5380 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005383 ssl->state++;
5384 return( 0 );
5385 }
5386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005388 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005389 {
5390 if( ssl->client_auth == 0 )
5391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005393 ssl->state++;
5394 return( 0 );
5395 }
5396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 /*
5399 * If using SSLv3 and got no cert, send an Alert message
5400 * (otherwise an empty Certificate message will be sent).
5401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5403 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005404 {
5405 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5407 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5408 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005411 goto write_msg;
5412 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005414 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#endif /* MBEDTLS_SSL_CLI_C */
5416#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005417 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5422 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 }
5424 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005425#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005428
5429 /*
5430 * 0 . 0 handshake type
5431 * 1 . 3 handshake length
5432 * 4 . 6 length of all certs
5433 * 7 . 9 length of cert. 1
5434 * 10 . n-1 peer certificate
5435 * n . n+2 length of cert. 2
5436 * n+3 . ... upper level cert, etc.
5437 */
5438 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005440
Paul Bakker29087132010-03-21 21:03:34 +00005441 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
5443 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005444 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005447 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005449 }
5450
5451 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5452 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5453 ssl->out_msg[i + 2] = (unsigned char)( n );
5454
5455 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5456 i += n; crt = crt->next;
5457 }
5458
5459 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5460 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5461 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5462
5463 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5465 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005466
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005467#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005468write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005470
5471 ssl->state++;
5472
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005473 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 return( ret );
5477 }
5478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005480
Paul Bakkered27a042013-04-18 22:46:23 +02005481 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005482}
5483
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005484/*
5485 * Once the certificate message is read, parse it into a cert chain and
5486 * perform basic checks, but leave actual verification to the caller
5487 */
5488static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005489{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005490 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005491 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005492 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494#if defined(MBEDTLS_SSL_SRV_C)
5495#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005496 /*
5497 * Check if the client sent an empty certificate
5498 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005499 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005502 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5504 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5505 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005509 /* The client was asked for a certificate but didn't send
5510 one. The client should know what's going on, so we
5511 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005512 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005513 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 }
5515 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5519 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005520 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5524 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5525 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5526 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005529
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005530 /* The client was asked for a certificate but didn't send
5531 one. The client should know what's going on, so we
5532 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005533 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005534 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535 }
5536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5538 MBEDTLS_SSL_PROTO_TLS1_2 */
5539#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005544 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5545 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 }
5548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5550 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5554 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 }
5557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005559
Paul Bakker5121ce52009-01-03 21:22:43 +00005560 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005562 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005563 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005564
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005565 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005569 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5570 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 }
5573
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005574 /* In case we tried to reuse a session but it failed */
5575 if( ssl->session_negotiate->peer_cert != NULL )
5576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5578 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005579 }
5580
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005581 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005583 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005586 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5587 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005588 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 }
5590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005592
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005593 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005594
5595 while( i < ssl->in_hslen )
5596 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005597 if ( i + 3 > ssl->in_hslen ) {
5598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5599 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5600 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5601 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 if( ssl->in_msg[i] != 0 )
5604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005606 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5607 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005609 }
5610
5611 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5612 | (unsigned int) ssl->in_msg[i + 2];
5613 i += 3;
5614
5615 if( n < 128 || i + n > ssl->in_hslen )
5616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005618 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5619 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 }
5622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005624 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005625 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005627 case 0: /*ok*/
5628 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5629 /* Ignore certificate with an unknown algorithm: maybe a
5630 prior certificate was already trusted. */
5631 break;
5632
5633 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5634 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5635 goto crt_parse_der_failed;
5636
5637 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5638 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5639 goto crt_parse_der_failed;
5640
5641 default:
5642 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5643 crt_parse_der_failed:
5644 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005646 return( ret );
5647 }
5648
5649 i += n;
5650 }
5651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005653
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005654 /*
5655 * On client, make sure the server cert doesn't change during renego to
5656 * avoid "triple handshake" attack: https://secure-resumption.com/
5657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005659 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005661 {
5662 if( ssl->session->peer_cert == NULL )
5663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005665 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5666 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005668 }
5669
5670 if( ssl->session->peer_cert->raw.len !=
5671 ssl->session_negotiate->peer_cert->raw.len ||
5672 memcmp( ssl->session->peer_cert->raw.p,
5673 ssl->session_negotiate->peer_cert->raw.p,
5674 ssl->session->peer_cert->raw.len ) != 0 )
5675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005677 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5678 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005680 }
5681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005683
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005684 return( 0 );
5685}
5686
5687int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5688{
5689 int ret;
5690 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5691 ssl->transform_negotiate->ciphersuite_info;
5692#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5693 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5694 ? ssl->handshake->sni_authmode
5695 : ssl->conf->authmode;
5696#else
5697 const int authmode = ssl->conf->authmode;
5698#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005699 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005700
5701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5702
5703 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5704 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5705 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5706 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5707 {
5708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5709 ssl->state++;
5710 return( 0 );
5711 }
5712
5713#if defined(MBEDTLS_SSL_SRV_C)
5714 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5715 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5716 {
5717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5718 ssl->state++;
5719 return( 0 );
5720 }
5721
5722 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5723 authmode == MBEDTLS_SSL_VERIFY_NONE )
5724 {
5725 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005727
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005728 ssl->state++;
5729 return( 0 );
5730 }
5731#endif
5732
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005733#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5734 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005735 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005736 {
5737 goto crt_verify;
5738 }
5739#endif
5740
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005741 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005742 {
5743 /* mbedtls_ssl_read_record may have sent an alert already. We
5744 let it decide whether to alert. */
5745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5746 return( ret );
5747 }
5748
5749 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5750 {
5751#if defined(MBEDTLS_SSL_SRV_C)
5752 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5753 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5754 {
5755 ret = 0;
5756 }
5757#endif
5758
5759 ssl->state++;
5760 return( ret );
5761 }
5762
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005763#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5764 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005765 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005766
5767crt_verify:
5768 if( ssl->handshake->ecrs_enabled)
5769 rs_ctx = &ssl->handshake->ecrs_ctx;
5770#endif
5771
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005772 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005774 mbedtls_x509_crt *ca_chain;
5775 mbedtls_x509_crl *ca_crl;
5776
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005777#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005778 if( ssl->handshake->sni_ca_chain != NULL )
5779 {
5780 ca_chain = ssl->handshake->sni_ca_chain;
5781 ca_crl = ssl->handshake->sni_ca_crl;
5782 }
5783 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005784#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005785 {
5786 ca_chain = ssl->conf->ca_chain;
5787 ca_crl = ssl->conf->ca_crl;
5788 }
5789
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005790 /*
5791 * Main check: verify certificate
5792 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005793 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005794 ssl->session_negotiate->peer_cert,
5795 ca_chain, ca_crl,
5796 ssl->conf->cert_profile,
5797 ssl->hostname,
5798 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005799 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005800
5801 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005804 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005805
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005806#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5807 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005808 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005809#endif
5810
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005811 /*
5812 * Secondary checks: always done, but change 'ret' only if it was 0
5813 */
5814
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005815#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005818
5819 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005821 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005822 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005823 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005826 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005828 }
5829 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005830#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005833 ciphersuite_info,
5834 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005835 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005838 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005840 }
5841
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005842 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5843 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5844 * with details encoded in the verification flags. All other kinds
5845 * of error codes, including those from the user provided f_vrfy
5846 * functions, are treated as fatal and lead to a failure of
5847 * ssl_parse_certificate even if verification was optional. */
5848 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5849 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5850 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5851 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005852 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005853 }
5854
5855 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5856 {
5857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5858 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5859 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005860
5861 if( ret != 0 )
5862 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005863 uint8_t alert;
5864
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005865 /* The certificate may have been rejected for several reasons.
5866 Pick one and send the corresponding alert. Which alert to send
5867 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005868 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5869 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5870 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5871 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5872 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5873 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5874 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5875 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5876 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5877 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5878 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5879 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5880 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5881 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5882 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5883 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5884 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5885 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5886 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5887 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005888 else
5889 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005890 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5891 alert );
5892 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005893
Hanno Beckere6706e62017-05-15 16:05:15 +01005894#if defined(MBEDTLS_DEBUG_C)
5895 if( ssl->session_negotiate->verify_result != 0 )
5896 {
5897 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5898 ssl->session_negotiate->verify_result ) );
5899 }
5900 else
5901 {
5902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5903 }
5904#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005905 }
5906
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005907 ssl->state++;
5908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005910
5911 return( ret );
5912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5914 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5915 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5916 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5917 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5918 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5919 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005922{
5923 int ret;
5924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 ssl->out_msglen = 1;
5929 ssl->out_msg[0] = 1;
5930
Paul Bakker5121ce52009-01-03 21:22:43 +00005931 ssl->state++;
5932
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005933 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005934 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005936 return( ret );
5937 }
5938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005940
5941 return( 0 );
5942}
5943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005945{
5946 int ret;
5947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005949
Hanno Becker327c93b2018-08-15 13:56:18 +01005950 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005953 return( ret );
5954 }
5955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005959 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5960 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 }
5963
Hanno Beckere678eaa2018-08-21 14:57:46 +01005964 /* CCS records are only accepted if they have length 1 and content '1',
5965 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005966
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005967 /*
5968 * Switch to our negotiated transform and session parameters for inbound
5969 * data.
5970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005972 ssl->transform_in = ssl->transform_negotiate;
5973 ssl->session_in = ssl->session_negotiate;
5974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005976 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005979 ssl_dtls_replay_reset( ssl );
5980#endif
5981
5982 /* Increment epoch */
5983 if( ++ssl->in_epoch == 0 )
5984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005986 /* This is highly unlikely to happen for legitimate reasons, so
5987 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005989 }
5990 }
5991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005993 memset( ssl->in_ctr, 0, 8 );
5994
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005995 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5998 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006003 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6004 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006006 }
6007 }
6008#endif
6009
Paul Bakker5121ce52009-01-03 21:22:43 +00006010 ssl->state++;
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006013
6014 return( 0 );
6015}
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6018 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006019{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006020 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6023 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6024 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006025 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006026 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6029#if defined(MBEDTLS_SHA512_C)
6030 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006031 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6032 else
6033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#if defined(MBEDTLS_SHA256_C)
6035 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006036 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006037 else
6038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006042 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006043 }
Paul Bakker380da532012-04-18 16:10:25 +00006044}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6049 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006050 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6051 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6054#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006055 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006058 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006061}
6062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006064 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6067 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006068 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6069 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6072#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006073 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006076 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006079}
6080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6082 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6083static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006084 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006085{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006086 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6087 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006088}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006089#endif
Paul Bakker380da532012-04-18 16:10:25 +00006090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6092#if defined(MBEDTLS_SHA256_C)
6093static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006094 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006095{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006096 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006097}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006098#endif
Paul Bakker380da532012-04-18 16:10:25 +00006099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100#if defined(MBEDTLS_SHA512_C)
6101static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006102 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006103{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006104 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006105}
Paul Bakker769075d2012-11-24 11:26:46 +01006106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006110static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006113 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006114 mbedtls_md5_context md5;
6115 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006116
Paul Bakker5121ce52009-01-03 21:22:43 +00006117 unsigned char padbuf[48];
6118 unsigned char md5sum[16];
6119 unsigned char sha1sum[20];
6120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006122 if( !session )
6123 session = ssl->session;
6124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006126
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006127 mbedtls_md5_init( &md5 );
6128 mbedtls_sha1_init( &sha1 );
6129
6130 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6131 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006132
6133 /*
6134 * SSLv3:
6135 * hash =
6136 * MD5( master + pad2 +
6137 * MD5( handshake + sender + master + pad1 ) )
6138 * + SHA1( master + pad2 +
6139 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 */
6141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006143 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6144 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006145#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006148 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6149 sha1.state, sizeof( sha1.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 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006153 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Paul Bakker1ef83d62012-04-11 12:09:53 +00006155 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006156
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006157 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6158 mbedtls_md5_update_ret( &md5, session->master, 48 );
6159 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6160 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006162 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6163 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6164 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6165 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006166
Paul Bakker1ef83d62012-04-11 12:09:53 +00006167 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006169 mbedtls_md5_starts_ret( &md5 );
6170 mbedtls_md5_update_ret( &md5, session->master, 48 );
6171 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6172 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6173 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006174
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006175 mbedtls_sha1_starts_ret( &sha1 );
6176 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6177 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6178 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6179 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006182
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006183 mbedtls_md5_free( &md5 );
6184 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006185
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006186 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6187 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6188 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006195static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006197{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006198 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006199 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006200 mbedtls_md5_context md5;
6201 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006202 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006205 if( !session )
6206 session = ssl->session;
6207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006209
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006210 mbedtls_md5_init( &md5 );
6211 mbedtls_sha1_init( &sha1 );
6212
6213 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6214 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006215
Paul Bakker1ef83d62012-04-11 12:09:53 +00006216 /*
6217 * TLSv1:
6218 * hash = PRF( master, finished_label,
6219 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6220 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006223 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6224 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006225#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006228 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6229 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006230#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006233 ? "client finished"
6234 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006235
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006236 mbedtls_md5_finish_ret( &md5, padbuf );
6237 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006238
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006239 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006240 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006243
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006244 mbedtls_md5_free( &md5 );
6245 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006246
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006247 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6254#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006255static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006257{
6258 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006259 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006260 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006261 unsigned char padbuf[32];
6262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006264 if( !session )
6265 session = ssl->session;
6266
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006267 mbedtls_sha256_init( &sha256 );
6268
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006270
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006271 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006272
6273 /*
6274 * TLSv1.2:
6275 * hash = PRF( master, finished_label,
6276 * Hash( handshake ) )[0.11]
6277 */
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279#if !defined(MBEDTLS_SHA256_ALT)
6280 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006281 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006282#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006285 ? "client finished"
6286 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006287
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006288 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006289
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006290 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006291 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006294
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006295 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006296
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006297 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006304static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006306{
6307 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006308 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006309 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006310 unsigned char padbuf[48];
6311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006313 if( !session )
6314 session = ssl->session;
6315
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006316 mbedtls_sha512_init( &sha512 );
6317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006319
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006320 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006321
6322 /*
6323 * TLSv1.2:
6324 * hash = PRF( master, finished_label,
6325 * Hash( handshake ) )[0.11]
6326 */
6327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006329 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6330 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006331#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006334 ? "client finished"
6335 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006336
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006337 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006338
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006339 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006340 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006343
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006344 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006346 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006349}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350#endif /* MBEDTLS_SHA512_C */
6351#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006356
6357 /*
6358 * Free our handshake params
6359 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006360 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006362 ssl->handshake = NULL;
6363
6364 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006365 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006366 */
6367 if( ssl->transform )
6368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 mbedtls_ssl_transform_free( ssl->transform );
6370 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006371 }
6372 ssl->transform = ssl->transform_negotiate;
6373 ssl->transform_negotiate = NULL;
6374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006376}
6377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006379{
6380 int resume = ssl->handshake->resume;
6381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384#if defined(MBEDTLS_SSL_RENEGOTIATION)
6385 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006388 ssl->renego_records_seen = 0;
6389 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006390#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006391
6392 /*
6393 * Free the previous session and switch in the current one
6394 */
Paul Bakker0a597072012-09-25 21:55:46 +00006395 if( ssl->session )
6396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006398 /* RFC 7366 3.1: keep the EtM state */
6399 ssl->session_negotiate->encrypt_then_mac =
6400 ssl->session->encrypt_then_mac;
6401#endif
6402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 mbedtls_ssl_session_free( ssl->session );
6404 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006405 }
6406 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006407 ssl->session_negotiate = NULL;
6408
Paul Bakker0a597072012-09-25 21:55:46 +00006409 /*
6410 * Add cache entry
6411 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006412 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006413 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006414 resume == 0 )
6415 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006416 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006418 }
Paul Bakker0a597072012-09-25 21:55:46 +00006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006422 ssl->handshake->flight != NULL )
6423 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006424 /* Cancel handshake timer */
6425 ssl_set_timer( ssl, 0 );
6426
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006427 /* Keep last flight around in case we need to resend it:
6428 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006430 }
6431 else
6432#endif
6433 ssl_handshake_wrapup_free_hs_transform( ssl );
6434
Paul Bakker48916f92012-09-16 19:57:18 +00006435 ssl->state++;
6436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006438}
6439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006441{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006442 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006445
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006446 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006447
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006448 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006449
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006450 /*
6451 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6452 * may define some other value. Currently (early 2016), no defined
6453 * ciphersuite does this (and this is unlikely to change as activity has
6454 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006459 ssl->verify_data_len = hash_len;
6460 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006461#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006462
Paul Bakker5121ce52009-01-03 21:22:43 +00006463 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6465 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006466
6467 /*
6468 * In case of session resuming, invert the client and server
6469 * ChangeCipherSpec messages order.
6470 */
Paul Bakker0a597072012-09-25 21:55:46 +00006471 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006474 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006476#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006478 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006481 }
6482 else
6483 ssl->state++;
6484
Paul Bakker48916f92012-09-16 19:57:18 +00006485 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006486 * Switch to our negotiated transform and session parameters for outbound
6487 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006493 {
6494 unsigned char i;
6495
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006496 /* Remember current epoch settings for resending */
6497 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006498 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006499
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006500 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006501 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006502
6503 /* Increment epoch */
6504 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006505 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006506 break;
6507
6508 /* The loop goes to its end iff the counter is wrapping */
6509 if( i == 0 )
6510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6512 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006513 }
6514 }
6515 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006517 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006518
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006519 ssl->transform_out = ssl->transform_negotiate;
6520 ssl->session_out = ssl->session_negotiate;
6521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6523 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6528 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006529 }
6530 }
6531#endif
6532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006536#endif
6537
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006538 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006541 return( ret );
6542 }
6543
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006544#if defined(MBEDTLS_SSL_PROTO_DTLS)
6545 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6546 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6547 {
6548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6549 return( ret );
6550 }
6551#endif
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006554
6555 return( 0 );
6556}
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006559#define SSL_MAX_HASH_LEN 36
6560#else
6561#define SSL_MAX_HASH_LEN 12
6562#endif
6563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006565{
Paul Bakker23986e52011-04-24 08:57:21 +00006566 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006567 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006568 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006571
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006572 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006573
Hanno Becker327c93b2018-08-15 13:56:18 +01006574 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 return( ret );
6578 }
6579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006583 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6584 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006586 }
6587
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006588 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589#if defined(MBEDTLS_SSL_PROTO_SSL3)
6590 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006591 hash_len = 36;
6592 else
6593#endif
6594 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6597 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006600 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6601 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 }
6604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006606 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006609 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6610 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 }
6613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006615 ssl->verify_data_len = hash_len;
6616 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006617#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006618
Paul Bakker0a597072012-09-25 21:55:46 +00006619 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006622 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006624#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006626 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006629 }
6630 else
6631 ssl->state++;
6632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006636#endif
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006639
6640 return( 0 );
6641}
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6648 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6649 mbedtls_md5_init( &handshake->fin_md5 );
6650 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006651 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6652 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6655#if defined(MBEDTLS_SHA256_C)
6656 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006657 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006658#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659#if defined(MBEDTLS_SHA512_C)
6660 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006661 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006662#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006664
6665 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006666
6667#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6668 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6669 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6670#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#if defined(MBEDTLS_DHM_C)
6673 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_ECDH_C)
6676 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006677#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006678#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006679 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006680#if defined(MBEDTLS_SSL_CLI_C)
6681 handshake->ecjpake_cache = NULL;
6682 handshake->ecjpake_cache_len = 0;
6683#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006684#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006685
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006686#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006687 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006688#endif
6689
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006690#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6691 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6692#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006693}
6694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6700 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 mbedtls_md_init( &transform->md_ctx_enc );
6703 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006704}
6705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006709}
6710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006712{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006713 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006714 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006716 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006718 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006719 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006720
6721 /*
6722 * Either the pointers are now NULL or cleared properly and can be freed.
6723 * Now allocate missing structures.
6724 */
6725 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006726 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006727 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006728 }
Paul Bakker48916f92012-09-16 19:57:18 +00006729
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006730 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006731 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006732 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006733 }
Paul Bakker48916f92012-09-16 19:57:18 +00006734
Paul Bakker82788fb2014-10-20 13:59:19 +02006735 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006736 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006737 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006738 }
Paul Bakker48916f92012-09-16 19:57:18 +00006739
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006740 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006741 if( ssl->handshake == NULL ||
6742 ssl->transform_negotiate == NULL ||
6743 ssl->session_negotiate == NULL )
6744 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 mbedtls_free( ssl->handshake );
6748 mbedtls_free( ssl->transform_negotiate );
6749 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006750
6751 ssl->handshake = NULL;
6752 ssl->transform_negotiate = NULL;
6753 ssl->session_negotiate = NULL;
6754
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006755 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006756 }
6757
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006758 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006760 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006761 ssl_handshake_params_init( ssl->handshake );
6762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6765 {
6766 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006767
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006768 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6769 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6770 else
6771 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006772
6773 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006774 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006775#endif
6776
Paul Bakker48916f92012-09-16 19:57:18 +00006777 return( 0 );
6778}
6779
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006780#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006781/* Dummy cookie callbacks for defaults */
6782static int ssl_cookie_write_dummy( void *ctx,
6783 unsigned char **p, unsigned char *end,
6784 const unsigned char *cli_id, size_t cli_id_len )
6785{
6786 ((void) ctx);
6787 ((void) p);
6788 ((void) end);
6789 ((void) cli_id);
6790 ((void) cli_id_len);
6791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006793}
6794
6795static int ssl_cookie_check_dummy( void *ctx,
6796 const unsigned char *cookie, size_t cookie_len,
6797 const unsigned char *cli_id, size_t cli_id_len )
6798{
6799 ((void) ctx);
6800 ((void) cookie);
6801 ((void) cookie_len);
6802 ((void) cli_id);
6803 ((void) cli_id_len);
6804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006806}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006807#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006808
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006809/* Once ssl->out_hdr as the address of the beginning of the
6810 * next outgoing record is set, deduce the other pointers.
6811 *
6812 * Note: For TLS, we save the implicit record sequence number
6813 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6814 * and the caller has to make sure there's space for this.
6815 */
6816
6817static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6818 mbedtls_ssl_transform *transform )
6819{
6820#if defined(MBEDTLS_SSL_PROTO_DTLS)
6821 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6822 {
6823 ssl->out_ctr = ssl->out_hdr + 3;
6824 ssl->out_len = ssl->out_hdr + 11;
6825 ssl->out_iv = ssl->out_hdr + 13;
6826 }
6827 else
6828#endif
6829 {
6830 ssl->out_ctr = ssl->out_hdr - 8;
6831 ssl->out_len = ssl->out_hdr + 3;
6832 ssl->out_iv = ssl->out_hdr + 5;
6833 }
6834
6835 /* Adjust out_msg to make space for explicit IV, if used. */
6836 if( transform != NULL &&
6837 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6838 {
6839 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6840 }
6841 else
6842 ssl->out_msg = ssl->out_iv;
6843}
6844
6845/* Once ssl->in_hdr as the address of the beginning of the
6846 * next incoming record is set, deduce the other pointers.
6847 *
6848 * Note: For TLS, we save the implicit record sequence number
6849 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6850 * and the caller has to make sure there's space for this.
6851 */
6852
6853static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6854 mbedtls_ssl_transform *transform )
6855{
6856#if defined(MBEDTLS_SSL_PROTO_DTLS)
6857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6858 {
6859 ssl->in_ctr = ssl->in_hdr + 3;
6860 ssl->in_len = ssl->in_hdr + 11;
6861 ssl->in_iv = ssl->in_hdr + 13;
6862 }
6863 else
6864#endif
6865 {
6866 ssl->in_ctr = ssl->in_hdr - 8;
6867 ssl->in_len = ssl->in_hdr + 3;
6868 ssl->in_iv = ssl->in_hdr + 5;
6869 }
6870
6871 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6872 if( transform != NULL &&
6873 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6874 {
6875 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6876 }
6877 else
6878 ssl->in_msg = ssl->in_iv;
6879}
6880
Paul Bakker5121ce52009-01-03 21:22:43 +00006881/*
6882 * Initialize an SSL context
6883 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006884void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6885{
6886 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6887}
6888
6889/*
6890 * Setup an SSL context
6891 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006892
6893static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6894{
6895 /* Set the incoming and outgoing record pointers. */
6896#if defined(MBEDTLS_SSL_PROTO_DTLS)
6897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6898 {
6899 ssl->out_hdr = ssl->out_buf;
6900 ssl->in_hdr = ssl->in_buf;
6901 }
6902 else
6903#endif /* MBEDTLS_SSL_PROTO_DTLS */
6904 {
6905 ssl->out_hdr = ssl->out_buf + 8;
6906 ssl->in_hdr = ssl->in_buf + 8;
6907 }
6908
6909 /* Derive other internal pointers. */
6910 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6911 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6912}
6913
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006914int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006915 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006916{
Paul Bakker48916f92012-09-16 19:57:18 +00006917 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006918
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006919 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006920
6921 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006922 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006923 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006924
6925 /* Set to NULL in case of an error condition */
6926 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006927
Angus Grattond8213d02016-05-25 20:56:48 +10006928 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6929 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006930 {
Angus Grattond8213d02016-05-25 20:56:48 +10006931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006932 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006933 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006934 }
6935
6936 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6937 if( ssl->out_buf == NULL )
6938 {
6939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006940 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006941 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006942 }
6943
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006944 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006945
Paul Bakker48916f92012-09-16 19:57:18 +00006946 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006947 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006948
6949 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006950
6951error:
6952 mbedtls_free( ssl->in_buf );
6953 mbedtls_free( ssl->out_buf );
6954
6955 ssl->conf = NULL;
6956
6957 ssl->in_buf = NULL;
6958 ssl->out_buf = NULL;
6959
6960 ssl->in_hdr = NULL;
6961 ssl->in_ctr = NULL;
6962 ssl->in_len = NULL;
6963 ssl->in_iv = NULL;
6964 ssl->in_msg = NULL;
6965
6966 ssl->out_hdr = NULL;
6967 ssl->out_ctr = NULL;
6968 ssl->out_len = NULL;
6969 ssl->out_iv = NULL;
6970 ssl->out_msg = NULL;
6971
k-stachowiak9f7798e2018-07-31 16:52:32 +02006972 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006973}
6974
6975/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006976 * Reset an initialized and used SSL context for re-use while retaining
6977 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006978 *
6979 * If partial is non-zero, keep data in the input buffer and client ID.
6980 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006981 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006982static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006983{
Paul Bakker48916f92012-09-16 19:57:18 +00006984 int ret;
6985
Hanno Becker7e772132018-08-10 12:38:21 +01006986#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6987 !defined(MBEDTLS_SSL_SRV_C)
6988 ((void) partial);
6989#endif
6990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006992
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006993 /* Cancel any possibly running timer */
6994 ssl_set_timer( ssl, 0 );
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996#if defined(MBEDTLS_SSL_RENEGOTIATION)
6997 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006998 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006999
7000 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7002 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007005
Paul Bakker7eb013f2011-10-06 12:37:39 +00007006 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007007 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007008
7009 ssl->in_msgtype = 0;
7010 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007012 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007013 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007016 ssl_dtls_replay_reset( ssl );
7017#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007018
7019 ssl->in_hslen = 0;
7020 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007021
7022 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007023
7024 ssl->out_msgtype = 0;
7025 ssl->out_msglen = 0;
7026 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7028 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007029 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007030#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007031
Hanno Becker19859472018-08-06 09:40:20 +01007032 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7033
Paul Bakker48916f92012-09-16 19:57:18 +00007034 ssl->transform_in = NULL;
7035 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007036
Hanno Becker78640902018-08-13 16:35:15 +01007037 ssl->session_in = NULL;
7038 ssl->session_out = NULL;
7039
Angus Grattond8213d02016-05-25 20:56:48 +10007040 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007041
7042#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007043 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007044#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7045 {
7046 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007047 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007048 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7051 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7054 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7057 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007058 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007059 }
7060#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007061
Paul Bakker48916f92012-09-16 19:57:18 +00007062 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 mbedtls_ssl_transform_free( ssl->transform );
7065 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007066 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007067 }
Paul Bakker48916f92012-09-16 19:57:18 +00007068
Paul Bakkerc0463502013-02-14 11:19:38 +01007069 if( ssl->session )
7070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 mbedtls_ssl_session_free( ssl->session );
7072 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007073 ssl->session = NULL;
7074 }
7075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007077 ssl->alpn_chosen = NULL;
7078#endif
7079
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007080#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007081#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007082 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007083#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007084 {
7085 mbedtls_free( ssl->cli_id );
7086 ssl->cli_id = NULL;
7087 ssl->cli_id_len = 0;
7088 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007089#endif
7090
Paul Bakker48916f92012-09-16 19:57:18 +00007091 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7092 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007093
7094 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007095}
7096
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007097/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007098 * Reset an initialized and used SSL context for re-use while retaining
7099 * all application-set variables, function pointers and data.
7100 */
7101int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7102{
7103 return( ssl_session_reset_int( ssl, 0 ) );
7104}
7105
7106/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 * SSL set accessors
7108 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007109void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007110{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007111 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007112}
7113
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007114void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007116 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007117}
7118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007120void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007122 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007123}
7124#endif
7125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007127void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007128{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007129 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007130}
7131#endif
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007134
Hanno Becker1841b0a2018-08-24 11:13:57 +01007135void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7136 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007137{
7138 ssl->disable_datagram_packing = !allow_packing;
7139}
7140
7141void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7142 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007144 conf->hs_timeout_min = min;
7145 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007146}
7147#endif
7148
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007149void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007151 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007152}
7153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007155void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007156 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007157 void *p_vrfy )
7158{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007159 conf->f_vrfy = f_vrfy;
7160 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007163
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007164void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007165 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007166 void *p_rng )
7167{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007168 conf->f_rng = f_rng;
7169 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007170}
7171
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007172void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007173 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007174 void *p_dbg )
7175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007176 conf->f_dbg = f_dbg;
7177 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007178}
7179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007181 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007182 mbedtls_ssl_send_t *f_send,
7183 mbedtls_ssl_recv_t *f_recv,
7184 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007185{
7186 ssl->p_bio = p_bio;
7187 ssl->f_send = f_send;
7188 ssl->f_recv = f_recv;
7189 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007190}
7191
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007192#if defined(MBEDTLS_SSL_PROTO_DTLS)
7193void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7194{
7195 ssl->mtu = mtu;
7196}
7197#endif
7198
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007199void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007200{
7201 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007202}
7203
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007204void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7205 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007206 mbedtls_ssl_set_timer_t *f_set_timer,
7207 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007208{
7209 ssl->p_timer = p_timer;
7210 ssl->f_set_timer = f_set_timer;
7211 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007212
7213 /* Make sure we start with no timer running */
7214 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007215}
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007218void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007219 void *p_cache,
7220 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7221 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007222{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007223 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007224 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007225 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#if defined(MBEDTLS_SSL_CLI_C)
7230int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007231{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007232 int ret;
7233
7234 if( ssl == NULL ||
7235 session == NULL ||
7236 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007237 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007240 }
7241
7242 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7243 return( ret );
7244
Paul Bakker0a597072012-09-25 21:55:46 +00007245 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007246
7247 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007250
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007251void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007252 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007254 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7255 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7256 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7257 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007258}
7259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007260void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007261 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007262 int major, int minor )
7263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007265 return;
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007268 return;
7269
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007270 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007271}
7272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007274void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007275 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007276{
7277 conf->cert_profile = profile;
7278}
7279
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007280/* Append a new keycert entry to a (possibly empty) list */
7281static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7282 mbedtls_x509_crt *cert,
7283 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007284{
niisato8ee24222018-06-25 19:05:48 +09007285 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007286
niisato8ee24222018-06-25 19:05:48 +09007287 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7288 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007290
niisato8ee24222018-06-25 19:05:48 +09007291 new_cert->cert = cert;
7292 new_cert->key = key;
7293 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007294
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007295 /* Update head is the list was null, else add to the end */
7296 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007297 {
niisato8ee24222018-06-25 19:05:48 +09007298 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007299 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007300 else
7301 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007302 mbedtls_ssl_key_cert *cur = *head;
7303 while( cur->next != NULL )
7304 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007305 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007306 }
7307
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007308 return( 0 );
7309}
7310
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007311int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007312 mbedtls_x509_crt *own_cert,
7313 mbedtls_pk_context *pk_key )
7314{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007315 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007316}
7317
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007318void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007319 mbedtls_x509_crt *ca_chain,
7320 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007321{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007322 conf->ca_chain = ca_chain;
7323 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007326
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007327#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7328int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7329 mbedtls_x509_crt *own_cert,
7330 mbedtls_pk_context *pk_key )
7331{
7332 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7333 own_cert, pk_key ) );
7334}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007335
7336void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7337 mbedtls_x509_crt *ca_chain,
7338 mbedtls_x509_crl *ca_crl )
7339{
7340 ssl->handshake->sni_ca_chain = ca_chain;
7341 ssl->handshake->sni_ca_crl = ca_crl;
7342}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007343
7344void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7345 int authmode )
7346{
7347 ssl->handshake->sni_authmode = authmode;
7348}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007349#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7350
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007351#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007352/*
7353 * Set EC J-PAKE password for current handshake
7354 */
7355int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7356 const unsigned char *pw,
7357 size_t pw_len )
7358{
7359 mbedtls_ecjpake_role role;
7360
Janos Follath8eb64132016-06-03 15:40:57 +01007361 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7363
7364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7365 role = MBEDTLS_ECJPAKE_SERVER;
7366 else
7367 role = MBEDTLS_ECJPAKE_CLIENT;
7368
7369 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7370 role,
7371 MBEDTLS_MD_SHA256,
7372 MBEDTLS_ECP_DP_SECP256R1,
7373 pw, pw_len ) );
7374}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007375#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007378int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007379 const unsigned char *psk, size_t psk_len,
7380 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007381{
Paul Bakker6db455e2013-09-18 17:29:31 +02007382 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007387
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007388 /* Identity len will be encoded on two bytes */
7389 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007390 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007391 {
7392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7393 }
7394
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007395 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007396 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007397 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007398
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007399 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007400 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007401 conf->psk_len = 0;
7402 }
7403 if( conf->psk_identity != NULL )
7404 {
7405 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007406 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007407 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007408 }
7409
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007410 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7411 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007412 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007413 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007414 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007415 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007416 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007417 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007418 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007419
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007420 conf->psk_len = psk_len;
7421 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007422
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007423 memcpy( conf->psk, psk, conf->psk_len );
7424 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007425
7426 return( 0 );
7427}
7428
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007429int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7430 const unsigned char *psk, size_t psk_len )
7431{
7432 if( psk == NULL || ssl->handshake == NULL )
7433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7434
7435 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7437
7438 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007439 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007440 mbedtls_platform_zeroize( ssl->handshake->psk,
7441 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007442 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007443 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007444 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007445
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007446 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007447 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007448
7449 ssl->handshake->psk_len = psk_len;
7450 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7451
7452 return( 0 );
7453}
7454
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007455void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007457 size_t),
7458 void *p_psk )
7459{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007460 conf->f_psk = f_psk;
7461 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007464
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007465#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007466
7467#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007468int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469{
7470 int ret;
7471
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007472 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7473 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7474 {
7475 mbedtls_mpi_free( &conf->dhm_P );
7476 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007477 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007479
7480 return( 0 );
7481}
Hanno Becker470a8c42017-10-04 15:28:46 +01007482#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007483
Hanno Beckera90658f2017-10-04 15:29:08 +01007484int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7485 const unsigned char *dhm_P, size_t P_len,
7486 const unsigned char *dhm_G, size_t G_len )
7487{
7488 int ret;
7489
7490 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7491 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7492 {
7493 mbedtls_mpi_free( &conf->dhm_P );
7494 mbedtls_mpi_free( &conf->dhm_G );
7495 return( ret );
7496 }
7497
7498 return( 0 );
7499}
Paul Bakker5121ce52009-01-03 21:22:43 +00007500
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007501int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007502{
7503 int ret;
7504
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007505 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7506 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7507 {
7508 mbedtls_mpi_free( &conf->dhm_P );
7509 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007510 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007511 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007512
7513 return( 0 );
7514}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007515#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007516
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007517#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7518/*
7519 * Set the minimum length for Diffie-Hellman parameters
7520 */
7521void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7522 unsigned int bitlen )
7523{
7524 conf->dhm_min_bitlen = bitlen;
7525}
7526#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7527
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007528#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007529/*
7530 * Set allowed/preferred hashes for handshake signatures
7531 */
7532void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7533 const int *hashes )
7534{
7535 conf->sig_hashes = hashes;
7536}
Hanno Becker947194e2017-04-07 13:25:49 +01007537#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007538
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007539#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007540/*
7541 * Set the allowed elliptic curves
7542 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007543void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007544 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007546 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007547}
Hanno Becker947194e2017-04-07 13:25:49 +01007548#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007549
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007550#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007552{
Hanno Becker947194e2017-04-07 13:25:49 +01007553 /* Initialize to suppress unnecessary compiler warning */
7554 size_t hostname_len = 0;
7555
7556 /* Check if new hostname is valid before
7557 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007558 if( hostname != NULL )
7559 {
7560 hostname_len = strlen( hostname );
7561
7562 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7564 }
7565
7566 /* Now it's clear that we will overwrite the old hostname,
7567 * so we can free it safely */
7568
7569 if( ssl->hostname != NULL )
7570 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007571 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007572 mbedtls_free( ssl->hostname );
7573 }
7574
7575 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007576
Paul Bakker5121ce52009-01-03 21:22:43 +00007577 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007578 {
7579 ssl->hostname = NULL;
7580 }
7581 else
7582 {
7583 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007584 if( ssl->hostname == NULL )
7585 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007586
Hanno Becker947194e2017-04-07 13:25:49 +01007587 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007588
Hanno Becker947194e2017-04-07 13:25:49 +01007589 ssl->hostname[hostname_len] = '\0';
7590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007591
7592 return( 0 );
7593}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007594#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007595
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007596#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007597void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007599 const unsigned char *, size_t),
7600 void *p_sni )
7601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007602 conf->f_sni = f_sni;
7603 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007608int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007609{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007610 size_t cur_len, tot_len;
7611 const char **p;
7612
7613 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007614 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7615 * MUST NOT be truncated."
7616 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007617 */
7618 tot_len = 0;
7619 for( p = protos; *p != NULL; p++ )
7620 {
7621 cur_len = strlen( *p );
7622 tot_len += cur_len;
7623
Ronald Cron157cffe2020-04-23 16:41:44 +02007624 if( ( cur_len == 0 ) ||
7625 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7626 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007628 }
7629
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007630 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007631
7632 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007633}
7634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007636{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007637 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007640
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007641void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007642{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007643 conf->max_major_ver = major;
7644 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007645}
7646
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007647void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007648{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007649 conf->min_major_ver = major;
7650 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007651}
7652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007654void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007655{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007656 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007657}
7658#endif
7659
Janos Follath088ce432017-04-10 12:42:31 +01007660#if defined(MBEDTLS_SSL_SRV_C)
7661void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7662 char cert_req_ca_list )
7663{
7664 conf->cert_req_ca_list = cert_req_ca_list;
7665}
7666#endif
7667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007669void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007671 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007672}
7673#endif
7674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007676void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007677{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007678 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007679}
7680#endif
7681
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007682#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007683void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007685 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007686}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007687#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007690int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007693 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007696 }
7697
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007698 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007699
7700 return( 0 );
7701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007705void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007707 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007712void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007713{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007714 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007715}
7716#endif
7717
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007718void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007719{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007720 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007721}
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007724void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007725{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007726 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007727}
7728
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007729void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007730{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007731 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007732}
7733
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007734void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007735 const unsigned char period[8] )
7736{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007737 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007738}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007742#if defined(MBEDTLS_SSL_CLI_C)
7743void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007744{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007745 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007746}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007747#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007748
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007749#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007750void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7751 mbedtls_ssl_ticket_write_t *f_ticket_write,
7752 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7753 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007754{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007755 conf->f_ticket_write = f_ticket_write;
7756 conf->f_ticket_parse = f_ticket_parse;
7757 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007758}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007761
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007762#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7763void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7764 mbedtls_ssl_export_keys_t *f_export_keys,
7765 void *p_export_keys )
7766{
7767 conf->f_export_keys = f_export_keys;
7768 conf->p_export_keys = p_export_keys;
7769}
7770#endif
7771
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007772#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007773void mbedtls_ssl_conf_async_private_cb(
7774 mbedtls_ssl_config *conf,
7775 mbedtls_ssl_async_sign_t *f_async_sign,
7776 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7777 mbedtls_ssl_async_resume_t *f_async_resume,
7778 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007779 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007780{
7781 conf->f_async_sign_start = f_async_sign;
7782 conf->f_async_decrypt_start = f_async_decrypt;
7783 conf->f_async_resume = f_async_resume;
7784 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007785 conf->p_async_config_data = async_config_data;
7786}
7787
Gilles Peskine8f97af72018-04-26 11:46:10 +02007788void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7789{
7790 return( conf->p_async_config_data );
7791}
7792
Gilles Peskine1febfef2018-04-30 11:54:39 +02007793void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007794{
7795 if( ssl->handshake == NULL )
7796 return( NULL );
7797 else
7798 return( ssl->handshake->user_async_ctx );
7799}
7800
Gilles Peskine1febfef2018-04-30 11:54:39 +02007801void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007802 void *ctx )
7803{
7804 if( ssl->handshake != NULL )
7805 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007806}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007807#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007808
Paul Bakker5121ce52009-01-03 21:22:43 +00007809/*
7810 * SSL get accessors
7811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007813{
7814 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7815}
7816
Hanno Becker8b170a02017-10-10 11:51:19 +01007817int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7818{
7819 /*
7820 * Case A: We're currently holding back
7821 * a message for further processing.
7822 */
7823
7824 if( ssl->keep_current_message == 1 )
7825 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007827 return( 1 );
7828 }
7829
7830 /*
7831 * Case B: Further records are pending in the current datagram.
7832 */
7833
7834#if defined(MBEDTLS_SSL_PROTO_DTLS)
7835 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7836 ssl->in_left > ssl->next_record_offset )
7837 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007838 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007839 return( 1 );
7840 }
7841#endif /* MBEDTLS_SSL_PROTO_DTLS */
7842
7843 /*
7844 * Case C: A handshake message is being processed.
7845 */
7846
Hanno Becker8b170a02017-10-10 11:51:19 +01007847 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7848 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007850 return( 1 );
7851 }
7852
7853 /*
7854 * Case D: An application data message is being processed
7855 */
7856 if( ssl->in_offt != NULL )
7857 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007859 return( 1 );
7860 }
7861
7862 /*
7863 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007864 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007865 * we implement support for multiple alerts in single records.
7866 */
7867
7868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7869 return( 0 );
7870}
7871
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007872uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007873{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007874 if( ssl->session != NULL )
7875 return( ssl->session->verify_result );
7876
7877 if( ssl->session_negotiate != NULL )
7878 return( ssl->session_negotiate->verify_result );
7879
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007880 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007881}
7882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007884{
Paul Bakker926c8e42013-03-06 10:23:34 +01007885 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007886 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007889}
7890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007892{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007895 {
7896 switch( ssl->minor_ver )
7897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007899 return( "DTLSv1.0" );
7900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007902 return( "DTLSv1.2" );
7903
7904 default:
7905 return( "unknown (DTLS)" );
7906 }
7907 }
7908#endif
7909
Paul Bakker43ca69c2011-01-15 17:35:19 +00007910 switch( ssl->minor_ver )
7911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007913 return( "SSLv3.0" );
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007916 return( "TLSv1.0" );
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007919 return( "TLSv1.1" );
7920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007922 return( "TLSv1.2" );
7923
Paul Bakker43ca69c2011-01-15 17:35:19 +00007924 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007925 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007926 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007927}
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007930{
Hanno Becker3136ede2018-08-17 15:28:19 +01007931 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007933 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007934
Hanno Becker78640902018-08-13 16:35:15 +01007935 if( transform == NULL )
7936 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_ZLIB_SUPPORT)
7939 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7940 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007941#endif
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 case MBEDTLS_MODE_GCM:
7946 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007947 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007949 transform_expansion = transform->minlen;
7950 break;
7951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007953
7954 block_size = mbedtls_cipher_get_block_size(
7955 &transform->cipher_ctx_enc );
7956
Hanno Becker3136ede2018-08-17 15:28:19 +01007957 /* Expansion due to the addition of the MAC. */
7958 transform_expansion += transform->maclen;
7959
7960 /* Expansion due to the addition of CBC padding;
7961 * Theoretically up to 256 bytes, but we never use
7962 * more than the block size of the underlying cipher. */
7963 transform_expansion += block_size;
7964
7965 /* For TLS 1.1 or higher, an explicit IV is added
7966 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007967#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7968 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007969 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007970#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007971
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007972 break;
7973
7974 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007977 }
7978
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007979 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007980}
7981
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007982#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7983size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7984{
7985 size_t max_len;
7986
7987 /*
7988 * Assume mfl_code is correct since it was checked when set
7989 */
Angus Grattond8213d02016-05-25 20:56:48 +10007990 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007991
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007992 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007993 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007994 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007995 {
Angus Grattond8213d02016-05-25 20:56:48 +10007996 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007997 }
7998
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007999 /* During a handshake, use the value being negotiated */
8000 if( ssl->session_negotiate != NULL &&
8001 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8002 {
8003 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8004 }
8005
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008006 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008007}
8008#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8009
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008010#if defined(MBEDTLS_SSL_PROTO_DTLS)
8011static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8012{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008013 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8014 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8015 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8016 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8017 return ( 0 );
8018
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008019 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8020 return( ssl->mtu );
8021
8022 if( ssl->mtu == 0 )
8023 return( ssl->handshake->mtu );
8024
8025 return( ssl->mtu < ssl->handshake->mtu ?
8026 ssl->mtu : ssl->handshake->mtu );
8027}
8028#endif /* MBEDTLS_SSL_PROTO_DTLS */
8029
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008030int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8031{
8032 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8033
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008034#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8035 !defined(MBEDTLS_SSL_PROTO_DTLS)
8036 (void) ssl;
8037#endif
8038
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008039#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8040 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8041
8042 if( max_len > mfl )
8043 max_len = mfl;
8044#endif
8045
8046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008047 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008048 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008049 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008050 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8051 const size_t overhead = (size_t) ret;
8052
8053 if( ret < 0 )
8054 return( ret );
8055
8056 if( mtu <= overhead )
8057 {
8058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8059 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8060 }
8061
8062 if( max_len > mtu - overhead )
8063 max_len = mtu - overhead;
8064 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008065#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008066
Hanno Becker0defedb2018-08-10 12:35:02 +01008067#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8068 !defined(MBEDTLS_SSL_PROTO_DTLS)
8069 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008070#endif
8071
8072 return( (int) max_len );
8073}
8074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075#if defined(MBEDTLS_X509_CRT_PARSE_C)
8076const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008077{
8078 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008079 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008080
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008081 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008082}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_SSL_CLI_C)
8086int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008087{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008088 if( ssl == NULL ||
8089 dst == NULL ||
8090 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008091 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008094 }
8095
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008096 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008099
Paul Bakker5121ce52009-01-03 21:22:43 +00008100/*
Paul Bakker1961b702013-01-25 14:49:24 +01008101 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008106
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008107 if( ssl == NULL || ssl->conf == NULL )
8108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008111 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008117#endif
8118
Paul Bakker1961b702013-01-25 14:49:24 +01008119 return( ret );
8120}
8121
8122/*
8123 * Perform the SSL handshake
8124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008126{
8127 int ret = 0;
8128
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008129 if( ssl == NULL || ssl->conf == NULL )
8130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008137
8138 if( ret != 0 )
8139 break;
8140 }
8141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008143
8144 return( ret );
8145}
8146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#if defined(MBEDTLS_SSL_RENEGOTIATION)
8148#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008149/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008150 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008153{
8154 int ret;
8155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008157
8158 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8160 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008161
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008162 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008163 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008165 return( ret );
8166 }
8167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008169
8170 return( 0 );
8171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008173
8174/*
8175 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176 * - any side: calling mbedtls_ssl_renegotiate(),
8177 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8178 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008179 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008180 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008184{
8185 int ret;
8186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008188
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008189 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8190 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008191
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008192 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8193 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008195 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008197 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008198 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008199 ssl->handshake->out_msg_seq = 1;
8200 else
8201 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008202 }
8203#endif
8204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8206 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008211 return( ret );
8212 }
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008215
8216 return( 0 );
8217}
8218
8219/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008220 * Renegotiate current connection on client,
8221 * or request renegotiation on server
8222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008224{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008226
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008227 if( ssl == NULL || ssl->conf == NULL )
8228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008231 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008232 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008238
8239 /* Did we already try/start sending HelloRequest? */
8240 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008242
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008243 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008248 /*
8249 * On client, either start the renegotiation process or,
8250 * if already in progress, continue the handshake
8251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008256
8257 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008260 return( ret );
8261 }
8262 }
8263 else
8264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008268 return( ret );
8269 }
8270 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008272
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008273 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008274}
8275
8276/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008277 * Check record counters and renegotiate if they're above the limit.
8278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008280{
Andres AG2196c7f2016-12-15 17:01:16 +00008281 size_t ep_len = ssl_ep_len( ssl );
8282 int in_ctr_cmp;
8283 int out_ctr_cmp;
8284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8286 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008287 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008288 {
8289 return( 0 );
8290 }
8291
Andres AG2196c7f2016-12-15 17:01:16 +00008292 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8293 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008294 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008295 ssl->conf->renego_period + ep_len, 8 - ep_len );
8296
8297 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008298 {
8299 return( 0 );
8300 }
8301
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008306
8307/*
8308 * Receive application data decrypted from the SSL layer
8309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008311{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008312 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008313 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008314
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008315 if( ssl == NULL || ssl->conf == NULL )
8316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008321 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008324 return( ret );
8325
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008326 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008328 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008329 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008330 return( ret );
8331 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008332 }
8333#endif
8334
Hanno Becker4a810fb2017-05-24 16:27:30 +01008335 /*
8336 * Check if renegotiation is necessary and/or handshake is
8337 * in process. If yes, perform/continue, and fall through
8338 * if an unexpected packet is received while the client
8339 * is waiting for the ServerHello.
8340 *
8341 * (There is no equivalent to the last condition on
8342 * the server-side as it is not treated as within
8343 * a handshake while waiting for the ClientHello
8344 * after a renegotiation request.)
8345 */
8346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008348 ret = ssl_check_ctr_renegotiate( ssl );
8349 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8350 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008353 return( ret );
8354 }
8355#endif
8356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008360 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8361 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008364 return( ret );
8365 }
8366 }
8367
Hanno Beckere41158b2017-10-23 13:30:32 +01008368 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008369 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008370 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008371 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008372 if( ssl->f_get_timer != NULL &&
8373 ssl->f_get_timer( ssl->p_timer ) == -1 )
8374 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008375 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008376 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008377
Hanno Becker327c93b2018-08-15 13:56:18 +01008378 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008379 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008380 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8381 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008382
Hanno Becker4a810fb2017-05-24 16:27:30 +01008383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8384 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008385 }
8386
8387 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008389 {
8390 /*
8391 * OpenSSL sends empty messages to randomize the IV
8392 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008393 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008396 return( 0 );
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008399 return( ret );
8400 }
8401 }
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008406
Hanno Becker4a810fb2017-05-24 16:27:30 +01008407 /*
8408 * - For client-side, expect SERVER_HELLO_REQUEST.
8409 * - For server-side, expect CLIENT_HELLO.
8410 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8411 */
8412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008414 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008416 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008419
8420 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008422 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008423 {
8424 continue;
8425 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008426#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008428 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008429#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008430
Hanno Becker4a810fb2017-05-24 16:27:30 +01008431#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008432 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008436
8437 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008439 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008440 {
8441 continue;
8442 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008445 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008446#endif /* MBEDTLS_SSL_SRV_C */
8447
Hanno Becker21df7f92017-10-17 11:03:26 +01008448#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008449 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008450 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8451 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8452 ssl->conf->allow_legacy_renegotiation ==
8453 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8454 {
8455 /*
8456 * Accept renegotiation request
8457 */
Paul Bakker48916f92012-09-16 19:57:18 +00008458
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008459 /* DTLS clients need to know renego is server-initiated */
8460#if defined(MBEDTLS_SSL_PROTO_DTLS)
8461 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8462 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8463 {
8464 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8465 }
8466#endif
8467 ret = ssl_start_renegotiation( ssl );
8468 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8469 ret != 0 )
8470 {
8471 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8472 return( ret );
8473 }
8474 }
8475 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008476#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008477 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008478 /*
8479 * Refuse renegotiation
8480 */
8481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484#if defined(MBEDTLS_SSL_PROTO_SSL3)
8485 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008486 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008487 /* SSLv3 does not have a "no_renegotiation" warning, so
8488 we send a fatal alert and abort the connection. */
8489 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8490 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8491 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008492 }
8493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8495#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8496 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8497 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8500 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8501 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008502 {
8503 return( ret );
8504 }
Paul Bakker48916f92012-09-16 19:57:18 +00008505 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008506 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8508 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8511 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008512 }
Paul Bakker48916f92012-09-16 19:57:18 +00008513 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008514
Hanno Becker90333da2017-10-10 11:27:13 +01008515 /* At this point, we don't know whether the renegotiation has been
8516 * completed or not. The cases to consider are the following:
8517 * 1) The renegotiation is complete. In this case, no new record
8518 * has been read yet.
8519 * 2) The renegotiation is incomplete because the client received
8520 * an application data record while awaiting the ServerHello.
8521 * 3) The renegotiation is incomplete because the client received
8522 * a non-handshake, non-application data message while awaiting
8523 * the ServerHello.
8524 * In each of these case, looping will be the proper action:
8525 * - For 1), the next iteration will read a new record and check
8526 * if it's application data.
8527 * - For 2), the loop condition isn't satisfied as application data
8528 * is present, hence continue is the same as break
8529 * - For 3), the loop condition is satisfied and read_record
8530 * will re-deliver the message that was held back by the client
8531 * when expecting the ServerHello.
8532 */
8533 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008534 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008535#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008537 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008538 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008539 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008540 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008543 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008545 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008546 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008547 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8551 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008554 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008555 }
8556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8560 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008561 }
8562
8563 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008564
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008565 /* We're going to return something now, cancel timer,
8566 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008568 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008569
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008570#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008571 /* If we requested renego but received AppData, resend HelloRequest.
8572 * Do it now, after setting in_offt, to avoid taking this branch
8573 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008575 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008577 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008578 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008581 return( ret );
8582 }
8583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008585#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008586 }
8587
8588 n = ( len < ssl->in_msglen )
8589 ? len : ssl->in_msglen;
8590
8591 memcpy( buf, ssl->in_offt, n );
8592 ssl->in_msglen -= n;
8593
8594 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008595 {
8596 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008597 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008598 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008600 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008601 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008602 /* more data available */
8603 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008607
Paul Bakker23986e52011-04-24 08:57:21 +00008608 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008609}
8610
8611/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008612 * Send application data to be encrypted by the SSL layer, taking care of max
8613 * fragment length and buffer size.
8614 *
8615 * According to RFC 5246 Section 6.2.1:
8616 *
8617 * Zero-length fragments of Application data MAY be sent as they are
8618 * potentially useful as a traffic analysis countermeasure.
8619 *
8620 * Therefore, it is possible that the input message length is 0 and the
8621 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008622 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008623static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008624 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008625{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008626 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8627 const size_t max_len = (size_t) ret;
8628
8629 if( ret < 0 )
8630 {
8631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8632 return( ret );
8633 }
8634
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008635 if( len > max_len )
8636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008638 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008641 "maximum fragment length: %d > %d",
8642 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008644 }
8645 else
8646#endif
8647 len = max_len;
8648 }
Paul Bakker887bd502011-06-08 13:10:54 +00008649
Paul Bakker5121ce52009-01-03 21:22:43 +00008650 if( ssl->out_left != 0 )
8651 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008652 /*
8653 * The user has previously tried to send the data and
8654 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8655 * written. In this case, we expect the high-level write function
8656 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008661 return( ret );
8662 }
8663 }
Paul Bakker887bd502011-06-08 13:10:54 +00008664 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008665 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008666 /*
8667 * The user is trying to send a message the first time, so we need to
8668 * copy the data into the internal buffers and setup the data structure
8669 * to keep track of partial writes
8670 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008671 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008673 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008674
Hanno Becker67bc7c32018-08-06 11:33:50 +01008675 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008678 return( ret );
8679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008680 }
8681
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008682 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008683}
8684
8685/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008686 * Write application data, doing 1/n-1 splitting if necessary.
8687 *
8688 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008689 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008690 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008693static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008694 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008695{
8696 int ret;
8697
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008698 if( ssl->conf->cbc_record_splitting ==
8699 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008700 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8702 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8703 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008704 {
8705 return( ssl_write_real( ssl, buf, len ) );
8706 }
8707
8708 if( ssl->split_done == 0 )
8709 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008710 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008711 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008712 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008713 }
8714
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008715 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8716 return( ret );
8717 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008718
8719 return( ret + 1 );
8720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008722
8723/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008724 * Write application data (public-facing wrapper)
8725 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008726int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008727{
8728 int ret;
8729
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008731
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008732 if( ssl == NULL || ssl->conf == NULL )
8733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8734
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008735#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008736 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8737 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008739 return( ret );
8740 }
8741#endif
8742
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008743 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008744 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008745 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008746 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008748 return( ret );
8749 }
8750 }
8751
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008752#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008753 ret = ssl_write_split( ssl, buf, len );
8754#else
8755 ret = ssl_write_real( ssl, buf, len );
8756#endif
8757
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008759
8760 return( ret );
8761}
8762
8763/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008764 * Notify the peer that the connection is being closed
8765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008767{
8768 int ret;
8769
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008770 if( ssl == NULL || ssl->conf == NULL )
8771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008774
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008775 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8781 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8782 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008785 return( ret );
8786 }
8787 }
8788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008790
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008791 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008792}
8793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008795{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008796 if( transform == NULL )
8797 return;
8798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008800 deflateEnd( &transform->ctx_deflate );
8801 inflateEnd( &transform->ctx_inflate );
8802#endif
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8805 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 mbedtls_md_free( &transform->md_ctx_enc );
8808 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008809
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008810 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008811}
8812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813#if defined(MBEDTLS_X509_CRT_PARSE_C)
8814static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008815{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008817
8818 while( cur != NULL )
8819 {
8820 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008822 cur = next;
8823 }
8824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008826
Hanno Becker0271f962018-08-16 13:23:47 +01008827#if defined(MBEDTLS_SSL_PROTO_DTLS)
8828
8829static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8830{
8831 unsigned offset;
8832 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8833
8834 if( hs == NULL )
8835 return;
8836
Hanno Becker283f5ef2018-08-24 09:34:47 +01008837 ssl_free_buffered_record( ssl );
8838
Hanno Becker0271f962018-08-16 13:23:47 +01008839 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008840 ssl_buffering_free_slot( ssl, offset );
8841}
8842
8843static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8844 uint8_t slot )
8845{
8846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8847 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008848
8849 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8850 return;
8851
Hanno Beckere605b192018-08-21 15:59:07 +01008852 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008853 {
Hanno Beckere605b192018-08-21 15:59:07 +01008854 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008855 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008856 mbedtls_free( hs_buf->data );
8857 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008858 }
8859}
8860
8861#endif /* MBEDTLS_SSL_PROTO_DTLS */
8862
Gilles Peskine9b562d52018-04-25 20:32:43 +02008863void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008864{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008865 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8866
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008867 if( handshake == NULL )
8868 return;
8869
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008870#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8871 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8872 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008873 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008874 handshake->async_in_progress = 0;
8875 }
8876#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8877
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008878#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8879 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8880 mbedtls_md5_free( &handshake->fin_md5 );
8881 mbedtls_sha1_free( &handshake->fin_sha1 );
8882#endif
8883#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8884#if defined(MBEDTLS_SHA256_C)
8885 mbedtls_sha256_free( &handshake->fin_sha256 );
8886#endif
8887#if defined(MBEDTLS_SHA512_C)
8888 mbedtls_sha512_free( &handshake->fin_sha512 );
8889#endif
8890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#if defined(MBEDTLS_DHM_C)
8893 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895#if defined(MBEDTLS_ECDH_C)
8896 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008897#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008898#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008899 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008900#if defined(MBEDTLS_SSL_CLI_C)
8901 mbedtls_free( handshake->ecjpake_cache );
8902 handshake->ecjpake_cache = NULL;
8903 handshake->ecjpake_cache_len = 0;
8904#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008905#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008906
Janos Follath4ae5c292016-02-10 11:27:43 +00008907#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8908 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008909 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008910 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008911#endif
8912
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008913#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8914 if( handshake->psk != NULL )
8915 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008916 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008917 mbedtls_free( handshake->psk );
8918 }
8919#endif
8920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8922 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008923 /*
8924 * Free only the linked list wrapper, not the keys themselves
8925 * since the belong to the SNI callback
8926 */
8927 if( handshake->sni_key_cert != NULL )
8928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008930
8931 while( cur != NULL )
8932 {
8933 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008935 cur = next;
8936 }
8937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008939
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008940#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008941 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008942#endif
8943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944#if defined(MBEDTLS_SSL_PROTO_DTLS)
8945 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008946 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008947 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008948#endif
8949
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008950 mbedtls_platform_zeroize( handshake,
8951 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008952}
8953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008955{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008956 if( session == NULL )
8957 return;
8958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008960 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 mbedtls_x509_crt_free( session->peer_cert );
8963 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008964 }
Paul Bakkered27a042013-04-18 22:46:23 +02008965#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008966
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008967#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008969#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008970
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008971 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008972}
8973
Paul Bakker5121ce52009-01-03 21:22:43 +00008974/*
8975 * Free an SSL context
8976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008978{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008979 if( ssl == NULL )
8980 return;
8981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008983
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008984 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008985 {
Angus Grattond8213d02016-05-25 20:56:48 +10008986 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008988 }
8989
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008990 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008991 {
Angus Grattond8213d02016-05-25 20:56:48 +10008992 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008994 }
8995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008997 if( ssl->compress_buf != NULL )
8998 {
Angus Grattond8213d02016-05-25 20:56:48 +10008999 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009001 }
9002#endif
9003
Paul Bakker48916f92012-09-16 19:57:18 +00009004 if( ssl->transform )
9005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006 mbedtls_ssl_transform_free( ssl->transform );
9007 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009008 }
9009
9010 if( ssl->handshake )
9011 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009012 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9014 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 mbedtls_free( ssl->handshake );
9017 mbedtls_free( ssl->transform_negotiate );
9018 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009019 }
9020
Paul Bakkerc0463502013-02-14 11:19:38 +01009021 if( ssl->session )
9022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023 mbedtls_ssl_session_free( ssl->session );
9024 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009025 }
9026
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009027#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009028 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009029 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009030 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009032 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009033#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9036 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9039 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009040 }
9041#endif
9042
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009043#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009045#endif
9046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009048
Paul Bakker86f04f42013-02-14 11:20:09 +01009049 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009050 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009051}
9052
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009053/*
9054 * Initialze mbedtls_ssl_config
9055 */
9056void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9057{
9058 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9059}
9060
Simon Butcherc97b6972015-12-27 23:48:17 +00009061#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009062static int ssl_preset_default_hashes[] = {
9063#if defined(MBEDTLS_SHA512_C)
9064 MBEDTLS_MD_SHA512,
9065 MBEDTLS_MD_SHA384,
9066#endif
9067#if defined(MBEDTLS_SHA256_C)
9068 MBEDTLS_MD_SHA256,
9069 MBEDTLS_MD_SHA224,
9070#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009071#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009072 MBEDTLS_MD_SHA1,
9073#endif
9074 MBEDTLS_MD_NONE
9075};
Simon Butcherc97b6972015-12-27 23:48:17 +00009076#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009077
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009078static int ssl_preset_suiteb_ciphersuites[] = {
9079 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9080 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9081 0
9082};
9083
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009084#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009085static int ssl_preset_suiteb_hashes[] = {
9086 MBEDTLS_MD_SHA256,
9087 MBEDTLS_MD_SHA384,
9088 MBEDTLS_MD_NONE
9089};
9090#endif
9091
9092#if defined(MBEDTLS_ECP_C)
9093static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009094#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009095 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009096#endif
9097#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009098 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009099#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009100 MBEDTLS_ECP_DP_NONE
9101};
9102#endif
9103
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009104/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009105 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009106 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009107int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009108 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009109{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009110#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009111 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009112#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009113
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009114 /* Use the functions here so that they are covered in tests,
9115 * but otherwise access member directly for efficiency */
9116 mbedtls_ssl_conf_endpoint( conf, endpoint );
9117 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009118
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009119 /*
9120 * Things that are common to all presets
9121 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009122#if defined(MBEDTLS_SSL_CLI_C)
9123 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9124 {
9125 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9126#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9127 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9128#endif
9129 }
9130#endif
9131
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009132#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009133 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009134#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009135
9136#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9137 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9138#endif
9139
9140#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9141 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9142#endif
9143
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009144#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9145 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9146#endif
9147
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009148#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009149 conf->f_cookie_write = ssl_cookie_write_dummy;
9150 conf->f_cookie_check = ssl_cookie_check_dummy;
9151#endif
9152
9153#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9154 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9155#endif
9156
Janos Follath088ce432017-04-10 12:42:31 +01009157#if defined(MBEDTLS_SSL_SRV_C)
9158 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9159#endif
9160
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009161#if defined(MBEDTLS_SSL_PROTO_DTLS)
9162 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9163 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9164#endif
9165
9166#if defined(MBEDTLS_SSL_RENEGOTIATION)
9167 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009168 memset( conf->renego_period, 0x00, 2 );
9169 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009170#endif
9171
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009172#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9173 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9174 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009175 const unsigned char dhm_p[] =
9176 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9177 const unsigned char dhm_g[] =
9178 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9179
Hanno Beckera90658f2017-10-04 15:29:08 +01009180 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9181 dhm_p, sizeof( dhm_p ),
9182 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009183 {
9184 return( ret );
9185 }
9186 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009187#endif
9188
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009189 /*
9190 * Preset-specific defaults
9191 */
9192 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009193 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009194 /*
9195 * NSA Suite B
9196 */
9197 case MBEDTLS_SSL_PRESET_SUITEB:
9198 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9199 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9200 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9201 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9202
9203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9206 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9207 ssl_preset_suiteb_ciphersuites;
9208
9209#if defined(MBEDTLS_X509_CRT_PARSE_C)
9210 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009211#endif
9212
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009213#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009214 conf->sig_hashes = ssl_preset_suiteb_hashes;
9215#endif
9216
9217#if defined(MBEDTLS_ECP_C)
9218 conf->curve_list = ssl_preset_suiteb_curves;
9219#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009220 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009221
9222 /*
9223 * Default
9224 */
9225 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009226 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9227 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9228 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9229 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9230 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9231 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9232 MBEDTLS_SSL_MIN_MINOR_VERSION :
9233 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009234 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9235 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9236
9237#if defined(MBEDTLS_SSL_PROTO_DTLS)
9238 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9239 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9240#endif
9241
9242 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9243 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9244 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9245 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9246 mbedtls_ssl_list_ciphersuites();
9247
9248#if defined(MBEDTLS_X509_CRT_PARSE_C)
9249 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9250#endif
9251
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009252#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009253 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009254#endif
9255
9256#if defined(MBEDTLS_ECP_C)
9257 conf->curve_list = mbedtls_ecp_grp_id_list();
9258#endif
9259
9260#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9261 conf->dhm_min_bitlen = 1024;
9262#endif
9263 }
9264
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009265 return( 0 );
9266}
9267
9268/*
9269 * Free mbedtls_ssl_config
9270 */
9271void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9272{
9273#if defined(MBEDTLS_DHM_C)
9274 mbedtls_mpi_free( &conf->dhm_P );
9275 mbedtls_mpi_free( &conf->dhm_G );
9276#endif
9277
9278#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9279 if( conf->psk != NULL )
9280 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009281 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009282 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009283 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009284 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009285 }
9286
9287 if( conf->psk_identity != NULL )
9288 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009289 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009290 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009291 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009292 conf->psk_identity_len = 0;
9293 }
9294#endif
9295
9296#if defined(MBEDTLS_X509_CRT_PARSE_C)
9297 ssl_key_cert_free( conf->key_cert );
9298#endif
9299
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009300 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009301}
9302
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009303#if defined(MBEDTLS_PK_C) && \
9304 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009305/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310#if defined(MBEDTLS_RSA_C)
9311 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9312 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314#if defined(MBEDTLS_ECDSA_C)
9315 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9316 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009319}
9320
Hanno Becker7e5437a2017-04-28 17:15:26 +01009321unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9322{
9323 switch( type ) {
9324 case MBEDTLS_PK_RSA:
9325 return( MBEDTLS_SSL_SIG_RSA );
9326 case MBEDTLS_PK_ECDSA:
9327 case MBEDTLS_PK_ECKEY:
9328 return( MBEDTLS_SSL_SIG_ECDSA );
9329 default:
9330 return( MBEDTLS_SSL_SIG_ANON );
9331 }
9332}
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009335{
9336 switch( sig )
9337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338#if defined(MBEDTLS_RSA_C)
9339 case MBEDTLS_SSL_SIG_RSA:
9340 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#if defined(MBEDTLS_ECDSA_C)
9343 case MBEDTLS_SSL_SIG_ECDSA:
9344 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009345#endif
9346 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009348 }
9349}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009350#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009351
Hanno Becker7e5437a2017-04-28 17:15:26 +01009352#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9353 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9354
9355/* Find an entry in a signature-hash set matching a given hash algorithm. */
9356mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9357 mbedtls_pk_type_t sig_alg )
9358{
9359 switch( sig_alg )
9360 {
9361 case MBEDTLS_PK_RSA:
9362 return( set->rsa );
9363 case MBEDTLS_PK_ECDSA:
9364 return( set->ecdsa );
9365 default:
9366 return( MBEDTLS_MD_NONE );
9367 }
9368}
9369
9370/* Add a signature-hash-pair to a signature-hash set */
9371void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9372 mbedtls_pk_type_t sig_alg,
9373 mbedtls_md_type_t md_alg )
9374{
9375 switch( sig_alg )
9376 {
9377 case MBEDTLS_PK_RSA:
9378 if( set->rsa == MBEDTLS_MD_NONE )
9379 set->rsa = md_alg;
9380 break;
9381
9382 case MBEDTLS_PK_ECDSA:
9383 if( set->ecdsa == MBEDTLS_MD_NONE )
9384 set->ecdsa = md_alg;
9385 break;
9386
9387 default:
9388 break;
9389 }
9390}
9391
9392/* Allow exactly one hash algorithm for each signature. */
9393void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9394 mbedtls_md_type_t md_alg )
9395{
9396 set->rsa = md_alg;
9397 set->ecdsa = md_alg;
9398}
9399
9400#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9401 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9402
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009403/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009404 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009407{
9408 switch( hash )
9409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410#if defined(MBEDTLS_MD5_C)
9411 case MBEDTLS_SSL_HASH_MD5:
9412 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#if defined(MBEDTLS_SHA1_C)
9415 case MBEDTLS_SSL_HASH_SHA1:
9416 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009417#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418#if defined(MBEDTLS_SHA256_C)
9419 case MBEDTLS_SSL_HASH_SHA224:
9420 return( MBEDTLS_MD_SHA224 );
9421 case MBEDTLS_SSL_HASH_SHA256:
9422 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424#if defined(MBEDTLS_SHA512_C)
9425 case MBEDTLS_SSL_HASH_SHA384:
9426 return( MBEDTLS_MD_SHA384 );
9427 case MBEDTLS_SSL_HASH_SHA512:
9428 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009429#endif
9430 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009432 }
9433}
9434
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009435/*
9436 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9437 */
9438unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9439{
9440 switch( md )
9441 {
9442#if defined(MBEDTLS_MD5_C)
9443 case MBEDTLS_MD_MD5:
9444 return( MBEDTLS_SSL_HASH_MD5 );
9445#endif
9446#if defined(MBEDTLS_SHA1_C)
9447 case MBEDTLS_MD_SHA1:
9448 return( MBEDTLS_SSL_HASH_SHA1 );
9449#endif
9450#if defined(MBEDTLS_SHA256_C)
9451 case MBEDTLS_MD_SHA224:
9452 return( MBEDTLS_SSL_HASH_SHA224 );
9453 case MBEDTLS_MD_SHA256:
9454 return( MBEDTLS_SSL_HASH_SHA256 );
9455#endif
9456#if defined(MBEDTLS_SHA512_C)
9457 case MBEDTLS_MD_SHA384:
9458 return( MBEDTLS_SSL_HASH_SHA384 );
9459 case MBEDTLS_MD_SHA512:
9460 return( MBEDTLS_SSL_HASH_SHA512 );
9461#endif
9462 default:
9463 return( MBEDTLS_SSL_HASH_NONE );
9464 }
9465}
9466
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009467#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009468/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009469 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009470 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009471 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009472int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009475
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009476 if( ssl->conf->curve_list == NULL )
9477 return( -1 );
9478
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009479 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009480 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009481 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009482
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009483 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009484}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009485#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009486
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009487#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009488/*
9489 * Check if a hash proposed by the peer is in our list.
9490 * Return 0 if we're willing to use it, -1 otherwise.
9491 */
9492int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9493 mbedtls_md_type_t md )
9494{
9495 const int *cur;
9496
9497 if( ssl->conf->sig_hashes == NULL )
9498 return( -1 );
9499
9500 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9501 if( *cur == (int) md )
9502 return( 0 );
9503
9504 return( -1 );
9505}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009506#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#if defined(MBEDTLS_X509_CRT_PARSE_C)
9509int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9510 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009511 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009512 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009513{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009514 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009516 int usage = 0;
9517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009519 const char *ext_oid;
9520 size_t ext_len;
9521#endif
9522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9524 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009525 ((void) cert);
9526 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009527 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009528#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9531 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009532 {
9533 /* Server part of the key exchange */
9534 switch( ciphersuite->key_exchange )
9535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 case MBEDTLS_KEY_EXCHANGE_RSA:
9537 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009538 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009539 break;
9540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9542 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9543 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9544 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009545 break;
9546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9548 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009549 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009550 break;
9551
9552 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 case MBEDTLS_KEY_EXCHANGE_NONE:
9554 case MBEDTLS_KEY_EXCHANGE_PSK:
9555 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9556 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009557 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009558 usage = 0;
9559 }
9560 }
9561 else
9562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9564 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009565 }
9566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009568 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009569 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009570 ret = -1;
9571 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009572#else
9573 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9577 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9580 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009581 }
9582 else
9583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9585 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009586 }
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009589 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009590 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009591 ret = -1;
9592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009594
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009595 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009596}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009598
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009599/*
9600 * Convert version numbers to/from wire format
9601 * and, for DTLS, to/from TLS equivalent.
9602 *
9603 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009604 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009605 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9606 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009609 unsigned char ver[2] )
9610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611#if defined(MBEDTLS_SSL_PROTO_DTLS)
9612 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009615 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9616
9617 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9618 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9619 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009620 else
9621#else
9622 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009623#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009624 {
9625 ver[0] = (unsigned char) major;
9626 ver[1] = (unsigned char) minor;
9627 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009628}
9629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009631 const unsigned char ver[2] )
9632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633#if defined(MBEDTLS_SSL_PROTO_DTLS)
9634 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009635 {
9636 *major = 255 - ver[0] + 2;
9637 *minor = 255 - ver[1] + 1;
9638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009640 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9641 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009642 else
9643#else
9644 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009645#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009646 {
9647 *major = ver[0];
9648 *minor = ver[1];
9649 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009650}
9651
Simon Butcher99000142016-10-13 17:21:01 +01009652int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9653{
9654#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9655 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9656 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9657
9658 switch( md )
9659 {
9660#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9661#if defined(MBEDTLS_MD5_C)
9662 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009663 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009664#endif
9665#if defined(MBEDTLS_SHA1_C)
9666 case MBEDTLS_SSL_HASH_SHA1:
9667 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9668 break;
9669#endif
9670#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9671#if defined(MBEDTLS_SHA512_C)
9672 case MBEDTLS_SSL_HASH_SHA384:
9673 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9674 break;
9675#endif
9676#if defined(MBEDTLS_SHA256_C)
9677 case MBEDTLS_SSL_HASH_SHA256:
9678 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9679 break;
9680#endif
9681 default:
9682 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9683 }
9684
9685 return 0;
9686#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9687 (void) ssl;
9688 (void) md;
9689
9690 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9692}
9693
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009694#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9695 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9696int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9697 unsigned char *output,
9698 unsigned char *data, size_t data_len )
9699{
9700 int ret = 0;
9701 mbedtls_md5_context mbedtls_md5;
9702 mbedtls_sha1_context mbedtls_sha1;
9703
9704 mbedtls_md5_init( &mbedtls_md5 );
9705 mbedtls_sha1_init( &mbedtls_sha1 );
9706
9707 /*
9708 * digitally-signed struct {
9709 * opaque md5_hash[16];
9710 * opaque sha_hash[20];
9711 * };
9712 *
9713 * md5_hash
9714 * MD5(ClientHello.random + ServerHello.random
9715 * + ServerParams);
9716 * sha_hash
9717 * SHA(ClientHello.random + ServerHello.random
9718 * + ServerParams);
9719 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009720 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009721 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009723 goto exit;
9724 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009725 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009726 ssl->handshake->randbytes, 64 ) ) != 0 )
9727 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009729 goto exit;
9730 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009731 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009732 {
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_finish_ret( &mbedtls_md5, output ) ) != 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_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009739 goto exit;
9740 }
9741
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009742 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009743 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009745 goto exit;
9746 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009747 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009748 ssl->handshake->randbytes, 64 ) ) != 0 )
9749 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009751 goto exit;
9752 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009753 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009754 data_len ) ) != 0 )
9755 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009757 goto exit;
9758 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009759 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009760 output + 16 ) ) != 0 )
9761 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009763 goto exit;
9764 }
9765
9766exit:
9767 mbedtls_md5_free( &mbedtls_md5 );
9768 mbedtls_sha1_free( &mbedtls_sha1 );
9769
9770 if( ret != 0 )
9771 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9772 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9773
9774 return( ret );
9775
9776}
9777#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9778 MBEDTLS_SSL_PROTO_TLS1_1 */
9779
9780#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9781 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9782int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009783 unsigned char *hash, size_t *hashlen,
9784 unsigned char *data, size_t data_len,
9785 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009786{
9787 int ret = 0;
9788 mbedtls_md_context_t ctx;
9789 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009790 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009791
9792 mbedtls_md_init( &ctx );
9793
9794 /*
9795 * digitally-signed struct {
9796 * opaque client_random[32];
9797 * opaque server_random[32];
9798 * ServerDHParams params;
9799 * };
9800 */
9801 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9802 {
9803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9804 goto exit;
9805 }
9806 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9807 {
9808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9809 goto exit;
9810 }
9811 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9812 {
9813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9814 goto exit;
9815 }
9816 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9817 {
9818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9819 goto exit;
9820 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009821 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009822 {
9823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9824 goto exit;
9825 }
9826
9827exit:
9828 mbedtls_md_free( &ctx );
9829
9830 if( ret != 0 )
9831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9832 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9833
9834 return( ret );
9835}
9836#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9837 MBEDTLS_SSL_PROTO_TLS1_2 */
9838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839#endif /* MBEDTLS_SSL_TLS_C */