blob: fdfd53774325c976d8ae41143ab20af3274bd054 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010059
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010060/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020064 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010066#else
67 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068#endif
69 return( 0 );
70}
71
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072/*
73 * Start a timer.
74 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020078 if( ssl->f_set_timer == NULL )
79 return;
80
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
82 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083}
84
85/*
86 * Return -1 is timer is expired, 0 if it isn't.
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020090 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020091 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092
93 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 {
95 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
99 return( 0 );
100}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
103 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100105
106#define SSL_DONT_FORCE_FLUSH 0
107#define SSL_FORCE_FLUSH 1
108
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100110
Hanno Beckerba8cd672019-04-23 12:31:42 +0100111#if defined(MBEDTLS_SSL_CID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100112/* Top-level Connection ID API */
113
Hanno Becker07489862019-04-25 16:01:49 +0100114/* WARNING: The CID feature isn't fully implemented yet
115 * and will not be used. */
Hanno Beckereec2be92019-05-03 13:06:44 +0100116int mbedtls_ssl_conf_cid_len( mbedtls_ssl_config *conf,
117 size_t len )
118{
119 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
121
122 conf->cid_len = len;
123 return( 0 );
124}
125
126/* WARNING: The CID feature isn't fully implemented yet
127 * and will not be used. */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100128int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
129 int enable,
130 unsigned char const *own_cid,
131 size_t own_cid_len )
132{
Hanno Becker78c43022019-05-03 14:38:32 +0100133 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
135
Hanno Becker07489862019-04-25 16:01:49 +0100136 ssl->negotiate_cid = enable;
137 if( enable == MBEDTLS_SSL_CID_DISABLED )
138 {
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
140 return( 0 );
141 }
142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100143 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100144
Hanno Beckereec2be92019-05-03 13:06:44 +0100145 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100146 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
148 (unsigned) own_cid_len,
149 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
151 }
152
153 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100154 /* Truncation is not an issue here because
155 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
156 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100157
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100158 return( 0 );
159}
160
161int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
162 int *enabled,
163 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
164 size_t *peer_cid_len )
165{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100166 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100167
Hanno Becker78c43022019-05-03 14:38:32 +0100168 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
169 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
170 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100172 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100173
Hanno Beckercb063f52019-05-03 12:54:52 +0100174 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
175 * were used, but client and server requested the empty CID.
176 * This is indistinguishable from not using the CID extension
177 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100178 if( ssl->transform_in->in_cid_len == 0 &&
179 ssl->transform_in->out_cid_len == 0 )
180 {
181 return( 0 );
182 }
183
184 *peer_cid_len = ssl->transform_in->out_cid_len;
185 memcpy( peer_cid, ssl->transform_in->out_cid,
186 ssl->transform_in->out_cid_len );
187
188 *enabled = MBEDTLS_SSL_CID_ENABLED;
189
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100190 return( 0 );
191}
Hanno Beckerba8cd672019-04-23 12:31:42 +0100192#endif /* MBEDTLS_SSL_CID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100193
Hanno Beckerd5847772018-08-28 10:09:23 +0100194/* Forward declarations for functions related to message buffering. */
195static void ssl_buffering_free( mbedtls_ssl_context *ssl );
196static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
197 uint8_t slot );
198static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
199static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
200static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
201static int ssl_buffer_message( mbedtls_ssl_context *ssl );
202static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100203static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100204
Hanno Beckera67dee22018-08-22 10:05:20 +0100205static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100206static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100207{
Hanno Becker11682cc2018-08-22 14:41:02 +0100208 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100209
210 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100211 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100212
213 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
214}
215
Hanno Becker67bc7c32018-08-06 11:33:50 +0100216static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
217{
Hanno Becker11682cc2018-08-22 14:41:02 +0100218 size_t const bytes_written = ssl->out_left;
219 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100220
221 /* Double-check that the write-index hasn't gone
222 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100223 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100224 {
225 /* Should never happen... */
226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
227 }
228
229 return( (int) ( mtu - bytes_written ) );
230}
231
232static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
233{
234 int ret;
235 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400236 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100237
238#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
239 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
240
241 if( max_len > mfl )
242 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100243
244 /* By the standard (RFC 6066 Sect. 4), the MFL extension
245 * only limits the maximum record payload size, so in theory
246 * we would be allowed to pack multiple records of payload size
247 * MFL into a single datagram. However, this would mean that there's
248 * no way to explicitly communicate MTU restrictions to the peer.
249 *
250 * The following reduction of max_len makes sure that we never
251 * write datagrams larger than MFL + Record Expansion Overhead.
252 */
253 if( max_len <= ssl->out_left )
254 return( 0 );
255
256 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100257#endif
258
259 ret = ssl_get_remaining_space_in_datagram( ssl );
260 if( ret < 0 )
261 return( ret );
262 remaining = (size_t) ret;
263
264 ret = mbedtls_ssl_get_record_expansion( ssl );
265 if( ret < 0 )
266 return( ret );
267 expansion = (size_t) ret;
268
269 if( remaining <= expansion )
270 return( 0 );
271
272 remaining -= expansion;
273 if( remaining >= max_len )
274 remaining = max_len;
275
276 return( (int) remaining );
277}
278
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279/*
280 * Double the retransmit timeout value, within the allowed range,
281 * returning -1 if the maximum value has already been reached.
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284{
285 uint32_t new_timeout;
286
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200287 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200288 return( -1 );
289
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200290 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
291 * in the following way: after the initial transmission and a first
292 * retransmission, back off to a temporary estimated MTU of 508 bytes.
293 * This value is guaranteed to be deliverable (if not guaranteed to be
294 * delivered) of any compliant IPv4 (and IPv6) network, and should work
295 * on most non-IP stacks too. */
296 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400297 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200298 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
300 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200301
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200302 new_timeout = 2 * ssl->handshake->retransmit_timeout;
303
304 /* Avoid arithmetic overflow and range overflow */
305 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200306 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200307 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200308 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200309 }
310
311 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313 ssl->handshake->retransmit_timeout ) );
314
315 return( 0 );
316}
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200320 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200322 ssl->handshake->retransmit_timeout ) );
323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200327/*
328 * Convert max_fragment_length codes to length.
329 * RFC 6066 says:
330 * enum{
331 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
332 * } MaxFragmentLength;
333 * and we add 0 -> extension unused
334 */
Angus Grattond8213d02016-05-25 20:56:48 +1000335static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200336{
Angus Grattond8213d02016-05-25 20:56:48 +1000337 switch( mfl )
338 {
339 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
340 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
341 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
342 return 512;
343 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
344 return 1024;
345 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
346 return 2048;
347 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
348 return 4096;
349 default:
350 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
351 }
352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200354
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200355#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_ssl_session_free( dst );
359 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200362 if( src->peer_cert != NULL )
363 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200364 int ret;
365
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200366 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200367 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200368 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200373 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376 dst->peer_cert = NULL;
377 return( ret );
378 }
379 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200381
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200382#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200383 if( src->ticket != NULL )
384 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200385 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200386 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200387 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200388
389 memcpy( dst->ticket, src->ticket, src->ticket_len );
390 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200391#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
393 return( 0 );
394}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200395#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
398int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200399 const unsigned char *key_enc, const unsigned char *key_dec,
400 size_t keylen,
401 const unsigned char *iv_enc, const unsigned char *iv_dec,
402 size_t ivlen,
403 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200404 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
406int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
407int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
408int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
409int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
410#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000411
Paul Bakker5121ce52009-01-03 21:22:43 +0000412/*
413 * Key material generation
414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200416static int ssl3_prf( const unsigned char *secret, size_t slen,
417 const char *label,
418 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000419 unsigned char *dstbuf, size_t dlen )
420{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100421 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000422 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200423 mbedtls_md5_context md5;
424 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000425 unsigned char padding[16];
426 unsigned char sha1sum[20];
427 ((void)label);
428
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200429 mbedtls_md5_init( &md5 );
430 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200431
Paul Bakker5f70b252012-09-13 14:23:06 +0000432 /*
433 * SSLv3:
434 * block =
435 * MD5( secret + SHA1( 'A' + secret + random ) ) +
436 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
437 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
438 * ...
439 */
440 for( i = 0; i < dlen / 16; i++ )
441 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200442 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000443
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100444 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100445 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100446 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100447 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100448 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100449 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100450 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100451 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100452 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100453 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100455 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100456 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000463 }
464
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100465exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200466 mbedtls_md5_free( &md5 );
467 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000468
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500469 mbedtls_platform_zeroize( padding, sizeof( padding ) );
470 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000471
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200477static int tls1_prf( const unsigned char *secret, size_t slen,
478 const char *label,
479 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000480 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000481{
Paul Bakker23986e52011-04-24 08:57:21 +0000482 size_t nb, hs;
483 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200484 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 unsigned char tmp[128];
486 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 const mbedtls_md_info_t *md_info;
488 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100489 int ret;
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 hs = ( slen + 1 ) / 2;
497 S1 = secret;
498 S2 = secret + slen - hs;
499
500 nb = strlen( label );
501 memcpy( tmp + 20, label, nb );
502 memcpy( tmp + 20 + nb, random, rlen );
503 nb += rlen;
504
505 /*
506 * First compute P_md5(secret,label+random)[0..dlen]
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100512 return( ret );
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
515 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
516 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 for( i = 0; i < dlen; i += 16 )
519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_md_hmac_reset ( &md_ctx );
521 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
522 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_md_hmac_reset ( &md_ctx );
525 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
526 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
529
530 for( j = 0; j < k; j++ )
531 dstbuf[i + j] = h_i[j];
532 }
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100535
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 /*
537 * XOR out with P_sha1(secret,label+random)[0..dlen]
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100543 return( ret );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
546 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
547 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 for( i = 0; i < dlen; i += 20 )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_md_hmac_reset ( &md_ctx );
552 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
553 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_reset ( &md_ctx );
556 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
557 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
560
561 for( j = 0; j < k; j++ )
562 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100566
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500567 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
568 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 return( 0 );
571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
575static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100576 const unsigned char *secret, size_t slen,
577 const char *label,
578 const unsigned char *random, size_t rlen,
579 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000580{
581 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100582 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000583 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100595
596 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000598
599 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100600 memcpy( tmp + md_len, label, nb );
601 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000602 nb += rlen;
603
604 /*
605 * Compute P_<hash>(secret, label + random)[0..dlen]
606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100608 return( ret );
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
611 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
612 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100613
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100614 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_md_hmac_reset ( &md_ctx );
617 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
618 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_md_hmac_reset ( &md_ctx );
621 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
622 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000623
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100624 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000625
626 for( j = 0; j < k; j++ )
627 dstbuf[i + j] = h_i[j];
628 }
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500632 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
633 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634
635 return( 0 );
636}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100639static int tls_prf_sha256( const unsigned char *secret, size_t slen,
640 const char *label,
641 const unsigned char *random, size_t rlen,
642 unsigned char *dstbuf, size_t dlen )
643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100645 label, random, rlen, dstbuf, dlen ) );
646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200650static int tls_prf_sha384( const unsigned char *secret, size_t slen,
651 const char *label,
652 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000653 unsigned char *dstbuf, size_t dlen )
654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_SHA512_C */
659#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
664 defined(MBEDTLS_SSL_PROTO_TLS1_1)
665static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200666#endif
Paul Bakker380da532012-04-18 16:10:25 +0000667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#if defined(MBEDTLS_SSL_PROTO_SSL3)
669static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
670static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200671#endif
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
674static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
675static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200676#endif
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
679#if defined(MBEDTLS_SHA256_C)
680static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
681static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
682static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200683#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SHA512_C)
686static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
687static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
688static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000693{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200694 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 unsigned char keyblk[256];
697 unsigned char *key1;
698 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100699 unsigned char *mac_enc;
700 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000701 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200702 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000703 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000704 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 const mbedtls_cipher_info_t *cipher_info;
706 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_ssl_session *session = ssl->session_negotiate;
709 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
710 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
Hanno Becker3307b532017-12-27 21:37:21 +0000714#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
715 transform->encrypt_then_mac = session->encrypt_then_mac;
716#endif
717 transform->minor_ver = ssl->minor_ver;
718
Hanno Becker8759e162017-12-27 21:34:08 +0000719 ciphersuite_info = handshake->ciphersuite_info;
720 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100721 if( cipher_info == NULL )
722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000724 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100726 }
727
Hanno Becker8759e162017-12-27 21:34:08 +0000728 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100729 if( md_info == NULL )
730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000732 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100734 }
735
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100736#if defined(MBEDTLS_SSL_CID)
737 /* Copy own and peer's CID if the use of the CID
738 * extension has been negotiated. */
739 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
740 {
741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100742
Hanno Becker4932f9f2019-05-03 15:23:51 +0100743 transform->in_cid_len = ssl->own_cid_len;
744 transform->out_cid_len = ssl->handshake->peer_cid_len;
745 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
746 memcpy( transform->out_cid, ssl->handshake->peer_cid,
747 ssl->handshake->peer_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100748
749 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
750 transform->out_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100751 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100752 transform->in_cid_len );
753 }
754#endif /* MBEDTLS_SSL_CID */
755
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000757 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SSL_PROTO_SSL3)
760 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000761 {
Paul Bakker48916f92012-09-16 19:57:18 +0000762 handshake->tls_prf = ssl3_prf;
763 handshake->calc_verify = ssl_calc_verify_ssl;
764 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000765 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200766 else
767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
769 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000770 {
Paul Bakker48916f92012-09-16 19:57:18 +0000771 handshake->tls_prf = tls1_prf;
772 handshake->calc_verify = ssl_calc_verify_tls;
773 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000774 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200775 else
776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
778#if defined(MBEDTLS_SHA512_C)
779 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker8759e162017-12-27 21:34:08 +0000780 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000781 {
Paul Bakker48916f92012-09-16 19:57:18 +0000782 handshake->tls_prf = tls_prf_sha384;
783 handshake->calc_verify = ssl_calc_verify_tls_sha384;
784 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000786 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_SHA256_C)
789 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000790 {
Paul Bakker48916f92012-09-16 19:57:18 +0000791 handshake->tls_prf = tls_prf_sha256;
792 handshake->calc_verify = ssl_calc_verify_tls_sha256;
793 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000794 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200795 else
796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200801 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000802
803 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 * SSLv3:
805 * master =
806 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
807 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
808 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200809 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 * master = PRF( premaster, "master secret", randbytes )[0..47]
812 */
Paul Bakker0a597072012-09-25 21:55:46 +0000813 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000816 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
819 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200820 {
821 unsigned char session_hash[48];
822 size_t hash_len;
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200825
826 ssl->handshake->calc_verify( ssl, session_hash );
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
829 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SHA512_C)
Hanno Becker8759e162017-12-27 21:34:08 +0000832 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200833 {
834 hash_len = 48;
835 }
836 else
837#endif
838 hash_len = 32;
839 }
840 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200842 hash_len = 36;
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200845
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100846 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
847 "extended master secret",
848 session_hash, hash_len,
849 session->master, 48 );
850 if( ret != 0 )
851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100853 return( ret );
854 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200855
856 }
857 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200858#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100859 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
860 "master secret",
861 handshake->randbytes, 64,
862 session->master, 48 );
863 if( ret != 0 )
864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100866 return( ret );
867 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200868
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500869 mbedtls_platform_zeroize( handshake->premaster,
870 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 }
872 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
875 /*
876 * Swap the client and server random values.
877 */
Paul Bakker48916f92012-09-16 19:57:18 +0000878 memcpy( tmp, handshake->randbytes, 64 );
879 memcpy( handshake->randbytes, tmp + 32, 32 );
880 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500881 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000882
883 /*
884 * SSLv3:
885 * key block =
886 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
887 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
888 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
889 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
890 * ...
891 *
892 * TLSv1:
893 * key block = PRF( master, "key expansion", randbytes )
894 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100895 ret = handshake->tls_prf( session->master, 48, "key expansion",
896 handshake->randbytes, 64, keyblk, 256 );
897 if( ret != 0 )
898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100900 return( ret );
901 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
904 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
905 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
906 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
907 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500909 mbedtls_platform_zeroize( handshake->randbytes,
910 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
912 /*
913 * Determine the appropriate key, IV and MAC length.
914 */
Paul Bakker68884e32013-01-07 18:20:04 +0100915
Hanno Beckere7f2df02017-12-27 08:17:40 +0000916 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200917
Hanno Beckerf1229442018-01-03 15:32:31 +0000918#if defined(MBEDTLS_GCM_C) || \
919 defined(MBEDTLS_CCM_C) || \
920 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200922 cipher_info->mode == MBEDTLS_MODE_CCM ||
923 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 {
Hanno Becker8759e162017-12-27 21:34:08 +0000925 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200926
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200927 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000928 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000929 transform->taglen =
930 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200932 /* All modes haves 96-bit IVs;
933 * GCM and CCM has 4 implicit and 8 explicit bytes
934 * ChachaPoly has all 12 bytes implicit
935 */
Paul Bakker68884e32013-01-07 18:20:04 +0100936 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200937 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
938 transform->fixed_ivlen = 12;
939 else
940 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200941
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200942 /* Minimum length of encrypted record */
943 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000944 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100945 }
946 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000947#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
948#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
949 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
950 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100951 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200952 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
954 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200957 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100958 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200960 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000961 mac_key_len = mbedtls_md_get_size( md_info );
962 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200965 /*
966 * If HMAC is to be truncated, we shall keep the leftmost bytes,
967 * (rfc 6066 page 13 or rfc 2104 section 4),
968 * so we only need to adjust the length here.
969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000973
974#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
975 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000976 * HMAC implementation which also truncates the key
977 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000978 mac_key_len = transform->maclen;
979#endif
980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200982
983 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100984 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200986 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200988 transform->minlen = transform->maclen;
989 else
Paul Bakker68884e32013-01-07 18:20:04 +0100990 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200991 /*
992 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100993 * 1. if EtM is in use: one block plus MAC
994 * otherwise: * first multiple of blocklen greater than maclen
995 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
998 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100999 {
1000 transform->minlen = transform->maclen
1001 + cipher_info->block_size;
1002 }
1003 else
1004#endif
1005 {
1006 transform->minlen = transform->maclen
1007 + cipher_info->block_size
1008 - transform->maclen % cipher_info->block_size;
1009 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1012 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1013 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001014 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001015 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1018 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1019 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001020 {
1021 transform->minlen += transform->ivlen;
1022 }
1023 else
1024#endif
1025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001028 }
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001031 else
1032#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1033 {
1034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Hanno Beckere7f2df02017-12-27 08:17:40 +00001038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1039 (unsigned) keylen,
1040 (unsigned) transform->minlen,
1041 (unsigned) transform->ivlen,
1042 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
1044 /*
1045 * Finally setup the cipher contexts, IVs and MAC secrets.
1046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001048 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001050 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001051 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Paul Bakker68884e32013-01-07 18:20:04 +01001053 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001054 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001056 /*
1057 * This is not used in TLS v1.1.
1058 */
Paul Bakker48916f92012-09-16 19:57:18 +00001059 iv_copy_len = ( transform->fixed_ivlen ) ?
1060 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001061 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1062 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001063 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 }
1065 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#endif /* MBEDTLS_SSL_CLI_C */
1067#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001068 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001070 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001071 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Hanno Becker81c7b182017-11-09 18:39:33 +00001073 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001074 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001076 /*
1077 * This is not used in TLS v1.1.
1078 */
Paul Bakker48916f92012-09-16 19:57:18 +00001079 iv_copy_len = ( transform->fixed_ivlen ) ?
1080 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001081 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1082 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001083 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1089 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001090 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Hanno Becker92231322018-01-03 15:32:51 +00001092#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_SSL_PROTO_SSL3)
1094 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001095 {
Hanno Becker92231322018-01-03 15:32:51 +00001096 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001100 }
1101
Hanno Becker81c7b182017-11-09 18:39:33 +00001102 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1103 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001104 }
1105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1107#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1108 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1109 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001110 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001111 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1112 For AEAD-based ciphersuites, there is nothing to do here. */
1113 if( mac_key_len != 0 )
1114 {
1115 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1116 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1117 }
Paul Bakker68884e32013-01-07 18:20:04 +01001118 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001119 else
1120#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001124 }
Hanno Becker92231322018-01-03 15:32:51 +00001125#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1128 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001129 {
1130 int ret = 0;
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001133
Hanno Beckere7f2df02017-12-27 08:17:40 +00001134 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001135 transform->iv_enc, transform->iv_dec,
1136 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001137 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001138 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1141 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001142 }
1143 }
Hanno Becker92231322018-01-03 15:32:51 +00001144#else
1145 ((void) mac_dec);
1146 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001148
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001149#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1150 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001151 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001152 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1153 session->master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001154 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001155 iv_copy_len );
1156 }
1157#endif
1158
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001159 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001160 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001163 return( ret );
1164 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001165
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001166 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001167 cipher_info ) ) != 0 )
1168 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001170 return( ret );
1171 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001174 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001178 return( ret );
1179 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001182 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001186 return( ret );
1187 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_CIPHER_MODE_CBC)
1190 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1193 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001196 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001197 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1200 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001203 return( ret );
1204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001208 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001211 // Initialize compression
1212 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001214 {
Paul Bakker16770332013-10-11 09:59:44 +02001215 if( ssl->compress_buf == NULL )
1216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001218 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001219 if( ssl->compress_buf == NULL )
1220 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001222 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001223 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001224 }
1225 }
1226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001228
Paul Bakker48916f92012-09-16 19:57:18 +00001229 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1230 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001231
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001232 if( deflateInit( &transform->ctx_deflate,
1233 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001234 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1237 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001238 }
1239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
1244 return( 0 );
1245}
1246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_SSL_PROTO_SSL3)
1248void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001250 mbedtls_md5_context md5;
1251 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 unsigned char pad_1[48];
1253 unsigned char pad_2[48];
1254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001257 mbedtls_md5_init( &md5 );
1258 mbedtls_sha1_init( &sha1 );
1259
1260 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1261 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Paul Bakker380da532012-04-18 16:10:25 +00001263 memset( pad_1, 0x36, 48 );
1264 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001266 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1267 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1268 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001270 mbedtls_md5_starts_ret( &md5 );
1271 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1272 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1273 mbedtls_md5_update_ret( &md5, hash, 16 );
1274 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001276 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1277 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1278 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001279
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001280 mbedtls_sha1_starts_ret( &sha1 );
1281 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1282 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1283 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1284 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001288
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001289 mbedtls_md5_free( &md5 );
1290 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001291
Paul Bakker380da532012-04-18 16:10:25 +00001292 return;
1293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1297void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001298{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001299 mbedtls_md5_context md5;
1300 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001303
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001304 mbedtls_md5_init( &md5 );
1305 mbedtls_sha1_init( &sha1 );
1306
1307 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1308 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001309
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001310 mbedtls_md5_finish_ret( &md5, hash );
1311 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001315
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001316 mbedtls_md5_free( &md5 );
1317 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001318
Paul Bakker380da532012-04-18 16:10:25 +00001319 return;
1320}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1324#if defined(MBEDTLS_SHA256_C)
1325void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001326{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001327 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001328
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001329 mbedtls_sha256_init( &sha256 );
1330
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001332
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001333 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001334 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001338
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001339 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001340
Paul Bakker380da532012-04-18 16:10:25 +00001341 return;
1342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_SHA512_C)
1346void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001347{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001348 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001349
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001350 mbedtls_sha512_init( &sha512 );
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001353
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001354 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001355 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001360 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001361
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 return;
1363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#endif /* MBEDTLS_SHA512_C */
1365#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1368int 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 +02001369{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001370 unsigned char *p = ssl->handshake->premaster;
1371 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001372 const unsigned char *psk = ssl->conf->psk;
1373 size_t psk_len = ssl->conf->psk_len;
1374
1375 /* If the psk callback was called, use its result */
1376 if( ssl->handshake->psk != NULL )
1377 {
1378 psk = ssl->handshake->psk;
1379 psk_len = ssl->handshake->psk_len;
1380 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001381
1382 /*
1383 * PMS = struct {
1384 * opaque other_secret<0..2^16-1>;
1385 * opaque psk<0..2^16-1>;
1386 * };
1387 * with "other_secret" depending on the particular key exchange
1388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1390 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001391 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001392 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001394
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001395 *(p++) = (unsigned char)( psk_len >> 8 );
1396 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001397
1398 if( end < p || (size_t)( end - p ) < psk_len )
1399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1400
1401 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001402 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001403 }
1404 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1406#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1407 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001408 {
1409 /*
1410 * other_secret already set by the ClientKeyExchange message,
1411 * and is 48 bytes long
1412 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001413 if( end - p < 2 )
1414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1415
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001416 *p++ = 0;
1417 *p++ = 48;
1418 p += 48;
1419 }
1420 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1422#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1423 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001424 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001425 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001426 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001427
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001428 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001430 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001431 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001434 return( ret );
1435 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001436 *(p++) = (unsigned char)( len >> 8 );
1437 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001438 p += len;
1439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001441 }
1442 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1444#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1445 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001446 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001447 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001448 size_t zlen;
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001451 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001452 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001455 return( ret );
1456 }
1457
1458 *(p++) = (unsigned char)( zlen >> 8 );
1459 *(p++) = (unsigned char)( zlen );
1460 p += zlen;
1461
Janos Follath3fbdada2018-08-15 10:26:53 +01001462 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1463 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001464 }
1465 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001470 }
1471
1472 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001473 if( end - p < 2 )
1474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001475
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001476 *(p++) = (unsigned char)( psk_len >> 8 );
1477 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001478
1479 if( end < p || (size_t)( end - p ) < psk_len )
1480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1481
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001482 memcpy( p, psk, psk_len );
1483 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001484
1485 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1486
1487 return( 0 );
1488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001492/*
1493 * SSLv3.0 MAC functions
1494 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001495#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001496static void ssl_mac( mbedtls_md_context_t *md_ctx,
1497 const unsigned char *secret,
1498 const unsigned char *buf, size_t len,
1499 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001500 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001501{
1502 unsigned char header[11];
1503 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001504 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1506 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001507
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001508 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001510 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001511 else
Paul Bakker68884e32013-01-07 18:20:04 +01001512 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
1514 memcpy( header, ctr, 8 );
1515 header[ 8] = (unsigned char) type;
1516 header[ 9] = (unsigned char)( len >> 8 );
1517 header[10] = (unsigned char)( len );
1518
Paul Bakker68884e32013-01-07 18:20:04 +01001519 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_md_starts( md_ctx );
1521 mbedtls_md_update( md_ctx, secret, md_size );
1522 mbedtls_md_update( md_ctx, padding, padlen );
1523 mbedtls_md_update( md_ctx, header, 11 );
1524 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001525 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001526
Paul Bakker68884e32013-01-07 18:20:04 +01001527 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_md_starts( md_ctx );
1529 mbedtls_md_update( md_ctx, secret, md_size );
1530 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001531 mbedtls_md_update( md_ctx, out, md_size );
1532 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001535
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001536/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001537 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001538#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001539 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1540 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1541 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1542/* This function makes sure every byte in the memory region is accessed
1543 * (in ascending addresses order) */
1544static void ssl_read_memory( unsigned char *p, size_t len )
1545{
1546 unsigned char acc = 0;
1547 volatile unsigned char force;
1548
1549 for( ; len != 0; p++, len-- )
1550 acc ^= *p;
1551
1552 force = acc;
1553 (void) force;
1554}
1555#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1556
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001557/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001559 */
Hanno Becker3307b532017-12-27 21:37:21 +00001560
Hanno Becker92c930f2019-04-29 17:31:37 +01001561#if defined(MBEDTLS_SSL_CID)
Hanno Becker89693692019-05-20 15:06:12 +01001562/* This functions transforms a DTLS plaintext fragment and a record content
1563 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001564 *
1565 * struct {
1566 * opaque content[DTLSPlaintext.length];
1567 * ContentType real_type;
1568 * uint8 zeros[length_of_padding];
1569 * } DTLSInnerPlaintext;
1570 *
1571 * Input:
1572 * - `content`: The beginning of the buffer holding the
1573 * plaintext to be wrapped.
1574 * - `*content_size`: The length of the plaintext in Bytes.
1575 * - `max_len`: The number of Bytes available starting from
1576 * `content`. This must be `>= *content_size`.
1577 * - `rec_type`: The desired record content type.
1578 *
1579 * Output:
1580 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1581 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1582 *
1583 * Returns:
1584 * - `0` on success.
1585 * - A negative error code if `max_len` didn't offer enough space
1586 * for the expansion.
1587 */
1588static int ssl_cid_build_inner_plaintext( unsigned char *content,
1589 size_t *content_size,
1590 size_t remaining,
1591 uint8_t rec_type )
1592{
1593 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001594 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1595 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1596 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001597
1598 /* Write real content type */
1599 if( remaining == 0 )
1600 return( -1 );
1601 content[ len ] = rec_type;
1602 len++;
1603 remaining--;
1604
1605 if( remaining < pad )
1606 return( -1 );
1607 memset( content + len, 0, pad );
1608 len += pad;
1609 remaining -= pad;
1610
1611 *content_size = len;
1612 return( 0 );
1613}
1614
Hanno Becker7dc25772019-05-20 15:08:01 +01001615/* This function parses a DTLSInnerPlaintext structure.
1616 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001617static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1618 size_t *content_size,
1619 uint8_t *rec_type )
1620{
1621 size_t remaining = *content_size;
1622
1623 /* Determine length of padding by skipping zeroes from the back. */
1624 do
1625 {
1626 if( remaining == 0 )
1627 return( -1 );
1628 remaining--;
1629 } while( content[ remaining ] == 0 );
1630
1631 *content_size = remaining;
1632 *rec_type = content[ remaining ];
1633
1634 return( 0 );
1635}
1636#endif /* MBEDTLS_SSL_CID */
1637
Hanno Becker99abf512019-05-20 14:50:53 +01001638/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001639 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001640static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001641 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001642 mbedtls_record *rec )
1643{
Hanno Becker99abf512019-05-20 14:50:53 +01001644 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001645 *
1646 * additional_data = seq_num + TLSCompressed.type +
1647 * TLSCompressed.version + TLSCompressed.length;
1648 *
Hanno Becker99abf512019-05-20 14:50:53 +01001649 * For the CID extension, this is extended as follows
1650 * (quoting draft-ietf-tls-dtls-connection-id-05,
1651 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001652 *
1653 * additional_data = seq_num + DTLSPlaintext.type +
1654 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001655 * cid +
1656 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001657 * length_of_DTLSInnerPlaintext;
1658 */
1659
Hanno Becker3307b532017-12-27 21:37:21 +00001660 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1661 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001662 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001663
1664#if defined(MBEDTLS_SSL_CID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001665 if( rec->cid_len != 0 )
1666 {
1667 memcpy( add_data + 11, rec->cid, rec->cid_len );
1668 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1669 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1670 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1671 *add_data_len = 13 + 1 + rec->cid_len;
1672 }
1673 else
Hanno Beckere83efe62019-04-29 13:52:53 +01001674#endif /* MBEDTLS_SSL_CID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001675 {
1676 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1677 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1678 *add_data_len = 13;
1679 }
Hanno Becker3307b532017-12-27 21:37:21 +00001680}
1681
Hanno Becker611a83b2018-01-03 14:27:32 +00001682int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1683 mbedtls_ssl_transform *transform,
1684 mbedtls_record *rec,
1685 int (*f_rng)(void *, unsigned char *, size_t),
1686 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001687{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001689 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001690 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001691 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001692 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001693 size_t post_avail;
1694
1695 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001696#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker3307b532017-12-27 21:37:21 +00001697 ((void) ssl);
1698#endif
1699
1700 /* The PRNG is used for dynamic IV generation that's used
1701 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1702#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1703 ( defined(MBEDTLS_AES_C) || \
1704 defined(MBEDTLS_ARIA_C) || \
1705 defined(MBEDTLS_CAMELLIA_C) ) && \
1706 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1707 ((void) f_rng);
1708 ((void) p_rng);
1709#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001712
Hanno Becker3307b532017-12-27 21:37:21 +00001713 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001714 {
Hanno Becker3307b532017-12-27 21:37:21 +00001715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1717 }
Hanno Becker505089d2019-05-01 09:45:57 +01001718 if( rec == NULL
1719 || rec->buf == NULL
1720 || rec->buf_len < rec->data_offset
1721 || rec->buf_len - rec->data_offset < rec->data_len
1722#if defined(MBEDTLS_SSL_CID)
1723 || rec->cid_len != 0
1724#endif
1725 )
Hanno Becker3307b532017-12-27 21:37:21 +00001726 {
1727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001729 }
1730
Hanno Becker3307b532017-12-27 21:37:21 +00001731 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001732 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001734 data, rec->data_len );
1735
1736 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1737
1738 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1739 {
1740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1741 (unsigned) rec->data_len,
1742 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1744 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001745
Hanno Beckere83efe62019-04-29 13:52:53 +01001746#if defined(MBEDTLS_SSL_CID)
1747 /*
1748 * Add CID information
1749 */
1750 rec->cid_len = transform->out_cid_len;
1751 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1752 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001753
1754 if( rec->cid_len != 0 )
1755 {
1756 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001757 * Wrap plaintext into DTLSInnerPlaintext structure.
1758 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001759 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001760 * Note that this changes `rec->data_len`, and hence
1761 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001762 */
1763 if( ssl_cid_build_inner_plaintext( data,
1764 &rec->data_len,
1765 post_avail,
1766 rec->type ) != 0 )
1767 {
1768 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1769 }
1770
1771 rec->type = MBEDTLS_SSL_MSG_CID;
1772 }
Hanno Beckere83efe62019-04-29 13:52:53 +01001773#endif /* MBEDTLS_SSL_CID */
1774
Hanno Becker92c930f2019-04-29 17:31:37 +01001775 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1776
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001778 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001780#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 if( mode == MBEDTLS_MODE_STREAM ||
1782 ( mode == MBEDTLS_MODE_CBC
1783#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001784 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785#endif
1786 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 {
Hanno Becker3307b532017-12-27 21:37:21 +00001788 if( post_avail < transform->maclen )
1789 {
1790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1791 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1792 }
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001795 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001796 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001797 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001798 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1799 data, rec->data_len, rec->ctr, rec->type, mac );
1800 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001801 }
1802 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001803#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1805 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001806 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001807 {
Hanno Becker992b6872017-11-09 18:57:39 +00001808 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1809
Hanno Beckere83efe62019-04-29 13:52:53 +01001810 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001811
Hanno Becker3307b532017-12-27 21:37:21 +00001812 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001813 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001814 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1815 data, rec->data_len );
1816 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1817 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1818
1819 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 }
1821 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001822#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1825 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001826 }
1827
Hanno Becker3307b532017-12-27 21:37:21 +00001828 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1829 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830
Hanno Becker3307b532017-12-27 21:37:21 +00001831 rec->data_len += transform->maclen;
1832 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001833 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001834 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001835#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 /*
1838 * Encrypt
1839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1841 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001843 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001844 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001846 "including %d bytes of padding",
1847 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Hanno Becker3307b532017-12-27 21:37:21 +00001849 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1850 transform->iv_enc, transform->ivlen,
1851 data, rec->data_len,
1852 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001855 return( ret );
1856 }
1857
Hanno Becker3307b532017-12-27 21:37:21 +00001858 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1861 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 }
Paul Bakker68884e32013-01-07 18:20:04 +01001864 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00001866
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001867#if defined(MBEDTLS_GCM_C) || \
1868 defined(MBEDTLS_CCM_C) || \
1869 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001871 mode == MBEDTLS_MODE_CCM ||
1872 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001873 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001874 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001875 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00001876 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001877
Hanno Becker3307b532017-12-27 21:37:21 +00001878 /* Check that there's space for both the authentication tag
1879 * and the explicit IV before and after the record content. */
1880 if( post_avail < transform->taglen ||
1881 rec->data_offset < explicit_iv_len )
1882 {
1883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1884 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1885 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001886
Paul Bakker68884e32013-01-07 18:20:04 +01001887 /*
1888 * Generate IV
1889 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001890 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1891 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001892 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001893 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00001894 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1895 explicit_iv_len );
1896 /* Prefix record content with explicit IV. */
1897 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001898 }
1899 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1900 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001901 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001902 unsigned char i;
1903
1904 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1905
1906 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001907 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001908 }
1909 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001910 {
1911 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001914 }
1915
Hanno Beckere83efe62019-04-29 13:52:53 +01001916 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01001917
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1919 iv, transform->ivlen );
1920 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00001921 data - explicit_iv_len, explicit_iv_len );
1922 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01001923 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001925 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00001926 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001927
Paul Bakker68884e32013-01-07 18:20:04 +01001928 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001929 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001930 */
Hanno Becker3307b532017-12-27 21:37:21 +00001931
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001932 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00001933 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01001934 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00001935 data, rec->data_len, /* source */
1936 data, &rec->data_len, /* destination */
1937 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001940 return( ret );
1941 }
1942
Hanno Becker3307b532017-12-27 21:37:21 +00001943 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1944 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001945
Hanno Becker3307b532017-12-27 21:37:21 +00001946 rec->data_len += transform->taglen + explicit_iv_len;
1947 rec->data_offset -= explicit_iv_len;
1948 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001949 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1953#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001954 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001957 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001958 size_t padlen, i;
1959 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001960
Hanno Becker3307b532017-12-27 21:37:21 +00001961 /* Currently we're always using minimal padding
1962 * (up to 255 bytes would be allowed). */
1963 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1964 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 padlen = 0;
1966
Hanno Becker3307b532017-12-27 21:37:21 +00001967 /* Check there's enough space in the buffer for the padding. */
1968 if( post_avail < padlen + 1 )
1969 {
1970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1971 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1972 }
1973
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001975 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Hanno Becker3307b532017-12-27 21:37:21 +00001977 rec->data_len += padlen + 1;
1978 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001981 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001982 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1983 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001984 */
Hanno Becker3307b532017-12-27 21:37:21 +00001985 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001986 {
Hanno Becker3307b532017-12-27 21:37:21 +00001987 if( f_rng == NULL )
1988 {
1989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
1990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1991 }
1992
1993 if( rec->data_offset < transform->ivlen )
1994 {
1995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1996 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1997 }
1998
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001999 /*
2000 * Generate IV
2001 */
Hanno Becker3307b532017-12-27 21:37:21 +00002002 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002003 if( ret != 0 )
2004 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002005
Hanno Becker3307b532017-12-27 21:37:21 +00002006 memcpy( data - transform->ivlen, transform->iv_enc,
2007 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002008
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002009 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002013 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002014 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002015 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Hanno Becker3307b532017-12-27 21:37:21 +00002017 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2018 transform->iv_enc,
2019 transform->ivlen,
2020 data, rec->data_len,
2021 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 return( ret );
2025 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002026
Hanno Becker3307b532017-12-27 21:37:21 +00002027 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002031 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002034 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002035 {
2036 /*
2037 * Save IV in SSL3 and TLS1
2038 */
Hanno Becker3307b532017-12-27 21:37:21 +00002039 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2040 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
Hanno Becker3307b532017-12-27 21:37:21 +00002042 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002043#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002044 {
2045 data -= transform->ivlen;
2046 rec->data_offset -= transform->ivlen;
2047 rec->data_len += transform->ivlen;
2048 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002051 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002052 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002053 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2054
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002055 /*
2056 * MAC(MAC_write_key, seq_num +
2057 * TLSCipherText.type +
2058 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002059 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002060 * IV + // except for TLS 1.0
2061 * ENC(content + padding + padding_length));
2062 */
Hanno Becker3307b532017-12-27 21:37:21 +00002063
2064 if( post_avail < transform->maclen)
2065 {
2066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2067 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2068 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002069
Hanno Beckere83efe62019-04-29 13:52:53 +01002070 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002073 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002074 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002075
Hanno Becker3307b532017-12-27 21:37:21 +00002076 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002077 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002078 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2079 data, rec->data_len );
2080 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2081 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002082
Hanno Becker3307b532017-12-27 21:37:21 +00002083 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002084
Hanno Becker3307b532017-12-27 21:37:21 +00002085 rec->data_len += transform->maclen;
2086 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002087 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002088 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002091 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002093 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002099 /* Make extra sure authentication was performed, exactly once */
2100 if( auth_done != 1 )
2101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002104 }
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002107
2108 return( 0 );
2109}
2110
Hanno Becker611a83b2018-01-03 14:27:32 +00002111int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2112 mbedtls_ssl_transform *transform,
2113 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002115 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002117 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002118#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002119 size_t padlen = 0, correct = 1;
2120#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002121 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002122 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002123 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002124
Hanno Becker611a83b2018-01-03 14:27:32 +00002125#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002126 ((void) ssl);
2127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002130 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002131 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2134 }
2135 if( rec == NULL ||
2136 rec->buf == NULL ||
2137 rec->buf_len < rec->data_offset ||
2138 rec->buf_len - rec->data_offset < rec->data_len )
2139 {
2140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002142 }
2143
Hanno Becker4c6876b2017-12-27 21:28:58 +00002144 data = rec->buf + rec->data_offset;
2145 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002146
Hanno Beckere83efe62019-04-29 13:52:53 +01002147#if defined(MBEDTLS_SSL_CID)
2148 /*
2149 * Match record's CID with incoming CID.
2150 */
2151
Hanno Beckerabd7c892019-05-08 13:02:22 +01002152 if( rec->cid_len != transform->in_cid_len ||
2153 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2154 {
Hanno Becker687e0fb2019-05-08 13:02:55 +01002155 /* Silently skip over record with mismatching CID. */
2156 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002157 }
Hanno Beckere83efe62019-04-29 13:52:53 +01002158#endif /* MBEDTLS_SSL_CID */
2159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2161 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002162 {
2163 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002164 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2165 transform->iv_dec,
2166 transform->ivlen,
2167 data, rec->data_len,
2168 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002171 return( ret );
2172 }
2173
Hanno Becker4c6876b2017-12-27 21:28:58 +00002174 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002178 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 }
Paul Bakker68884e32013-01-07 18:20:04 +01002180 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002182#if defined(MBEDTLS_GCM_C) || \
2183 defined(MBEDTLS_CCM_C) || \
2184 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002186 mode == MBEDTLS_MODE_CCM ||
2187 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002188 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002189 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002190 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002191
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002192 /*
2193 * Compute and update sizes
2194 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002195 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002198 "+ taglen (%d)", rec->data_len,
2199 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002201 }
Paul Bakker68884e32013-01-07 18:20:04 +01002202
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002203 /*
2204 * Prepare IV
2205 */
2206 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2207 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002208 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002209 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002210 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002211
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002212 }
2213 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2214 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002215 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002216 unsigned char i;
2217
2218 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2219
2220 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002221 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002222 }
2223 else
2224 {
2225 /* Reminder if we ever add an AEAD mode with a different size */
2226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2228 }
2229
Hanno Becker4c6876b2017-12-27 21:28:58 +00002230 data += explicit_iv_len;
2231 rec->data_offset += explicit_iv_len;
2232 rec->data_len -= explicit_iv_len + transform->taglen;
2233
Hanno Beckere83efe62019-04-29 13:52:53 +01002234 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002235 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002236 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002237
2238 memcpy( transform->iv_dec + transform->fixed_ivlen,
2239 data - explicit_iv_len, explicit_iv_len );
2240
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002241 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002242 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002243 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002244
Paul Bakker68884e32013-01-07 18:20:04 +01002245
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002246 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002247 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002248 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002249 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2250 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002251 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002252 data, rec->data_len,
2253 data, &olen,
2254 data + rec->data_len,
2255 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2260 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002261
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002262 return( ret );
2263 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002264 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002265
Hanno Becker4c6876b2017-12-27 21:28:58 +00002266 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002270 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2274#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002275 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002278 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002279
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 /*
Paul Bakker45829992013-01-03 14:52:21 +01002281 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002284 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2285 {
2286 /* The ciphertext is prefixed with the CBC IV. */
2287 minlen += transform->ivlen;
2288 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002289#endif
Paul Bakker45829992013-01-03 14:52:21 +01002290
Hanno Becker4c6876b2017-12-27 21:28:58 +00002291 /* Size considerations:
2292 *
2293 * - The CBC cipher text must not be empty and hence
2294 * at least of size transform->ivlen.
2295 *
2296 * Together with the potential IV-prefix, this explains
2297 * the first of the two checks below.
2298 *
2299 * - The record must contain a MAC, either in plain or
2300 * encrypted, depending on whether Encrypt-then-MAC
2301 * is used or not.
2302 * - If it is, the message contains the IV-prefix,
2303 * the CBC ciphertext, and the MAC.
2304 * - If it is not, the padded plaintext, and hence
2305 * the CBC ciphertext, has at least length maclen + 1
2306 * because there is at least the padding length byte.
2307 *
2308 * As the CBC ciphertext is not empty, both cases give the
2309 * lower bound minlen + maclen + 1 on the record size, which
2310 * we test for in the second check below.
2311 */
2312 if( rec->data_len < minlen + transform->ivlen ||
2313 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002316 "+ 1 ) ( + expl IV )", rec->data_len,
2317 transform->ivlen,
2318 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002320 }
2321
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002322 /*
2323 * Authenticate before decrypt if enabled
2324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002326 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002327 {
Hanno Becker992b6872017-11-09 18:57:39 +00002328 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002331
Hanno Becker4c6876b2017-12-27 21:28:58 +00002332 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2333 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002334
Hanno Beckere83efe62019-04-29 13:52:53 +01002335 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002336
Hanno Beckere83efe62019-04-29 13:52:53 +01002337 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2338 add_data_len );
2339 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2340 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002341 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2342 data, rec->data_len );
2343 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2344 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002345
Hanno Becker4c6876b2017-12-27 21:28:58 +00002346 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2347 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002348 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002349 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002350
Hanno Becker4c6876b2017-12-27 21:28:58 +00002351 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2352 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002356 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002357 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002360
2361 /*
2362 * Check length sanity
2363 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002364 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002367 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002369 }
2370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002372 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002373 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002374 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002375 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002376 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002377 /* This is safe because data_len >= minlen + maclen + 1 initially,
2378 * and at this point we have at most subtracted maclen (note that
2379 * minlen == transform->ivlen here). */
2380 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002381
Hanno Becker4c6876b2017-12-27 21:28:58 +00002382 data += transform->ivlen;
2383 rec->data_offset += transform->ivlen;
2384 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002387
Hanno Becker4c6876b2017-12-27 21:28:58 +00002388 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2389 transform->iv_dec, transform->ivlen,
2390 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002393 return( ret );
2394 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002395
Hanno Becker4c6876b2017-12-27 21:28:58 +00002396 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002400 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002403 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002404 {
2405 /*
2406 * Save IV in SSL3 and TLS1
2407 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002408 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2409 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002411#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Hanno Becker4c6876b2017-12-27 21:28:58 +00002413 /* Safe since data_len >= minlen + maclen + 1, so after having
2414 * subtracted at most minlen and maclen up to this point,
2415 * data_len > 0. */
2416 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002417
Hanno Becker4c6876b2017-12-27 21:28:58 +00002418 if( auth_done == 1 )
2419 {
2420 correct *= ( rec->data_len >= padlen + 1 );
2421 padlen *= ( rec->data_len >= padlen + 1 );
2422 }
2423 else
Paul Bakker45829992013-01-03 14:52:21 +01002424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002426 if( rec->data_len < transform->maclen + padlen + 1 )
2427 {
2428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2429 rec->data_len,
2430 transform->maclen,
2431 padlen + 1 ) );
2432 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002433#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002434
2435 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2436 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002437 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Hanno Becker4c6876b2017-12-27 21:28:58 +00002439 padlen++;
2440
2441 /* Regardless of the validity of the padding,
2442 * we have data_len >= padlen here. */
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002445 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002447 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#if defined(MBEDTLS_SSL_DEBUG_ALL)
2450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002451 "should be no more than %d",
2452 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002453#endif
Paul Bakker45829992013-01-03 14:52:21 +01002454 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
2456 }
2457 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2459#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2460 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002461 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002463 /* The padding check involves a series of up to 256
2464 * consecutive memory reads at the end of the record
2465 * plaintext buffer. In order to hide the length and
2466 * validity of the padding, always perform exactly
2467 * `min(256,plaintext_len)` reads (but take into account
2468 * only the last `padlen` bytes for the padding check). */
2469 size_t pad_count = 0;
2470 size_t real_count = 0;
2471 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002472
Hanno Becker4c6876b2017-12-27 21:28:58 +00002473 /* Index of first padding byte; it has been ensured above
2474 * that the subtraction is safe. */
2475 size_t const padding_idx = rec->data_len - padlen;
2476 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2477 size_t const start_idx = rec->data_len - num_checks;
2478 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002479
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002481 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002482 real_count |= ( idx >= padding_idx );
2483 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002484 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002485 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002488 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002490#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002491 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2495 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002499 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500
Hanno Becker4c6876b2017-12-27 21:28:58 +00002501 /* If the padding was found to be invalid, padlen == 0
2502 * and the subtraction is safe. If the padding was found valid,
2503 * padlen hasn't been changed and the previous assertion
2504 * data_len >= padlen still holds. */
2505 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002507 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002509 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2512 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002515#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002517 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
2520 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002521 * Authenticate if not done yet.
2522 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002524#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002525 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002526 {
Hanno Becker992b6872017-11-09 18:57:39 +00002527 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002528
Hanno Becker4c6876b2017-12-27 21:28:58 +00002529 /* If the initial value of padlen was such that
2530 * data_len < maclen + padlen + 1, then padlen
2531 * got reset to 1, and the initial check
2532 * data_len >= minlen + maclen + 1
2533 * guarantees that at this point we still
2534 * have at least data_len >= maclen.
2535 *
2536 * If the initial value of padlen was such that
2537 * data_len >= maclen + padlen + 1, then we have
2538 * subtracted either padlen + 1 (if the padding was correct)
2539 * or 0 (if the padding was incorrect) since then,
2540 * hence data_len >= maclen in any case.
2541 */
2542 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Hanno Beckere83efe62019-04-29 13:52:53 +01002544 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002548 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002549 ssl_mac( &transform->md_ctx_dec,
2550 transform->mac_dec,
2551 data, rec->data_len,
2552 rec->ctr, rec->type,
2553 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002554 }
2555 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2557#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2558 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002559 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002560 {
2561 /*
2562 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002563 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002564 *
2565 * Known timing attacks:
2566 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2567 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002568 * To compensate for different timings for the MAC calculation
2569 * depending on how much padding was removed (which is determined
2570 * by padlen), process extra_run more blocks through the hash
2571 * function.
2572 *
2573 * The formula in the paper is
2574 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2575 * where L1 is the size of the header plus the decrypted message
2576 * plus CBC padding and L2 is the size of the header plus the
2577 * decrypted message. This is for an underlying hash function
2578 * with 64-byte blocks.
2579 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2580 * correctly. We round down instead of up, so -56 is the correct
2581 * value for our calculations instead of -55.
2582 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002583 * Repeat the formula rather than defining a block_size variable.
2584 * This avoids requiring division by a variable at runtime
2585 * (which would be marginally less efficient and would require
2586 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002587 */
2588 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002590
2591 /*
2592 * The next two sizes are the minimum and maximum values of
2593 * in_msglen over all padlen values.
2594 *
2595 * They're independent of padlen, since we previously did
2596 * in_msglen -= padlen.
2597 *
2598 * Note that max_len + maclen is never more than the buffer
2599 * length, as we previously did in_msglen -= maclen too.
2600 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002601 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002602 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2603
Hanno Becker4c6876b2017-12-27 21:28:58 +00002604 memset( tmp, 0, sizeof( tmp ) );
2605
2606 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002607 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002608#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2609 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002610 case MBEDTLS_MD_MD5:
2611 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002612 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002613 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002614 extra_run =
2615 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2616 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002617 break;
2618#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002619#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002620 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002621 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002622 extra_run =
2623 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2624 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002625 break;
2626#endif
2627 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2630 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002631
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002632 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002633
Hanno Beckere83efe62019-04-29 13:52:53 +01002634 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2635 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002636 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2637 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002638 /* Make sure we access everything even when padlen > 0. This
2639 * makes the synchronisation requirements for just-in-time
2640 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002641 ssl_read_memory( data + rec->data_len, padlen );
2642 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002643
2644 /* Call mbedtls_md_process at least once due to cache attacks
2645 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002646 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002647 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002648
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002650
2651 /* Make sure we access all the memory that could contain the MAC,
2652 * before we check it in the next code block. This makes the
2653 * synchronisation requirements for just-in-time Prime+Probe
2654 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002655 ssl_read_memory( data + min_len,
2656 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002657 }
2658 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2660 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002666#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002667 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2668 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002669#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Hanno Becker4c6876b2017-12-27 21:28:58 +00002671 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2672 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#if defined(MBEDTLS_SSL_DEBUG_ALL)
2675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002676#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002677 correct = 0;
2678 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002679 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002680 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002681
2682 /*
2683 * Finally check the correct flag
2684 */
2685 if( correct == 0 )
2686 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002687#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002688
2689 /* Make extra sure authentication was performed, exactly once */
2690 if( auth_done != 1 )
2691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Hanno Becker92c930f2019-04-29 17:31:37 +01002696#if defined(MBEDTLS_SSL_CID)
2697 if( rec->cid_len != 0 )
2698 {
2699 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2700 &rec->type );
2701 if( ret != 0 )
2702 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2703 }
2704#endif /* MBEDTLS_SSL_CID */
2705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 return( 0 );
2709}
2710
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002711#undef MAC_NONE
2712#undef MAC_PLAINTEXT
2713#undef MAC_CIPHERTEXT
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002716/*
2717 * Compression/decompression functions
2718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720{
2721 int ret;
2722 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002723 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002725 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002729 if( len_pre == 0 )
2730 return( 0 );
2731
Paul Bakker2770fbd2012-07-03 13:30:23 +00002732 memcpy( msg_pre, ssl->out_msg, len_pre );
2733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002735 ssl->out_msglen ) );
2736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002738 ssl->out_msg, ssl->out_msglen );
2739
Paul Bakker48916f92012-09-16 19:57:18 +00002740 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2741 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2742 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002743 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002744
Paul Bakker48916f92012-09-16 19:57:18 +00002745 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002746 if( ret != Z_OK )
2747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2749 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002750 }
2751
Angus Grattond8213d02016-05-25 20:56:48 +10002752 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002753 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002756 ssl->out_msglen ) );
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002759 ssl->out_msg, ssl->out_msglen );
2760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002762
2763 return( 0 );
2764}
2765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002767{
2768 int ret;
2769 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002770 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002771 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002772 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002775
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002776 if( len_pre == 0 )
2777 return( 0 );
2778
Paul Bakker2770fbd2012-07-03 13:30:23 +00002779 memcpy( msg_pre, ssl->in_msg, len_pre );
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002782 ssl->in_msglen ) );
2783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002785 ssl->in_msg, ssl->in_msglen );
2786
Paul Bakker48916f92012-09-16 19:57:18 +00002787 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2788 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2789 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002790 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002791 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002792
Paul Bakker48916f92012-09-16 19:57:18 +00002793 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002794 if( ret != Z_OK )
2795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2797 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002798 }
2799
Angus Grattond8213d02016-05-25 20:56:48 +10002800 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002801 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002804 ssl->in_msglen ) );
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002807 ssl->in_msg, ssl->in_msglen );
2808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002810
2811 return( 0 );
2812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2816static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_PROTO_DTLS)
2819static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002820{
2821 /* If renegotiation is not enforced, retransmit until we would reach max
2822 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002823 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002824 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002825 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002826 unsigned char doublings = 1;
2827
2828 while( ratio != 0 )
2829 {
2830 ++doublings;
2831 ratio >>= 1;
2832 }
2833
2834 if( ++ssl->renego_records_seen > doublings )
2835 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002837 return( 0 );
2838 }
2839 }
2840
2841 return( ssl_write_hello_request( ssl ) );
2842}
2843#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002845
Paul Bakker5121ce52009-01-03 21:22:43 +00002846/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002847 * Fill the input message buffer by appending data to it.
2848 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002849 *
2850 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2851 * available (from this read and/or a previous one). Otherwise, an error code
2852 * is returned (possibly EOF or WANT_READ).
2853 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002854 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2855 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2856 * since we always read a whole datagram at once.
2857 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002858 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002859 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002862{
Paul Bakker23986e52011-04-24 08:57:21 +00002863 int ret;
2864 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002868 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002871 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002873 }
2874
Angus Grattond8213d02016-05-25 20:56:48 +10002875 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002879 }
2880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002884 uint32_t timeout;
2885
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002886 /* Just to be sure */
2887 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2888 {
2889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2890 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2892 }
2893
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002894 /*
2895 * The point is, we need to always read a full datagram at once, so we
2896 * sometimes read more then requested, and handle the additional data.
2897 * It could be the rest of the current record (while fetching the
2898 * header) and/or some other records in the same datagram.
2899 */
2900
2901 /*
2902 * Move to the next record in the already read datagram if applicable
2903 */
2904 if( ssl->next_record_offset != 0 )
2905 {
2906 if( ssl->in_left < ssl->next_record_offset )
2907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002910 }
2911
2912 ssl->in_left -= ssl->next_record_offset;
2913
2914 if( ssl->in_left != 0 )
2915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002917 ssl->next_record_offset ) );
2918 memmove( ssl->in_hdr,
2919 ssl->in_hdr + ssl->next_record_offset,
2920 ssl->in_left );
2921 }
2922
2923 ssl->next_record_offset = 0;
2924 }
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002928
2929 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002930 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002931 */
2932 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002935 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002936 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002937
2938 /*
2939 * A record can't be split accross datagrams. If we need to read but
2940 * are not at the beginning of a new record, the caller did something
2941 * wrong.
2942 */
2943 if( ssl->in_left != 0 )
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002947 }
2948
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002949 /*
2950 * Don't even try to read if time's out already.
2951 * This avoids by-passing the timer when repeatedly receiving messages
2952 * that will end up being dropped.
2953 */
2954 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002955 {
2956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002957 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002958 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002959 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002960 {
Angus Grattond8213d02016-05-25 20:56:48 +10002961 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002964 timeout = ssl->handshake->retransmit_timeout;
2965 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002966 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002969
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002970 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002971 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2972 timeout );
2973 else
2974 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002977
2978 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002980 }
2981
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002982 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002985 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002988 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002989 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002992 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002993 }
2994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002998 return( ret );
2999 }
3000
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003001 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003002 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003004 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003006 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003007 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003010 return( ret );
3011 }
3012
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003013 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003014 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003016 }
3017
Paul Bakker5121ce52009-01-03 21:22:43 +00003018 if( ret < 0 )
3019 return( ret );
3020
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003021 ssl->in_left = ret;
3022 }
3023 else
3024#endif
3025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003027 ssl->in_left, nb_want ) );
3028
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003029 while( ssl->in_left < nb_want )
3030 {
3031 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003032
3033 if( ssl_check_timer( ssl ) != 0 )
3034 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3035 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003036 {
3037 if( ssl->f_recv_timeout != NULL )
3038 {
3039 ret = ssl->f_recv_timeout( ssl->p_bio,
3040 ssl->in_hdr + ssl->in_left, len,
3041 ssl->conf->read_timeout );
3042 }
3043 else
3044 {
3045 ret = ssl->f_recv( ssl->p_bio,
3046 ssl->in_hdr + ssl->in_left, len );
3047 }
3048 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003051 ssl->in_left, nb_want ) );
3052 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003053
3054 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003056
3057 if( ret < 0 )
3058 return( ret );
3059
mohammad160352aecb92018-03-28 23:41:40 -07003060 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003061 {
Darryl Green11999bb2018-03-13 15:22:58 +00003062 MBEDTLS_SSL_DEBUG_MSG( 1,
3063 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003064 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3066 }
3067
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003068 ssl->in_left += ret;
3069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 }
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
3074 return( 0 );
3075}
3076
3077/*
3078 * Flush any data not yet written
3079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003081{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003082 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003083 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003087 if( ssl->f_send == NULL )
3088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003090 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003092 }
3093
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003094 /* Avoid incrementing counter if data is flushed */
3095 if( ssl->out_left == 0 )
3096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003098 return( 0 );
3099 }
3100
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 while( ssl->out_left > 0 )
3102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003104 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Hanno Becker2b1e3542018-08-06 11:19:13 +01003106 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003107 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003110
3111 if( ret <= 0 )
3112 return( ret );
3113
mohammad160352aecb92018-03-28 23:41:40 -07003114 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003115 {
Darryl Green11999bb2018-03-13 15:22:58 +00003116 MBEDTLS_SSL_DEBUG_MSG( 1,
3117 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003118 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3120 }
3121
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 ssl->out_left -= ret;
3123 }
3124
Hanno Becker2b1e3542018-08-06 11:19:13 +01003125#if defined(MBEDTLS_SSL_PROTO_DTLS)
3126 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003127 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003128 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003129 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003130 else
3131#endif
3132 {
3133 ssl->out_hdr = ssl->out_buf + 8;
3134 }
3135 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
3139 return( 0 );
3140}
3141
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003142/*
3143 * Functions to handle the DTLS retransmission state machine
3144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003146/*
3147 * Append current handshake message to current outgoing flight
3148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3153 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3154 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155
3156 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003157 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003158 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003161 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003162 }
3163
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003164 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003165 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003168 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003169 }
3170
3171 /* Copy current handshake message with headers */
3172 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3173 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003174 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003175 msg->next = NULL;
3176
3177 /* Append to the current flight */
3178 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003179 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180 else
3181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003183 while( cur->next != NULL )
3184 cur = cur->next;
3185 cur->next = msg;
3186 }
3187
Hanno Becker3b235902018-08-06 09:54:53 +01003188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189 return( 0 );
3190}
3191
3192/*
3193 * Free the current flight of handshake messages
3194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 mbedtls_ssl_flight_item *cur = flight;
3198 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003199
3200 while( cur != NULL )
3201 {
3202 next = cur->next;
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 mbedtls_free( cur->p );
3205 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206
3207 cur = next;
3208 }
3209}
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3212static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003213#endif
3214
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003215/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003216 * Swap transform_out and out_ctr with the alternative ones
3217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003221 unsigned char tmp_out_ctr[8];
3222
3223 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003226 return;
3227 }
3228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003230
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003231 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003232 tmp_transform = ssl->transform_out;
3233 ssl->transform_out = ssl->handshake->alt_transform_out;
3234 ssl->handshake->alt_transform_out = tmp_transform;
3235
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003236 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003237 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3238 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003239 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003240
3241 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003242 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3245 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3250 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003251 }
3252 }
3253#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003254}
3255
3256/*
3257 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003258 */
3259int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3260{
3261 int ret = 0;
3262
3263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3264
3265 ret = mbedtls_ssl_flight_transmit( ssl );
3266
3267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3268
3269 return( ret );
3270}
3271
3272/*
3273 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003274 *
3275 * Need to remember the current message in case flush_output returns
3276 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003277 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003278 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003279int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003280{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003281 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003285 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003287
3288 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003289 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003290 ssl_swap_epochs( ssl );
3291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003293 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003294
3295 while( ssl->handshake->cur_msg != NULL )
3296 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003297 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003298 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003299
Hanno Beckere1dcb032018-08-17 16:47:58 +01003300 int const is_finished =
3301 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3302 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3303
Hanno Becker04da1892018-08-14 13:22:10 +01003304 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3305 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3306
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003307 /* Swap epochs before sending Finished: we can't do it after
3308 * sending ChangeCipherSpec, in case write returns WANT_READ.
3309 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003310 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003311 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003313 ssl_swap_epochs( ssl );
3314 }
3315
Hanno Becker67bc7c32018-08-06 11:33:50 +01003316 ret = ssl_get_remaining_payload_in_datagram( ssl );
3317 if( ret < 0 )
3318 return( ret );
3319 max_frag_len = (size_t) ret;
3320
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003321 /* CCS is copied as is, while HS messages may need fragmentation */
3322 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3323 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003324 if( max_frag_len == 0 )
3325 {
3326 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3327 return( ret );
3328
3329 continue;
3330 }
3331
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003332 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003333 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003334 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003335
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003336 /* Update position inside current message */
3337 ssl->handshake->cur_msg_p += cur->len;
3338 }
3339 else
3340 {
3341 const unsigned char * const p = ssl->handshake->cur_msg_p;
3342 const size_t hs_len = cur->len - 12;
3343 const size_t frag_off = p - ( cur->p + 12 );
3344 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003345 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003346
Hanno Beckere1dcb032018-08-17 16:47:58 +01003347 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003348 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003349 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003350 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351
Hanno Becker67bc7c32018-08-06 11:33:50 +01003352 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3353 return( ret );
3354
3355 continue;
3356 }
3357 max_hs_frag_len = max_frag_len - 12;
3358
3359 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3360 max_hs_frag_len : rem_len;
3361
3362 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003363 {
3364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003365 (unsigned) cur_hs_frag_len,
3366 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003367 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003368
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003369 /* Messages are stored with handshake headers as if not fragmented,
3370 * copy beginning of headers then fill fragmentation fields.
3371 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3372 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003374 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3375 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3376 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3377
Hanno Becker67bc7c32018-08-06 11:33:50 +01003378 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3379 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3380 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003381
3382 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3383
Hanno Becker3f7b9732018-08-28 09:53:25 +01003384 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003385 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3386 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003387 ssl->out_msgtype = cur->type;
3388
3389 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003390 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003391 }
3392
3393 /* If done with the current message move to the next one if any */
3394 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3395 {
3396 if( cur->next != NULL )
3397 {
3398 ssl->handshake->cur_msg = cur->next;
3399 ssl->handshake->cur_msg_p = cur->next->p + 12;
3400 }
3401 else
3402 {
3403 ssl->handshake->cur_msg = NULL;
3404 ssl->handshake->cur_msg_p = NULL;
3405 }
3406 }
3407
3408 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003409 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003412 return( ret );
3413 }
3414 }
3415
Hanno Becker67bc7c32018-08-06 11:33:50 +01003416 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3417 return( ret );
3418
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003419 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3421 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003422 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003425 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3426 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003427
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429
3430 return( 0 );
3431}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003432
3433/*
3434 * To be called when the last message of an incoming flight is received.
3435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003437{
3438 /* We won't need to resend that one any more */
3439 ssl_flight_free( ssl->handshake->flight );
3440 ssl->handshake->flight = NULL;
3441 ssl->handshake->cur_msg = NULL;
3442
3443 /* The next incoming flight will start with this msg_seq */
3444 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3445
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003446 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003447 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003448
Hanno Becker0271f962018-08-16 13:23:47 +01003449 /* Clear future message buffering structure. */
3450 ssl_buffering_free( ssl );
3451
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003452 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003453 ssl_set_timer( ssl, 0 );
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3456 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459 }
3460 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003463
3464/*
3465 * To be called when the last message of an outgoing flight is send.
3466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003468{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003469 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003470 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3473 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003476 }
3477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003481
Paul Bakker5121ce52009-01-03 21:22:43 +00003482/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003483 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003485
3486/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003487 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003488 *
3489 * - fill in handshake headers
3490 * - update handshake checksum
3491 * - DTLS: save message for resending
3492 * - then pass to the record layer
3493 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003494 * DTLS: except for HelloRequest, messages are only queued, and will only be
3495 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003496 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003497 * Inputs:
3498 * - ssl->out_msglen: 4 + actual handshake message len
3499 * (4 is the size of handshake headers for TLS)
3500 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3501 * - ssl->out_msg + 4: the handshake message body
3502 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003503 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003504 * - ssl->out_msglen: the length of the record contents
3505 * (including handshake headers but excluding record headers)
3506 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003507 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003508int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003509{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003510 int ret;
3511 const size_t hs_len = ssl->out_msglen - 4;
3512 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3515
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003516 /*
3517 * Sanity checks
3518 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003519 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003520 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3521 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003522 /* In SSLv3, the client might send a NoCertificate alert. */
3523#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3524 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3525 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3526 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3527#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3528 {
3529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3531 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003534 /* Whenever we send anything different from a
3535 * HelloRequest we should be in a handshake - double check. */
3536 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3537 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003538 ssl->handshake == NULL )
3539 {
3540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003545 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003546 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003548 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003551 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003552#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003553
Hanno Beckerb50a2532018-08-06 11:52:54 +01003554 /* Double-check that we did not exceed the bounds
3555 * of the outgoing record buffer.
3556 * This should never fail as the various message
3557 * writing functions must obey the bounds of the
3558 * outgoing record buffer, but better be safe.
3559 *
3560 * Note: We deliberately do not check for the MTU or MFL here.
3561 */
3562 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3563 {
3564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3565 "size %u, maximum %u",
3566 (unsigned) ssl->out_msglen,
3567 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3568 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3569 }
3570
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003571 /*
3572 * Fill handshake headers
3573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003576 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3577 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3578 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003579
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003580 /*
3581 * DTLS has additional fields in the Handshake layer,
3582 * between the length field and the actual payload:
3583 * uint16 message_seq;
3584 * uint24 fragment_offset;
3585 * uint24 fragment_length;
3586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003588 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003589 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003590 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003591 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003592 {
3593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3594 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003595 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003596 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3598 }
3599
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003600 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003601 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003602
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003603 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003604 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003605 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003606 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3607 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3608 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003609 }
3610 else
3611 {
3612 ssl->out_msg[4] = 0;
3613 ssl->out_msg[5] = 0;
3614 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003615
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003616 /* Handshake hashes are computed without fragmentation,
3617 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003618 memset( ssl->out_msg + 6, 0x00, 3 );
3619 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003620 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003622
Hanno Becker0207e532018-08-28 10:28:28 +01003623 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003624 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3625 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003626 }
3627
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003628 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003630 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003631 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3632 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003633 {
3634 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003637 return( ret );
3638 }
3639 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003640 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003641#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003642 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003643 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003644 {
3645 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3646 return( ret );
3647 }
3648 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003649
3650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3651
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003652 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003653}
3654
3655/*
3656 * Record layer functions
3657 */
3658
3659/*
3660 * Write current record.
3661 *
3662 * Uses:
3663 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3664 * - ssl->out_msglen: length of the record content (excl headers)
3665 * - ssl->out_msg: record content
3666 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003667int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003668{
3669 int ret, done = 0;
3670 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003671 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672
3673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003676 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003678 {
3679 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003682 return( ret );
3683 }
3684
3685 len = ssl->out_msglen;
3686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3690 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 ret = mbedtls_ssl_hw_record_write( ssl );
3695 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3698 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003699 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003700
3701 if( ret == 0 )
3702 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003705 if( !done )
3706 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003707 unsigned i;
3708 size_t protected_record_size;
3709
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003710 /* Skip writing the record content type to after the encryption,
3711 * as it may change when using the CID extension. */
3712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003714 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003715
Hanno Becker19859472018-08-06 09:40:20 +01003716 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003717 ssl->out_len[0] = (unsigned char)( len >> 8 );
3718 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003719
Paul Bakker48916f92012-09-16 19:57:18 +00003720 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003721 {
Hanno Becker3307b532017-12-27 21:37:21 +00003722 mbedtls_record rec;
3723
3724 rec.buf = ssl->out_iv;
3725 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3726 ( ssl->out_iv - ssl->out_buf );
3727 rec.data_len = ssl->out_msglen;
3728 rec.data_offset = ssl->out_msg - rec.buf;
3729
3730 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3731 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3732 ssl->conf->transport, rec.ver );
3733 rec.type = ssl->out_msgtype;
3734
Hanno Becker505089d2019-05-01 09:45:57 +01003735#if defined(MBEDTLS_SSL_CID)
3736 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003737 rec.cid_len = 0;
Hanno Becker505089d2019-05-01 09:45:57 +01003738#endif /* MBEDTLS_SSL_CID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003739
Hanno Becker611a83b2018-01-03 14:27:32 +00003740 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003741 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003744 return( ret );
3745 }
3746
Hanno Becker3307b532017-12-27 21:37:21 +00003747 if( rec.data_offset != 0 )
3748 {
3749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3750 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3751 }
3752
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003753 /* Update the record content type and CID. */
3754 ssl->out_msgtype = rec.type;
Hanno Becker70e79282019-05-03 14:34:53 +01003755#if defined(MBEDTLS_SSL_CID )
3756 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
3757#endif /* MBEDTLS_SSL_CID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003758 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003759 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3760 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003761 }
3762
Hanno Becker43395762019-05-03 14:46:38 +01003763 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003764
3765#if defined(MBEDTLS_SSL_PROTO_DTLS)
3766 /* In case of DTLS, double-check that we don't exceed
3767 * the remaining space in the datagram. */
3768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3769 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003770 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003771 if( ret < 0 )
3772 return( ret );
3773
3774 if( protected_record_size > (size_t) ret )
3775 {
3776 /* Should never happen */
3777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3778 }
3779 }
3780#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003781
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003782 /* Now write the potentially updated record content type. */
3783 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003786 "version = [%d:%d], msglen = %d",
3787 ssl->out_hdr[0], ssl->out_hdr[1],
3788 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003791 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003792
3793 ssl->out_left += protected_record_size;
3794 ssl->out_hdr += protected_record_size;
3795 ssl_update_out_pointers( ssl, ssl->transform_out );
3796
Hanno Becker04484622018-08-06 09:49:38 +01003797 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3798 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3799 break;
3800
3801 /* The loop goes to its end iff the counter is wrapping */
3802 if( i == ssl_ep_len( ssl ) )
3803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3805 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3806 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003807 }
3808
Hanno Becker67bc7c32018-08-06 11:33:50 +01003809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3811 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003813 size_t remaining;
3814 ret = ssl_get_remaining_payload_in_datagram( ssl );
3815 if( ret < 0 )
3816 {
3817 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3818 ret );
3819 return( ret );
3820 }
3821
3822 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003823 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003824 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003825 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003826 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003827 else
3828 {
Hanno Becker513815a2018-08-20 11:56:09 +01003829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003830 }
3831 }
3832#endif /* MBEDTLS_SSL_PROTO_DTLS */
3833
3834 if( ( flush == SSL_FORCE_FLUSH ) &&
3835 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 return( ret );
3839 }
3840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
3843 return( 0 );
3844}
3845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003847
3848static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3849{
3850 if( ssl->in_msglen < ssl->in_hslen ||
3851 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3852 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3853 {
3854 return( 1 );
3855 }
3856 return( 0 );
3857}
Hanno Becker44650b72018-08-16 12:51:11 +01003858
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003859static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003860{
3861 return( ( ssl->in_msg[9] << 16 ) |
3862 ( ssl->in_msg[10] << 8 ) |
3863 ssl->in_msg[11] );
3864}
3865
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003866static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003867{
3868 return( ( ssl->in_msg[6] << 16 ) |
3869 ( ssl->in_msg[7] << 8 ) |
3870 ssl->in_msg[8] );
3871}
3872
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003873static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003874{
3875 uint32_t msg_len, frag_off, frag_len;
3876
3877 msg_len = ssl_get_hs_total_len( ssl );
3878 frag_off = ssl_get_hs_frag_off( ssl );
3879 frag_len = ssl_get_hs_frag_len( ssl );
3880
3881 if( frag_off > msg_len )
3882 return( -1 );
3883
3884 if( frag_len > msg_len - frag_off )
3885 return( -1 );
3886
3887 if( frag_len + 12 > ssl->in_msglen )
3888 return( -1 );
3889
3890 return( 0 );
3891}
3892
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003893/*
3894 * Mark bits in bitmask (used for DTLS HS reassembly)
3895 */
3896static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3897{
3898 unsigned int start_bits, end_bits;
3899
3900 start_bits = 8 - ( offset % 8 );
3901 if( start_bits != 8 )
3902 {
3903 size_t first_byte_idx = offset / 8;
3904
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003905 /* Special case */
3906 if( len <= start_bits )
3907 {
3908 for( ; len != 0; len-- )
3909 mask[first_byte_idx] |= 1 << ( start_bits - len );
3910
3911 /* Avoid potential issues with offset or len becoming invalid */
3912 return;
3913 }
3914
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003915 offset += start_bits; /* Now offset % 8 == 0 */
3916 len -= start_bits;
3917
3918 for( ; start_bits != 0; start_bits-- )
3919 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3920 }
3921
3922 end_bits = len % 8;
3923 if( end_bits != 0 )
3924 {
3925 size_t last_byte_idx = ( offset + len ) / 8;
3926
3927 len -= end_bits; /* Now len % 8 == 0 */
3928
3929 for( ; end_bits != 0; end_bits-- )
3930 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3931 }
3932
3933 memset( mask + offset / 8, 0xFF, len / 8 );
3934}
3935
3936/*
3937 * Check that bitmask is full
3938 */
3939static int ssl_bitmask_check( unsigned char *mask, size_t len )
3940{
3941 size_t i;
3942
3943 for( i = 0; i < len / 8; i++ )
3944 if( mask[i] != 0xFF )
3945 return( -1 );
3946
3947 for( i = 0; i < len % 8; i++ )
3948 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3949 return( -1 );
3950
3951 return( 0 );
3952}
3953
Hanno Becker56e205e2018-08-16 09:06:12 +01003954/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003955static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003956 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003957{
Hanno Becker56e205e2018-08-16 09:06:12 +01003958 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003959
Hanno Becker56e205e2018-08-16 09:06:12 +01003960 alloc_len = 12; /* Handshake header */
3961 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003962
Hanno Beckerd07df862018-08-16 09:14:58 +01003963 if( add_bitmap )
3964 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003965
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003966 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003967}
Hanno Becker56e205e2018-08-16 09:06:12 +01003968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003970
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003971static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003972{
3973 return( ( ssl->in_msg[1] << 16 ) |
3974 ( ssl->in_msg[2] << 8 ) |
3975 ssl->in_msg[3] );
3976}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003977
Simon Butcher99000142016-10-13 17:21:01 +01003978int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003979{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003983 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003985 }
3986
Hanno Becker12555c62018-08-16 12:47:53 +01003987 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003990 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003991 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003995 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003996 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003997 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003998
Hanno Becker44650b72018-08-16 12:51:11 +01003999 if( ssl_check_hs_header( ssl ) != 0 )
4000 {
4001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4002 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4003 }
4004
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004005 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004006 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4007 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4008 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4009 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004010 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004011 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4012 {
4013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4014 recv_msg_seq,
4015 ssl->handshake->in_msg_seq ) );
4016 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4017 }
4018
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004019 /* Retransmit only on last message from previous flight, to avoid
4020 * too many retransmissions.
4021 * Besides, No sane server ever retransmits HelloVerifyRequest */
4022 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004026 "message_seq = %d, start_of_flight = %d",
4027 recv_msg_seq,
4028 ssl->handshake->in_flight_start_seq ) );
4029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004033 return( ret );
4034 }
4035 }
4036 else
4037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004039 "message_seq = %d, expected = %d",
4040 recv_msg_seq,
4041 ssl->handshake->in_msg_seq ) );
4042 }
4043
Hanno Becker90333da2017-10-10 11:27:13 +01004044 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004045 }
4046 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004047
Hanno Becker6d97ef52018-08-16 13:09:04 +01004048 /* Message reassembly is handled alongside buffering of future
4049 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004050 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004051 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004052 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004055 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004056 }
4057 }
4058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004060 /* With TLS we don't handle fragmentation (for now) */
4061 if( ssl->in_msglen < ssl->in_hslen )
4062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4064 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004065 }
4066
Simon Butcher99000142016-10-13 17:21:01 +01004067 return( 0 );
4068}
4069
4070void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4071{
Hanno Becker0271f962018-08-16 13:23:47 +01004072 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004073
Hanno Becker0271f962018-08-16 13:23:47 +01004074 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004075 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004076 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004077 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004078
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004079 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004081 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004082 ssl->handshake != NULL )
4083 {
Hanno Becker0271f962018-08-16 13:23:47 +01004084 unsigned offset;
4085 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004086
Hanno Becker0271f962018-08-16 13:23:47 +01004087 /* Increment handshake sequence number */
4088 hs->in_msg_seq++;
4089
4090 /*
4091 * Clear up handshake buffering and reassembly structure.
4092 */
4093
4094 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004095 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004096
4097 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004098 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4099 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004100 offset++, hs_buf++ )
4101 {
4102 *hs_buf = *(hs_buf + 1);
4103 }
4104
4105 /* Create a fresh last entry */
4106 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004107 }
4108#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004109}
4110
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004111/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004112 * DTLS anti-replay: RFC 6347 4.1.2.6
4113 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004114 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4115 * Bit n is set iff record number in_window_top - n has been seen.
4116 *
4117 * Usually, in_window_top is the last record number seen and the lsb of
4118 * in_window is set. The only exception is the initial state (record number 0
4119 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4122static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004123{
4124 ssl->in_window_top = 0;
4125 ssl->in_window = 0;
4126}
4127
4128static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4129{
4130 return( ( (uint64_t) buf[0] << 40 ) |
4131 ( (uint64_t) buf[1] << 32 ) |
4132 ( (uint64_t) buf[2] << 24 ) |
4133 ( (uint64_t) buf[3] << 16 ) |
4134 ( (uint64_t) buf[4] << 8 ) |
4135 ( (uint64_t) buf[5] ) );
4136}
4137
4138/*
4139 * Return 0 if sequence number is acceptable, -1 otherwise
4140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004142{
4143 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4144 uint64_t bit;
4145
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004146 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004147 return( 0 );
4148
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004149 if( rec_seqnum > ssl->in_window_top )
4150 return( 0 );
4151
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004152 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004153
4154 if( bit >= 64 )
4155 return( -1 );
4156
4157 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4158 return( -1 );
4159
4160 return( 0 );
4161}
4162
4163/*
4164 * Update replay window on new validated record
4165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004167{
4168 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4169
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004170 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004171 return;
4172
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004173 if( rec_seqnum > ssl->in_window_top )
4174 {
4175 /* Update window_top and the contents of the window */
4176 uint64_t shift = rec_seqnum - ssl->in_window_top;
4177
4178 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004179 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004180 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004181 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004182 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004183 ssl->in_window |= 1;
4184 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004185
4186 ssl->in_window_top = rec_seqnum;
4187 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004188 else
4189 {
4190 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004191 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004192
4193 if( bit < 64 ) /* Always true, but be extra sure */
4194 ssl->in_window |= (uint64_t) 1 << bit;
4195 }
4196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004198
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004199#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004200/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004201static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4202
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004203/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004204 * Without any SSL context, check if a datagram looks like a ClientHello with
4205 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004206 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004207 *
4208 * - if cookie is valid, return 0
4209 * - if ClientHello looks superficially valid but cookie is not,
4210 * fill obuf and set olen, then
4211 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4212 * - otherwise return a specific error code
4213 */
4214static int ssl_check_dtls_clihlo_cookie(
4215 mbedtls_ssl_cookie_write_t *f_cookie_write,
4216 mbedtls_ssl_cookie_check_t *f_cookie_check,
4217 void *p_cookie,
4218 const unsigned char *cli_id, size_t cli_id_len,
4219 const unsigned char *in, size_t in_len,
4220 unsigned char *obuf, size_t buf_len, size_t *olen )
4221{
4222 size_t sid_len, cookie_len;
4223 unsigned char *p;
4224
4225 if( f_cookie_write == NULL || f_cookie_check == NULL )
4226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4227
4228 /*
4229 * Structure of ClientHello with record and handshake headers,
4230 * and expected values. We don't need to check a lot, more checks will be
4231 * done when actually parsing the ClientHello - skipping those checks
4232 * avoids code duplication and does not make cookie forging any easier.
4233 *
4234 * 0-0 ContentType type; copied, must be handshake
4235 * 1-2 ProtocolVersion version; copied
4236 * 3-4 uint16 epoch; copied, must be 0
4237 * 5-10 uint48 sequence_number; copied
4238 * 11-12 uint16 length; (ignored)
4239 *
4240 * 13-13 HandshakeType msg_type; (ignored)
4241 * 14-16 uint24 length; (ignored)
4242 * 17-18 uint16 message_seq; copied
4243 * 19-21 uint24 fragment_offset; copied, must be 0
4244 * 22-24 uint24 fragment_length; (ignored)
4245 *
4246 * 25-26 ProtocolVersion client_version; (ignored)
4247 * 27-58 Random random; (ignored)
4248 * 59-xx SessionID session_id; 1 byte len + sid_len content
4249 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4250 * ...
4251 *
4252 * Minimum length is 61 bytes.
4253 */
4254 if( in_len < 61 ||
4255 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4256 in[3] != 0 || in[4] != 0 ||
4257 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4258 {
4259 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4260 }
4261
4262 sid_len = in[59];
4263 if( sid_len > in_len - 61 )
4264 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4265
4266 cookie_len = in[60 + sid_len];
4267 if( cookie_len > in_len - 60 )
4268 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4269
4270 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4271 cli_id, cli_id_len ) == 0 )
4272 {
4273 /* Valid cookie */
4274 return( 0 );
4275 }
4276
4277 /*
4278 * If we get here, we've got an invalid cookie, let's prepare HVR.
4279 *
4280 * 0-0 ContentType type; copied
4281 * 1-2 ProtocolVersion version; copied
4282 * 3-4 uint16 epoch; copied
4283 * 5-10 uint48 sequence_number; copied
4284 * 11-12 uint16 length; olen - 13
4285 *
4286 * 13-13 HandshakeType msg_type; hello_verify_request
4287 * 14-16 uint24 length; olen - 25
4288 * 17-18 uint16 message_seq; copied
4289 * 19-21 uint24 fragment_offset; copied
4290 * 22-24 uint24 fragment_length; olen - 25
4291 *
4292 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4293 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4294 *
4295 * Minimum length is 28.
4296 */
4297 if( buf_len < 28 )
4298 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4299
4300 /* Copy most fields and adapt others */
4301 memcpy( obuf, in, 25 );
4302 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4303 obuf[25] = 0xfe;
4304 obuf[26] = 0xff;
4305
4306 /* Generate and write actual cookie */
4307 p = obuf + 28;
4308 if( f_cookie_write( p_cookie,
4309 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4310 {
4311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4312 }
4313
4314 *olen = p - obuf;
4315
4316 /* Go back and fill length fields */
4317 obuf[27] = (unsigned char)( *olen - 28 );
4318
4319 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4320 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4321 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4322
4323 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4324 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4325
4326 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4327}
4328
4329/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004330 * Handle possible client reconnect with the same UDP quadruplet
4331 * (RFC 6347 Section 4.2.8).
4332 *
4333 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4334 * that looks like a ClientHello.
4335 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004336 * - if the input looks like a ClientHello without cookies,
4337 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004338 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004339 * - if the input looks like a ClientHello with a valid cookie,
4340 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004341 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004342 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004343 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004344 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004345 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4346 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004347 */
4348static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4349{
4350 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004351 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004352
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004353 ret = ssl_check_dtls_clihlo_cookie(
4354 ssl->conf->f_cookie_write,
4355 ssl->conf->f_cookie_check,
4356 ssl->conf->p_cookie,
4357 ssl->cli_id, ssl->cli_id_len,
4358 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004359 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004360
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004361 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4362
4363 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004364 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004365 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004366 * If the error is permanent we'll catch it later,
4367 * if it's not, then hopefully it'll work next time. */
4368 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4369
4370 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004371 }
4372
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004373 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004374 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004375 /* Got a valid cookie, partially reset context */
4376 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4377 {
4378 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4379 return( ret );
4380 }
4381
4382 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004383 }
4384
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004385 return( ret );
4386}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004387#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004388
Hanno Becker46483f12019-05-03 13:25:54 +01004389static int ssl_check_record_type( uint8_t record_type )
4390{
4391 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4392 record_type != MBEDTLS_SSL_MSG_ALERT &&
4393 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4394 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4395 {
4396 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4397 }
4398
4399 return( 0 );
4400}
4401
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004402/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004403 * ContentType type;
4404 * ProtocolVersion version;
4405 * uint16 epoch; // DTLS only
4406 * uint48 sequence_number; // DTLS only
4407 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004408 *
4409 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004410 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004411 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4412 *
4413 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004414 * 1. proceed with the record if this function returns 0
4415 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4416 * 3. return CLIENT_RECONNECT if this function return that value
4417 * 4. drop the whole datagram if this function returns anything else.
4418 * Point 2 is needed when the peer is resending, and we have already received
4419 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004422{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004423 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004424 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004425
Hanno Becker8b09b732019-05-08 12:03:28 +01004426 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004427
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004429 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004430
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004431 /* Check record type */
Hanno Becker8b09b732019-05-08 12:03:28 +01004432#if defined(MBEDTLS_SSL_CID)
4433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4434 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4435 ssl->conf->cid_len != 0 )
4436 {
4437 /* Shift pointers to account for record header including CID
4438 * struct {
4439 * ContentType special_type = tls12_cid;
4440 * ProtocolVersion version;
4441 * uint16 epoch;
4442 * uint48 sequence_number;
4443 * opaque cid[cid_length]; // New field
4444 * uint16 length;
4445 * opaque enc_content[DTLSCiphertext.length];
4446 * } DTLSCiphertext;
4447 */
4448
4449 /* So far, we only support static CID lengths
4450 * fixed in the configuration. */
4451 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4452 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4453 }
4454 else
4455#endif /* MBEDTLS_SSL_CID */
Hanno Becker46483f12019-05-03 13:25:54 +01004456 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004459
4460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004461 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4462 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004463 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4464#endif /* MBEDTLS_SSL_PROTO_DTLS */
4465 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4466 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004469 }
4470
4471 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004472 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4475 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 }
4477
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004478 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4481 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 }
4483
Hanno Becker8b09b732019-05-08 12:03:28 +01004484 /* Now that the total length of the record header is known, ensure
4485 * that the current datagram is large enough to hold it.
4486 * This would fail, for example, if we received a datagram of
4487 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4488 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4489 if( ret != 0 )
4490 {
4491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4492 return( ret );
4493 }
4494 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4495
4496 /* Parse and validate record length
4497 * This must happen after the CID parsing because
4498 * its position in the record header depends on
4499 * the presence of a CID. */
4500
4501 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004502 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004503 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4506 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004507 }
4508
Hanno Becker8b09b732019-05-08 12:03:28 +01004509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4510 "version = [%d:%d], msglen = %d",
4511 ssl->in_msgtype,
4512 major_ver, minor_ver, ssl->in_msglen ) );
4513
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004514 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004515 * DTLS-related tests.
4516 * Check epoch before checking length constraint because
4517 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4518 * message gets duplicated before the corresponding Finished message,
4519 * the second ChangeCipherSpec should be discarded because it belongs
4520 * to an old epoch, but not because its length is shorter than
4521 * the minimum record length for packets using the new record transform.
4522 * Note that these two kinds of failures are handled differently,
4523 * as an unexpected record is silently skipped but an invalid
4524 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004525 */
4526#if defined(MBEDTLS_SSL_PROTO_DTLS)
4527 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4528 {
4529 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4530
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004531 /* Check epoch (and sequence number) with DTLS */
4532 if( rec_epoch != ssl->in_epoch )
4533 {
4534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4535 "expected %d, received %d",
4536 ssl->in_epoch, rec_epoch ) );
4537
4538#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4539 /*
4540 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4541 * access the first byte of record content (handshake type), as we
4542 * have an active transform (possibly iv_len != 0), so use the
4543 * fact that the record header len is 13 instead.
4544 */
4545 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4546 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4547 rec_epoch == 0 &&
4548 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4549 ssl->in_left > 13 &&
4550 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4551 {
4552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4553 "from the same port" ) );
4554 return( ssl_handle_possible_reconnect( ssl ) );
4555 }
4556 else
4557#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004558 {
4559 /* Consider buffering the record. */
4560 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4561 {
4562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4563 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4564 }
4565
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004566 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004567 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 }
4569
4570#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4571 /* Replay detection only works for the current epoch */
4572 if( rec_epoch == ssl->in_epoch &&
4573 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4574 {
4575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4576 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4577 }
4578#endif
4579 }
4580#endif /* MBEDTLS_SSL_PROTO_DTLS */
4581
Hanno Becker52c6dc62017-05-26 16:07:36 +01004582
4583 /* Check length against bounds of the current transform and version */
4584 if( ssl->transform_in == NULL )
4585 {
4586 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004587 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004588 {
4589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4590 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4591 }
4592 }
4593 else
4594 {
4595 if( ssl->in_msglen < ssl->transform_in->minlen )
4596 {
4597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4598 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4599 }
4600
4601#if defined(MBEDTLS_SSL_PROTO_SSL3)
4602 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004603 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004604 {
4605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4606 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4607 }
4608#endif
4609#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4610 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4611 /*
4612 * TLS encrypted messages can have up to 256 bytes of padding
4613 */
4614 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4615 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004616 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004617 {
4618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4619 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4620 }
4621#endif
4622 }
4623
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004624 return( 0 );
4625}
Paul Bakker5121ce52009-01-03 21:22:43 +00004626
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004627/*
4628 * If applicable, decrypt (and decompress) record content
4629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004631{
4632 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004635 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4638 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642 ret = mbedtls_ssl_hw_record_read( ssl );
4643 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4646 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004647 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004648
4649 if( ret == 0 )
4650 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004653 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004655 mbedtls_record rec;
4656
4657 rec.buf = ssl->in_iv;
4658 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4659 - ( ssl->in_iv - ssl->in_buf );
4660 rec.data_len = ssl->in_msglen;
4661 rec.data_offset = 0;
Hanno Becker70e79282019-05-03 14:34:53 +01004662#if defined(MBEDTLS_SSL_CID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004663 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004664 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
4665#endif /* MBEDTLS_SSL_CID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004666
4667 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4668 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4669 ssl->conf->transport, rec.ver );
4670 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004671 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4672 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004675 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4676 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4677
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 return( ret );
4679 }
4680
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004681 if( ssl->in_msgtype != rec.type )
4682 {
4683 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4684 ssl->in_msgtype, rec.type ) );
4685 }
4686
4687 /* The record content type may change during decryption,
4688 * so re-read it. */
4689 ssl->in_msgtype = rec.type;
4690 /* Also update the input buffer, because unfortunately
4691 * the server-side ssl_parse_client_hello() reparses the
4692 * record header when receiving a ClientHello initiating
4693 * a renegotiation. */
4694 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004695 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004696 ssl->in_msglen = rec.data_len;
4697 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4698 ssl->in_len[1] = (unsigned char)( rec.data_len );
4699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 ssl->in_msg, ssl->in_msglen );
4702
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004703#if defined(MBEDTLS_SSL_CID)
4704 /* We have already checked the record content type
4705 * in ssl_parse_record_header(), failing or silently
4706 * dropping the record in the case of an unknown type.
4707 *
4708 * Since with the use of CIDs, the record content type
4709 * might change during decryption, re-check the record
4710 * content type, but treat a failure as fatal this time. */
4711 if( ssl_check_record_type( ssl->in_msgtype ) )
4712 {
4713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4714 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4715 }
4716#endif /* MBEDTLS_SSL_CID */
4717
Angus Grattond8213d02016-05-25 20:56:48 +10004718 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4721 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004723 else if( ssl->in_msglen == 0 )
4724 {
4725#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4726 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4727 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4728 {
4729 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4731 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4732 }
4733#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4734
4735 ssl->nb_zero++;
4736
4737 /*
4738 * Three or more empty messages may be a DoS attack
4739 * (excessive CPU consumption).
4740 */
4741 if( ssl->nb_zero > 3 )
4742 {
4743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004744 "messages, possible DoS attack" ) );
4745 /* Treat the records as if they were not properly authenticated,
4746 * thereby failing the connection if we see more than allowed
4747 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004748 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4749 }
4750 }
4751 else
4752 ssl->nb_zero = 0;
4753
4754#if defined(MBEDTLS_SSL_PROTO_DTLS)
4755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4756 {
4757 ; /* in_ctr read from peer, not maintained internally */
4758 }
4759 else
4760#endif
4761 {
4762 unsigned i;
4763 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4764 if( ++ssl->in_ctr[i - 1] != 0 )
4765 break;
4766
4767 /* The loop goes to its end iff the counter is wrapping */
4768 if( i == ssl_ep_len( ssl ) )
4769 {
4770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4771 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4772 }
4773 }
4774
Paul Bakker5121ce52009-01-03 21:22:43 +00004775 }
4776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004778 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004780 {
4781 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004784 return( ret );
4785 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004790 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004793 }
4794#endif
4795
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004796 return( 0 );
4797}
4798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004800
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004801/*
4802 * Read a record.
4803 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004804 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4805 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4806 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004807 */
Hanno Becker1097b342018-08-15 14:09:41 +01004808
4809/* Helper functions for mbedtls_ssl_read_record(). */
4810static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004811static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4812static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004813
Hanno Becker327c93b2018-08-15 13:56:18 +01004814int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004815 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004816{
4817 int ret;
4818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004820
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004821 if( ssl->keep_current_message == 0 )
4822 {
4823 do {
Simon Butcher99000142016-10-13 17:21:01 +01004824
Hanno Becker26994592018-08-15 14:14:59 +01004825 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004826 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004827 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004828
Hanno Beckere74d5562018-08-15 14:26:08 +01004829 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004830 {
Hanno Becker40f50842018-08-15 14:48:01 +01004831#if defined(MBEDTLS_SSL_PROTO_DTLS)
4832 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004833
Hanno Becker40f50842018-08-15 14:48:01 +01004834 /* We only check for buffered messages if the
4835 * current datagram is fully consumed. */
4836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004837 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004838 {
Hanno Becker40f50842018-08-15 14:48:01 +01004839 if( ssl_load_buffered_message( ssl ) == 0 )
4840 have_buffered = 1;
4841 }
4842
4843 if( have_buffered == 0 )
4844#endif /* MBEDTLS_SSL_PROTO_DTLS */
4845 {
4846 ret = ssl_get_next_record( ssl );
4847 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4848 continue;
4849
4850 if( ret != 0 )
4851 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004852 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004853 return( ret );
4854 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004855 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004856 }
4857
4858 ret = mbedtls_ssl_handle_message_type( ssl );
4859
Hanno Becker40f50842018-08-15 14:48:01 +01004860#if defined(MBEDTLS_SSL_PROTO_DTLS)
4861 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4862 {
4863 /* Buffer future message */
4864 ret = ssl_buffer_message( ssl );
4865 if( ret != 0 )
4866 return( ret );
4867
4868 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4869 }
4870#endif /* MBEDTLS_SSL_PROTO_DTLS */
4871
Hanno Becker90333da2017-10-10 11:27:13 +01004872 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4873 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004874
4875 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004876 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004877 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004878 return( ret );
4879 }
4880
Hanno Becker327c93b2018-08-15 13:56:18 +01004881 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004882 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004883 {
4884 mbedtls_ssl_update_handshake_status( ssl );
4885 }
Simon Butcher99000142016-10-13 17:21:01 +01004886 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004887 else
Simon Butcher99000142016-10-13 17:21:01 +01004888 {
Hanno Becker02f59072018-08-15 14:00:24 +01004889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004890 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004891 }
4892
4893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4894
4895 return( 0 );
4896}
4897
Hanno Becker40f50842018-08-15 14:48:01 +01004898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004899static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004900{
Hanno Becker40f50842018-08-15 14:48:01 +01004901 if( ssl->in_left > ssl->next_record_offset )
4902 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004903
Hanno Becker40f50842018-08-15 14:48:01 +01004904 return( 0 );
4905}
4906
4907static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4908{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004909 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004910 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004911 int ret = 0;
4912
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004913 if( hs == NULL )
4914 return( -1 );
4915
Hanno Beckere00ae372018-08-20 09:39:42 +01004916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4917
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004918 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4919 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4920 {
4921 /* Check if we have seen a ChangeCipherSpec before.
4922 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004923 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004924 {
4925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4926 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004927 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004928 }
4929
Hanno Becker39b8bc92018-08-28 17:17:13 +01004930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004931 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4932 ssl->in_msglen = 1;
4933 ssl->in_msg[0] = 1;
4934
4935 /* As long as they are equal, the exact value doesn't matter. */
4936 ssl->in_left = 0;
4937 ssl->next_record_offset = 0;
4938
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004939 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004940 goto exit;
4941 }
Hanno Becker37f95322018-08-16 13:55:32 +01004942
Hanno Beckerb8f50142018-08-28 10:01:34 +01004943#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004944 /* Debug only */
4945 {
4946 unsigned offset;
4947 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4948 {
4949 hs_buf = &hs->buffering.hs[offset];
4950 if( hs_buf->is_valid == 1 )
4951 {
4952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4953 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004954 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004955 }
4956 }
4957 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004958#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004959
4960 /* Check if we have buffered and/or fully reassembled the
4961 * next handshake message. */
4962 hs_buf = &hs->buffering.hs[0];
4963 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4964 {
4965 /* Synthesize a record containing the buffered HS message. */
4966 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4967 ( hs_buf->data[2] << 8 ) |
4968 hs_buf->data[3];
4969
4970 /* Double-check that we haven't accidentally buffered
4971 * a message that doesn't fit into the input buffer. */
4972 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4973 {
4974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4976 }
4977
4978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4979 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4980 hs_buf->data, msg_len + 12 );
4981
4982 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4983 ssl->in_hslen = msg_len + 12;
4984 ssl->in_msglen = msg_len + 12;
4985 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4986
4987 ret = 0;
4988 goto exit;
4989 }
4990 else
4991 {
4992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4993 hs->in_msg_seq ) );
4994 }
4995
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004996 ret = -1;
4997
4998exit:
4999
5000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5001 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005002}
5003
Hanno Beckera02b0b42018-08-21 17:20:27 +01005004static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5005 size_t desired )
5006{
5007 int offset;
5008 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5010 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005011
Hanno Becker01315ea2018-08-21 17:22:17 +01005012 /* Get rid of future records epoch first, if such exist. */
5013 ssl_free_buffered_record( ssl );
5014
5015 /* Check if we have enough space available now. */
5016 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5017 hs->buffering.total_bytes_buffered ) )
5018 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005020 return( 0 );
5021 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005022
Hanno Becker4f432ad2018-08-28 10:02:32 +01005023 /* We don't have enough space to buffer the next expected handshake
5024 * message. Remove buffers used for future messages to gain space,
5025 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005026 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5027 offset >= 0; offset-- )
5028 {
5029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5030 offset ) );
5031
Hanno Beckerb309b922018-08-23 13:18:05 +01005032 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005033
5034 /* Check if we have enough space available now. */
5035 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5036 hs->buffering.total_bytes_buffered ) )
5037 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005039 return( 0 );
5040 }
5041 }
5042
5043 return( -1 );
5044}
5045
Hanno Becker40f50842018-08-15 14:48:01 +01005046static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5047{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005048 int ret = 0;
5049 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5050
5051 if( hs == NULL )
5052 return( 0 );
5053
5054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5055
5056 switch( ssl->in_msgtype )
5057 {
5058 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005060
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005061 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005062 break;
5063
5064 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005065 {
5066 unsigned recv_msg_seq_offset;
5067 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5068 mbedtls_ssl_hs_buffer *hs_buf;
5069 size_t msg_len = ssl->in_hslen - 12;
5070
5071 /* We should never receive an old handshake
5072 * message - double-check nonetheless. */
5073 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5074 {
5075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5077 }
5078
5079 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5080 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5081 {
5082 /* Silently ignore -- message too far in the future */
5083 MBEDTLS_SSL_DEBUG_MSG( 2,
5084 ( "Ignore future HS message with sequence number %u, "
5085 "buffering window %u - %u",
5086 recv_msg_seq, ssl->handshake->in_msg_seq,
5087 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5088
5089 goto exit;
5090 }
5091
5092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5093 recv_msg_seq, recv_msg_seq_offset ) );
5094
5095 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5096
5097 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005098 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005099 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005100 size_t reassembly_buf_sz;
5101
Hanno Becker37f95322018-08-16 13:55:32 +01005102 hs_buf->is_fragmented =
5103 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5104
5105 /* We copy the message back into the input buffer
5106 * after reassembly, so check that it's not too large.
5107 * This is an implementation-specific limitation
5108 * and not one from the standard, hence it is not
5109 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005110 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005111 {
5112 /* Ignore message */
5113 goto exit;
5114 }
5115
Hanno Beckere0b150f2018-08-21 15:51:03 +01005116 /* Check if we have enough space to buffer the message. */
5117 if( hs->buffering.total_bytes_buffered >
5118 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5119 {
5120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5122 }
5123
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005124 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5125 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005126
5127 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5128 hs->buffering.total_bytes_buffered ) )
5129 {
5130 if( recv_msg_seq_offset > 0 )
5131 {
5132 /* If we can't buffer a future message because
5133 * of space limitations -- ignore. */
5134 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",
5135 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5136 (unsigned) hs->buffering.total_bytes_buffered ) );
5137 goto exit;
5138 }
Hanno Beckere1801392018-08-21 16:51:05 +01005139 else
5140 {
5141 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",
5142 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5143 (unsigned) hs->buffering.total_bytes_buffered ) );
5144 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005145
Hanno Beckera02b0b42018-08-21 17:20:27 +01005146 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005147 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005148 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",
5149 (unsigned) msg_len,
5150 (unsigned) reassembly_buf_sz,
5151 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005152 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005153 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5154 goto exit;
5155 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005156 }
5157
5158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5159 msg_len ) );
5160
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005161 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5162 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005163 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005164 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005165 goto exit;
5166 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005167 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005168
5169 /* Prepare final header: copy msg_type, length and message_seq,
5170 * then add standardised fragment_offset and fragment_length */
5171 memcpy( hs_buf->data, ssl->in_msg, 6 );
5172 memset( hs_buf->data + 6, 0, 3 );
5173 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5174
5175 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005176
5177 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005178 }
5179 else
5180 {
5181 /* Make sure msg_type and length are consistent */
5182 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5183 {
5184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5185 /* Ignore */
5186 goto exit;
5187 }
5188 }
5189
Hanno Becker4422bbb2018-08-20 09:40:19 +01005190 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005191 {
5192 size_t frag_len, frag_off;
5193 unsigned char * const msg = hs_buf->data + 12;
5194
5195 /*
5196 * Check and copy current fragment
5197 */
5198
5199 /* Validation of header fields already done in
5200 * mbedtls_ssl_prepare_handshake_record(). */
5201 frag_off = ssl_get_hs_frag_off( ssl );
5202 frag_len = ssl_get_hs_frag_len( ssl );
5203
5204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5205 frag_off, frag_len ) );
5206 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5207
5208 if( hs_buf->is_fragmented )
5209 {
5210 unsigned char * const bitmask = msg + msg_len;
5211 ssl_bitmask_set( bitmask, frag_off, frag_len );
5212 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5213 msg_len ) == 0 );
5214 }
5215 else
5216 {
5217 hs_buf->is_complete = 1;
5218 }
5219
5220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5221 hs_buf->is_complete ? "" : "not yet " ) );
5222 }
5223
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005224 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005225 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005226
5227 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005228 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005229 break;
5230 }
5231
5232exit:
5233
5234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5235 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005236}
5237#endif /* MBEDTLS_SSL_PROTO_DTLS */
5238
Hanno Becker1097b342018-08-15 14:09:41 +01005239static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005240{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005241 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005242 * Consume last content-layer message and potentially
5243 * update in_msglen which keeps track of the contents'
5244 * consumption state.
5245 *
5246 * (1) Handshake messages:
5247 * Remove last handshake message, move content
5248 * and adapt in_msglen.
5249 *
5250 * (2) Alert messages:
5251 * Consume whole record content, in_msglen = 0.
5252 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005253 * (3) Change cipher spec:
5254 * Consume whole record content, in_msglen = 0.
5255 *
5256 * (4) Application data:
5257 * Don't do anything - the record layer provides
5258 * the application data as a stream transport
5259 * and consumes through mbedtls_ssl_read only.
5260 *
5261 */
5262
5263 /* Case (1): Handshake messages */
5264 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005265 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005266 /* Hard assertion to be sure that no application data
5267 * is in flight, as corrupting ssl->in_msglen during
5268 * ssl->in_offt != NULL is fatal. */
5269 if( ssl->in_offt != NULL )
5270 {
5271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5272 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5273 }
5274
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005275 /*
5276 * Get next Handshake message in the current record
5277 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005278
Hanno Becker4a810fb2017-05-24 16:27:30 +01005279 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005280 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005281 * current handshake content: If DTLS handshake
5282 * fragmentation is used, that's the fragment
5283 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005284 * size here is faulty and should be changed at
5285 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005286 * (2) While it doesn't seem to cause problems, one
5287 * has to be very careful not to assume that in_hslen
5288 * is always <= in_msglen in a sensible communication.
5289 * Again, it's wrong for DTLS handshake fragmentation.
5290 * The following check is therefore mandatory, and
5291 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005292 * Additionally, ssl->in_hslen might be arbitrarily out of
5293 * bounds after handling a DTLS message with an unexpected
5294 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005295 */
5296 if( ssl->in_hslen < ssl->in_msglen )
5297 {
5298 ssl->in_msglen -= ssl->in_hslen;
5299 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5300 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005301
Hanno Becker4a810fb2017-05-24 16:27:30 +01005302 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5303 ssl->in_msg, ssl->in_msglen );
5304 }
5305 else
5306 {
5307 ssl->in_msglen = 0;
5308 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005309
Hanno Becker4a810fb2017-05-24 16:27:30 +01005310 ssl->in_hslen = 0;
5311 }
5312 /* Case (4): Application data */
5313 else if( ssl->in_offt != NULL )
5314 {
5315 return( 0 );
5316 }
5317 /* Everything else (CCS & Alerts) */
5318 else
5319 {
5320 ssl->in_msglen = 0;
5321 }
5322
Hanno Becker1097b342018-08-15 14:09:41 +01005323 return( 0 );
5324}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005325
Hanno Beckere74d5562018-08-15 14:26:08 +01005326static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5327{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005328 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005329 return( 1 );
5330
5331 return( 0 );
5332}
5333
Hanno Becker5f066e72018-08-16 14:56:31 +01005334#if defined(MBEDTLS_SSL_PROTO_DTLS)
5335
5336static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5337{
5338 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5339 if( hs == NULL )
5340 return;
5341
Hanno Becker01315ea2018-08-21 17:22:17 +01005342 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005343 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005344 hs->buffering.total_bytes_buffered -=
5345 hs->buffering.future_record.len;
5346
5347 mbedtls_free( hs->buffering.future_record.data );
5348 hs->buffering.future_record.data = NULL;
5349 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005350}
5351
5352static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5353{
5354 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5355 unsigned char * rec;
5356 size_t rec_len;
5357 unsigned rec_epoch;
5358
5359 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5360 return( 0 );
5361
5362 if( hs == NULL )
5363 return( 0 );
5364
Hanno Becker5f066e72018-08-16 14:56:31 +01005365 rec = hs->buffering.future_record.data;
5366 rec_len = hs->buffering.future_record.len;
5367 rec_epoch = hs->buffering.future_record.epoch;
5368
5369 if( rec == NULL )
5370 return( 0 );
5371
Hanno Becker4cb782d2018-08-20 11:19:05 +01005372 /* Only consider loading future records if the
5373 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005374 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005375 return( 0 );
5376
Hanno Becker5f066e72018-08-16 14:56:31 +01005377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5378
5379 if( rec_epoch != ssl->in_epoch )
5380 {
5381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5382 goto exit;
5383 }
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5386
5387 /* Double-check that the record is not too large */
5388 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5389 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5390 {
5391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5393 }
5394
5395 memcpy( ssl->in_hdr, rec, rec_len );
5396 ssl->in_left = rec_len;
5397 ssl->next_record_offset = 0;
5398
5399 ssl_free_buffered_record( ssl );
5400
5401exit:
5402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5403 return( 0 );
5404}
5405
5406static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5407{
5408 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5409 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005410 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005411
5412 /* Don't buffer future records outside handshakes. */
5413 if( hs == NULL )
5414 return( 0 );
5415
5416 /* Only buffer handshake records (we are only interested
5417 * in Finished messages). */
5418 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5419 return( 0 );
5420
5421 /* Don't buffer more than one future epoch record. */
5422 if( hs->buffering.future_record.data != NULL )
5423 return( 0 );
5424
Hanno Becker01315ea2018-08-21 17:22:17 +01005425 /* Don't buffer record if there's not enough buffering space remaining. */
5426 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5427 hs->buffering.total_bytes_buffered ) )
5428 {
5429 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",
5430 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5431 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005432 return( 0 );
5433 }
5434
Hanno Becker5f066e72018-08-16 14:56:31 +01005435 /* Buffer record */
5436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5437 ssl->in_epoch + 1 ) );
5438 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5439 rec_hdr_len + ssl->in_msglen );
5440
5441 /* ssl_parse_record_header() only considers records
5442 * of the next epoch as candidates for buffering. */
5443 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005444 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005445
5446 hs->buffering.future_record.data =
5447 mbedtls_calloc( 1, hs->buffering.future_record.len );
5448 if( hs->buffering.future_record.data == NULL )
5449 {
5450 /* If we run out of RAM trying to buffer a
5451 * record from the next epoch, just ignore. */
5452 return( 0 );
5453 }
5454
Hanno Becker01315ea2018-08-21 17:22:17 +01005455 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005456
Hanno Becker01315ea2018-08-21 17:22:17 +01005457 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005458 return( 0 );
5459}
5460
5461#endif /* MBEDTLS_SSL_PROTO_DTLS */
5462
Hanno Beckere74d5562018-08-15 14:26:08 +01005463static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005464{
5465 int ret;
5466
Hanno Becker5f066e72018-08-16 14:56:31 +01005467#if defined(MBEDTLS_SSL_PROTO_DTLS)
5468 /* We might have buffered a future record; if so,
5469 * and if the epoch matches now, load it.
5470 * On success, this call will set ssl->in_left to
5471 * the length of the buffered record, so that
5472 * the calls to ssl_fetch_input() below will
5473 * essentially be no-ops. */
5474 ret = ssl_load_buffered_record( ssl );
5475 if( ret != 0 )
5476 return( ret );
5477#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005478
Hanno Becker8b09b732019-05-08 12:03:28 +01005479 /* Reset in pointers to default state for TLS/DTLS records,
5480 * assuming no CID and no offset between record content and
5481 * record plaintext. */
5482 ssl_update_in_pointers( ssl );
5483
5484 /* Ensure that we have enough space available for the default form
5485 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5486 * with no space for CIDs counted in). */
5487 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5488 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005491 return( ret );
5492 }
5493
5494 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005497 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5498 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005499 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005500 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5501 {
5502 ret = ssl_buffer_future_record( ssl );
5503 if( ret != 0 )
5504 return( ret );
5505
5506 /* Fall through to handling of unexpected records */
5507 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5508 }
5509
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005510 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5511 {
5512 /* Skip unexpected record (but not whole datagram) */
5513 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005514 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005515
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5517 "(header)" ) );
5518 }
5519 else
5520 {
5521 /* Skip invalid record and the rest of the datagram */
5522 ssl->next_record_offset = 0;
5523 ssl->in_left = 0;
5524
5525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5526 "(header)" ) );
5527 }
5528
5529 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005530 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005531 }
5532#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005533 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005534 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005535
5536 /*
5537 * Read and optionally decrypt the message contents
5538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005540 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005543 return( ret );
5544 }
5545
5546 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005548 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005549 {
Hanno Becker43395762019-05-03 14:46:38 +01005550 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005551 if( ssl->next_record_offset < ssl->in_left )
5552 {
5553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5554 }
5555 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005556 else
5557#endif
5558 ssl->in_left = 0;
5559
5560 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005564 {
5565 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005566 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005567 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005568 /* Except when waiting for Finished as a bad mac here
5569 * probably means something went wrong in the handshake
5570 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5571 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5572 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5573 {
5574#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5575 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5576 {
5577 mbedtls_ssl_send_alert_message( ssl,
5578 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5579 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5580 }
5581#endif
5582 return( ret );
5583 }
5584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005586 if( ssl->conf->badmac_limit != 0 &&
5587 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5590 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005591 }
5592#endif
5593
Hanno Becker4a810fb2017-05-24 16:27:30 +01005594 /* As above, invalid records cause
5595 * dismissal of the whole datagram. */
5596
5597 ssl->next_record_offset = 0;
5598 ssl->in_left = 0;
5599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005601 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005602 }
5603
5604 return( ret );
5605 }
5606 else
5607#endif
5608 {
5609 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5611 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 mbedtls_ssl_send_alert_message( ssl,
5614 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5615 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005616 }
5617#endif
5618 return( ret );
5619 }
5620 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005621
Simon Butcher99000142016-10-13 17:21:01 +01005622 return( 0 );
5623}
5624
5625int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5626{
5627 int ret;
5628
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005629 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630 * Handle particular types of records
5631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 {
Simon Butcher99000142016-10-13 17:21:01 +01005634 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5635 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005636 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005637 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 }
5639
Hanno Beckere678eaa2018-08-21 14:57:46 +01005640 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005641 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005642 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005643 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5645 ssl->in_msglen ) );
5646 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005647 }
5648
Hanno Beckere678eaa2018-08-21 14:57:46 +01005649 if( ssl->in_msg[0] != 1 )
5650 {
5651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5652 ssl->in_msg[0] ) );
5653 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5654 }
5655
5656#if defined(MBEDTLS_SSL_PROTO_DTLS)
5657 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5658 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5659 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5660 {
5661 if( ssl->handshake == NULL )
5662 {
5663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5664 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5665 }
5666
5667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5668 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5669 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005670#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005671 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005674 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005675 if( ssl->in_msglen != 2 )
5676 {
5677 /* Note: Standard allows for more than one 2 byte alert
5678 to be packed in a single message, but Mbed TLS doesn't
5679 currently support this. */
5680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5681 ssl->in_msglen ) );
5682 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5683 }
5684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 ssl->in_msg[0], ssl->in_msg[1] ) );
5687
5688 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005689 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005694 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005696 }
5697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5699 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5702 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005704
5705#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5706 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5707 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5708 {
Hanno Becker90333da2017-10-10 11:27:13 +01005709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005710 /* Will be handled when trying to parse ServerHello */
5711 return( 0 );
5712 }
5713#endif
5714
5715#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5716 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5717 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5718 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5719 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5720 {
5721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5722 /* Will be handled in mbedtls_ssl_parse_certificate() */
5723 return( 0 );
5724 }
5725#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5726
5727 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005728 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005729 }
5730
Hanno Beckerc76c6192017-06-06 10:03:17 +01005731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005733 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005734 /* Drop unexpected ApplicationData records,
5735 * except at the beginning of renegotiations */
5736 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5737 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5738#if defined(MBEDTLS_SSL_RENEGOTIATION)
5739 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5740 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005741#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005742 )
5743 {
5744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5745 return( MBEDTLS_ERR_SSL_NON_FATAL );
5746 }
5747
5748 if( ssl->handshake != NULL &&
5749 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5750 {
5751 ssl_handshake_wrapup_free_hs_transform( ssl );
5752 }
5753 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005754#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005755
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 return( 0 );
5757}
5758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005760{
5761 int ret;
5762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5764 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5765 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005766 {
5767 return( ret );
5768 }
5769
5770 return( 0 );
5771}
5772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005774 unsigned char level,
5775 unsigned char message )
5776{
5777 int ret;
5778
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005779 if( ssl == NULL || ssl->conf == NULL )
5780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005786 ssl->out_msglen = 2;
5787 ssl->out_msg[0] = level;
5788 ssl->out_msg[1] = message;
5789
Hanno Becker67bc7c32018-08-06 11:33:50 +01005790 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005793 return( ret );
5794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005796
5797 return( 0 );
5798}
5799
Paul Bakker5121ce52009-01-03 21:22:43 +00005800/*
5801 * Handshake functions
5802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5804 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5805 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5806 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5807 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5808 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5809 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005810/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005812{
Hanno Becker8759e162017-12-27 21:34:08 +00005813 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5818 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005819 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5820 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005823 ssl->state++;
5824 return( 0 );
5825 }
5826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005829}
5830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005832{
Hanno Becker8759e162017-12-27 21:34:08 +00005833 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5838 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005839 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5840 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005843 ssl->state++;
5844 return( 0 );
5845 }
5846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005849}
Gilles Peskinef9828522017-05-03 12:28:43 +02005850
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005851#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005852/* Some certificate support -> implement write and parse */
5853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005857 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005859 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5864 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005865 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5866 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005869 ssl->state++;
5870 return( 0 );
5871 }
5872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005874 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005875 {
5876 if( ssl->client_auth == 0 )
5877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 ssl->state++;
5880 return( 0 );
5881 }
5882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 /*
5885 * If using SSLv3 and got no cert, send an Alert message
5886 * (otherwise an empty Certificate message will be sent).
5887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5889 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 {
5891 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5893 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5894 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 goto write_msg;
5898 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901#endif /* MBEDTLS_SSL_CLI_C */
5902#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005903 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5908 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 }
5910 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005911#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005914
5915 /*
5916 * 0 . 0 handshake type
5917 * 1 . 3 handshake length
5918 * 4 . 6 length of all certs
5919 * 7 . 9 length of cert. 1
5920 * 10 . n-1 peer certificate
5921 * n . n+2 length of cert. 2
5922 * n+3 . ... upper level cert, etc.
5923 */
5924 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926
Paul Bakker29087132010-03-21 21:03:34 +00005927 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 {
5929 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005930 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005933 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005935 }
5936
5937 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5938 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5939 ssl->out_msg[i + 2] = (unsigned char)( n );
5940
5941 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5942 i += n; crt = crt->next;
5943 }
5944
5945 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5946 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5947 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5948
5949 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5951 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005952
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005953#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005954write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005955#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005956
5957 ssl->state++;
5958
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005959 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005960 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 return( ret );
5963 }
5964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005966
Paul Bakkered27a042013-04-18 22:46:23 +02005967 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005968}
5969
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005970/*
5971 * Once the certificate message is read, parse it into a cert chain and
5972 * perform basic checks, but leave actual verification to the caller
5973 */
5974static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005975{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005976 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005977 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005978 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980#if defined(MBEDTLS_SSL_SRV_C)
5981#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005982 /*
5983 * Check if the client sent an empty certificate
5984 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005985 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005987 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005988 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5990 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5991 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005995 /* The client was asked for a certificate but didn't send
5996 one. The client should know what's going on, so we
5997 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005998 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005999 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 }
6001 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6005 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006006 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6010 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6011 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6012 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006015
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006016 /* The client was asked for a certificate but didn't send
6017 one. The client should know what's going on, so we
6018 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006019 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006020 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 }
6022 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6024 MBEDTLS_SSL_PROTO_TLS1_2 */
6025#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006030 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6031 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 }
6034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6036 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006039 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6040 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006045
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006048 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006049 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006050
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006051 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006055 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6056 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 }
6059
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006060 /* In case we tried to reuse a session but it failed */
6061 if( ssl->session_negotiate->peer_cert != NULL )
6062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6064 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006065 }
6066
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006067 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006072 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6073 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006074 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 }
6076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006078
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006079 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006080
6081 while( i < ssl->in_hslen )
6082 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006083 if ( i + 3 > ssl->in_hslen ) {
6084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6085 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6086 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6087 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6088 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006089 if( ssl->in_msg[i] != 0 )
6090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006092 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6093 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 }
6096
6097 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6098 | (unsigned int) ssl->in_msg[i + 2];
6099 i += 3;
6100
6101 if( n < 128 || i + n > ssl->in_hslen )
6102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6105 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006107 }
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006110 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006111 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006113 case 0: /*ok*/
6114 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6115 /* Ignore certificate with an unknown algorithm: maybe a
6116 prior certificate was already trusted. */
6117 break;
6118
6119 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6120 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6121 goto crt_parse_der_failed;
6122
6123 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6124 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6125 goto crt_parse_der_failed;
6126
6127 default:
6128 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6129 crt_parse_der_failed:
6130 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 return( ret );
6133 }
6134
6135 i += n;
6136 }
6137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006140 /*
6141 * On client, make sure the server cert doesn't change during renego to
6142 * avoid "triple handshake" attack: https://secure-resumption.com/
6143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006145 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006147 {
6148 if( ssl->session->peer_cert == NULL )
6149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006151 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6152 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006154 }
6155
6156 if( ssl->session->peer_cert->raw.len !=
6157 ssl->session_negotiate->peer_cert->raw.len ||
6158 memcmp( ssl->session->peer_cert->raw.p,
6159 ssl->session_negotiate->peer_cert->raw.p,
6160 ssl->session->peer_cert->raw.len ) != 0 )
6161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006163 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6164 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006166 }
6167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006169
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006170 return( 0 );
6171}
6172
6173int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6174{
6175 int ret;
6176 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006177 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006178#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6179 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6180 ? ssl->handshake->sni_authmode
6181 : ssl->conf->authmode;
6182#else
6183 const int authmode = ssl->conf->authmode;
6184#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006185 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006186
6187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6188
6189 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6190 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6191 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6192 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6193 {
6194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6195 ssl->state++;
6196 return( 0 );
6197 }
6198
6199#if defined(MBEDTLS_SSL_SRV_C)
6200 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6201 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6202 {
6203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6204 ssl->state++;
6205 return( 0 );
6206 }
6207
6208 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6209 authmode == MBEDTLS_SSL_VERIFY_NONE )
6210 {
6211 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006213
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006214 ssl->state++;
6215 return( 0 );
6216 }
6217#endif
6218
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006219#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6220 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006221 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006222 {
6223 goto crt_verify;
6224 }
6225#endif
6226
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006227 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006228 {
6229 /* mbedtls_ssl_read_record may have sent an alert already. We
6230 let it decide whether to alert. */
6231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6232 return( ret );
6233 }
6234
6235 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6236 {
6237#if defined(MBEDTLS_SSL_SRV_C)
6238 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6239 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6240 {
6241 ret = 0;
6242 }
6243#endif
6244
6245 ssl->state++;
6246 return( ret );
6247 }
6248
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006249#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6250 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006251 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006252
6253crt_verify:
6254 if( ssl->handshake->ecrs_enabled)
6255 rs_ctx = &ssl->handshake->ecrs_ctx;
6256#endif
6257
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006258 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006259 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006260 mbedtls_x509_crt *ca_chain;
6261 mbedtls_x509_crl *ca_crl;
6262
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006263#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006264 if( ssl->handshake->sni_ca_chain != NULL )
6265 {
6266 ca_chain = ssl->handshake->sni_ca_chain;
6267 ca_crl = ssl->handshake->sni_ca_crl;
6268 }
6269 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006270#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006271 {
6272 ca_chain = ssl->conf->ca_chain;
6273 ca_crl = ssl->conf->ca_crl;
6274 }
6275
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006276 /*
6277 * Main check: verify certificate
6278 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006279 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006280 ssl->session_negotiate->peer_cert,
6281 ca_chain, ca_crl,
6282 ssl->conf->cert_profile,
6283 ssl->hostname,
6284 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006285 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006286
6287 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006290 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006291
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006292#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6293 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006294 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006295#endif
6296
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006297 /*
6298 * Secondary checks: always done, but change 'ret' only if it was 0
6299 */
6300
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006301#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006304
6305 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006307 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006308 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006309 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006312 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006314 }
6315 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006316#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006319 ciphersuite_info,
6320 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006321 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006324 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006326 }
6327
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006328 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6329 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6330 * with details encoded in the verification flags. All other kinds
6331 * of error codes, including those from the user provided f_vrfy
6332 * functions, are treated as fatal and lead to a failure of
6333 * ssl_parse_certificate even if verification was optional. */
6334 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6335 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6336 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6337 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006339 }
6340
6341 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6342 {
6343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6344 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6345 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006346
6347 if( ret != 0 )
6348 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006349 uint8_t alert;
6350
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006351 /* The certificate may have been rejected for several reasons.
6352 Pick one and send the corresponding alert. Which alert to send
6353 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006354 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6355 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6356 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6357 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6358 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6359 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6360 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6361 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6362 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6363 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6364 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6365 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6366 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6367 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6368 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6369 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6370 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6371 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6372 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6373 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006374 else
6375 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006376 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6377 alert );
6378 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006379
Hanno Beckere6706e62017-05-15 16:05:15 +01006380#if defined(MBEDTLS_DEBUG_C)
6381 if( ssl->session_negotiate->verify_result != 0 )
6382 {
6383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6384 ssl->session_negotiate->verify_result ) );
6385 }
6386 else
6387 {
6388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6389 }
6390#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 }
6392
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006393 ssl->state++;
6394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006396
6397 return( ret );
6398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6400 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6401 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6402 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6403 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6404 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6405 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006408{
6409 int ret;
6410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006414 ssl->out_msglen = 1;
6415 ssl->out_msg[0] = 1;
6416
Paul Bakker5121ce52009-01-03 21:22:43 +00006417 ssl->state++;
6418
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006419 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006420 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006422 return( ret );
6423 }
6424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006426
6427 return( 0 );
6428}
6429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006431{
6432 int ret;
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006435
Hanno Becker327c93b2018-08-15 13:56:18 +01006436 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439 return( ret );
6440 }
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006445 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6446 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006448 }
6449
Hanno Beckere678eaa2018-08-21 14:57:46 +01006450 /* CCS records are only accepted if they have length 1 and content '1',
6451 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006452
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006453 /*
6454 * Switch to our negotiated transform and session parameters for inbound
6455 * data.
6456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006458 ssl->transform_in = ssl->transform_negotiate;
6459 ssl->session_in = ssl->session_negotiate;
6460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006462 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006465 ssl_dtls_replay_reset( ssl );
6466#endif
6467
6468 /* Increment epoch */
6469 if( ++ssl->in_epoch == 0 )
6470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006472 /* This is highly unlikely to happen for legitimate reasons, so
6473 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006475 }
6476 }
6477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006479 memset( ssl->in_ctr, 0, 8 );
6480
Hanno Beckerf5970a02019-05-08 09:38:41 +01006481 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6484 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006489 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6490 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006492 }
6493 }
6494#endif
6495
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 ssl->state++;
6497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006499
6500 return( 0 );
6501}
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6504 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006505{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006506 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6509 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6510 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006511 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006512 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6515#if defined(MBEDTLS_SHA512_C)
6516 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006517 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6518 else
6519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520#if defined(MBEDTLS_SHA256_C)
6521 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006522 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006523 else
6524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006528 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006529 }
Paul Bakker380da532012-04-18 16:10:25 +00006530}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6535 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006536 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6537 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006538#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6540#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006541 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006544 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006547}
6548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006550 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6553 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006554 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6555 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6558#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006559 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006560#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006562 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006563#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006565}
6566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6568 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6569static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006570 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006571{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006572 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6573 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006574}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006575#endif
Paul Bakker380da532012-04-18 16:10:25 +00006576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6578#if defined(MBEDTLS_SHA256_C)
6579static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006580 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006581{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006582 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006583}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006584#endif
Paul Bakker380da532012-04-18 16:10:25 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586#if defined(MBEDTLS_SHA512_C)
6587static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006588 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006589{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006590 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006591}
Paul Bakker769075d2012-11-24 11:26:46 +01006592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006596static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006598{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006599 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006600 mbedtls_md5_context md5;
6601 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006602
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 unsigned char padbuf[48];
6604 unsigned char md5sum[16];
6605 unsigned char sha1sum[20];
6606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006608 if( !session )
6609 session = ssl->session;
6610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006612
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006613 mbedtls_md5_init( &md5 );
6614 mbedtls_sha1_init( &sha1 );
6615
6616 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6617 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
6619 /*
6620 * SSLv3:
6621 * hash =
6622 * MD5( master + pad2 +
6623 * MD5( handshake + sender + master + pad1 ) )
6624 * + SHA1( master + pad2 +
6625 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006626 */
6627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006629 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6630 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006631#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006634 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6635 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006636#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006639 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006640
Paul Bakker1ef83d62012-04-11 12:09:53 +00006641 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006642
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006643 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6644 mbedtls_md5_update_ret( &md5, session->master, 48 );
6645 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6646 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006647
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006648 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6649 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6650 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6651 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006652
Paul Bakker1ef83d62012-04-11 12:09:53 +00006653 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006654
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006655 mbedtls_md5_starts_ret( &md5 );
6656 mbedtls_md5_update_ret( &md5, session->master, 48 );
6657 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6658 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6659 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006660
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006661 mbedtls_sha1_starts_ret( &sha1 );
6662 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6663 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6664 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6665 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006668
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006669 mbedtls_md5_free( &md5 );
6670 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006672 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6673 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6674 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006681static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006683{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006684 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006685 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006686 mbedtls_md5_context md5;
6687 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006688 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006691 if( !session )
6692 session = ssl->session;
6693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006696 mbedtls_md5_init( &md5 );
6697 mbedtls_sha1_init( &sha1 );
6698
6699 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6700 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006701
Paul Bakker1ef83d62012-04-11 12:09:53 +00006702 /*
6703 * TLSv1:
6704 * hash = PRF( master, finished_label,
6705 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6706 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006709 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6710 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006711#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006714 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6715 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006716#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006719 ? "client finished"
6720 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006721
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006722 mbedtls_md5_finish_ret( &md5, padbuf );
6723 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006724
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006725 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006726 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006729
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006730 mbedtls_md5_free( &md5 );
6731 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006732
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006733 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6740#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006741static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006743{
6744 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006745 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006746 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006747 unsigned char padbuf[32];
6748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006750 if( !session )
6751 session = ssl->session;
6752
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006753 mbedtls_sha256_init( &sha256 );
6754
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006756
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006757 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006758
6759 /*
6760 * TLSv1.2:
6761 * hash = PRF( master, finished_label,
6762 * Hash( handshake ) )[0.11]
6763 */
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765#if !defined(MBEDTLS_SHA256_ALT)
6766 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006767 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006768#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006771 ? "client finished"
6772 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006773
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006774 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006775
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006776 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006777 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006780
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006781 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006782
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006783 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006790static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006792{
6793 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006794 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006795 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006796 unsigned char padbuf[48];
6797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006799 if( !session )
6800 session = ssl->session;
6801
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006802 mbedtls_sha512_init( &sha512 );
6803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006805
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006806 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006807
6808 /*
6809 * TLSv1.2:
6810 * hash = PRF( master, finished_label,
6811 * Hash( handshake ) )[0.11]
6812 */
6813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006815 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6816 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006817#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006820 ? "client finished"
6821 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006822
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006823 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006824
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006825 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006826 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006829
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006830 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006831
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006832 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836#endif /* MBEDTLS_SHA512_C */
6837#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006842
6843 /*
6844 * Free our handshake params
6845 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006846 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006848 ssl->handshake = NULL;
6849
6850 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006851 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006852 */
6853 if( ssl->transform )
6854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 mbedtls_ssl_transform_free( ssl->transform );
6856 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006857 }
6858 ssl->transform = ssl->transform_negotiate;
6859 ssl->transform_negotiate = NULL;
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006862}
6863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006865{
6866 int resume = ssl->handshake->resume;
6867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#if defined(MBEDTLS_SSL_RENEGOTIATION)
6871 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006874 ssl->renego_records_seen = 0;
6875 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006876#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006877
6878 /*
6879 * Free the previous session and switch in the current one
6880 */
Paul Bakker0a597072012-09-25 21:55:46 +00006881 if( ssl->session )
6882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006884 /* RFC 7366 3.1: keep the EtM state */
6885 ssl->session_negotiate->encrypt_then_mac =
6886 ssl->session->encrypt_then_mac;
6887#endif
6888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 mbedtls_ssl_session_free( ssl->session );
6890 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006891 }
6892 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006893 ssl->session_negotiate = NULL;
6894
Paul Bakker0a597072012-09-25 21:55:46 +00006895 /*
6896 * Add cache entry
6897 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006898 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006899 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006900 resume == 0 )
6901 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006902 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006904 }
Paul Bakker0a597072012-09-25 21:55:46 +00006905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006908 ssl->handshake->flight != NULL )
6909 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006910 /* Cancel handshake timer */
6911 ssl_set_timer( ssl, 0 );
6912
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006913 /* Keep last flight around in case we need to resend it:
6914 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006916 }
6917 else
6918#endif
6919 ssl_handshake_wrapup_free_hs_transform( ssl );
6920
Paul Bakker48916f92012-09-16 19:57:18 +00006921 ssl->state++;
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006924}
6925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006927{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006928 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006931
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006932 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006933
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006934 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006935
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006936 /*
6937 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6938 * may define some other value. Currently (early 2016), no defined
6939 * ciphersuite does this (and this is unlikely to change as activity has
6940 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006945 ssl->verify_data_len = hash_len;
6946 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006947#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006948
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6951 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
6953 /*
6954 * In case of session resuming, invert the client and server
6955 * ChangeCipherSpec messages order.
6956 */
Paul Bakker0a597072012-09-25 21:55:46 +00006957 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006960 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006964 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006966#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006967 }
6968 else
6969 ssl->state++;
6970
Paul Bakker48916f92012-09-16 19:57:18 +00006971 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006972 * Switch to our negotiated transform and session parameters for outbound
6973 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006978 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006979 {
6980 unsigned char i;
6981
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006982 /* Remember current epoch settings for resending */
6983 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006984 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006985
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006986 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006987 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006988
6989 /* Increment epoch */
6990 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006991 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006992 break;
6993
6994 /* The loop goes to its end iff the counter is wrapping */
6995 if( i == 0 )
6996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6998 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006999 }
7000 }
7001 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007003 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007004
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007005 ssl->transform_out = ssl->transform_negotiate;
7006 ssl->session_out = ssl->session_negotiate;
7007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7009 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7014 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007015 }
7016 }
7017#endif
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007020 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007022#endif
7023
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007024 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007025 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007027 return( ret );
7028 }
7029
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007030#if defined(MBEDTLS_SSL_PROTO_DTLS)
7031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7032 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7033 {
7034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7035 return( ret );
7036 }
7037#endif
7038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007040
7041 return( 0 );
7042}
7043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007045#define SSL_MAX_HASH_LEN 36
7046#else
7047#define SSL_MAX_HASH_LEN 12
7048#endif
7049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007051{
Paul Bakker23986e52011-04-24 08:57:21 +00007052 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007053 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007054 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007058 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Hanno Becker327c93b2018-08-15 13:56:18 +01007060 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007063 return( ret );
7064 }
7065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007069 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7070 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072 }
7073
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007074 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075#if defined(MBEDTLS_SSL_PROTO_SSL3)
7076 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007077 hash_len = 36;
7078 else
7079#endif
7080 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7083 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007086 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7087 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007089 }
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007092 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007095 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7096 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007098 }
7099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007101 ssl->verify_data_len = hash_len;
7102 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007103#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007104
Paul Bakker0a597072012-09-25 21:55:46 +00007105 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007108 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007112 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007115 }
7116 else
7117 ssl->state++;
7118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007120 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007122#endif
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007125
7126 return( 0 );
7127}
7128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7134 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7135 mbedtls_md5_init( &handshake->fin_md5 );
7136 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007137 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7138 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7141#if defined(MBEDTLS_SHA256_C)
7142 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007143 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#if defined(MBEDTLS_SHA512_C)
7146 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007147 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007150
7151 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007152
7153#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7154 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7155 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7156#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#if defined(MBEDTLS_DHM_C)
7159 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161#if defined(MBEDTLS_ECDH_C)
7162 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007163#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007164#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007165 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007166#if defined(MBEDTLS_SSL_CLI_C)
7167 handshake->ecjpake_cache = NULL;
7168 handshake->ecjpake_cache_len = 0;
7169#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007170#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007171
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007172#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007173 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007174#endif
7175
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007176#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7177 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7178#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007179}
7180
Hanno Becker611a83b2018-01-03 14:27:32 +00007181void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7186 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007187
Hanno Becker92231322018-01-03 15:32:51 +00007188#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 mbedtls_md_init( &transform->md_ctx_enc );
7190 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007191#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007192}
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007197}
7198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007200{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007201 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007202 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007204 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007206 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007207 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007208
7209 /*
7210 * Either the pointers are now NULL or cleared properly and can be freed.
7211 * Now allocate missing structures.
7212 */
7213 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007214 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007215 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007216 }
Paul Bakker48916f92012-09-16 19:57:18 +00007217
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007218 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007219 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007220 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007221 }
Paul Bakker48916f92012-09-16 19:57:18 +00007222
Paul Bakker82788fb2014-10-20 13:59:19 +02007223 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007224 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007225 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007226 }
Paul Bakker48916f92012-09-16 19:57:18 +00007227
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007228 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007229 if( ssl->handshake == NULL ||
7230 ssl->transform_negotiate == NULL ||
7231 ssl->session_negotiate == NULL )
7232 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 mbedtls_free( ssl->handshake );
7236 mbedtls_free( ssl->transform_negotiate );
7237 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007238
7239 ssl->handshake = NULL;
7240 ssl->transform_negotiate = NULL;
7241 ssl->session_negotiate = NULL;
7242
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007243 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007244 }
7245
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007246 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007248 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007249 ssl_handshake_params_init( ssl->handshake );
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7253 {
7254 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007255
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007256 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7257 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7258 else
7259 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007260
7261 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007262 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007263#endif
7264
Paul Bakker48916f92012-09-16 19:57:18 +00007265 return( 0 );
7266}
7267
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007268#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007269/* Dummy cookie callbacks for defaults */
7270static int ssl_cookie_write_dummy( void *ctx,
7271 unsigned char **p, unsigned char *end,
7272 const unsigned char *cli_id, size_t cli_id_len )
7273{
7274 ((void) ctx);
7275 ((void) p);
7276 ((void) end);
7277 ((void) cli_id);
7278 ((void) cli_id_len);
7279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007281}
7282
7283static int ssl_cookie_check_dummy( void *ctx,
7284 const unsigned char *cookie, size_t cookie_len,
7285 const unsigned char *cli_id, size_t cli_id_len )
7286{
7287 ((void) ctx);
7288 ((void) cookie);
7289 ((void) cookie_len);
7290 ((void) cli_id);
7291 ((void) cli_id_len);
7292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007294}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007295#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007296
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007297/* Once ssl->out_hdr as the address of the beginning of the
7298 * next outgoing record is set, deduce the other pointers.
7299 *
7300 * Note: For TLS, we save the implicit record sequence number
7301 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7302 * and the caller has to make sure there's space for this.
7303 */
7304
7305static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7306 mbedtls_ssl_transform *transform )
7307{
7308#if defined(MBEDTLS_SSL_PROTO_DTLS)
7309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7310 {
7311 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Becker70e79282019-05-03 14:34:53 +01007312#if defined(MBEDTLS_SSL_CID)
7313 ssl->out_cid = ssl->out_ctr + 8;
7314 ssl->out_len = ssl->out_cid;
7315 if( transform != NULL )
7316 ssl->out_len += transform->out_cid_len;
7317#else /* MBEDTLS_SSL_CID */
7318 ssl->out_len = ssl->out_ctr + 8;
7319#endif /* MBEDTLS_SSL_CID */
7320 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007321 }
7322 else
7323#endif
7324 {
7325 ssl->out_ctr = ssl->out_hdr - 8;
7326 ssl->out_len = ssl->out_hdr + 3;
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007327#if defined(MBEDTLS_SSL_CID)
7328 ssl->out_cid = ssl->out_len;
7329#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007330 ssl->out_iv = ssl->out_hdr + 5;
7331 }
7332
7333 /* Adjust out_msg to make space for explicit IV, if used. */
7334 if( transform != NULL &&
7335 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7336 {
7337 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7338 }
7339 else
7340 ssl->out_msg = ssl->out_iv;
7341}
7342
7343/* Once ssl->in_hdr as the address of the beginning of the
7344 * next incoming record is set, deduce the other pointers.
7345 *
7346 * Note: For TLS, we save the implicit record sequence number
7347 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7348 * and the caller has to make sure there's space for this.
7349 */
7350
Hanno Beckerf5970a02019-05-08 09:38:41 +01007351static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007352{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007353 /* This function sets the pointers to match the case
7354 * of unprotected TLS/DTLS records, with both ssl->in_iv
7355 * and ssl->in_msg pointing to the beginning of the record
7356 * content.
7357 *
7358 * When decrypting a protected record, ssl->in_msg
7359 * will be shifted to point to the beginning of the
7360 * record plaintext.
7361 */
7362
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007363#if defined(MBEDTLS_SSL_PROTO_DTLS)
7364 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7365 {
Hanno Becker70e79282019-05-03 14:34:53 +01007366 /* This sets the header pointers to match records
7367 * without CID. When we receive a record containing
7368 * a CID, the fields are shifted accordingly in
7369 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007370 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Becker70e79282019-05-03 14:34:53 +01007371#if defined(MBEDTLS_SSL_CID)
7372 ssl->in_cid = ssl->in_ctr + 8;
7373 ssl->in_len = ssl->in_cid; /* Default: no CID */
7374#else /* MBEDTLS_SSL_CID */
7375 ssl->in_len = ssl->in_ctr + 8;
7376#endif /* MBEDTLS_SSL_CID */
7377 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007378 }
7379 else
7380#endif
7381 {
7382 ssl->in_ctr = ssl->in_hdr - 8;
7383 ssl->in_len = ssl->in_hdr + 3;
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007384#if defined(MBEDTLS_SSL_CID)
7385 ssl->in_cid = ssl->in_len;
7386#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007387 ssl->in_iv = ssl->in_hdr + 5;
7388 }
7389
Hanno Beckerf5970a02019-05-08 09:38:41 +01007390 /* This will be adjusted at record decryption time. */
7391 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007392}
7393
Paul Bakker5121ce52009-01-03 21:22:43 +00007394/*
7395 * Initialize an SSL context
7396 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007397void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7398{
7399 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7400}
7401
7402/*
7403 * Setup an SSL context
7404 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007405
7406static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7407{
7408 /* Set the incoming and outgoing record pointers. */
7409#if defined(MBEDTLS_SSL_PROTO_DTLS)
7410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7411 {
7412 ssl->out_hdr = ssl->out_buf;
7413 ssl->in_hdr = ssl->in_buf;
7414 }
7415 else
7416#endif /* MBEDTLS_SSL_PROTO_DTLS */
7417 {
7418 ssl->out_hdr = ssl->out_buf + 8;
7419 ssl->in_hdr = ssl->in_buf + 8;
7420 }
7421
7422 /* Derive other internal pointers. */
7423 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007424 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007425}
7426
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007427int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007428 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007429{
Paul Bakker48916f92012-09-16 19:57:18 +00007430 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007431
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007432 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007433
7434 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007435 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007436 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007437
7438 /* Set to NULL in case of an error condition */
7439 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007440
Angus Grattond8213d02016-05-25 20:56:48 +10007441 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7442 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007443 {
Angus Grattond8213d02016-05-25 20:56:48 +10007444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007445 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007446 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007447 }
7448
7449 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7450 if( ssl->out_buf == NULL )
7451 {
7452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007453 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007454 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007455 }
7456
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007457 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007458
Paul Bakker48916f92012-09-16 19:57:18 +00007459 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007460 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007461
7462 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007463
7464error:
7465 mbedtls_free( ssl->in_buf );
7466 mbedtls_free( ssl->out_buf );
7467
7468 ssl->conf = NULL;
7469
7470 ssl->in_buf = NULL;
7471 ssl->out_buf = NULL;
7472
7473 ssl->in_hdr = NULL;
7474 ssl->in_ctr = NULL;
7475 ssl->in_len = NULL;
7476 ssl->in_iv = NULL;
7477 ssl->in_msg = NULL;
7478
7479 ssl->out_hdr = NULL;
7480 ssl->out_ctr = NULL;
7481 ssl->out_len = NULL;
7482 ssl->out_iv = NULL;
7483 ssl->out_msg = NULL;
7484
k-stachowiak9f7798e2018-07-31 16:52:32 +02007485 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007486}
7487
7488/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007489 * Reset an initialized and used SSL context for re-use while retaining
7490 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007491 *
7492 * If partial is non-zero, keep data in the input buffer and client ID.
7493 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007494 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007495static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007496{
Paul Bakker48916f92012-09-16 19:57:18 +00007497 int ret;
7498
Hanno Becker7e772132018-08-10 12:38:21 +01007499#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7500 !defined(MBEDTLS_SSL_SRV_C)
7501 ((void) partial);
7502#endif
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007505
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007506 /* Cancel any possibly running timer */
7507 ssl_set_timer( ssl, 0 );
7508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509#if defined(MBEDTLS_SSL_RENEGOTIATION)
7510 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007511 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007512
7513 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7515 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007518
Paul Bakker7eb013f2011-10-06 12:37:39 +00007519 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007520 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007521
7522 ssl->in_msgtype = 0;
7523 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007525 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007526 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007529 ssl_dtls_replay_reset( ssl );
7530#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007531
7532 ssl->in_hslen = 0;
7533 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007534
7535 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007536
7537 ssl->out_msgtype = 0;
7538 ssl->out_msglen = 0;
7539 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7541 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007542 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007543#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007544
Hanno Becker19859472018-08-06 09:40:20 +01007545 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7546
Paul Bakker48916f92012-09-16 19:57:18 +00007547 ssl->transform_in = NULL;
7548 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007549
Hanno Becker78640902018-08-13 16:35:15 +01007550 ssl->session_in = NULL;
7551 ssl->session_out = NULL;
7552
Angus Grattond8213d02016-05-25 20:56:48 +10007553 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007554
7555#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007556 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007557#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7558 {
7559 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007560 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007561 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7564 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7567 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7570 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007571 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007572 }
7573#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007574
Paul Bakker48916f92012-09-16 19:57:18 +00007575 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 mbedtls_ssl_transform_free( ssl->transform );
7578 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007579 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007580 }
Paul Bakker48916f92012-09-16 19:57:18 +00007581
Paul Bakkerc0463502013-02-14 11:19:38 +01007582 if( ssl->session )
7583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 mbedtls_ssl_session_free( ssl->session );
7585 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007586 ssl->session = NULL;
7587 }
7588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007590 ssl->alpn_chosen = NULL;
7591#endif
7592
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007593#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007594#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007595 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007596#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007597 {
7598 mbedtls_free( ssl->cli_id );
7599 ssl->cli_id = NULL;
7600 ssl->cli_id_len = 0;
7601 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007602#endif
7603
Paul Bakker48916f92012-09-16 19:57:18 +00007604 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7605 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007606
7607 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007608}
7609
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007610/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007611 * Reset an initialized and used SSL context for re-use while retaining
7612 * all application-set variables, function pointers and data.
7613 */
7614int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7615{
7616 return( ssl_session_reset_int( ssl, 0 ) );
7617}
7618
7619/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007620 * SSL set accessors
7621 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007622void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007624 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007625}
7626
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007627void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007628{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007629 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007630}
7631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007633void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007635 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007636}
7637#endif
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007640void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007641{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007642 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007643}
7644#endif
7645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007647
Hanno Becker1841b0a2018-08-24 11:13:57 +01007648void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7649 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007650{
7651 ssl->disable_datagram_packing = !allow_packing;
7652}
7653
7654void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7655 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007657 conf->hs_timeout_min = min;
7658 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007659}
7660#endif
7661
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007662void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007664 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007665}
7666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007668void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007669 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007670 void *p_vrfy )
7671{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007672 conf->f_vrfy = f_vrfy;
7673 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007674}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007676
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007677void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007678 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007679 void *p_rng )
7680{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007681 conf->f_rng = f_rng;
7682 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007683}
7684
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007685void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007686 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007687 void *p_dbg )
7688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007689 conf->f_dbg = f_dbg;
7690 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007691}
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007694 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007695 mbedtls_ssl_send_t *f_send,
7696 mbedtls_ssl_recv_t *f_recv,
7697 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007698{
7699 ssl->p_bio = p_bio;
7700 ssl->f_send = f_send;
7701 ssl->f_recv = f_recv;
7702 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007703}
7704
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007705#if defined(MBEDTLS_SSL_PROTO_DTLS)
7706void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7707{
7708 ssl->mtu = mtu;
7709}
7710#endif
7711
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007712void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007713{
7714 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007715}
7716
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007717void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7718 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007719 mbedtls_ssl_set_timer_t *f_set_timer,
7720 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007721{
7722 ssl->p_timer = p_timer;
7723 ssl->f_set_timer = f_set_timer;
7724 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007725
7726 /* Make sure we start with no timer running */
7727 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007728}
7729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007731void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007732 void *p_cache,
7733 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7734 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007735{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007736 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007737 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007738 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_SSL_CLI_C)
7743int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007744{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007745 int ret;
7746
7747 if( ssl == NULL ||
7748 session == NULL ||
7749 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007750 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007753 }
7754
7755 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7756 return( ret );
7757
Paul Bakker0a597072012-09-25 21:55:46 +00007758 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007759
7760 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007763
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007764void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007765 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007767 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7768 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7769 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7770 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007771}
7772
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007773void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007774 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007775 int major, int minor )
7776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007778 return;
7779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007781 return;
7782
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007783 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007784}
7785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007787void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007788 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007789{
7790 conf->cert_profile = profile;
7791}
7792
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007793/* Append a new keycert entry to a (possibly empty) list */
7794static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7795 mbedtls_x509_crt *cert,
7796 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007797{
niisato8ee24222018-06-25 19:05:48 +09007798 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007799
niisato8ee24222018-06-25 19:05:48 +09007800 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7801 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007802 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007803
niisato8ee24222018-06-25 19:05:48 +09007804 new_cert->cert = cert;
7805 new_cert->key = key;
7806 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007807
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007808 /* Update head is the list was null, else add to the end */
7809 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007810 {
niisato8ee24222018-06-25 19:05:48 +09007811 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007812 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007813 else
7814 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007815 mbedtls_ssl_key_cert *cur = *head;
7816 while( cur->next != NULL )
7817 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007818 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007819 }
7820
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007821 return( 0 );
7822}
7823
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007824int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007825 mbedtls_x509_crt *own_cert,
7826 mbedtls_pk_context *pk_key )
7827{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007828 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007829}
7830
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007831void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007832 mbedtls_x509_crt *ca_chain,
7833 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007834{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007835 conf->ca_chain = ca_chain;
7836 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007839
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007840#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7841int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7842 mbedtls_x509_crt *own_cert,
7843 mbedtls_pk_context *pk_key )
7844{
7845 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7846 own_cert, pk_key ) );
7847}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007848
7849void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7850 mbedtls_x509_crt *ca_chain,
7851 mbedtls_x509_crl *ca_crl )
7852{
7853 ssl->handshake->sni_ca_chain = ca_chain;
7854 ssl->handshake->sni_ca_crl = ca_crl;
7855}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007856
7857void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7858 int authmode )
7859{
7860 ssl->handshake->sni_authmode = authmode;
7861}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007862#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7863
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007864#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007865/*
7866 * Set EC J-PAKE password for current handshake
7867 */
7868int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7869 const unsigned char *pw,
7870 size_t pw_len )
7871{
7872 mbedtls_ecjpake_role role;
7873
Janos Follath8eb64132016-06-03 15:40:57 +01007874 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7876
7877 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7878 role = MBEDTLS_ECJPAKE_SERVER;
7879 else
7880 role = MBEDTLS_ECJPAKE_CLIENT;
7881
7882 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7883 role,
7884 MBEDTLS_MD_SHA256,
7885 MBEDTLS_ECP_DP_SECP256R1,
7886 pw, pw_len ) );
7887}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007888#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007891int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007892 const unsigned char *psk, size_t psk_len,
7893 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007894{
Paul Bakker6db455e2013-09-18 17:29:31 +02007895 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007900
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007901 /* Identity len will be encoded on two bytes */
7902 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007903 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007904 {
7905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7906 }
7907
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007908 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007909 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007910 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007911
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007912 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007913 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007914 conf->psk_len = 0;
7915 }
7916 if( conf->psk_identity != NULL )
7917 {
7918 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007919 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007920 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007921 }
7922
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007923 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7924 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007925 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007926 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007927 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007928 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007929 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007930 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007931 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007932
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007933 conf->psk_len = psk_len;
7934 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007935
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007936 memcpy( conf->psk, psk, conf->psk_len );
7937 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007938
7939 return( 0 );
7940}
7941
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007942int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7943 const unsigned char *psk, size_t psk_len )
7944{
7945 if( psk == NULL || ssl->handshake == NULL )
7946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7947
7948 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7950
7951 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007952 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007953 mbedtls_platform_zeroize( ssl->handshake->psk,
7954 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007955 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007956 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007957 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007958
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007959 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007960 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007961
7962 ssl->handshake->psk_len = psk_len;
7963 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7964
7965 return( 0 );
7966}
7967
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007968void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007970 size_t),
7971 void *p_psk )
7972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007973 conf->f_psk = f_psk;
7974 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007977
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007978#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007979
7980#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007981int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007982{
7983 int ret;
7984
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007985 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7986 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7987 {
7988 mbedtls_mpi_free( &conf->dhm_P );
7989 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007990 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007991 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007992
7993 return( 0 );
7994}
Hanno Becker470a8c42017-10-04 15:28:46 +01007995#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007996
Hanno Beckera90658f2017-10-04 15:29:08 +01007997int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7998 const unsigned char *dhm_P, size_t P_len,
7999 const unsigned char *dhm_G, size_t G_len )
8000{
8001 int ret;
8002
8003 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8004 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8005 {
8006 mbedtls_mpi_free( &conf->dhm_P );
8007 mbedtls_mpi_free( &conf->dhm_G );
8008 return( ret );
8009 }
8010
8011 return( 0 );
8012}
Paul Bakker5121ce52009-01-03 21:22:43 +00008013
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008014int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008015{
8016 int ret;
8017
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008018 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8019 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8020 {
8021 mbedtls_mpi_free( &conf->dhm_P );
8022 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008023 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008024 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008025
8026 return( 0 );
8027}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008028#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008029
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008030#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8031/*
8032 * Set the minimum length for Diffie-Hellman parameters
8033 */
8034void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8035 unsigned int bitlen )
8036{
8037 conf->dhm_min_bitlen = bitlen;
8038}
8039#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8040
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008041#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008042/*
8043 * Set allowed/preferred hashes for handshake signatures
8044 */
8045void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8046 const int *hashes )
8047{
8048 conf->sig_hashes = hashes;
8049}
Hanno Becker947194e2017-04-07 13:25:49 +01008050#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008051
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008052#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008053/*
8054 * Set the allowed elliptic curves
8055 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008056void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008057 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008059 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008060}
Hanno Becker947194e2017-04-07 13:25:49 +01008061#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008062
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008063#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008065{
Hanno Becker947194e2017-04-07 13:25:49 +01008066 /* Initialize to suppress unnecessary compiler warning */
8067 size_t hostname_len = 0;
8068
8069 /* Check if new hostname is valid before
8070 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008071 if( hostname != NULL )
8072 {
8073 hostname_len = strlen( hostname );
8074
8075 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8077 }
8078
8079 /* Now it's clear that we will overwrite the old hostname,
8080 * so we can free it safely */
8081
8082 if( ssl->hostname != NULL )
8083 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008084 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008085 mbedtls_free( ssl->hostname );
8086 }
8087
8088 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008089
Paul Bakker5121ce52009-01-03 21:22:43 +00008090 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008091 {
8092 ssl->hostname = NULL;
8093 }
8094 else
8095 {
8096 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008097 if( ssl->hostname == NULL )
8098 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008099
Hanno Becker947194e2017-04-07 13:25:49 +01008100 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008101
Hanno Becker947194e2017-04-07 13:25:49 +01008102 ssl->hostname[hostname_len] = '\0';
8103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008104
8105 return( 0 );
8106}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008107#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008108
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008109#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008110void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008112 const unsigned char *, size_t),
8113 void *p_sni )
8114{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008115 conf->f_sni = f_sni;
8116 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008121int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008122{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008123 size_t cur_len, tot_len;
8124 const char **p;
8125
8126 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008127 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8128 * MUST NOT be truncated."
8129 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008130 */
8131 tot_len = 0;
8132 for( p = protos; *p != NULL; p++ )
8133 {
8134 cur_len = strlen( *p );
8135 tot_len += cur_len;
8136
8137 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008139 }
8140
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008141 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008142
8143 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008144}
8145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008147{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008148 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008151
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008152void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008153{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008154 conf->max_major_ver = major;
8155 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008156}
8157
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008158void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008159{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008160 conf->min_major_ver = major;
8161 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008162}
8163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008165void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008166{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008167 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008168}
8169#endif
8170
Janos Follath088ce432017-04-10 12:42:31 +01008171#if defined(MBEDTLS_SSL_SRV_C)
8172void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8173 char cert_req_ca_list )
8174{
8175 conf->cert_req_ca_list = cert_req_ca_list;
8176}
8177#endif
8178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008180void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008182 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008183}
8184#endif
8185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008187void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008188{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008189 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008190}
8191#endif
8192
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008193#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008194void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008195{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008196 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008197}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008198#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008201int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008204 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008207 }
8208
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008209 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008210
8211 return( 0 );
8212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008216void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008217{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008218 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008223void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008224{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008225 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008226}
8227#endif
8228
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008229void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008230{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008231 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008232}
8233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008235void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008236{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008237 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008238}
8239
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008240void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008241{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008242 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008243}
8244
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008245void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008246 const unsigned char period[8] )
8247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008248 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008253#if defined(MBEDTLS_SSL_CLI_C)
8254void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008255{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008256 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008257}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008258#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008259
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008260#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008261void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8262 mbedtls_ssl_ticket_write_t *f_ticket_write,
8263 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8264 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008265{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008266 conf->f_ticket_write = f_ticket_write;
8267 conf->f_ticket_parse = f_ticket_parse;
8268 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008269}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008272
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008273#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8274void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8275 mbedtls_ssl_export_keys_t *f_export_keys,
8276 void *p_export_keys )
8277{
8278 conf->f_export_keys = f_export_keys;
8279 conf->p_export_keys = p_export_keys;
8280}
8281#endif
8282
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008283#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008284void mbedtls_ssl_conf_async_private_cb(
8285 mbedtls_ssl_config *conf,
8286 mbedtls_ssl_async_sign_t *f_async_sign,
8287 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8288 mbedtls_ssl_async_resume_t *f_async_resume,
8289 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008290 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008291{
8292 conf->f_async_sign_start = f_async_sign;
8293 conf->f_async_decrypt_start = f_async_decrypt;
8294 conf->f_async_resume = f_async_resume;
8295 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008296 conf->p_async_config_data = async_config_data;
8297}
8298
Gilles Peskine8f97af72018-04-26 11:46:10 +02008299void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8300{
8301 return( conf->p_async_config_data );
8302}
8303
Gilles Peskine1febfef2018-04-30 11:54:39 +02008304void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008305{
8306 if( ssl->handshake == NULL )
8307 return( NULL );
8308 else
8309 return( ssl->handshake->user_async_ctx );
8310}
8311
Gilles Peskine1febfef2018-04-30 11:54:39 +02008312void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008313 void *ctx )
8314{
8315 if( ssl->handshake != NULL )
8316 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008317}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008318#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008319
Paul Bakker5121ce52009-01-03 21:22:43 +00008320/*
8321 * SSL get accessors
8322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008324{
8325 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8326}
8327
Hanno Becker8b170a02017-10-10 11:51:19 +01008328int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8329{
8330 /*
8331 * Case A: We're currently holding back
8332 * a message for further processing.
8333 */
8334
8335 if( ssl->keep_current_message == 1 )
8336 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008338 return( 1 );
8339 }
8340
8341 /*
8342 * Case B: Further records are pending in the current datagram.
8343 */
8344
8345#if defined(MBEDTLS_SSL_PROTO_DTLS)
8346 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8347 ssl->in_left > ssl->next_record_offset )
8348 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008350 return( 1 );
8351 }
8352#endif /* MBEDTLS_SSL_PROTO_DTLS */
8353
8354 /*
8355 * Case C: A handshake message is being processed.
8356 */
8357
Hanno Becker8b170a02017-10-10 11:51:19 +01008358 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8359 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008361 return( 1 );
8362 }
8363
8364 /*
8365 * Case D: An application data message is being processed
8366 */
8367 if( ssl->in_offt != NULL )
8368 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008370 return( 1 );
8371 }
8372
8373 /*
8374 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008375 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008376 * we implement support for multiple alerts in single records.
8377 */
8378
8379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8380 return( 0 );
8381}
8382
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008383uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008384{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008385 if( ssl->session != NULL )
8386 return( ssl->session->verify_result );
8387
8388 if( ssl->session_negotiate != NULL )
8389 return( ssl->session_negotiate->verify_result );
8390
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008391 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008392}
8393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008395{
Paul Bakker926c8e42013-03-06 10:23:34 +01008396 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008397 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008400}
8401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008403{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008405 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008406 {
8407 switch( ssl->minor_ver )
8408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008410 return( "DTLSv1.0" );
8411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008413 return( "DTLSv1.2" );
8414
8415 default:
8416 return( "unknown (DTLS)" );
8417 }
8418 }
8419#endif
8420
Paul Bakker43ca69c2011-01-15 17:35:19 +00008421 switch( ssl->minor_ver )
8422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008424 return( "SSLv3.0" );
8425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008427 return( "TLSv1.0" );
8428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008430 return( "TLSv1.1" );
8431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008433 return( "TLSv1.2" );
8434
Paul Bakker43ca69c2011-01-15 17:35:19 +00008435 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008436 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008437 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008438}
8439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008441{
Hanno Becker3136ede2018-08-17 15:28:19 +01008442 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008444 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008445
Hanno Becker43395762019-05-03 14:46:38 +01008446 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8447
Hanno Becker78640902018-08-13 16:35:15 +01008448 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008449 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451#if defined(MBEDTLS_ZLIB_SUPPORT)
8452 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8453 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008454#endif
8455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 case MBEDTLS_MODE_GCM:
8459 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008460 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008462 transform_expansion = transform->minlen;
8463 break;
8464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008466
8467 block_size = mbedtls_cipher_get_block_size(
8468 &transform->cipher_ctx_enc );
8469
Hanno Becker3136ede2018-08-17 15:28:19 +01008470 /* Expansion due to the addition of the MAC. */
8471 transform_expansion += transform->maclen;
8472
8473 /* Expansion due to the addition of CBC padding;
8474 * Theoretically up to 256 bytes, but we never use
8475 * more than the block size of the underlying cipher. */
8476 transform_expansion += block_size;
8477
8478 /* For TLS 1.1 or higher, an explicit IV is added
8479 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008480#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8481 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008482 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008483#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008484
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008485 break;
8486
8487 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008490 }
8491
Hanno Beckeradd01902019-05-08 15:40:11 +01008492#if defined(MBEDTLS_SSL_CID)
8493 if( transform->out_cid_len != 0 )
8494 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
8495#endif /* MBEDTLS_SSL_CID */
8496
Hanno Becker43395762019-05-03 14:46:38 +01008497 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008498}
8499
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008500#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8501size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8502{
8503 size_t max_len;
8504
8505 /*
8506 * Assume mfl_code is correct since it was checked when set
8507 */
Angus Grattond8213d02016-05-25 20:56:48 +10008508 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008509
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008510 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008511 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008512 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008513 {
Angus Grattond8213d02016-05-25 20:56:48 +10008514 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008515 }
8516
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008517 /* During a handshake, use the value being negotiated */
8518 if( ssl->session_negotiate != NULL &&
8519 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8520 {
8521 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8522 }
8523
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008524 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008525}
8526#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8527
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008528#if defined(MBEDTLS_SSL_PROTO_DTLS)
8529static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8530{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008531 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8532 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8533 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8534 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8535 return ( 0 );
8536
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008537 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8538 return( ssl->mtu );
8539
8540 if( ssl->mtu == 0 )
8541 return( ssl->handshake->mtu );
8542
8543 return( ssl->mtu < ssl->handshake->mtu ?
8544 ssl->mtu : ssl->handshake->mtu );
8545}
8546#endif /* MBEDTLS_SSL_PROTO_DTLS */
8547
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008548int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8549{
8550 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8551
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008552#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8553 !defined(MBEDTLS_SSL_PROTO_DTLS)
8554 (void) ssl;
8555#endif
8556
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008557#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8558 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8559
8560 if( max_len > mfl )
8561 max_len = mfl;
8562#endif
8563
8564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008565 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008566 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008567 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008568 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8569 const size_t overhead = (size_t) ret;
8570
8571 if( ret < 0 )
8572 return( ret );
8573
8574 if( mtu <= overhead )
8575 {
8576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8577 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8578 }
8579
8580 if( max_len > mtu - overhead )
8581 max_len = mtu - overhead;
8582 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008583#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008584
Hanno Becker0defedb2018-08-10 12:35:02 +01008585#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8586 !defined(MBEDTLS_SSL_PROTO_DTLS)
8587 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008588#endif
8589
8590 return( (int) max_len );
8591}
8592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#if defined(MBEDTLS_X509_CRT_PARSE_C)
8594const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008595{
8596 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008597 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008598
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008599 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008600}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603#if defined(MBEDTLS_SSL_CLI_C)
8604int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008605{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008606 if( ssl == NULL ||
8607 dst == NULL ||
8608 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008609 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008612 }
8613
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008614 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008617
Paul Bakker5121ce52009-01-03 21:22:43 +00008618/*
Paul Bakker1961b702013-01-25 14:49:24 +01008619 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008624
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008625 if( ssl == NULL || ssl->conf == NULL )
8626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008629 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008631#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008633 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008635#endif
8636
Paul Bakker1961b702013-01-25 14:49:24 +01008637 return( ret );
8638}
8639
8640/*
8641 * Perform the SSL handshake
8642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008644{
8645 int ret = 0;
8646
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008647 if( ssl == NULL || ssl->conf == NULL )
8648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008655
8656 if( ret != 0 )
8657 break;
8658 }
8659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008661
8662 return( ret );
8663}
8664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665#if defined(MBEDTLS_SSL_RENEGOTIATION)
8666#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008667/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008668 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008671{
8672 int ret;
8673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008675
8676 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8678 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008679
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008680 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008681 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008683 return( ret );
8684 }
8685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008687
8688 return( 0 );
8689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008691
8692/*
8693 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694 * - any side: calling mbedtls_ssl_renegotiate(),
8695 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8696 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008697 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008698 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008702{
8703 int ret;
8704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008706
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008707 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8708 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008709
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008710 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8711 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008713 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008715 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008717 ssl->handshake->out_msg_seq = 1;
8718 else
8719 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008720 }
8721#endif
8722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8724 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008729 return( ret );
8730 }
8731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008733
8734 return( 0 );
8735}
8736
8737/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008738 * Renegotiate current connection on client,
8739 * or request renegotiation on server
8740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008744
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008745 if( ssl == NULL || ssl->conf == NULL )
8746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008749 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008750 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008756
8757 /* Did we already try/start sending HelloRequest? */
8758 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008760
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008761 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008762 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008766 /*
8767 * On client, either start the renegotiation process or,
8768 * if already in progress, continue the handshake
8769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008774
8775 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008778 return( ret );
8779 }
8780 }
8781 else
8782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008786 return( ret );
8787 }
8788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008790
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008791 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008792}
8793
8794/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008795 * Check record counters and renegotiate if they're above the limit.
8796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008798{
Andres AG2196c7f2016-12-15 17:01:16 +00008799 size_t ep_len = ssl_ep_len( ssl );
8800 int in_ctr_cmp;
8801 int out_ctr_cmp;
8802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8804 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008805 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008806 {
8807 return( 0 );
8808 }
8809
Andres AG2196c7f2016-12-15 17:01:16 +00008810 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8811 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008812 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008813 ssl->conf->renego_period + ep_len, 8 - ep_len );
8814
8815 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008816 {
8817 return( 0 );
8818 }
8819
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008824
8825/*
8826 * Receive application data decrypted from the SSL layer
8827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008829{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008830 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008831 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008832
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008833 if( ssl == NULL || ssl->conf == NULL )
8834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008839 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008842 return( ret );
8843
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008844 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008846 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008847 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008848 return( ret );
8849 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008850 }
8851#endif
8852
Hanno Becker4a810fb2017-05-24 16:27:30 +01008853 /*
8854 * Check if renegotiation is necessary and/or handshake is
8855 * in process. If yes, perform/continue, and fall through
8856 * if an unexpected packet is received while the client
8857 * is waiting for the ServerHello.
8858 *
8859 * (There is no equivalent to the last condition on
8860 * the server-side as it is not treated as within
8861 * a handshake while waiting for the ClientHello
8862 * after a renegotiation request.)
8863 */
8864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008866 ret = ssl_check_ctr_renegotiate( ssl );
8867 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8868 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008871 return( ret );
8872 }
8873#endif
8874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008878 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8879 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008882 return( ret );
8883 }
8884 }
8885
Hanno Beckere41158b2017-10-23 13:30:32 +01008886 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008887 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008888 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008889 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008890 if( ssl->f_get_timer != NULL &&
8891 ssl->f_get_timer( ssl->p_timer ) == -1 )
8892 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008893 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008894 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008895
Hanno Becker327c93b2018-08-15 13:56:18 +01008896 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008897 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008898 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8899 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008900
Hanno Becker4a810fb2017-05-24 16:27:30 +01008901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8902 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008903 }
8904
8905 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008907 {
8908 /*
8909 * OpenSSL sends empty messages to randomize the IV
8910 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008911 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008914 return( 0 );
8915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008917 return( ret );
8918 }
8919 }
8920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008924
Hanno Becker4a810fb2017-05-24 16:27:30 +01008925 /*
8926 * - For client-side, expect SERVER_HELLO_REQUEST.
8927 * - For server-side, expect CLIENT_HELLO.
8928 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8929 */
8930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008932 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008934 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008937
8938 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008940 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008941 {
8942 continue;
8943 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008946 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008947#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008948
Hanno Becker4a810fb2017-05-24 16:27:30 +01008949#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008950 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008954
8955 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008958 {
8959 continue;
8960 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008963 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008964#endif /* MBEDTLS_SSL_SRV_C */
8965
Hanno Becker21df7f92017-10-17 11:03:26 +01008966#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008967 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008968 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8969 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8970 ssl->conf->allow_legacy_renegotiation ==
8971 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8972 {
8973 /*
8974 * Accept renegotiation request
8975 */
Paul Bakker48916f92012-09-16 19:57:18 +00008976
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008977 /* DTLS clients need to know renego is server-initiated */
8978#if defined(MBEDTLS_SSL_PROTO_DTLS)
8979 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8980 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8981 {
8982 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8983 }
8984#endif
8985 ret = ssl_start_renegotiation( ssl );
8986 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8987 ret != 0 )
8988 {
8989 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8990 return( ret );
8991 }
8992 }
8993 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008994#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008995 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008996 /*
8997 * Refuse renegotiation
8998 */
8999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002#if defined(MBEDTLS_SSL_PROTO_SSL3)
9003 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009004 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009005 /* SSLv3 does not have a "no_renegotiation" warning, so
9006 we send a fatal alert and abort the connection. */
9007 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9008 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9009 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009010 }
9011 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9013#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9014 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9015 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9018 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9019 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009020 {
9021 return( ret );
9022 }
Paul Bakker48916f92012-09-16 19:57:18 +00009023 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009024 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9026 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009030 }
Paul Bakker48916f92012-09-16 19:57:18 +00009031 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009032
Hanno Becker90333da2017-10-10 11:27:13 +01009033 /* At this point, we don't know whether the renegotiation has been
9034 * completed or not. The cases to consider are the following:
9035 * 1) The renegotiation is complete. In this case, no new record
9036 * has been read yet.
9037 * 2) The renegotiation is incomplete because the client received
9038 * an application data record while awaiting the ServerHello.
9039 * 3) The renegotiation is incomplete because the client received
9040 * a non-handshake, non-application data message while awaiting
9041 * the ServerHello.
9042 * In each of these case, looping will be the proper action:
9043 * - For 1), the next iteration will read a new record and check
9044 * if it's application data.
9045 * - For 2), the loop condition isn't satisfied as application data
9046 * is present, hence continue is the same as break
9047 * - For 3), the loop condition is satisfied and read_record
9048 * will re-deliver the message that was held back by the client
9049 * when expecting the ServerHello.
9050 */
9051 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009052 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009053#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009055 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009056 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009057 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009058 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009061 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009063 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009064 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009065 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9069 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009072 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009073 }
9074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9078 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009079 }
9080
9081 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009082
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009083 /* We're going to return something now, cancel timer,
9084 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009086 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009087
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009089 /* If we requested renego but received AppData, resend HelloRequest.
9090 * Do it now, after setting in_offt, to avoid taking this branch
9091 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009093 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009095 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009096 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009099 return( ret );
9100 }
9101 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009103#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009104 }
9105
9106 n = ( len < ssl->in_msglen )
9107 ? len : ssl->in_msglen;
9108
9109 memcpy( buf, ssl->in_offt, n );
9110 ssl->in_msglen -= n;
9111
9112 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009113 {
9114 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009115 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009116 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009118 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009119 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009120 /* more data available */
9121 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009125
Paul Bakker23986e52011-04-24 08:57:21 +00009126 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009127}
9128
9129/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009130 * Send application data to be encrypted by the SSL layer, taking care of max
9131 * fragment length and buffer size.
9132 *
9133 * According to RFC 5246 Section 6.2.1:
9134 *
9135 * Zero-length fragments of Application data MAY be sent as they are
9136 * potentially useful as a traffic analysis countermeasure.
9137 *
9138 * Therefore, it is possible that the input message length is 0 and the
9139 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009140 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009141static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009142 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009143{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009144 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9145 const size_t max_len = (size_t) ret;
9146
9147 if( ret < 0 )
9148 {
9149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9150 return( ret );
9151 }
9152
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009153 if( len > max_len )
9154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009156 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009159 "maximum fragment length: %d > %d",
9160 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009162 }
9163 else
9164#endif
9165 len = max_len;
9166 }
Paul Bakker887bd502011-06-08 13:10:54 +00009167
Paul Bakker5121ce52009-01-03 21:22:43 +00009168 if( ssl->out_left != 0 )
9169 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009170 /*
9171 * The user has previously tried to send the data and
9172 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9173 * written. In this case, we expect the high-level write function
9174 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009179 return( ret );
9180 }
9181 }
Paul Bakker887bd502011-06-08 13:10:54 +00009182 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009183 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009184 /*
9185 * The user is trying to send a message the first time, so we need to
9186 * copy the data into the internal buffers and setup the data structure
9187 * to keep track of partial writes
9188 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009189 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009191 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009192
Hanno Becker67bc7c32018-08-06 11:33:50 +01009193 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009196 return( ret );
9197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009198 }
9199
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009200 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009201}
9202
9203/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009204 * Write application data, doing 1/n-1 splitting if necessary.
9205 *
9206 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009207 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009208 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009211static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009212 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009213{
9214 int ret;
9215
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009216 if( ssl->conf->cbc_record_splitting ==
9217 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009218 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9220 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9221 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009222 {
9223 return( ssl_write_real( ssl, buf, len ) );
9224 }
9225
9226 if( ssl->split_done == 0 )
9227 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009228 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009229 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009230 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009231 }
9232
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009233 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9234 return( ret );
9235 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009236
9237 return( ret + 1 );
9238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009240
9241/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009242 * Write application data (public-facing wrapper)
9243 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009244int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009245{
9246 int ret;
9247
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009249
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009250 if( ssl == NULL || ssl->conf == NULL )
9251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9252
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009253#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009254 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9255 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009256 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009257 return( ret );
9258 }
9259#endif
9260
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009261 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009262 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009263 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009264 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009266 return( ret );
9267 }
9268 }
9269
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009270#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009271 ret = ssl_write_split( ssl, buf, len );
9272#else
9273 ret = ssl_write_real( ssl, buf, len );
9274#endif
9275
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009277
9278 return( ret );
9279}
9280
9281/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009282 * Notify the peer that the connection is being closed
9283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009285{
9286 int ret;
9287
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009288 if( ssl == NULL || ssl->conf == NULL )
9289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009292
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009293 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9299 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9300 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009303 return( ret );
9304 }
9305 }
9306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009308
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009309 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009310}
9311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009313{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009314 if( transform == NULL )
9315 return;
9316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009318 deflateEnd( &transform->ctx_deflate );
9319 inflateEnd( &transform->ctx_inflate );
9320#endif
9321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9323 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009324
Hanno Becker92231322018-01-03 15:32:51 +00009325#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 mbedtls_md_free( &transform->md_ctx_enc );
9327 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009328#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009329
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009330 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009331}
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_X509_CRT_PARSE_C)
9334static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009337
9338 while( cur != NULL )
9339 {
9340 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009342 cur = next;
9343 }
9344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009346
Hanno Becker0271f962018-08-16 13:23:47 +01009347#if defined(MBEDTLS_SSL_PROTO_DTLS)
9348
9349static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9350{
9351 unsigned offset;
9352 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9353
9354 if( hs == NULL )
9355 return;
9356
Hanno Becker283f5ef2018-08-24 09:34:47 +01009357 ssl_free_buffered_record( ssl );
9358
Hanno Becker0271f962018-08-16 13:23:47 +01009359 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009360 ssl_buffering_free_slot( ssl, offset );
9361}
9362
9363static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9364 uint8_t slot )
9365{
9366 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9367 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009368
9369 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9370 return;
9371
Hanno Beckere605b192018-08-21 15:59:07 +01009372 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009373 {
Hanno Beckere605b192018-08-21 15:59:07 +01009374 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009375 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009376 mbedtls_free( hs_buf->data );
9377 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009378 }
9379}
9380
9381#endif /* MBEDTLS_SSL_PROTO_DTLS */
9382
Gilles Peskine9b562d52018-04-25 20:32:43 +02009383void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009384{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009385 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9386
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009387 if( handshake == NULL )
9388 return;
9389
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009390#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9391 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9392 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009393 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009394 handshake->async_in_progress = 0;
9395 }
9396#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9397
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009398#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9399 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9400 mbedtls_md5_free( &handshake->fin_md5 );
9401 mbedtls_sha1_free( &handshake->fin_sha1 );
9402#endif
9403#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9404#if defined(MBEDTLS_SHA256_C)
9405 mbedtls_sha256_free( &handshake->fin_sha256 );
9406#endif
9407#if defined(MBEDTLS_SHA512_C)
9408 mbedtls_sha512_free( &handshake->fin_sha512 );
9409#endif
9410#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412#if defined(MBEDTLS_DHM_C)
9413 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415#if defined(MBEDTLS_ECDH_C)
9416 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009417#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009418#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009419 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009420#if defined(MBEDTLS_SSL_CLI_C)
9421 mbedtls_free( handshake->ecjpake_cache );
9422 handshake->ecjpake_cache = NULL;
9423 handshake->ecjpake_cache_len = 0;
9424#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009425#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009426
Janos Follath4ae5c292016-02-10 11:27:43 +00009427#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9428 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009429 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009431#endif
9432
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009433#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9434 if( handshake->psk != NULL )
9435 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009436 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009437 mbedtls_free( handshake->psk );
9438 }
9439#endif
9440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9442 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009443 /*
9444 * Free only the linked list wrapper, not the keys themselves
9445 * since the belong to the SNI callback
9446 */
9447 if( handshake->sni_key_cert != NULL )
9448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009450
9451 while( cur != NULL )
9452 {
9453 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009455 cur = next;
9456 }
9457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009459
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009460#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009461 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009462#endif
9463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464#if defined(MBEDTLS_SSL_PROTO_DTLS)
9465 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009466 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009467 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009468#endif
9469
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009470 mbedtls_platform_zeroize( handshake,
9471 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009472}
9473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009475{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009476 if( session == NULL )
9477 return;
9478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009480 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482 mbedtls_x509_crt_free( session->peer_cert );
9483 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009484 }
Paul Bakkered27a042013-04-18 22:46:23 +02009485#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009486
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009487#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009489#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009490
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009491 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009492}
9493
Paul Bakker5121ce52009-01-03 21:22:43 +00009494/*
9495 * Free an SSL context
9496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009498{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009499 if( ssl == NULL )
9500 return;
9501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009503
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009504 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009505 {
Angus Grattond8213d02016-05-25 20:56:48 +10009506 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009508 }
9509
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009510 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009511 {
Angus Grattond8213d02016-05-25 20:56:48 +10009512 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009514 }
9515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009517 if( ssl->compress_buf != NULL )
9518 {
Angus Grattond8213d02016-05-25 20:56:48 +10009519 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009521 }
9522#endif
9523
Paul Bakker48916f92012-09-16 19:57:18 +00009524 if( ssl->transform )
9525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 mbedtls_ssl_transform_free( ssl->transform );
9527 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009528 }
9529
9530 if( ssl->handshake )
9531 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009532 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9534 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 mbedtls_free( ssl->handshake );
9537 mbedtls_free( ssl->transform_negotiate );
9538 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009539 }
9540
Paul Bakkerc0463502013-02-14 11:19:38 +01009541 if( ssl->session )
9542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 mbedtls_ssl_session_free( ssl->session );
9544 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009545 }
9546
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009547#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009548 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009549 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009550 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009552 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009553#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9556 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9559 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009560 }
9561#endif
9562
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009563#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009565#endif
9566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009568
Paul Bakker86f04f42013-02-14 11:20:09 +01009569 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009570 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009571}
9572
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009573/*
9574 * Initialze mbedtls_ssl_config
9575 */
9576void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9577{
9578 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9579}
9580
Simon Butcherc97b6972015-12-27 23:48:17 +00009581#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009582static int ssl_preset_default_hashes[] = {
9583#if defined(MBEDTLS_SHA512_C)
9584 MBEDTLS_MD_SHA512,
9585 MBEDTLS_MD_SHA384,
9586#endif
9587#if defined(MBEDTLS_SHA256_C)
9588 MBEDTLS_MD_SHA256,
9589 MBEDTLS_MD_SHA224,
9590#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009591#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009592 MBEDTLS_MD_SHA1,
9593#endif
9594 MBEDTLS_MD_NONE
9595};
Simon Butcherc97b6972015-12-27 23:48:17 +00009596#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009597
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009598static int ssl_preset_suiteb_ciphersuites[] = {
9599 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9600 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9601 0
9602};
9603
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009604#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009605static int ssl_preset_suiteb_hashes[] = {
9606 MBEDTLS_MD_SHA256,
9607 MBEDTLS_MD_SHA384,
9608 MBEDTLS_MD_NONE
9609};
9610#endif
9611
9612#if defined(MBEDTLS_ECP_C)
9613static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9614 MBEDTLS_ECP_DP_SECP256R1,
9615 MBEDTLS_ECP_DP_SECP384R1,
9616 MBEDTLS_ECP_DP_NONE
9617};
9618#endif
9619
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009620/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009621 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009622 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009623int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009624 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009625{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009626#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009627 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009628#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009629
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009630 /* Use the functions here so that they are covered in tests,
9631 * but otherwise access member directly for efficiency */
9632 mbedtls_ssl_conf_endpoint( conf, endpoint );
9633 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009634
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009635 /*
9636 * Things that are common to all presets
9637 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009638#if defined(MBEDTLS_SSL_CLI_C)
9639 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9640 {
9641 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9642#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9643 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9644#endif
9645 }
9646#endif
9647
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009648#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009649 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009650#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009651
9652#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9653 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9654#endif
9655
9656#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9657 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9658#endif
9659
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009660#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9661 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9662#endif
9663
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009664#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009665 conf->f_cookie_write = ssl_cookie_write_dummy;
9666 conf->f_cookie_check = ssl_cookie_check_dummy;
9667#endif
9668
9669#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9670 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9671#endif
9672
Janos Follath088ce432017-04-10 12:42:31 +01009673#if defined(MBEDTLS_SSL_SRV_C)
9674 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9675#endif
9676
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009677#if defined(MBEDTLS_SSL_PROTO_DTLS)
9678 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9679 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9680#endif
9681
9682#if defined(MBEDTLS_SSL_RENEGOTIATION)
9683 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009684 memset( conf->renego_period, 0x00, 2 );
9685 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009686#endif
9687
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009688#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9689 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9690 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009691 const unsigned char dhm_p[] =
9692 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9693 const unsigned char dhm_g[] =
9694 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9695
Hanno Beckera90658f2017-10-04 15:29:08 +01009696 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9697 dhm_p, sizeof( dhm_p ),
9698 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009699 {
9700 return( ret );
9701 }
9702 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009703#endif
9704
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009705 /*
9706 * Preset-specific defaults
9707 */
9708 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009709 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009710 /*
9711 * NSA Suite B
9712 */
9713 case MBEDTLS_SSL_PRESET_SUITEB:
9714 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9715 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9716 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9717 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9718
9719 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9720 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9721 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9722 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9723 ssl_preset_suiteb_ciphersuites;
9724
9725#if defined(MBEDTLS_X509_CRT_PARSE_C)
9726 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009727#endif
9728
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009729#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009730 conf->sig_hashes = ssl_preset_suiteb_hashes;
9731#endif
9732
9733#if defined(MBEDTLS_ECP_C)
9734 conf->curve_list = ssl_preset_suiteb_curves;
9735#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009736 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009737
9738 /*
9739 * Default
9740 */
9741 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009742 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9743 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9744 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9745 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9746 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9747 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9748 MBEDTLS_SSL_MIN_MINOR_VERSION :
9749 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009750 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9751 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9752
9753#if defined(MBEDTLS_SSL_PROTO_DTLS)
9754 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9755 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9756#endif
9757
9758 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9759 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9760 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9761 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9762 mbedtls_ssl_list_ciphersuites();
9763
9764#if defined(MBEDTLS_X509_CRT_PARSE_C)
9765 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9766#endif
9767
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009768#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009769 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009770#endif
9771
9772#if defined(MBEDTLS_ECP_C)
9773 conf->curve_list = mbedtls_ecp_grp_id_list();
9774#endif
9775
9776#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9777 conf->dhm_min_bitlen = 1024;
9778#endif
9779 }
9780
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009781 return( 0 );
9782}
9783
9784/*
9785 * Free mbedtls_ssl_config
9786 */
9787void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9788{
9789#if defined(MBEDTLS_DHM_C)
9790 mbedtls_mpi_free( &conf->dhm_P );
9791 mbedtls_mpi_free( &conf->dhm_G );
9792#endif
9793
9794#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9795 if( conf->psk != NULL )
9796 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009797 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009798 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009799 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009800 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009801 }
9802
9803 if( conf->psk_identity != NULL )
9804 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009805 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009806 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009807 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009808 conf->psk_identity_len = 0;
9809 }
9810#endif
9811
9812#if defined(MBEDTLS_X509_CRT_PARSE_C)
9813 ssl_key_cert_free( conf->key_cert );
9814#endif
9815
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009816 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009817}
9818
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009819#if defined(MBEDTLS_PK_C) && \
9820 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009821/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826#if defined(MBEDTLS_RSA_C)
9827 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9828 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830#if defined(MBEDTLS_ECDSA_C)
9831 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9832 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009835}
9836
Hanno Becker7e5437a2017-04-28 17:15:26 +01009837unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9838{
9839 switch( type ) {
9840 case MBEDTLS_PK_RSA:
9841 return( MBEDTLS_SSL_SIG_RSA );
9842 case MBEDTLS_PK_ECDSA:
9843 case MBEDTLS_PK_ECKEY:
9844 return( MBEDTLS_SSL_SIG_ECDSA );
9845 default:
9846 return( MBEDTLS_SSL_SIG_ANON );
9847 }
9848}
9849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009851{
9852 switch( sig )
9853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854#if defined(MBEDTLS_RSA_C)
9855 case MBEDTLS_SSL_SIG_RSA:
9856 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858#if defined(MBEDTLS_ECDSA_C)
9859 case MBEDTLS_SSL_SIG_ECDSA:
9860 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009861#endif
9862 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009864 }
9865}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009866#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009867
Hanno Becker7e5437a2017-04-28 17:15:26 +01009868#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9869 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9870
9871/* Find an entry in a signature-hash set matching a given hash algorithm. */
9872mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9873 mbedtls_pk_type_t sig_alg )
9874{
9875 switch( sig_alg )
9876 {
9877 case MBEDTLS_PK_RSA:
9878 return( set->rsa );
9879 case MBEDTLS_PK_ECDSA:
9880 return( set->ecdsa );
9881 default:
9882 return( MBEDTLS_MD_NONE );
9883 }
9884}
9885
9886/* Add a signature-hash-pair to a signature-hash set */
9887void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9888 mbedtls_pk_type_t sig_alg,
9889 mbedtls_md_type_t md_alg )
9890{
9891 switch( sig_alg )
9892 {
9893 case MBEDTLS_PK_RSA:
9894 if( set->rsa == MBEDTLS_MD_NONE )
9895 set->rsa = md_alg;
9896 break;
9897
9898 case MBEDTLS_PK_ECDSA:
9899 if( set->ecdsa == MBEDTLS_MD_NONE )
9900 set->ecdsa = md_alg;
9901 break;
9902
9903 default:
9904 break;
9905 }
9906}
9907
9908/* Allow exactly one hash algorithm for each signature. */
9909void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9910 mbedtls_md_type_t md_alg )
9911{
9912 set->rsa = md_alg;
9913 set->ecdsa = md_alg;
9914}
9915
9916#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9917 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9918
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009919/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009920 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009923{
9924 switch( hash )
9925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926#if defined(MBEDTLS_MD5_C)
9927 case MBEDTLS_SSL_HASH_MD5:
9928 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930#if defined(MBEDTLS_SHA1_C)
9931 case MBEDTLS_SSL_HASH_SHA1:
9932 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934#if defined(MBEDTLS_SHA256_C)
9935 case MBEDTLS_SSL_HASH_SHA224:
9936 return( MBEDTLS_MD_SHA224 );
9937 case MBEDTLS_SSL_HASH_SHA256:
9938 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940#if defined(MBEDTLS_SHA512_C)
9941 case MBEDTLS_SSL_HASH_SHA384:
9942 return( MBEDTLS_MD_SHA384 );
9943 case MBEDTLS_SSL_HASH_SHA512:
9944 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009945#endif
9946 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009948 }
9949}
9950
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009951/*
9952 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9953 */
9954unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9955{
9956 switch( md )
9957 {
9958#if defined(MBEDTLS_MD5_C)
9959 case MBEDTLS_MD_MD5:
9960 return( MBEDTLS_SSL_HASH_MD5 );
9961#endif
9962#if defined(MBEDTLS_SHA1_C)
9963 case MBEDTLS_MD_SHA1:
9964 return( MBEDTLS_SSL_HASH_SHA1 );
9965#endif
9966#if defined(MBEDTLS_SHA256_C)
9967 case MBEDTLS_MD_SHA224:
9968 return( MBEDTLS_SSL_HASH_SHA224 );
9969 case MBEDTLS_MD_SHA256:
9970 return( MBEDTLS_SSL_HASH_SHA256 );
9971#endif
9972#if defined(MBEDTLS_SHA512_C)
9973 case MBEDTLS_MD_SHA384:
9974 return( MBEDTLS_SSL_HASH_SHA384 );
9975 case MBEDTLS_MD_SHA512:
9976 return( MBEDTLS_SSL_HASH_SHA512 );
9977#endif
9978 default:
9979 return( MBEDTLS_SSL_HASH_NONE );
9980 }
9981}
9982
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009983#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009984/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009985 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009986 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009987 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009988int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009991
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009992 if( ssl->conf->curve_list == NULL )
9993 return( -1 );
9994
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009995 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009996 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009997 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009998
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009999 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010000}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010001#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010002
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010003#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010004/*
10005 * Check if a hash proposed by the peer is in our list.
10006 * Return 0 if we're willing to use it, -1 otherwise.
10007 */
10008int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10009 mbedtls_md_type_t md )
10010{
10011 const int *cur;
10012
10013 if( ssl->conf->sig_hashes == NULL )
10014 return( -1 );
10015
10016 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10017 if( *cur == (int) md )
10018 return( 0 );
10019
10020 return( -1 );
10021}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010022#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010024#if defined(MBEDTLS_X509_CRT_PARSE_C)
10025int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10026 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010027 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010028 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010029{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010030 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010032 int usage = 0;
10033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010035 const char *ext_oid;
10036 size_t ext_len;
10037#endif
10038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10040 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010041 ((void) cert);
10042 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010043 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010044#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10047 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010048 {
10049 /* Server part of the key exchange */
10050 switch( ciphersuite->key_exchange )
10051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052 case MBEDTLS_KEY_EXCHANGE_RSA:
10053 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010054 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010055 break;
10056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10058 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10059 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10060 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010061 break;
10062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10064 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010065 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010066 break;
10067
10068 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 case MBEDTLS_KEY_EXCHANGE_NONE:
10070 case MBEDTLS_KEY_EXCHANGE_PSK:
10071 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10072 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010073 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010074 usage = 0;
10075 }
10076 }
10077 else
10078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10080 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010081 }
10082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010084 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010085 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010086 ret = -1;
10087 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010088#else
10089 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10093 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10096 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010097 }
10098 else
10099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10101 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010102 }
10103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010104 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010105 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010106 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010107 ret = -1;
10108 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010110
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010111 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010114
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010115/*
10116 * Convert version numbers to/from wire format
10117 * and, for DTLS, to/from TLS equivalent.
10118 *
10119 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010120 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010121 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10122 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010125 unsigned char ver[2] )
10126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010127#if defined(MBEDTLS_SSL_PROTO_DTLS)
10128 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010131 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10132
10133 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10134 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10135 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010136 else
10137#else
10138 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010139#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010140 {
10141 ver[0] = (unsigned char) major;
10142 ver[1] = (unsigned char) minor;
10143 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010144}
10145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010147 const unsigned char ver[2] )
10148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149#if defined(MBEDTLS_SSL_PROTO_DTLS)
10150 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010151 {
10152 *major = 255 - ver[0] + 2;
10153 *minor = 255 - ver[1] + 1;
10154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010156 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10157 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010158 else
10159#else
10160 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010161#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010162 {
10163 *major = ver[0];
10164 *minor = ver[1];
10165 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010166}
10167
Simon Butcher99000142016-10-13 17:21:01 +010010168int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10169{
10170#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10171 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10172 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10173
10174 switch( md )
10175 {
10176#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10177#if defined(MBEDTLS_MD5_C)
10178 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010179 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010180#endif
10181#if defined(MBEDTLS_SHA1_C)
10182 case MBEDTLS_SSL_HASH_SHA1:
10183 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10184 break;
10185#endif
10186#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10187#if defined(MBEDTLS_SHA512_C)
10188 case MBEDTLS_SSL_HASH_SHA384:
10189 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10190 break;
10191#endif
10192#if defined(MBEDTLS_SHA256_C)
10193 case MBEDTLS_SSL_HASH_SHA256:
10194 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10195 break;
10196#endif
10197 default:
10198 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10199 }
10200
10201 return 0;
10202#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10203 (void) ssl;
10204 (void) md;
10205
10206 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10207#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10208}
10209
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010210#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10211 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10212int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10213 unsigned char *output,
10214 unsigned char *data, size_t data_len )
10215{
10216 int ret = 0;
10217 mbedtls_md5_context mbedtls_md5;
10218 mbedtls_sha1_context mbedtls_sha1;
10219
10220 mbedtls_md5_init( &mbedtls_md5 );
10221 mbedtls_sha1_init( &mbedtls_sha1 );
10222
10223 /*
10224 * digitally-signed struct {
10225 * opaque md5_hash[16];
10226 * opaque sha_hash[20];
10227 * };
10228 *
10229 * md5_hash
10230 * MD5(ClientHello.random + ServerHello.random
10231 * + ServerParams);
10232 * sha_hash
10233 * SHA(ClientHello.random + ServerHello.random
10234 * + ServerParams);
10235 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010236 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010237 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010239 goto exit;
10240 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010241 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010242 ssl->handshake->randbytes, 64 ) ) != 0 )
10243 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010245 goto exit;
10246 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010247 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010248 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010250 goto exit;
10251 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010252 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010253 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010255 goto exit;
10256 }
10257
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010258 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010259 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010261 goto exit;
10262 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010263 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010264 ssl->handshake->randbytes, 64 ) ) != 0 )
10265 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010267 goto exit;
10268 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010269 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010270 data_len ) ) != 0 )
10271 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010273 goto exit;
10274 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010275 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010276 output + 16 ) ) != 0 )
10277 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010279 goto exit;
10280 }
10281
10282exit:
10283 mbedtls_md5_free( &mbedtls_md5 );
10284 mbedtls_sha1_free( &mbedtls_sha1 );
10285
10286 if( ret != 0 )
10287 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10288 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10289
10290 return( ret );
10291
10292}
10293#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10294 MBEDTLS_SSL_PROTO_TLS1_1 */
10295
10296#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10297 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10298int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010299 unsigned char *hash, size_t *hashlen,
10300 unsigned char *data, size_t data_len,
10301 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010302{
10303 int ret = 0;
10304 mbedtls_md_context_t ctx;
10305 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010306 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010307
10308 mbedtls_md_init( &ctx );
10309
10310 /*
10311 * digitally-signed struct {
10312 * opaque client_random[32];
10313 * opaque server_random[32];
10314 * ServerDHParams params;
10315 * };
10316 */
10317 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10318 {
10319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10320 goto exit;
10321 }
10322 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10323 {
10324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10325 goto exit;
10326 }
10327 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10328 {
10329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10330 goto exit;
10331 }
10332 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10333 {
10334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10335 goto exit;
10336 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010337 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010338 {
10339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10340 goto exit;
10341 }
10342
10343exit:
10344 mbedtls_md_free( &ctx );
10345
10346 if( ret != 0 )
10347 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10348 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10349
10350 return( ret );
10351}
10352#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10353 MBEDTLS_SSL_PROTO_TLS1_2 */
10354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#endif /* MBEDTLS_SSL_TLS_C */