blob: 6ffbf7b839813eba9832d53dedcf66553559922c [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"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010067#else
68 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069#endif
70 return( 0 );
71}
72
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020073/*
74 * Start a timer.
75 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020079 if( ssl->f_set_timer == NULL )
80 return;
81
82 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
83 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084}
85
86/*
87 * Return -1 is timer is expired, 0 if it isn't.
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020090{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020091 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020092 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020093
94 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020095 {
96 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020098 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020099
100 return( 0 );
101}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100103static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
104 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100105static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100106
107#define SSL_DONT_FORCE_FLUSH 0
108#define SSL_FORCE_FLUSH 1
109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100111
Hanno Beckera5a2b082019-05-15 14:03:01 +0100112#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100113/* Top-level Connection ID API */
114
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100115int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
116 size_t len,
117 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100118{
119 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
121
Hanno Becker791ec6b2019-05-14 11:45:26 +0100122 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
123 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
124 {
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126 }
127
128 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100129 conf->cid_len = len;
130 return( 0 );
131}
132
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100133int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
134 int enable,
135 unsigned char const *own_cid,
136 size_t own_cid_len )
137{
Hanno Becker78c43022019-05-03 14:38:32 +0100138 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
140
Hanno Becker07489862019-04-25 16:01:49 +0100141 ssl->negotiate_cid = enable;
142 if( enable == MBEDTLS_SSL_CID_DISABLED )
143 {
144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
145 return( 0 );
146 }
147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100148 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100149
Hanno Beckereec2be92019-05-03 13:06:44 +0100150 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100151 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
153 (unsigned) own_cid_len,
154 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
156 }
157
158 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100159 /* Truncation is not an issue here because
160 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
161 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100162
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100163 return( 0 );
164}
165
166int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
167 int *enabled,
168 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
169 size_t *peer_cid_len )
170{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100171 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100172
Hanno Becker78c43022019-05-03 14:38:32 +0100173 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
174 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
175 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100177 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100178
Hanno Beckercb063f52019-05-03 12:54:52 +0100179 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
180 * were used, but client and server requested the empty CID.
181 * This is indistinguishable from not using the CID extension
182 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183 if( ssl->transform_in->in_cid_len == 0 &&
184 ssl->transform_in->out_cid_len == 0 )
185 {
186 return( 0 );
187 }
188
Hanno Becker633d6042019-05-22 16:50:35 +0100189 if( peer_cid_len != NULL )
190 {
191 *peer_cid_len = ssl->transform_in->out_cid_len;
192 if( peer_cid != NULL )
193 {
194 memcpy( peer_cid, ssl->transform_in->out_cid,
195 ssl->transform_in->out_cid_len );
196 }
197 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100198
199 *enabled = MBEDTLS_SSL_CID_ENABLED;
200
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100201 return( 0 );
202}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100203#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100204
Hanno Beckerd5847772018-08-28 10:09:23 +0100205/* Forward declarations for functions related to message buffering. */
206static void ssl_buffering_free( mbedtls_ssl_context *ssl );
207static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
208 uint8_t slot );
209static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
210static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
211static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
212static int ssl_buffer_message( mbedtls_ssl_context *ssl );
213static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100214static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100215
Hanno Beckera67dee22018-08-22 10:05:20 +0100216static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100217static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100218{
Hanno Becker11682cc2018-08-22 14:41:02 +0100219 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100220
221 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100222 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223
224 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
225}
226
Hanno Becker67bc7c32018-08-06 11:33:50 +0100227static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
228{
Hanno Becker11682cc2018-08-22 14:41:02 +0100229 size_t const bytes_written = ssl->out_left;
230 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231
232 /* Double-check that the write-index hasn't gone
233 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235 {
236 /* Should never happen... */
237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
238 }
239
240 return( (int) ( mtu - bytes_written ) );
241}
242
243static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
244{
245 int ret;
246 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400247 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100248
249#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
250 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
251
252 if( max_len > mfl )
253 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100254
255 /* By the standard (RFC 6066 Sect. 4), the MFL extension
256 * only limits the maximum record payload size, so in theory
257 * we would be allowed to pack multiple records of payload size
258 * MFL into a single datagram. However, this would mean that there's
259 * no way to explicitly communicate MTU restrictions to the peer.
260 *
261 * The following reduction of max_len makes sure that we never
262 * write datagrams larger than MFL + Record Expansion Overhead.
263 */
264 if( max_len <= ssl->out_left )
265 return( 0 );
266
267 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100268#endif
269
270 ret = ssl_get_remaining_space_in_datagram( ssl );
271 if( ret < 0 )
272 return( ret );
273 remaining = (size_t) ret;
274
275 ret = mbedtls_ssl_get_record_expansion( ssl );
276 if( ret < 0 )
277 return( ret );
278 expansion = (size_t) ret;
279
280 if( remaining <= expansion )
281 return( 0 );
282
283 remaining -= expansion;
284 if( remaining >= max_len )
285 remaining = max_len;
286
287 return( (int) remaining );
288}
289
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290/*
291 * Double the retransmit timeout value, within the allowed range,
292 * returning -1 if the maximum value has already been reached.
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295{
296 uint32_t new_timeout;
297
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200298 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299 return( -1 );
300
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200301 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
302 * in the following way: after the initial transmission and a first
303 * retransmission, back off to a temporary estimated MTU of 508 bytes.
304 * This value is guaranteed to be deliverable (if not guaranteed to be
305 * delivered) of any compliant IPv4 (and IPv6) network, and should work
306 * on most non-IP stacks too. */
307 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400308 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200309 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
311 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200312
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313 new_timeout = 2 * ssl->handshake->retransmit_timeout;
314
315 /* Avoid arithmetic overflow and range overflow */
316 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200317 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200319 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200320 }
321
322 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200324 ssl->handshake->retransmit_timeout ) );
325
326 return( 0 );
327}
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200330{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200331 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200333 ssl->handshake->retransmit_timeout ) );
334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200338/*
339 * Convert max_fragment_length codes to length.
340 * RFC 6066 says:
341 * enum{
342 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
343 * } MaxFragmentLength;
344 * and we add 0 -> extension unused
345 */
Angus Grattond8213d02016-05-25 20:56:48 +1000346static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200347{
Angus Grattond8213d02016-05-25 20:56:48 +1000348 switch( mfl )
349 {
350 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
351 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
352 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
353 return 512;
354 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
355 return 1024;
356 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
357 return 2048;
358 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
359 return 4096;
360 default:
361 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
362 }
363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200365
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200366#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_ssl_session_free( dst );
370 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373 if( src->peer_cert != NULL )
374 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200375 int ret;
376
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200377 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200378 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200384 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387 dst->peer_cert = NULL;
388 return( ret );
389 }
390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200393#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 if( src->ticket != NULL )
395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200396 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399
400 memcpy( dst->ticket, src->ticket, src->ticket_len );
401 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403
404 return( 0 );
405}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200406#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
409int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200410 const unsigned char *key_enc, const unsigned char *key_dec,
411 size_t keylen,
412 const unsigned char *iv_enc, const unsigned char *iv_dec,
413 size_t ivlen,
414 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200415 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
417int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
420int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
421#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423/*
424 * Key material generation
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427static int ssl3_prf( const unsigned char *secret, size_t slen,
428 const char *label,
429 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000430 unsigned char *dstbuf, size_t dlen )
431{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100432 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000433 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200434 mbedtls_md5_context md5;
435 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000436 unsigned char padding[16];
437 unsigned char sha1sum[20];
438 ((void)label);
439
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200440 mbedtls_md5_init( &md5 );
441 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200442
Paul Bakker5f70b252012-09-13 14:23:06 +0000443 /*
444 * SSLv3:
445 * block =
446 * MD5( secret + SHA1( 'A' + secret + random ) ) +
447 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
448 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
449 * ...
450 */
451 for( i = 0; i < dlen / 16; i++ )
452 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200453 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100455 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100456 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000465
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000474 }
475
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200477 mbedtls_md5_free( &md5 );
478 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000479
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500480 mbedtls_platform_zeroize( padding, sizeof( padding ) );
481 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000482
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200488static int tls1_prf( const unsigned char *secret, size_t slen,
489 const char *label,
490 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000491 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000492{
Paul Bakker23986e52011-04-24 08:57:21 +0000493 size_t nb, hs;
494 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200495 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 unsigned char tmp[128];
497 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 const mbedtls_md_info_t *md_info;
499 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100500 int ret;
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507 hs = ( slen + 1 ) / 2;
508 S1 = secret;
509 S2 = secret + slen - hs;
510
511 nb = strlen( label );
512 memcpy( tmp + 20, label, nb );
513 memcpy( tmp + 20 + nb, random, rlen );
514 nb += rlen;
515
516 /*
517 * First compute P_md5(secret,label+random)[0..dlen]
518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100523 return( ret );
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
526 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
527 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 for( i = 0; i < dlen; i += 16 )
530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_md_hmac_reset ( &md_ctx );
532 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
533 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_reset ( &md_ctx );
536 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
537 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
540
541 for( j = 0; j < k; j++ )
542 dstbuf[i + j] = h_i[j];
543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100546
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 /*
548 * XOR out with P_sha1(secret,label+random)[0..dlen]
549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554 return( ret );
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
557 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
558 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 for( i = 0; i < dlen; i += 20 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_md_hmac_reset ( &md_ctx );
563 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
564 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_hmac_reset ( &md_ctx );
567 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
568 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
571
572 for( j = 0; j < k; j++ )
573 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
574 }
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100577
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500578 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
579 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 return( 0 );
582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
586static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100587 const unsigned char *secret, size_t slen,
588 const char *label,
589 const unsigned char *random, size_t rlen,
590 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000591{
592 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100593 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000594 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
596 const mbedtls_md_info_t *md_info;
597 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598 int ret;
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
607 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000609
610 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100611 memcpy( tmp + md_len, label, nb );
612 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000613 nb += rlen;
614
615 /*
616 * Compute P_<hash>(secret, label + random)[0..dlen]
617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 return( ret );
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
622 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
623 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100624
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_md_hmac_reset ( &md_ctx );
628 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
629 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_md_hmac_reset ( &md_ctx );
632 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
633 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100635 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
637 for( j = 0; j < k; j++ )
638 dstbuf[i + j] = h_i[j];
639 }
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100642
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500643 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
644 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000645
646 return( 0 );
647}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100650static int tls_prf_sha256( const unsigned char *secret, size_t slen,
651 const char *label,
652 const unsigned char *random, size_t rlen,
653 unsigned char *dstbuf, size_t dlen )
654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 label, random, rlen, dstbuf, dlen ) );
657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200661static int tls_prf_sha384( const unsigned char *secret, size_t slen,
662 const char *label,
663 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 unsigned char *dstbuf, size_t dlen )
665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100667 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#endif /* MBEDTLS_SHA512_C */
670#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
675 defined(MBEDTLS_SSL_PROTO_TLS1_1)
676static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677#endif
Paul Bakker380da532012-04-18 16:10:25 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200680static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682#endif
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200685static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
690#if defined(MBEDTLS_SHA256_C)
691static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200692static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_SHA512_C)
697static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200698static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200703/* Type for the TLS PRF */
704typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
705 const unsigned char *, size_t,
706 unsigned char *, size_t);
707
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200708/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200709 * Populate a transform structure with session keys and all the other
710 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200711 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200712 * Parameters:
713 * - [in/out]: transform: structure to populate
714 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200715 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200716 * - [in] ciphersuite
717 * - [in] master
718 * - [in] encrypt_then_mac
719 * - [in] trunc_hmac
720 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200721 * - [in] tls_prf: pointer to PRF to use for key derivation
722 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200723 * - [in] minor_ver: SSL/TLS minor version
724 * - [in] endpoint: client or server
725 * - [in] ssl: optionally used for:
726 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
727 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
728 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200731 int ciphersuite,
732 const unsigned char master[48],
733#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
734 int encrypt_then_mac,
735#endif
736#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
737 int trunc_hmac,
738#endif
739#if defined(MBEDTLS_ZLIB_SUPPORT)
740 int compression,
741#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 ssl_tls_prf_t tls_prf,
743 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 int minor_ver,
745 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200746 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000747{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200748 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 unsigned char keyblk[256];
750 unsigned char *key1;
751 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100752 unsigned char *mac_enc;
753 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000754 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200755 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000756 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000757 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 const mbedtls_cipher_info_t *cipher_info;
759 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100760
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200761#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
762 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
763 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200764 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200765 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000766#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000767
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200768 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200770 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000771#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200772 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200774 /*
775 * Get various info structures
776 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200777 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200778 if( ciphersuite_info == NULL )
779 {
780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200781 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
783 }
784
Hanno Becker8759e162017-12-27 21:34:08 +0000785 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100786 if( cipher_info == NULL )
787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000789 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100791 }
792
Hanno Becker8759e162017-12-27 21:34:08 +0000793 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100794 if( md_info == NULL )
795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000797 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 }
800
Hanno Beckera5a2b082019-05-15 14:03:01 +0100801#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100802 /* Copy own and peer's CID if the use of the CID
803 * extension has been negotiated. */
804 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
805 {
806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100807
Hanno Becker4932f9f2019-05-03 15:23:51 +0100808 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100809 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100810 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100811 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100812
813 transform->out_cid_len = ssl->handshake->peer_cid_len;
814 memcpy( transform->out_cid, ssl->handshake->peer_cid,
815 ssl->handshake->peer_cid_len );
816 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
817 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100818 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100819#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200822 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000823 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200824 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100825 if( ret != 0 )
826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100828 return( ret );
829 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200832 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200833 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200834 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 /*
838 * Determine the appropriate key, IV and MAC length.
839 */
Paul Bakker68884e32013-01-07 18:20:04 +0100840
Hanno Beckere7f2df02017-12-27 08:17:40 +0000841 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200842
Hanno Beckerf1229442018-01-03 15:32:31 +0000843#if defined(MBEDTLS_GCM_C) || \
844 defined(MBEDTLS_CCM_C) || \
845 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200847 cipher_info->mode == MBEDTLS_MODE_CCM ||
848 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 {
Hanno Becker8759e162017-12-27 21:34:08 +0000850 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200851
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200852 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000853 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000854 transform->taglen =
855 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200856
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200857 /* All modes haves 96-bit IVs;
858 * GCM and CCM has 4 implicit and 8 explicit bytes
859 * ChachaPoly has all 12 bytes implicit
860 */
Paul Bakker68884e32013-01-07 18:20:04 +0100861 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200862 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
863 transform->fixed_ivlen = 12;
864 else
865 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 /* Minimum length of encrypted record */
868 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000869 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100870 }
871 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000872#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
873#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
874 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
875 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100876 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
879 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200882 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100883 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200885 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000886 mac_key_len = mbedtls_md_get_size( md_info );
887 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890 /*
891 * If HMAC is to be truncated, we shall keep the leftmost bytes,
892 * (rfc 6066 page 13 or rfc 2104 section 4),
893 * so we only need to adjust the length here.
894 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200895 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000898
899#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
900 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000901 * HMAC implementation which also truncates the key
902 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000903 mac_key_len = transform->maclen;
904#endif
905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200907
908 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100909 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200911 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200913 transform->minlen = transform->maclen;
914 else
Paul Bakker68884e32013-01-07 18:20:04 +0100915 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200916 /*
917 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100918 * 1. if EtM is in use: one block plus MAC
919 * otherwise: * first multiple of blocklen greater than maclen
920 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200923 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100924 {
925 transform->minlen = transform->maclen
926 + cipher_info->block_size;
927 }
928 else
929#endif
930 {
931 transform->minlen = transform->maclen
932 + cipher_info->block_size
933 - transform->maclen % cipher_info->block_size;
934 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200937 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
938 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200939 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100940 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200943 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
944 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 {
946 transform->minlen += transform->ivlen;
947 }
948 else
949#endif
950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200953 }
Paul Bakker68884e32013-01-07 18:20:04 +0100954 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000956 else
957#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
958 {
959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
961 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Hanno Beckere7f2df02017-12-27 08:17:40 +0000963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
964 (unsigned) keylen,
965 (unsigned) transform->minlen,
966 (unsigned) transform->ivlen,
967 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
969 /*
970 * Finally setup the cipher contexts, IVs and MAC secrets.
971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200973 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000975 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000976 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Paul Bakker68884e32013-01-07 18:20:04 +0100978 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000979 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000981 /*
982 * This is not used in TLS v1.1.
983 */
Paul Bakker48916f92012-09-16 19:57:18 +0000984 iv_copy_len = ( transform->fixed_ivlen ) ?
985 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000986 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
987 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000988 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 }
990 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_SSL_CLI_C */
992#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200993 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000995 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000996 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100999 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001001 /*
1002 * This is not used in TLS v1.1.
1003 */
Paul Bakker48916f92012-09-16 19:57:18 +00001004 iv_copy_len = ( transform->fixed_ivlen ) ?
1005 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001006 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1007 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1014 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Hanno Becker92231322018-01-03 15:32:51 +00001017#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001019 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001020 {
Hanno Becker92231322018-01-03 15:32:51 +00001021 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001025 }
1026
Hanno Becker81c7b182017-11-09 18:39:33 +00001027 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1028 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
1030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1032#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1033 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001034 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001035 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001036 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1037 For AEAD-based ciphersuites, there is nothing to do here. */
1038 if( mac_key_len != 0 )
1039 {
1040 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1041 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1042 }
Paul Bakker68884e32013-01-07 18:20:04 +01001043 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001044 else
1045#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001049 }
Hanno Becker92231322018-01-03 15:32:51 +00001050#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1053 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001054 {
1055 int ret = 0;
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001058
Hanno Beckere7f2df02017-12-27 08:17:40 +00001059 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001060 transform->iv_enc, transform->iv_dec,
1061 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001062 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001063 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1066 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001067 }
1068 }
Hanno Becker92231322018-01-03 15:32:51 +00001069#else
1070 ((void) mac_dec);
1071 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001073
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001074#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1075 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001076 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001077 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001078 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001079 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001080 iv_copy_len );
1081 }
1082#endif
1083
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001084 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001085 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001088 return( ret );
1089 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001090
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001091 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001092 cipher_info ) ) != 0 )
1093 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095 return( ret );
1096 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001099 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001103 return( ret );
1104 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001107 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001111 return( ret );
1112 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_CIPHER_MODE_CBC)
1115 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1118 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001122 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1125 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128 return( ret );
1129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001133 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001135 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001137 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001140
Paul Bakker48916f92012-09-16 19:57:18 +00001141 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1142 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001143
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001144 if( deflateInit( &transform->ctx_deflate,
1145 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001146 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1149 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001150 }
1151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001153
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( 0 );
1155}
1156
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001157/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001158 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001159 *
1160 * Inputs:
1161 * - SSL/TLS minor version
1162 * - hash associated with the ciphersuite (only used by TLS 1.2)
1163 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001164 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001165 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1166 */
1167static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1168 int minor_ver,
1169 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001170{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001171#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1172 (void) hash;
1173#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001174
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001175#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001176 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001177 {
1178 handshake->tls_prf = ssl3_prf;
1179 handshake->calc_verify = ssl_calc_verify_ssl;
1180 handshake->calc_finished = ssl_calc_finished_ssl;
1181 }
1182 else
1183#endif
1184#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001185 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001186 {
1187 handshake->tls_prf = tls1_prf;
1188 handshake->calc_verify = ssl_calc_verify_tls;
1189 handshake->calc_finished = ssl_calc_finished_tls;
1190 }
1191 else
1192#endif
1193#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1194#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1196 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001197 {
1198 handshake->tls_prf = tls_prf_sha384;
1199 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1200 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1201 }
1202 else
1203#endif
1204#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001206 {
1207 handshake->tls_prf = tls_prf_sha256;
1208 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1209 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1210 }
1211 else
1212#endif
1213#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1214 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1216 }
1217
1218 return( 0 );
1219}
1220
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001221/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001222 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001223 *
1224 * Parameters:
1225 * [in/out] handshake
1226 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1227 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001228 * [out] master
1229 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001230 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001231static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001232 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001233 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001234{
1235 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001236
1237#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001238 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001239 (void) ssl;
1240#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001241
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001242 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001243 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1245 return( 0 );
1246 }
1247
1248 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1249 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001250
1251#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001253 {
1254 unsigned char session_hash[48];
1255 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001256
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001257 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001258
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001259 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1260 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001261
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001262 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263 "extended master secret",
1264 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001265 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001266 }
1267 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001269 {
1270 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1271 "master secret",
1272 handshake->randbytes, 64,
1273 master, 48 );
1274 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001275 if( ret != 0 )
1276 {
1277 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1278 return( ret );
1279 }
1280
1281 mbedtls_platform_zeroize( handshake->premaster,
1282 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283
1284 return( 0 );
1285}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001286
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001287int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1288{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001289 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001290 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1291 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001292
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1294
1295 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001296 ret = ssl_set_handshake_prfs( ssl->handshake,
1297 ssl->minor_ver,
1298 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001299 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001300 {
1301 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001302 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001303 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001304
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001305 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001306 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001307 ssl->session_negotiate->master,
1308 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001309 if( ret != 0 )
1310 {
1311 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1312 return( ret );
1313 }
1314
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001315 /* Swap the client and server random values:
1316 * - MS derivation wanted client+server (RFC 5246 8.1)
1317 * - key derivation wants server+client (RFC 5246 6.3) */
1318 {
1319 unsigned char tmp[64];
1320 memcpy( tmp, ssl->handshake->randbytes, 64 );
1321 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1322 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1323 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1324 }
1325
1326 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001327 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001328 ssl->session_negotiate->ciphersuite,
1329 ssl->session_negotiate->master,
1330#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1331 ssl->session_negotiate->encrypt_then_mac,
1332#endif
1333#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1334 ssl->session_negotiate->trunc_hmac,
1335#endif
1336#if defined(MBEDTLS_ZLIB_SUPPORT)
1337 ssl->session_negotiate->compression,
1338#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 ssl->handshake->tls_prf,
1340 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001341 ssl->minor_ver,
1342 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001343 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001344 if( ret != 0 )
1345 {
1346 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1347 return( ret );
1348 }
1349
1350 /* We no longer need Server/ClientHello.random values */
1351 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1352 sizeof( ssl->handshake->randbytes ) );
1353
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001354 /* Allocate compression buffer */
1355#if defined(MBEDTLS_ZLIB_SUPPORT)
1356 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1357 ssl->compress_buf == NULL )
1358 {
1359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1360 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1361 if( ssl->compress_buf == NULL )
1362 {
1363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001364 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001365 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1366 }
1367 }
1368#endif
1369
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1371
1372 return( 0 );
1373}
1374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001376void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1377 unsigned char hash[36],
1378 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001380 mbedtls_md5_context md5;
1381 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 unsigned char pad_1[48];
1383 unsigned char pad_2[48];
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001387 mbedtls_md5_init( &md5 );
1388 mbedtls_sha1_init( &sha1 );
1389
1390 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1391 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Paul Bakker380da532012-04-18 16:10:25 +00001393 memset( pad_1, 0x36, 48 );
1394 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001396 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1397 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1398 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_md5_starts_ret( &md5 );
1401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1403 mbedtls_md5_update_ret( &md5, hash, 16 );
1404 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001406 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1407 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1408 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001410 mbedtls_sha1_starts_ret( &sha1 );
1411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1413 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1414 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001415
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001416 *hlen = 36;
1417
1418 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001421 mbedtls_md5_free( &md5 );
1422 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001423
Paul Bakker380da532012-04-18 16:10:25 +00001424 return;
1425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001429void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1430 unsigned char hash[36],
1431 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001432{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001433 mbedtls_md5_context md5;
1434 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001438 mbedtls_md5_init( &md5 );
1439 mbedtls_sha1_init( &sha1 );
1440
1441 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1442 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001444 mbedtls_md5_finish_ret( &md5, hash );
1445 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001447 *hlen = 36;
1448
1449 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001452 mbedtls_md5_free( &md5 );
1453 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001454
Paul Bakker380da532012-04-18 16:10:25 +00001455 return;
1456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1460#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001461void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1462 unsigned char hash[32],
1463 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001464{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001465 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001467 mbedtls_sha256_init( &sha256 );
1468
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001470
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001471 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001472 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001474 *hlen = 32;
1475
1476 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001479 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001480
Paul Bakker380da532012-04-18 16:10:25 +00001481 return;
1482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001486void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1487 unsigned char hash[48],
1488 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001489{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001490 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_sha512_init( &sha512 );
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001496 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001497 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001499 *hlen = 48;
1500
1501 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001504 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001505
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 return;
1507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SHA512_C */
1509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1512int 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 +02001513{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001514 unsigned char *p = ssl->handshake->premaster;
1515 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001516 const unsigned char *psk = ssl->conf->psk;
1517 size_t psk_len = ssl->conf->psk_len;
1518
1519 /* If the psk callback was called, use its result */
1520 if( ssl->handshake->psk != NULL )
1521 {
1522 psk = ssl->handshake->psk;
1523 psk_len = ssl->handshake->psk_len;
1524 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001525
1526 /*
1527 * PMS = struct {
1528 * opaque other_secret<0..2^16-1>;
1529 * opaque psk<0..2^16-1>;
1530 * };
1531 * with "other_secret" depending on the particular key exchange
1532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1534 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001535 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001536 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001538
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001539 *(p++) = (unsigned char)( psk_len >> 8 );
1540 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001541
1542 if( end < p || (size_t)( end - p ) < psk_len )
1543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1544
1545 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001546 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547 }
1548 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1550#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1551 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001552 {
1553 /*
1554 * other_secret already set by the ClientKeyExchange message,
1555 * and is 48 bytes long
1556 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001557 if( end - p < 2 )
1558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1559
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001560 *p++ = 0;
1561 *p++ = 48;
1562 p += 48;
1563 }
1564 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1566#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1567 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001569 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001570 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001571
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001572 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001574 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001575 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001578 return( ret );
1579 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001580 *(p++) = (unsigned char)( len >> 8 );
1581 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001582 p += len;
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001585 }
1586 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1588#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1589 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001591 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592 size_t zlen;
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001595 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599 return( ret );
1600 }
1601
1602 *(p++) = (unsigned char)( zlen >> 8 );
1603 *(p++) = (unsigned char)( zlen );
1604 p += zlen;
1605
Janos Follath3fbdada2018-08-15 10:26:53 +01001606 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1607 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608 }
1609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001614 }
1615
1616 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001617 if( end - p < 2 )
1618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001619
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001620 *(p++) = (unsigned char)( psk_len >> 8 );
1621 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001622
1623 if( end < p || (size_t)( end - p ) < psk_len )
1624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1625
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001626 memcpy( p, psk, psk_len );
1627 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628
1629 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1630
1631 return( 0 );
1632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001636/*
1637 * SSLv3.0 MAC functions
1638 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001639#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001640static void ssl_mac( mbedtls_md_context_t *md_ctx,
1641 const unsigned char *secret,
1642 const unsigned char *buf, size_t len,
1643 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001644 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001645{
1646 unsigned char header[11];
1647 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001648 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1650 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001651
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001652 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001654 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001655 else
Paul Bakker68884e32013-01-07 18:20:04 +01001656 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
1658 memcpy( header, ctr, 8 );
1659 header[ 8] = (unsigned char) type;
1660 header[ 9] = (unsigned char)( len >> 8 );
1661 header[10] = (unsigned char)( len );
1662
Paul Bakker68884e32013-01-07 18:20:04 +01001663 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_md_starts( md_ctx );
1665 mbedtls_md_update( md_ctx, secret, md_size );
1666 mbedtls_md_update( md_ctx, padding, padlen );
1667 mbedtls_md_update( md_ctx, header, 11 );
1668 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001669 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Paul Bakker68884e32013-01-07 18:20:04 +01001671 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 mbedtls_md_starts( md_ctx );
1673 mbedtls_md_update( md_ctx, secret, md_size );
1674 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001675 mbedtls_md_update( md_ctx, out, md_size );
1676 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001679
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001680/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001681 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001682#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001683 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1684 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1685 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1686/* This function makes sure every byte in the memory region is accessed
1687 * (in ascending addresses order) */
1688static void ssl_read_memory( unsigned char *p, size_t len )
1689{
1690 unsigned char acc = 0;
1691 volatile unsigned char force;
1692
1693 for( ; len != 0; p++, len-- )
1694 acc ^= *p;
1695
1696 force = acc;
1697 (void) force;
1698}
1699#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1700
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001701/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001703 */
Hanno Becker3307b532017-12-27 21:37:21 +00001704
Hanno Beckera5a2b082019-05-15 14:03:01 +01001705#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001706/* This functions transforms a DTLS plaintext fragment and a record content
1707 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001708 *
1709 * struct {
1710 * opaque content[DTLSPlaintext.length];
1711 * ContentType real_type;
1712 * uint8 zeros[length_of_padding];
1713 * } DTLSInnerPlaintext;
1714 *
1715 * Input:
1716 * - `content`: The beginning of the buffer holding the
1717 * plaintext to be wrapped.
1718 * - `*content_size`: The length of the plaintext in Bytes.
1719 * - `max_len`: The number of Bytes available starting from
1720 * `content`. This must be `>= *content_size`.
1721 * - `rec_type`: The desired record content type.
1722 *
1723 * Output:
1724 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1725 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1726 *
1727 * Returns:
1728 * - `0` on success.
1729 * - A negative error code if `max_len` didn't offer enough space
1730 * for the expansion.
1731 */
1732static int ssl_cid_build_inner_plaintext( unsigned char *content,
1733 size_t *content_size,
1734 size_t remaining,
1735 uint8_t rec_type )
1736{
1737 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001738 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1739 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1740 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001741
1742 /* Write real content type */
1743 if( remaining == 0 )
1744 return( -1 );
1745 content[ len ] = rec_type;
1746 len++;
1747 remaining--;
1748
1749 if( remaining < pad )
1750 return( -1 );
1751 memset( content + len, 0, pad );
1752 len += pad;
1753 remaining -= pad;
1754
1755 *content_size = len;
1756 return( 0 );
1757}
1758
Hanno Becker7dc25772019-05-20 15:08:01 +01001759/* This function parses a DTLSInnerPlaintext structure.
1760 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001761static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1762 size_t *content_size,
1763 uint8_t *rec_type )
1764{
1765 size_t remaining = *content_size;
1766
1767 /* Determine length of padding by skipping zeroes from the back. */
1768 do
1769 {
1770 if( remaining == 0 )
1771 return( -1 );
1772 remaining--;
1773 } while( content[ remaining ] == 0 );
1774
1775 *content_size = remaining;
1776 *rec_type = content[ remaining ];
1777
1778 return( 0 );
1779}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001780#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001781
Hanno Becker99abf512019-05-20 14:50:53 +01001782/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001783 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001784static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001785 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001786 mbedtls_record *rec )
1787{
Hanno Becker99abf512019-05-20 14:50:53 +01001788 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001789 *
1790 * additional_data = seq_num + TLSCompressed.type +
1791 * TLSCompressed.version + TLSCompressed.length;
1792 *
Hanno Becker99abf512019-05-20 14:50:53 +01001793 * For the CID extension, this is extended as follows
1794 * (quoting draft-ietf-tls-dtls-connection-id-05,
1795 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001796 *
1797 * additional_data = seq_num + DTLSPlaintext.type +
1798 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001799 * cid +
1800 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001801 * length_of_DTLSInnerPlaintext;
1802 */
1803
Hanno Becker3307b532017-12-27 21:37:21 +00001804 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1805 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001806 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001807
Hanno Beckera5a2b082019-05-15 14:03:01 +01001808#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001809 if( rec->cid_len != 0 )
1810 {
1811 memcpy( add_data + 11, rec->cid, rec->cid_len );
1812 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1813 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1814 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1815 *add_data_len = 13 + 1 + rec->cid_len;
1816 }
1817 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001818#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001819 {
1820 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1821 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1822 *add_data_len = 13;
1823 }
Hanno Becker3307b532017-12-27 21:37:21 +00001824}
1825
Hanno Becker611a83b2018-01-03 14:27:32 +00001826int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1827 mbedtls_ssl_transform *transform,
1828 mbedtls_record *rec,
1829 int (*f_rng)(void *, unsigned char *, size_t),
1830 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001833 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001834 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001835 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001836 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001837 size_t post_avail;
1838
1839 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001840#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001841 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001842 ((void) ssl);
1843#endif
1844
1845 /* The PRNG is used for dynamic IV generation that's used
1846 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1847#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1848 ( defined(MBEDTLS_AES_C) || \
1849 defined(MBEDTLS_ARIA_C) || \
1850 defined(MBEDTLS_CAMELLIA_C) ) && \
1851 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1852 ((void) f_rng);
1853 ((void) p_rng);
1854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Hanno Becker3307b532017-12-27 21:37:21 +00001858 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859 {
Hanno Becker3307b532017-12-27 21:37:21 +00001860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1861 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1862 }
Hanno Becker505089d2019-05-01 09:45:57 +01001863 if( rec == NULL
1864 || rec->buf == NULL
1865 || rec->buf_len < rec->data_offset
1866 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001867#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001868 || rec->cid_len != 0
1869#endif
1870 )
Hanno Becker3307b532017-12-27 21:37:21 +00001871 {
1872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001874 }
1875
Hanno Becker3307b532017-12-27 21:37:21 +00001876 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001877 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001879 data, rec->data_len );
1880
1881 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1882
1883 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1884 {
1885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1886 (unsigned) rec->data_len,
1887 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1889 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001890
Hanno Beckera5a2b082019-05-15 14:03:01 +01001891#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001892 /*
1893 * Add CID information
1894 */
1895 rec->cid_len = transform->out_cid_len;
1896 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1897 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001898
1899 if( rec->cid_len != 0 )
1900 {
1901 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001902 * Wrap plaintext into DTLSInnerPlaintext structure.
1903 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001904 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001905 * Note that this changes `rec->data_len`, and hence
1906 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001907 */
1908 if( ssl_cid_build_inner_plaintext( data,
1909 &rec->data_len,
1910 post_avail,
1911 rec->type ) != 0 )
1912 {
1913 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1914 }
1915
1916 rec->type = MBEDTLS_SSL_MSG_CID;
1917 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001918#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001919
Hanno Becker92c930f2019-04-29 17:31:37 +01001920 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1921
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001923 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001925#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 if( mode == MBEDTLS_MODE_STREAM ||
1927 ( mode == MBEDTLS_MODE_CBC
1928#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001929 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001930#endif
1931 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 {
Hanno Becker3307b532017-12-27 21:37:21 +00001933 if( post_avail < transform->maclen )
1934 {
1935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1936 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1937 }
1938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001940 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001941 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001942 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001943 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1944 data, rec->data_len, rec->ctr, rec->type, mac );
1945 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001946 }
1947 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1950 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001951 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001952 {
Hanno Becker992b6872017-11-09 18:57:39 +00001953 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1954
Hanno Beckere83efe62019-04-29 13:52:53 +01001955 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001956
Hanno Becker3307b532017-12-27 21:37:21 +00001957 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001958 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001959 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1960 data, rec->data_len );
1961 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1962 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1963
1964 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001965 }
1966 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001967#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001971 }
1972
Hanno Becker3307b532017-12-27 21:37:21 +00001973 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1974 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001975
Hanno Becker3307b532017-12-27 21:37:21 +00001976 rec->data_len += transform->maclen;
1977 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001978 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001979 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001980#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001982 /*
1983 * Encrypt
1984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1986 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001988 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001989 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001991 "including %d bytes of padding",
1992 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
Hanno Becker3307b532017-12-27 21:37:21 +00001994 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1995 transform->iv_enc, transform->ivlen,
1996 data, rec->data_len,
1997 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002000 return( ret );
2001 }
2002
Hanno Becker3307b532017-12-27 21:37:21 +00002003 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
Paul Bakker68884e32013-01-07 18:20:04 +01002009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002011
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002012#if defined(MBEDTLS_GCM_C) || \
2013 defined(MBEDTLS_CCM_C) || \
2014 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002016 mode == MBEDTLS_MODE_CCM ||
2017 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002018 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002019 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002021 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 /* Check that there's space for both the authentication tag
2024 * and the explicit IV before and after the record content. */
2025 if( post_avail < transform->taglen ||
2026 rec->data_offset < explicit_iv_len )
2027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2029 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2030 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002031
Paul Bakker68884e32013-01-07 18:20:04 +01002032 /*
2033 * Generate IV
2034 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002035 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2036 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002037 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002038 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002039 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2040 explicit_iv_len );
2041 /* Prefix record content with explicit IV. */
2042 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 }
2044 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2045 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002046 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002047 unsigned char i;
2048
2049 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2050
2051 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002052 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002053 }
2054 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002055 {
2056 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002059 }
2060
Hanno Beckere83efe62019-04-29 13:52:53 +01002061 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002062
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2064 iv, transform->ivlen );
2065 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002066 data - explicit_iv_len, explicit_iv_len );
2067 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002068 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002070 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002071 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002072
Paul Bakker68884e32013-01-07 18:20:04 +01002073 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002074 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002075 */
Hanno Becker3307b532017-12-27 21:37:21 +00002076
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002077 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002078 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002079 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002080 data, rec->data_len, /* source */
2081 data, &rec->data_len, /* destination */
2082 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002085 return( ret );
2086 }
2087
Hanno Becker3307b532017-12-27 21:37:21 +00002088 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2089 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002090
Hanno Becker3307b532017-12-27 21:37:21 +00002091 rec->data_len += transform->taglen + explicit_iv_len;
2092 rec->data_offset -= explicit_iv_len;
2093 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002094 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2098#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002099 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002102 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002103 size_t padlen, i;
2104 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002105
Hanno Becker3307b532017-12-27 21:37:21 +00002106 /* Currently we're always using minimal padding
2107 * (up to 255 bytes would be allowed). */
2108 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2109 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 padlen = 0;
2111
Hanno Becker3307b532017-12-27 21:37:21 +00002112 /* Check there's enough space in the buffer for the padding. */
2113 if( post_avail < padlen + 1 )
2114 {
2115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2116 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2117 }
2118
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002120 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Hanno Becker3307b532017-12-27 21:37:21 +00002122 rec->data_len += padlen + 1;
2123 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002127 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2128 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002129 */
Hanno Becker3307b532017-12-27 21:37:21 +00002130 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 {
Hanno Becker3307b532017-12-27 21:37:21 +00002132 if( f_rng == NULL )
2133 {
2134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2136 }
2137
2138 if( rec->data_offset < transform->ivlen )
2139 {
2140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2142 }
2143
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144 /*
2145 * Generate IV
2146 */
Hanno Becker3307b532017-12-27 21:37:21 +00002147 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002148 if( ret != 0 )
2149 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150
Hanno Becker3307b532017-12-27 21:37:21 +00002151 memcpy( data - transform->ivlen, transform->iv_enc,
2152 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002159 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002160 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Hanno Becker3307b532017-12-27 21:37:21 +00002162 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2163 transform->iv_enc,
2164 transform->ivlen,
2165 data, rec->data_len,
2166 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002169 return( ret );
2170 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002171
Hanno Becker3307b532017-12-27 21:37:21 +00002172 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002176 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002179 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002180 {
2181 /*
2182 * Save IV in SSL3 and TLS1
2183 */
Hanno Becker3307b532017-12-27 21:37:21 +00002184 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2185 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 }
Hanno Becker3307b532017-12-27 21:37:21 +00002187 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002188#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002189 {
2190 data -= transform->ivlen;
2191 rec->data_offset -= transform->ivlen;
2192 rec->data_len += transform->ivlen;
2193 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002196 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002197 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002198 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2199
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002200 /*
2201 * MAC(MAC_write_key, seq_num +
2202 * TLSCipherText.type +
2203 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002204 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002205 * IV + // except for TLS 1.0
2206 * ENC(content + padding + padding_length));
2207 */
Hanno Becker3307b532017-12-27 21:37:21 +00002208
2209 if( post_avail < transform->maclen)
2210 {
2211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2212 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2213 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002214
Hanno Beckere83efe62019-04-29 13:52:53 +01002215 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002218 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002219 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002220
Hanno Becker3307b532017-12-27 21:37:21 +00002221 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002222 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002223 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2224 data, rec->data_len );
2225 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2226 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002227
Hanno Becker3307b532017-12-27 21:37:21 +00002228 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002229
Hanno Becker3307b532017-12-27 21:37:21 +00002230 rec->data_len += transform->maclen;
2231 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002232 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002238 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002244 /* Make extra sure authentication was performed, exactly once */
2245 if( auth_done != 1 )
2246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249 }
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
2253 return( 0 );
2254}
2255
Hanno Becker611a83b2018-01-03 14:27:32 +00002256int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2257 mbedtls_ssl_transform *transform,
2258 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002260 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002262 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002263#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002264 size_t padlen = 0, correct = 1;
2265#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002266 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002267 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002268 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002269
Hanno Becker611a83b2018-01-03 14:27:32 +00002270#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002271 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002272 ((void) ssl);
2273#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002276 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002277 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2280 }
2281 if( rec == NULL ||
2282 rec->buf == NULL ||
2283 rec->buf_len < rec->data_offset ||
2284 rec->buf_len - rec->data_offset < rec->data_len )
2285 {
2286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002288 }
2289
Hanno Becker4c6876b2017-12-27 21:28:58 +00002290 data = rec->buf + rec->data_offset;
2291 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Hanno Beckera5a2b082019-05-15 14:03:01 +01002293#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002294 /*
2295 * Match record's CID with incoming CID.
2296 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002297 if( rec->cid_len != transform->in_cid_len ||
2298 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2299 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002300 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002301 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002302#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2305 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002306 {
2307 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002308 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2309 transform->iv_dec,
2310 transform->ivlen,
2311 data, rec->data_len,
2312 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002315 return( ret );
2316 }
2317
Hanno Becker4c6876b2017-12-27 21:28:58 +00002318 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 }
Paul Bakker68884e32013-01-07 18:20:04 +01002324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002326#if defined(MBEDTLS_GCM_C) || \
2327 defined(MBEDTLS_CCM_C) || \
2328 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330 mode == MBEDTLS_MODE_CCM ||
2331 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002332 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002333 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002335
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002336 /*
2337 * Compute and update sizes
2338 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002342 "+ taglen (%d)", rec->data_len,
2343 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002345 }
Paul Bakker68884e32013-01-07 18:20:04 +01002346
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 /*
2348 * Prepare IV
2349 */
2350 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2351 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002352 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002354 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 }
2357 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2358 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002359 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 unsigned char i;
2361
2362 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2363
2364 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002365 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002366 }
2367 else
2368 {
2369 /* Reminder if we ever add an AEAD mode with a different size */
2370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2372 }
2373
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 data += explicit_iv_len;
2375 rec->data_offset += explicit_iv_len;
2376 rec->data_len -= explicit_iv_len + transform->taglen;
2377
Hanno Beckere83efe62019-04-29 13:52:53 +01002378 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002380 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002381
2382 memcpy( transform->iv_dec + transform->fixed_ivlen,
2383 data - explicit_iv_len, explicit_iv_len );
2384
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002385 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002387 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002388
Paul Bakker68884e32013-01-07 18:20:04 +01002389
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002390 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002391 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002392 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002393 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2394 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002395 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002396 data, rec->data_len,
2397 data, &olen,
2398 data + rec->data_len,
2399 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2404 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002405
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002406 return( ret );
2407 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002408 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002409
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002414 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2418#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002419 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002422 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 /*
Paul Bakker45829992013-01-03 14:52:21 +01002425 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002428 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2429 {
2430 /* The ciphertext is prefixed with the CBC IV. */
2431 minlen += transform->ivlen;
2432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#endif
Paul Bakker45829992013-01-03 14:52:21 +01002434
Hanno Becker4c6876b2017-12-27 21:28:58 +00002435 /* Size considerations:
2436 *
2437 * - The CBC cipher text must not be empty and hence
2438 * at least of size transform->ivlen.
2439 *
2440 * Together with the potential IV-prefix, this explains
2441 * the first of the two checks below.
2442 *
2443 * - The record must contain a MAC, either in plain or
2444 * encrypted, depending on whether Encrypt-then-MAC
2445 * is used or not.
2446 * - If it is, the message contains the IV-prefix,
2447 * the CBC ciphertext, and the MAC.
2448 * - If it is not, the padded plaintext, and hence
2449 * the CBC ciphertext, has at least length maclen + 1
2450 * because there is at least the padding length byte.
2451 *
2452 * As the CBC ciphertext is not empty, both cases give the
2453 * lower bound minlen + maclen + 1 on the record size, which
2454 * we test for in the second check below.
2455 */
2456 if( rec->data_len < minlen + transform->ivlen ||
2457 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002460 "+ 1 ) ( + expl IV )", rec->data_len,
2461 transform->ivlen,
2462 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002464 }
2465
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002466 /*
2467 * Authenticate before decrypt if enabled
2468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002470 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 {
Hanno Becker992b6872017-11-09 18:57:39 +00002472 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475
Hanno Becker4c6876b2017-12-27 21:28:58 +00002476 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2477 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Hanno Beckere83efe62019-04-29 13:52:53 +01002479 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002480
Hanno Beckere83efe62019-04-29 13:52:53 +01002481 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2482 add_data_len );
2483 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2484 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002485 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2486 data, rec->data_len );
2487 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2488 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002489
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2491 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002492 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002493 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2496 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002501 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
2505 /*
2506 * Check length sanity
2507 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002508 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002513 }
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002516 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002517 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002518 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002520 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002521 /* This is safe because data_len >= minlen + maclen + 1 initially,
2522 * and at this point we have at most subtracted maclen (note that
2523 * minlen == transform->ivlen here). */
2524 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525
Hanno Becker4c6876b2017-12-27 21:28:58 +00002526 data += transform->ivlen;
2527 rec->data_offset += transform->ivlen;
2528 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002531
Hanno Becker4c6876b2017-12-27 21:28:58 +00002532 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2533 transform->iv_dec, transform->ivlen,
2534 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002537 return( ret );
2538 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002539
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002544 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002548 {
2549 /*
2550 * Save IV in SSL3 and TLS1
2551 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2553 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002555#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 /* Safe since data_len >= minlen + maclen + 1, so after having
2558 * subtracted at most minlen and maclen up to this point,
2559 * data_len > 0. */
2560 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002561
Hanno Becker4c6876b2017-12-27 21:28:58 +00002562 if( auth_done == 1 )
2563 {
2564 correct *= ( rec->data_len >= padlen + 1 );
2565 padlen *= ( rec->data_len >= padlen + 1 );
2566 }
2567 else
Paul Bakker45829992013-01-03 14:52:21 +01002568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002570 if( rec->data_len < transform->maclen + padlen + 1 )
2571 {
2572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2573 rec->data_len,
2574 transform->maclen,
2575 padlen + 1 ) );
2576 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002577#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578
2579 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2580 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 padlen++;
2584
2585 /* Regardless of the validity of the padding,
2586 * we have data_len >= padlen here. */
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_DEBUG_ALL)
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002595 "should be no more than %d",
2596 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002597#endif
Paul Bakker45829992013-01-03 14:52:21 +01002598 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600 }
2601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2603#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2604 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002605 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002607 /* The padding check involves a series of up to 256
2608 * consecutive memory reads at the end of the record
2609 * plaintext buffer. In order to hide the length and
2610 * validity of the padding, always perform exactly
2611 * `min(256,plaintext_len)` reads (but take into account
2612 * only the last `padlen` bytes for the padding check). */
2613 size_t pad_count = 0;
2614 size_t real_count = 0;
2615 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002616
Hanno Becker4c6876b2017-12-27 21:28:58 +00002617 /* Index of first padding byte; it has been ensured above
2618 * that the subtraction is safe. */
2619 size_t const padding_idx = rec->data_len - padlen;
2620 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2621 size_t const start_idx = rec->data_len - num_checks;
2622 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002623
Hanno Becker4c6876b2017-12-27 21:28:58 +00002624 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002625 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002626 real_count |= ( idx >= padding_idx );
2627 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002628 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002629 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002632 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002634#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2639 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002643 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002644
Hanno Becker4c6876b2017-12-27 21:28:58 +00002645 /* If the padding was found to be invalid, padlen == 0
2646 * and the subtraction is safe. If the padding was found valid,
2647 * padlen hasn't been changed and the previous assertion
2648 * data_len >= padlen still holds. */
2649 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002651 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002653 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002659#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002661 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002662#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002665 * Authenticate if not done yet.
2666 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002668#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002669 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002670 {
Hanno Becker992b6872017-11-09 18:57:39 +00002671 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002672
Hanno Becker4c6876b2017-12-27 21:28:58 +00002673 /* If the initial value of padlen was such that
2674 * data_len < maclen + padlen + 1, then padlen
2675 * got reset to 1, and the initial check
2676 * data_len >= minlen + maclen + 1
2677 * guarantees that at this point we still
2678 * have at least data_len >= maclen.
2679 *
2680 * If the initial value of padlen was such that
2681 * data_len >= maclen + padlen + 1, then we have
2682 * subtracted either padlen + 1 (if the padding was correct)
2683 * or 0 (if the padding was incorrect) since then,
2684 * hence data_len >= maclen in any case.
2685 */
2686 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Hanno Beckere83efe62019-04-29 13:52:53 +01002688 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002691 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002692 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 ssl_mac( &transform->md_ctx_dec,
2694 transform->mac_dec,
2695 data, rec->data_len,
2696 rec->ctr, rec->type,
2697 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002698 }
2699 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2701#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2702 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002703 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002704 {
2705 /*
2706 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002707 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002708 *
2709 * Known timing attacks:
2710 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2711 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002712 * To compensate for different timings for the MAC calculation
2713 * depending on how much padding was removed (which is determined
2714 * by padlen), process extra_run more blocks through the hash
2715 * function.
2716 *
2717 * The formula in the paper is
2718 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2719 * where L1 is the size of the header plus the decrypted message
2720 * plus CBC padding and L2 is the size of the header plus the
2721 * decrypted message. This is for an underlying hash function
2722 * with 64-byte blocks.
2723 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2724 * correctly. We round down instead of up, so -56 is the correct
2725 * value for our calculations instead of -55.
2726 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002727 * Repeat the formula rather than defining a block_size variable.
2728 * This avoids requiring division by a variable at runtime
2729 * (which would be marginally less efficient and would require
2730 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002731 */
2732 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002733 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002734
2735 /*
2736 * The next two sizes are the minimum and maximum values of
2737 * in_msglen over all padlen values.
2738 *
2739 * They're independent of padlen, since we previously did
2740 * in_msglen -= padlen.
2741 *
2742 * Note that max_len + maclen is never more than the buffer
2743 * length, as we previously did in_msglen -= maclen too.
2744 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002745 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002746 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2747
Hanno Becker4c6876b2017-12-27 21:28:58 +00002748 memset( tmp, 0, sizeof( tmp ) );
2749
2750 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002751 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002752#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2753 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002754 case MBEDTLS_MD_MD5:
2755 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002757 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002758 extra_run =
2759 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2760 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 break;
2762#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002763#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002764 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002765 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002766 extra_run =
2767 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2768 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 break;
2770#endif
2771 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2774 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002775
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002776 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002777
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2779 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002780 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2781 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002782 /* Make sure we access everything even when padlen > 0. This
2783 * makes the synchronisation requirements for just-in-time
2784 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002785 ssl_read_memory( data + rec->data_len, padlen );
2786 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002787
2788 /* Call mbedtls_md_process at least once due to cache attacks
2789 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002790 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002791 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002792
Hanno Becker4c6876b2017-12-27 21:28:58 +00002793 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002794
2795 /* Make sure we access all the memory that could contain the MAC,
2796 * before we check it in the next code block. This makes the
2797 * synchronisation requirements for just-in-time Prime+Probe
2798 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002799 ssl_read_memory( data + min_len,
2800 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002801 }
2802 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2804 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002810#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2812 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002813#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Hanno Becker4c6876b2017-12-27 21:28:58 +00002815 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2816 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_DEBUG_ALL)
2819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002820#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 correct = 0;
2822 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002823 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002824 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002825
2826 /*
2827 * Finally check the correct flag
2828 */
2829 if( correct == 0 )
2830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002831#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002832
2833 /* Make extra sure authentication was performed, exactly once */
2834 if( auth_done != 1 )
2835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Hanno Beckera5a2b082019-05-15 14:03:01 +01002840#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002841 if( rec->cid_len != 0 )
2842 {
2843 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2844 &rec->type );
2845 if( ret != 0 )
2846 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2847 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002848#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
2852 return( 0 );
2853}
2854
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002855#undef MAC_NONE
2856#undef MAC_PLAINTEXT
2857#undef MAC_CIPHERTEXT
2858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002860/*
2861 * Compression/decompression functions
2862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002864{
2865 int ret;
2866 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002867 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002868 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002869 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002872
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002873 if( len_pre == 0 )
2874 return( 0 );
2875
Paul Bakker2770fbd2012-07-03 13:30:23 +00002876 memcpy( msg_pre, ssl->out_msg, len_pre );
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002879 ssl->out_msglen ) );
2880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002882 ssl->out_msg, ssl->out_msglen );
2883
Paul Bakker48916f92012-09-16 19:57:18 +00002884 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2885 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2886 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002887 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890 if( ret != Z_OK )
2891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2893 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002894 }
2895
Angus Grattond8213d02016-05-25 20:56:48 +10002896 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002897 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900 ssl->out_msglen ) );
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 ssl->out_msg, ssl->out_msglen );
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002906
2907 return( 0 );
2908}
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911{
2912 int ret;
2913 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002914 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002916 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002920 if( len_pre == 0 )
2921 return( 0 );
2922
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 memcpy( msg_pre, ssl->in_msg, len_pre );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926 ssl->in_msglen ) );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->in_msg, ssl->in_msglen );
2930
Paul Bakker48916f92012-09-16 19:57:18 +00002931 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2932 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2933 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002934 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002935 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936
Paul Bakker48916f92012-09-16 19:57:18 +00002937 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938 if( ret != Z_OK )
2939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2941 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002942 }
2943
Angus Grattond8213d02016-05-25 20:56:48 +10002944 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002945 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948 ssl->in_msglen ) );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951 ssl->in_msg, ssl->in_msglen );
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002954
2955 return( 0 );
2956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2960static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_SSL_PROTO_DTLS)
2963static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002964{
2965 /* If renegotiation is not enforced, retransmit until we would reach max
2966 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002967 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002968 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002969 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002970 unsigned char doublings = 1;
2971
2972 while( ratio != 0 )
2973 {
2974 ++doublings;
2975 ratio >>= 1;
2976 }
2977
2978 if( ++ssl->renego_records_seen > doublings )
2979 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981 return( 0 );
2982 }
2983 }
2984
2985 return( ssl_write_hello_request( ssl ) );
2986}
2987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002989
Paul Bakker5121ce52009-01-03 21:22:43 +00002990/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002991 * Fill the input message buffer by appending data to it.
2992 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002993 *
2994 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2995 * available (from this read and/or a previous one). Otherwise, an error code
2996 * is returned (possibly EOF or WANT_READ).
2997 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002998 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2999 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3000 * since we always read a whole datagram at once.
3001 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003002 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003003 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003006{
Paul Bakker23986e52011-04-24 08:57:21 +00003007 int ret;
3008 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003012 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003015 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 }
3018
Angus Grattond8213d02016-05-25 20:56:48 +10003019 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003023 }
3024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003028 uint32_t timeout;
3029
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003030 /* Just to be sure */
3031 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3032 {
3033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3034 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3036 }
3037
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003038 /*
3039 * The point is, we need to always read a full datagram at once, so we
3040 * sometimes read more then requested, and handle the additional data.
3041 * It could be the rest of the current record (while fetching the
3042 * header) and/or some other records in the same datagram.
3043 */
3044
3045 /*
3046 * Move to the next record in the already read datagram if applicable
3047 */
3048 if( ssl->next_record_offset != 0 )
3049 {
3050 if( ssl->in_left < ssl->next_record_offset )
3051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003054 }
3055
3056 ssl->in_left -= ssl->next_record_offset;
3057
3058 if( ssl->in_left != 0 )
3059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003061 ssl->next_record_offset ) );
3062 memmove( ssl->in_hdr,
3063 ssl->in_hdr + ssl->next_record_offset,
3064 ssl->in_left );
3065 }
3066
3067 ssl->next_record_offset = 0;
3068 }
3069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003071 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003072
3073 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003075 */
3076 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003079 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003080 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003081
3082 /*
3083 * A record can't be split accross datagrams. If we need to read but
3084 * are not at the beginning of a new record, the caller did something
3085 * wrong.
3086 */
3087 if( ssl->in_left != 0 )
3088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003091 }
3092
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003093 /*
3094 * Don't even try to read if time's out already.
3095 * This avoids by-passing the timer when repeatedly receiving messages
3096 * that will end up being dropped.
3097 */
3098 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003099 {
3100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003101 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003102 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003103 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003104 {
Angus Grattond8213d02016-05-25 20:56:48 +10003105 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003108 timeout = ssl->handshake->retransmit_timeout;
3109 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003110 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003114 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003115 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3116 timeout );
3117 else
3118 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003121
3122 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003124 }
3125
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003126 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003132 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003133 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003136 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003137 }
3138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 return( ret );
3143 }
3144
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003145 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003148 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003150 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003151 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003154 return( ret );
3155 }
3156
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003157 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003160 }
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 if( ret < 0 )
3163 return( ret );
3164
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003165 ssl->in_left = ret;
3166 }
3167 else
3168#endif
3169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003171 ssl->in_left, nb_want ) );
3172
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003173 while( ssl->in_left < nb_want )
3174 {
3175 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003176
3177 if( ssl_check_timer( ssl ) != 0 )
3178 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3179 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003180 {
3181 if( ssl->f_recv_timeout != NULL )
3182 {
3183 ret = ssl->f_recv_timeout( ssl->p_bio,
3184 ssl->in_hdr + ssl->in_left, len,
3185 ssl->conf->read_timeout );
3186 }
3187 else
3188 {
3189 ret = ssl->f_recv( ssl->p_bio,
3190 ssl->in_hdr + ssl->in_left, len );
3191 }
3192 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003195 ssl->in_left, nb_want ) );
3196 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003197
3198 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003200
3201 if( ret < 0 )
3202 return( ret );
3203
mohammad160352aecb92018-03-28 23:41:40 -07003204 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003205 {
Darryl Green11999bb2018-03-13 15:22:58 +00003206 MBEDTLS_SSL_DEBUG_MSG( 1,
3207 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003208 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3210 }
3211
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003212 ssl->in_left += ret;
3213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 }
3215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
3218 return( 0 );
3219}
3220
3221/*
3222 * Flush any data not yet written
3223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003225{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003226 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003227 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003231 if( ssl->f_send == NULL )
3232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003234 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003236 }
3237
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003238 /* Avoid incrementing counter if data is flushed */
3239 if( ssl->out_left == 0 )
3240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003242 return( 0 );
3243 }
3244
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 while( ssl->out_left > 0 )
3246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003248 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
Hanno Becker2b1e3542018-08-06 11:19:13 +01003250 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003251 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
3255 if( ret <= 0 )
3256 return( ret );
3257
mohammad160352aecb92018-03-28 23:41:40 -07003258 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003259 {
Darryl Green11999bb2018-03-13 15:22:58 +00003260 MBEDTLS_SSL_DEBUG_MSG( 1,
3261 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003262 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3264 }
3265
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 ssl->out_left -= ret;
3267 }
3268
Hanno Becker2b1e3542018-08-06 11:19:13 +01003269#if defined(MBEDTLS_SSL_PROTO_DTLS)
3270 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003271 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003272 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003273 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003274 else
3275#endif
3276 {
3277 ssl->out_hdr = ssl->out_buf + 8;
3278 }
3279 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
3283 return( 0 );
3284}
3285
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003286/*
3287 * Functions to handle the DTLS retransmission state machine
3288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003290/*
3291 * Append current handshake message to current outgoing flight
3292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3297 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3298 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003299
3300 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003301 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003302 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003306 }
3307
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003308 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003312 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003313 }
3314
3315 /* Copy current handshake message with headers */
3316 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3317 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003318 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319 msg->next = NULL;
3320
3321 /* Append to the current flight */
3322 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003323 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324 else
3325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327 while( cur->next != NULL )
3328 cur = cur->next;
3329 cur->next = msg;
3330 }
3331
Hanno Becker3b235902018-08-06 09:54:53 +01003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 return( 0 );
3334}
3335
3336/*
3337 * Free the current flight of handshake messages
3338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_ssl_flight_item *cur = flight;
3342 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343
3344 while( cur != NULL )
3345 {
3346 next = cur->next;
3347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 mbedtls_free( cur->p );
3349 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350
3351 cur = next;
3352 }
3353}
3354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3356static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003357#endif
3358
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003360 * Swap transform_out and out_ctr with the alternative ones
3361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003365 unsigned char tmp_out_ctr[8];
3366
3367 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003370 return;
3371 }
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003374
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003375 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003376 tmp_transform = ssl->transform_out;
3377 ssl->transform_out = ssl->handshake->alt_transform_out;
3378 ssl->handshake->alt_transform_out = tmp_transform;
3379
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003380 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003381 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3382 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003383 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003384
3385 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003386 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3389 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3394 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003395 }
3396 }
3397#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398}
3399
3400/*
3401 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003402 */
3403int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3404{
3405 int ret = 0;
3406
3407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3408
3409 ret = mbedtls_ssl_flight_transmit( ssl );
3410
3411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3412
3413 return( ret );
3414}
3415
3416/*
3417 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003418 *
3419 * Need to remember the current message in case flush_output returns
3420 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003421 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003422 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003423int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003424{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003425 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431
3432 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003433 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003434 ssl_swap_epochs( ssl );
3435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003437 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003438
3439 while( ssl->handshake->cur_msg != NULL )
3440 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003441 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003442 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003443
Hanno Beckere1dcb032018-08-17 16:47:58 +01003444 int const is_finished =
3445 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3446 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3447
Hanno Becker04da1892018-08-14 13:22:10 +01003448 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3449 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3450
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003451 /* Swap epochs before sending Finished: we can't do it after
3452 * sending ChangeCipherSpec, in case write returns WANT_READ.
3453 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003454 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003455 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003457 ssl_swap_epochs( ssl );
3458 }
3459
Hanno Becker67bc7c32018-08-06 11:33:50 +01003460 ret = ssl_get_remaining_payload_in_datagram( ssl );
3461 if( ret < 0 )
3462 return( ret );
3463 max_frag_len = (size_t) ret;
3464
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003465 /* CCS is copied as is, while HS messages may need fragmentation */
3466 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3467 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003468 if( max_frag_len == 0 )
3469 {
3470 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3471 return( ret );
3472
3473 continue;
3474 }
3475
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003476 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003477 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003478 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003479
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003480 /* Update position inside current message */
3481 ssl->handshake->cur_msg_p += cur->len;
3482 }
3483 else
3484 {
3485 const unsigned char * const p = ssl->handshake->cur_msg_p;
3486 const size_t hs_len = cur->len - 12;
3487 const size_t frag_off = p - ( cur->p + 12 );
3488 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003489 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003490
Hanno Beckere1dcb032018-08-17 16:47:58 +01003491 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003492 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003493 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003494 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003495
Hanno Becker67bc7c32018-08-06 11:33:50 +01003496 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3497 return( ret );
3498
3499 continue;
3500 }
3501 max_hs_frag_len = max_frag_len - 12;
3502
3503 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3504 max_hs_frag_len : rem_len;
3505
3506 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003507 {
3508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003509 (unsigned) cur_hs_frag_len,
3510 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003511 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003512
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003513 /* Messages are stored with handshake headers as if not fragmented,
3514 * copy beginning of headers then fill fragmentation fields.
3515 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3516 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003517
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003518 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3519 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3520 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3521
Hanno Becker67bc7c32018-08-06 11:33:50 +01003522 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3523 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3524 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003525
3526 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3527
Hanno Becker3f7b9732018-08-28 09:53:25 +01003528 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003529 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3530 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003531 ssl->out_msgtype = cur->type;
3532
3533 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003534 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003535 }
3536
3537 /* If done with the current message move to the next one if any */
3538 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3539 {
3540 if( cur->next != NULL )
3541 {
3542 ssl->handshake->cur_msg = cur->next;
3543 ssl->handshake->cur_msg_p = cur->next->p + 12;
3544 }
3545 else
3546 {
3547 ssl->handshake->cur_msg = NULL;
3548 ssl->handshake->cur_msg_p = NULL;
3549 }
3550 }
3551
3552 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003553 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003556 return( ret );
3557 }
3558 }
3559
Hanno Becker67bc7c32018-08-06 11:33:50 +01003560 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3561 return( ret );
3562
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003563 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3565 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003566 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003569 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3570 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003571
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573
3574 return( 0 );
3575}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003576
3577/*
3578 * To be called when the last message of an incoming flight is received.
3579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003581{
3582 /* We won't need to resend that one any more */
3583 ssl_flight_free( ssl->handshake->flight );
3584 ssl->handshake->flight = NULL;
3585 ssl->handshake->cur_msg = NULL;
3586
3587 /* The next incoming flight will start with this msg_seq */
3588 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3589
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003590 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003591 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003592
Hanno Becker0271f962018-08-16 13:23:47 +01003593 /* Clear future message buffering structure. */
3594 ssl_buffering_free( ssl );
3595
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003596 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003597 ssl_set_timer( ssl, 0 );
3598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3600 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003603 }
3604 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003607
3608/*
3609 * To be called when the last message of an outgoing flight is send.
3610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003612{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003613 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003614 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3617 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003620 }
3621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625
Paul Bakker5121ce52009-01-03 21:22:43 +00003626/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003627 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003629
3630/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003631 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003632 *
3633 * - fill in handshake headers
3634 * - update handshake checksum
3635 * - DTLS: save message for resending
3636 * - then pass to the record layer
3637 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003638 * DTLS: except for HelloRequest, messages are only queued, and will only be
3639 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003640 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003641 * Inputs:
3642 * - ssl->out_msglen: 4 + actual handshake message len
3643 * (4 is the size of handshake headers for TLS)
3644 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3645 * - ssl->out_msg + 4: the handshake message body
3646 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003647 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003648 * - ssl->out_msglen: the length of the record contents
3649 * (including handshake headers but excluding record headers)
3650 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003651 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003652int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003653{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003654 int ret;
3655 const size_t hs_len = ssl->out_msglen - 4;
3656 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3659
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003660 /*
3661 * Sanity checks
3662 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003663 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003664 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3665 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003666 /* In SSLv3, the client might send a NoCertificate alert. */
3667#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3668 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3669 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3670 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3671#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3672 {
3673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3675 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003677
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003678 /* Whenever we send anything different from a
3679 * HelloRequest we should be in a handshake - double check. */
3680 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3681 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003682 ssl->handshake == NULL )
3683 {
3684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003689 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003690 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003695 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003697
Hanno Beckerb50a2532018-08-06 11:52:54 +01003698 /* Double-check that we did not exceed the bounds
3699 * of the outgoing record buffer.
3700 * This should never fail as the various message
3701 * writing functions must obey the bounds of the
3702 * outgoing record buffer, but better be safe.
3703 *
3704 * Note: We deliberately do not check for the MTU or MFL here.
3705 */
3706 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3707 {
3708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3709 "size %u, maximum %u",
3710 (unsigned) ssl->out_msglen,
3711 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3713 }
3714
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003715 /*
3716 * Fill handshake headers
3717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003719 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003720 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3721 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3722 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003724 /*
3725 * DTLS has additional fields in the Handshake layer,
3726 * between the length field and the actual payload:
3727 * uint16 message_seq;
3728 * uint24 fragment_offset;
3729 * uint24 fragment_length;
3730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003733 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003734 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003735 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003736 {
3737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3738 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003739 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003740 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3742 }
3743
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003745 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003746
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003747 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003749 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003750 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3751 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3752 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003753 }
3754 else
3755 {
3756 ssl->out_msg[4] = 0;
3757 ssl->out_msg[5] = 0;
3758 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003759
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003760 /* Handshake hashes are computed without fragmentation,
3761 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003762 memset( ssl->out_msg + 6, 0x00, 3 );
3763 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003766
Hanno Becker0207e532018-08-28 10:28:28 +01003767 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3769 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 }
3771
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003772 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003775 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3776 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777 {
3778 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781 return( ret );
3782 }
3783 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003784 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003785#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003786 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003787 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003788 {
3789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3790 return( ret );
3791 }
3792 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003793
3794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3795
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003796 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003797}
3798
3799/*
3800 * Record layer functions
3801 */
3802
3803/*
3804 * Write current record.
3805 *
3806 * Uses:
3807 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3808 * - ssl->out_msglen: length of the record content (excl headers)
3809 * - ssl->out_msg: record content
3810 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003812{
3813 int ret, done = 0;
3814 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003815 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003816
3817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003820 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003822 {
3823 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003826 return( ret );
3827 }
3828
3829 len = ssl->out_msglen;
3830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3834 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 ret = mbedtls_ssl_hw_record_write( ssl );
3839 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3842 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003843 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003844
3845 if( ret == 0 )
3846 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003847 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003849 if( !done )
3850 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003851 unsigned i;
3852 size_t protected_record_size;
3853
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003854 /* Skip writing the record content type to after the encryption,
3855 * as it may change when using the CID extension. */
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003858 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003859
Hanno Becker19859472018-08-06 09:40:20 +01003860 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003861 ssl->out_len[0] = (unsigned char)( len >> 8 );
3862 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003863
Paul Bakker48916f92012-09-16 19:57:18 +00003864 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003865 {
Hanno Becker3307b532017-12-27 21:37:21 +00003866 mbedtls_record rec;
3867
3868 rec.buf = ssl->out_iv;
3869 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3870 ( ssl->out_iv - ssl->out_buf );
3871 rec.data_len = ssl->out_msglen;
3872 rec.data_offset = ssl->out_msg - rec.buf;
3873
3874 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3875 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3876 ssl->conf->transport, rec.ver );
3877 rec.type = ssl->out_msgtype;
3878
Hanno Beckera5a2b082019-05-15 14:03:01 +01003879#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003880 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003881 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003882#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003883
Hanno Becker611a83b2018-01-03 14:27:32 +00003884 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003885 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003888 return( ret );
3889 }
3890
Hanno Becker3307b532017-12-27 21:37:21 +00003891 if( rec.data_offset != 0 )
3892 {
3893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3895 }
3896
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003897 /* Update the record content type and CID. */
3898 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003899#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003900 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003901#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003902 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003903 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3904 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003905 }
3906
Hanno Becker43395762019-05-03 14:46:38 +01003907 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003908
3909#if defined(MBEDTLS_SSL_PROTO_DTLS)
3910 /* In case of DTLS, double-check that we don't exceed
3911 * the remaining space in the datagram. */
3912 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3913 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003914 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003915 if( ret < 0 )
3916 return( ret );
3917
3918 if( protected_record_size > (size_t) ret )
3919 {
3920 /* Should never happen */
3921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3922 }
3923 }
3924#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003925
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003926 /* Now write the potentially updated record content type. */
3927 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003930 "version = [%d:%d], msglen = %d",
3931 ssl->out_hdr[0], ssl->out_hdr[1],
3932 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003935 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003936
3937 ssl->out_left += protected_record_size;
3938 ssl->out_hdr += protected_record_size;
3939 ssl_update_out_pointers( ssl, ssl->transform_out );
3940
Hanno Becker04484622018-08-06 09:49:38 +01003941 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3942 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3943 break;
3944
3945 /* The loop goes to its end iff the counter is wrapping */
3946 if( i == ssl_ep_len( ssl ) )
3947 {
3948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3949 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003951 }
3952
Hanno Becker67bc7c32018-08-06 11:33:50 +01003953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003954 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3955 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003956 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003957 size_t remaining;
3958 ret = ssl_get_remaining_payload_in_datagram( ssl );
3959 if( ret < 0 )
3960 {
3961 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3962 ret );
3963 return( ret );
3964 }
3965
3966 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003967 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003968 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003969 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003970 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003971 else
3972 {
Hanno Becker513815a2018-08-20 11:56:09 +01003973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003974 }
3975 }
3976#endif /* MBEDTLS_SSL_PROTO_DTLS */
3977
3978 if( ( flush == SSL_FORCE_FLUSH ) &&
3979 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 return( ret );
3983 }
3984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
3987 return( 0 );
3988}
3989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003991
3992static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3993{
3994 if( ssl->in_msglen < ssl->in_hslen ||
3995 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3996 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3997 {
3998 return( 1 );
3999 }
4000 return( 0 );
4001}
Hanno Becker44650b72018-08-16 12:51:11 +01004002
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004003static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004004{
4005 return( ( ssl->in_msg[9] << 16 ) |
4006 ( ssl->in_msg[10] << 8 ) |
4007 ssl->in_msg[11] );
4008}
4009
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004010static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004011{
4012 return( ( ssl->in_msg[6] << 16 ) |
4013 ( ssl->in_msg[7] << 8 ) |
4014 ssl->in_msg[8] );
4015}
4016
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004017static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004018{
4019 uint32_t msg_len, frag_off, frag_len;
4020
4021 msg_len = ssl_get_hs_total_len( ssl );
4022 frag_off = ssl_get_hs_frag_off( ssl );
4023 frag_len = ssl_get_hs_frag_len( ssl );
4024
4025 if( frag_off > msg_len )
4026 return( -1 );
4027
4028 if( frag_len > msg_len - frag_off )
4029 return( -1 );
4030
4031 if( frag_len + 12 > ssl->in_msglen )
4032 return( -1 );
4033
4034 return( 0 );
4035}
4036
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004037/*
4038 * Mark bits in bitmask (used for DTLS HS reassembly)
4039 */
4040static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4041{
4042 unsigned int start_bits, end_bits;
4043
4044 start_bits = 8 - ( offset % 8 );
4045 if( start_bits != 8 )
4046 {
4047 size_t first_byte_idx = offset / 8;
4048
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004049 /* Special case */
4050 if( len <= start_bits )
4051 {
4052 for( ; len != 0; len-- )
4053 mask[first_byte_idx] |= 1 << ( start_bits - len );
4054
4055 /* Avoid potential issues with offset or len becoming invalid */
4056 return;
4057 }
4058
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004059 offset += start_bits; /* Now offset % 8 == 0 */
4060 len -= start_bits;
4061
4062 for( ; start_bits != 0; start_bits-- )
4063 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4064 }
4065
4066 end_bits = len % 8;
4067 if( end_bits != 0 )
4068 {
4069 size_t last_byte_idx = ( offset + len ) / 8;
4070
4071 len -= end_bits; /* Now len % 8 == 0 */
4072
4073 for( ; end_bits != 0; end_bits-- )
4074 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4075 }
4076
4077 memset( mask + offset / 8, 0xFF, len / 8 );
4078}
4079
4080/*
4081 * Check that bitmask is full
4082 */
4083static int ssl_bitmask_check( unsigned char *mask, size_t len )
4084{
4085 size_t i;
4086
4087 for( i = 0; i < len / 8; i++ )
4088 if( mask[i] != 0xFF )
4089 return( -1 );
4090
4091 for( i = 0; i < len % 8; i++ )
4092 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4093 return( -1 );
4094
4095 return( 0 );
4096}
4097
Hanno Becker56e205e2018-08-16 09:06:12 +01004098/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004099static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004100 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004101{
Hanno Becker56e205e2018-08-16 09:06:12 +01004102 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004103
Hanno Becker56e205e2018-08-16 09:06:12 +01004104 alloc_len = 12; /* Handshake header */
4105 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004106
Hanno Beckerd07df862018-08-16 09:14:58 +01004107 if( add_bitmap )
4108 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004109
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004110 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004111}
Hanno Becker56e205e2018-08-16 09:06:12 +01004112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004114
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004115static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004116{
4117 return( ( ssl->in_msg[1] << 16 ) |
4118 ( ssl->in_msg[2] << 8 ) |
4119 ssl->in_msg[3] );
4120}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004121
Simon Butcher99000142016-10-13 17:21:01 +01004122int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004127 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004129 }
4130
Hanno Becker12555c62018-08-16 12:47:53 +01004131 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004134 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004135 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004139 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004140 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004141 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004142
Hanno Becker44650b72018-08-16 12:51:11 +01004143 if( ssl_check_hs_header( ssl ) != 0 )
4144 {
4145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4146 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4147 }
4148
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004149 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004150 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4151 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4152 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4153 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004154 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004155 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4156 {
4157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4158 recv_msg_seq,
4159 ssl->handshake->in_msg_seq ) );
4160 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4161 }
4162
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004163 /* Retransmit only on last message from previous flight, to avoid
4164 * too many retransmissions.
4165 * Besides, No sane server ever retransmits HelloVerifyRequest */
4166 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004170 "message_seq = %d, start_of_flight = %d",
4171 recv_msg_seq,
4172 ssl->handshake->in_flight_start_seq ) );
4173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004177 return( ret );
4178 }
4179 }
4180 else
4181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004183 "message_seq = %d, expected = %d",
4184 recv_msg_seq,
4185 ssl->handshake->in_msg_seq ) );
4186 }
4187
Hanno Becker90333da2017-10-10 11:27:13 +01004188 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004189 }
4190 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004191
Hanno Becker6d97ef52018-08-16 13:09:04 +01004192 /* Message reassembly is handled alongside buffering of future
4193 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004194 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004195 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004196 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004199 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004200 }
4201 }
4202 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004204 /* With TLS we don't handle fragmentation (for now) */
4205 if( ssl->in_msglen < ssl->in_hslen )
4206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4208 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004209 }
4210
Simon Butcher99000142016-10-13 17:21:01 +01004211 return( 0 );
4212}
4213
4214void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4215{
Hanno Becker0271f962018-08-16 13:23:47 +01004216 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004217
Hanno Becker0271f962018-08-16 13:23:47 +01004218 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004219 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004220 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004221 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004222
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004223 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004225 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004226 ssl->handshake != NULL )
4227 {
Hanno Becker0271f962018-08-16 13:23:47 +01004228 unsigned offset;
4229 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004230
Hanno Becker0271f962018-08-16 13:23:47 +01004231 /* Increment handshake sequence number */
4232 hs->in_msg_seq++;
4233
4234 /*
4235 * Clear up handshake buffering and reassembly structure.
4236 */
4237
4238 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004239 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004240
4241 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004242 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4243 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004244 offset++, hs_buf++ )
4245 {
4246 *hs_buf = *(hs_buf + 1);
4247 }
4248
4249 /* Create a fresh last entry */
4250 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004251 }
4252#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004253}
4254
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004255/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004256 * DTLS anti-replay: RFC 6347 4.1.2.6
4257 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004258 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4259 * Bit n is set iff record number in_window_top - n has been seen.
4260 *
4261 * Usually, in_window_top is the last record number seen and the lsb of
4262 * in_window is set. The only exception is the initial state (record number 0
4263 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4266static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004267{
4268 ssl->in_window_top = 0;
4269 ssl->in_window = 0;
4270}
4271
4272static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4273{
4274 return( ( (uint64_t) buf[0] << 40 ) |
4275 ( (uint64_t) buf[1] << 32 ) |
4276 ( (uint64_t) buf[2] << 24 ) |
4277 ( (uint64_t) buf[3] << 16 ) |
4278 ( (uint64_t) buf[4] << 8 ) |
4279 ( (uint64_t) buf[5] ) );
4280}
4281
4282/*
4283 * Return 0 if sequence number is acceptable, -1 otherwise
4284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004286{
4287 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4288 uint64_t bit;
4289
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004290 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004291 return( 0 );
4292
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 if( rec_seqnum > ssl->in_window_top )
4294 return( 0 );
4295
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004296 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004297
4298 if( bit >= 64 )
4299 return( -1 );
4300
4301 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4302 return( -1 );
4303
4304 return( 0 );
4305}
4306
4307/*
4308 * Update replay window on new validated record
4309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004311{
4312 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4313
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004314 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004315 return;
4316
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004317 if( rec_seqnum > ssl->in_window_top )
4318 {
4319 /* Update window_top and the contents of the window */
4320 uint64_t shift = rec_seqnum - ssl->in_window_top;
4321
4322 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004323 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004324 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004325 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004327 ssl->in_window |= 1;
4328 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004329
4330 ssl->in_window_top = rec_seqnum;
4331 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004332 else
4333 {
4334 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004335 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004336
4337 if( bit < 64 ) /* Always true, but be extra sure */
4338 ssl->in_window |= (uint64_t) 1 << bit;
4339 }
4340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004342
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004343#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004344/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004345static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4346
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004347/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004348 * Without any SSL context, check if a datagram looks like a ClientHello with
4349 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004350 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004351 *
4352 * - if cookie is valid, return 0
4353 * - if ClientHello looks superficially valid but cookie is not,
4354 * fill obuf and set olen, then
4355 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4356 * - otherwise return a specific error code
4357 */
4358static int ssl_check_dtls_clihlo_cookie(
4359 mbedtls_ssl_cookie_write_t *f_cookie_write,
4360 mbedtls_ssl_cookie_check_t *f_cookie_check,
4361 void *p_cookie,
4362 const unsigned char *cli_id, size_t cli_id_len,
4363 const unsigned char *in, size_t in_len,
4364 unsigned char *obuf, size_t buf_len, size_t *olen )
4365{
4366 size_t sid_len, cookie_len;
4367 unsigned char *p;
4368
4369 if( f_cookie_write == NULL || f_cookie_check == NULL )
4370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4371
4372 /*
4373 * Structure of ClientHello with record and handshake headers,
4374 * and expected values. We don't need to check a lot, more checks will be
4375 * done when actually parsing the ClientHello - skipping those checks
4376 * avoids code duplication and does not make cookie forging any easier.
4377 *
4378 * 0-0 ContentType type; copied, must be handshake
4379 * 1-2 ProtocolVersion version; copied
4380 * 3-4 uint16 epoch; copied, must be 0
4381 * 5-10 uint48 sequence_number; copied
4382 * 11-12 uint16 length; (ignored)
4383 *
4384 * 13-13 HandshakeType msg_type; (ignored)
4385 * 14-16 uint24 length; (ignored)
4386 * 17-18 uint16 message_seq; copied
4387 * 19-21 uint24 fragment_offset; copied, must be 0
4388 * 22-24 uint24 fragment_length; (ignored)
4389 *
4390 * 25-26 ProtocolVersion client_version; (ignored)
4391 * 27-58 Random random; (ignored)
4392 * 59-xx SessionID session_id; 1 byte len + sid_len content
4393 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4394 * ...
4395 *
4396 * Minimum length is 61 bytes.
4397 */
4398 if( in_len < 61 ||
4399 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4400 in[3] != 0 || in[4] != 0 ||
4401 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4402 {
4403 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4404 }
4405
4406 sid_len = in[59];
4407 if( sid_len > in_len - 61 )
4408 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4409
4410 cookie_len = in[60 + sid_len];
4411 if( cookie_len > in_len - 60 )
4412 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4413
4414 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4415 cli_id, cli_id_len ) == 0 )
4416 {
4417 /* Valid cookie */
4418 return( 0 );
4419 }
4420
4421 /*
4422 * If we get here, we've got an invalid cookie, let's prepare HVR.
4423 *
4424 * 0-0 ContentType type; copied
4425 * 1-2 ProtocolVersion version; copied
4426 * 3-4 uint16 epoch; copied
4427 * 5-10 uint48 sequence_number; copied
4428 * 11-12 uint16 length; olen - 13
4429 *
4430 * 13-13 HandshakeType msg_type; hello_verify_request
4431 * 14-16 uint24 length; olen - 25
4432 * 17-18 uint16 message_seq; copied
4433 * 19-21 uint24 fragment_offset; copied
4434 * 22-24 uint24 fragment_length; olen - 25
4435 *
4436 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4437 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4438 *
4439 * Minimum length is 28.
4440 */
4441 if( buf_len < 28 )
4442 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4443
4444 /* Copy most fields and adapt others */
4445 memcpy( obuf, in, 25 );
4446 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4447 obuf[25] = 0xfe;
4448 obuf[26] = 0xff;
4449
4450 /* Generate and write actual cookie */
4451 p = obuf + 28;
4452 if( f_cookie_write( p_cookie,
4453 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4454 {
4455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4456 }
4457
4458 *olen = p - obuf;
4459
4460 /* Go back and fill length fields */
4461 obuf[27] = (unsigned char)( *olen - 28 );
4462
4463 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4464 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4465 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4466
4467 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4468 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4469
4470 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4471}
4472
4473/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004474 * Handle possible client reconnect with the same UDP quadruplet
4475 * (RFC 6347 Section 4.2.8).
4476 *
4477 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4478 * that looks like a ClientHello.
4479 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004480 * - if the input looks like a ClientHello without cookies,
4481 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004482 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004483 * - if the input looks like a ClientHello with a valid cookie,
4484 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004485 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004486 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004487 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004488 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004489 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4490 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004491 */
4492static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4493{
4494 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004495 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004496
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004497 ret = ssl_check_dtls_clihlo_cookie(
4498 ssl->conf->f_cookie_write,
4499 ssl->conf->f_cookie_check,
4500 ssl->conf->p_cookie,
4501 ssl->cli_id, ssl->cli_id_len,
4502 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004503 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004504
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004505 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4506
4507 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004509 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 * If the error is permanent we'll catch it later,
4511 * if it's not, then hopefully it'll work next time. */
4512 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4513
4514 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 }
4516
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004518 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004519 /* Got a valid cookie, partially reset context */
4520 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4521 {
4522 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4523 return( ret );
4524 }
4525
4526 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004527 }
4528
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004529 return( ret );
4530}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004531#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532
Hanno Becker46483f12019-05-03 13:25:54 +01004533static int ssl_check_record_type( uint8_t record_type )
4534{
4535 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4536 record_type != MBEDTLS_SSL_MSG_ALERT &&
4537 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4538 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4539 {
4540 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4541 }
4542
4543 return( 0 );
4544}
4545
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004546/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004547 * ContentType type;
4548 * ProtocolVersion version;
4549 * uint16 epoch; // DTLS only
4550 * uint48 sequence_number; // DTLS only
4551 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004552 *
4553 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004554 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004555 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4556 *
4557 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004558 * 1. proceed with the record if this function returns 0
4559 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4560 * 3. return CLIENT_RECONNECT if this function return that value
4561 * 4. drop the whole datagram if this function returns anything else.
4562 * Point 2 is needed when the peer is resending, and we have already received
4563 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004567 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004568 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004569
Hanno Becker8b09b732019-05-08 12:03:28 +01004570 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004571
Paul Bakker5121ce52009-01-03 21:22:43 +00004572 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004573 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004574
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004575 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004576#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004577 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4578 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4579 ssl->conf->cid_len != 0 )
4580 {
4581 /* Shift pointers to account for record header including CID
4582 * struct {
4583 * ContentType special_type = tls12_cid;
4584 * ProtocolVersion version;
4585 * uint16 epoch;
4586 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004587 * opaque cid[cid_length]; // Additional field compared to
4588 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004589 * uint16 length;
4590 * opaque enc_content[DTLSCiphertext.length];
4591 * } DTLSCiphertext;
4592 */
4593
4594 /* So far, we only support static CID lengths
4595 * fixed in the configuration. */
4596 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4597 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4598 }
4599 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004600#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004601 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004604
4605#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004606 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4607 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004608 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4609#endif /* MBEDTLS_SSL_PROTO_DTLS */
4610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4611 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004614 }
4615
4616 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004617 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4620 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 }
4622
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004623 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 }
4628
Hanno Becker8b09b732019-05-08 12:03:28 +01004629 /* Now that the total length of the record header is known, ensure
4630 * that the current datagram is large enough to hold it.
4631 * This would fail, for example, if we received a datagram of
4632 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4633 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4634 if( ret != 0 )
4635 {
4636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4637 return( ret );
4638 }
4639 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4640
4641 /* Parse and validate record length
4642 * This must happen after the CID parsing because
4643 * its position in the record header depends on
4644 * the presence of a CID. */
4645
4646 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004647 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004648 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4651 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004652 }
4653
Hanno Becker8b09b732019-05-08 12:03:28 +01004654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004655 "version = [%d:%d], msglen = %d",
4656 ssl->in_msgtype,
4657 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004658
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004659 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004660 * DTLS-related tests.
4661 * Check epoch before checking length constraint because
4662 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4663 * message gets duplicated before the corresponding Finished message,
4664 * the second ChangeCipherSpec should be discarded because it belongs
4665 * to an old epoch, but not because its length is shorter than
4666 * the minimum record length for packets using the new record transform.
4667 * Note that these two kinds of failures are handled differently,
4668 * as an unexpected record is silently skipped but an invalid
4669 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004670 */
4671#if defined(MBEDTLS_SSL_PROTO_DTLS)
4672 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4673 {
4674 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4675
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004676 /* Check epoch (and sequence number) with DTLS */
4677 if( rec_epoch != ssl->in_epoch )
4678 {
4679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4680 "expected %d, received %d",
4681 ssl->in_epoch, rec_epoch ) );
4682
4683#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4684 /*
4685 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4686 * access the first byte of record content (handshake type), as we
4687 * have an active transform (possibly iv_len != 0), so use the
4688 * fact that the record header len is 13 instead.
4689 */
4690 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4691 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4692 rec_epoch == 0 &&
4693 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4694 ssl->in_left > 13 &&
4695 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4696 {
4697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4698 "from the same port" ) );
4699 return( ssl_handle_possible_reconnect( ssl ) );
4700 }
4701 else
4702#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004703 {
4704 /* Consider buffering the record. */
4705 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4706 {
4707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4708 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4709 }
4710
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004711 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004712 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004713 }
4714
4715#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4716 /* Replay detection only works for the current epoch */
4717 if( rec_epoch == ssl->in_epoch &&
4718 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4719 {
4720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4721 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4722 }
4723#endif
4724 }
4725#endif /* MBEDTLS_SSL_PROTO_DTLS */
4726
Hanno Becker52c6dc62017-05-26 16:07:36 +01004727
4728 /* Check length against bounds of the current transform and version */
4729 if( ssl->transform_in == NULL )
4730 {
4731 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004732 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004733 {
4734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4735 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4736 }
4737 }
4738 else
4739 {
4740 if( ssl->in_msglen < ssl->transform_in->minlen )
4741 {
4742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4743 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4744 }
4745
4746#if defined(MBEDTLS_SSL_PROTO_SSL3)
4747 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004748 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4751 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4752 }
4753#endif
4754#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4755 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4756 /*
4757 * TLS encrypted messages can have up to 256 bytes of padding
4758 */
4759 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4760 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004761 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004762 {
4763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4764 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4765 }
4766#endif
4767 }
4768
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004769 return( 0 );
4770}
Paul Bakker5121ce52009-01-03 21:22:43 +00004771
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004772/*
4773 * If applicable, decrypt (and decompress) record content
4774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004776{
4777 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004780 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4783 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 ret = mbedtls_ssl_hw_record_read( ssl );
4788 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4791 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004792 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004793
4794 if( ret == 0 )
4795 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004798 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004799 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004800 mbedtls_record rec;
4801
4802 rec.buf = ssl->in_iv;
4803 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4804 - ( ssl->in_iv - ssl->in_buf );
4805 rec.data_len = ssl->in_msglen;
4806 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004807#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004808 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004809 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004810#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004811
4812 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4813 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4814 ssl->conf->transport, rec.ver );
4815 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004816 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4817 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004820
Hanno Beckera5a2b082019-05-15 14:03:01 +01004821#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004822 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4823 ssl->conf->ignore_unexpected_cid
4824 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4825 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004827 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004828 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004829#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004830
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 return( ret );
4832 }
4833
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004834 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004835 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004836 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4837 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004839
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004840 /* The record content type may change during decryption,
4841 * so re-read it. */
4842 ssl->in_msgtype = rec.type;
4843 /* Also update the input buffer, because unfortunately
4844 * the server-side ssl_parse_client_hello() reparses the
4845 * record header when receiving a ClientHello initiating
4846 * a renegotiation. */
4847 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004848 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004849 ssl->in_msglen = rec.data_len;
4850 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4851 ssl->in_len[1] = (unsigned char)( rec.data_len );
4852
Paul Bakker5121ce52009-01-03 21:22:43 +00004853 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4854 ssl->in_msg, ssl->in_msglen );
4855
Hanno Beckera5a2b082019-05-15 14:03:01 +01004856#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004857 /* We have already checked the record content type
4858 * in ssl_parse_record_header(), failing or silently
4859 * dropping the record in the case of an unknown type.
4860 *
4861 * Since with the use of CIDs, the record content type
4862 * might change during decryption, re-check the record
4863 * content type, but treat a failure as fatal this time. */
4864 if( ssl_check_record_type( ssl->in_msgtype ) )
4865 {
4866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4867 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4868 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004869#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870
Angus Grattond8213d02016-05-25 20:56:48 +10004871 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4874 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004875 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004876 else if( ssl->in_msglen == 0 )
4877 {
4878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4879 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4880 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4881 {
4882 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4884 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4885 }
4886#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4887
4888 ssl->nb_zero++;
4889
4890 /*
4891 * Three or more empty messages may be a DoS attack
4892 * (excessive CPU consumption).
4893 */
4894 if( ssl->nb_zero > 3 )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004897 "messages, possible DoS attack" ) );
4898 /* Treat the records as if they were not properly authenticated,
4899 * thereby failing the connection if we see more than allowed
4900 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004901 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4902 }
4903 }
4904 else
4905 ssl->nb_zero = 0;
4906
4907#if defined(MBEDTLS_SSL_PROTO_DTLS)
4908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4909 {
4910 ; /* in_ctr read from peer, not maintained internally */
4911 }
4912 else
4913#endif
4914 {
4915 unsigned i;
4916 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4917 if( ++ssl->in_ctr[i - 1] != 0 )
4918 break;
4919
4920 /* The loop goes to its end iff the counter is wrapping */
4921 if( i == ssl_ep_len( ssl ) )
4922 {
4923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4924 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4925 }
4926 }
4927
Paul Bakker5121ce52009-01-03 21:22:43 +00004928 }
4929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004931 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004933 {
4934 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004937 return( ret );
4938 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004939 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004946 }
4947#endif
4948
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004949 return( 0 );
4950}
4951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004953
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004954/*
4955 * Read a record.
4956 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004957 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4958 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4959 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004960 */
Hanno Becker1097b342018-08-15 14:09:41 +01004961
4962/* Helper functions for mbedtls_ssl_read_record(). */
4963static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004964static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4965static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004966
Hanno Becker327c93b2018-08-15 13:56:18 +01004967int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004968 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004969{
4970 int ret;
4971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004974 if( ssl->keep_current_message == 0 )
4975 {
4976 do {
Simon Butcher99000142016-10-13 17:21:01 +01004977
Hanno Becker26994592018-08-15 14:14:59 +01004978 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004979 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004980 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004981
Hanno Beckere74d5562018-08-15 14:26:08 +01004982 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004983 {
Hanno Becker40f50842018-08-15 14:48:01 +01004984#if defined(MBEDTLS_SSL_PROTO_DTLS)
4985 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004986
Hanno Becker40f50842018-08-15 14:48:01 +01004987 /* We only check for buffered messages if the
4988 * current datagram is fully consumed. */
4989 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004990 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004991 {
Hanno Becker40f50842018-08-15 14:48:01 +01004992 if( ssl_load_buffered_message( ssl ) == 0 )
4993 have_buffered = 1;
4994 }
4995
4996 if( have_buffered == 0 )
4997#endif /* MBEDTLS_SSL_PROTO_DTLS */
4998 {
4999 ret = ssl_get_next_record( ssl );
5000 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5001 continue;
5002
5003 if( ret != 0 )
5004 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005005 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005006 return( ret );
5007 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005008 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005009 }
5010
5011 ret = mbedtls_ssl_handle_message_type( ssl );
5012
Hanno Becker40f50842018-08-15 14:48:01 +01005013#if defined(MBEDTLS_SSL_PROTO_DTLS)
5014 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5015 {
5016 /* Buffer future message */
5017 ret = ssl_buffer_message( ssl );
5018 if( ret != 0 )
5019 return( ret );
5020
5021 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5022 }
5023#endif /* MBEDTLS_SSL_PROTO_DTLS */
5024
Hanno Becker90333da2017-10-10 11:27:13 +01005025 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5026 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005027
5028 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005029 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005030 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005031 return( ret );
5032 }
5033
Hanno Becker327c93b2018-08-15 13:56:18 +01005034 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005035 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036 {
5037 mbedtls_ssl_update_handshake_status( ssl );
5038 }
Simon Butcher99000142016-10-13 17:21:01 +01005039 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005040 else
Simon Butcher99000142016-10-13 17:21:01 +01005041 {
Hanno Becker02f59072018-08-15 14:00:24 +01005042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005043 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005044 }
5045
5046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5047
5048 return( 0 );
5049}
5050
Hanno Becker40f50842018-08-15 14:48:01 +01005051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005052static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005053{
Hanno Becker40f50842018-08-15 14:48:01 +01005054 if( ssl->in_left > ssl->next_record_offset )
5055 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005056
Hanno Becker40f50842018-08-15 14:48:01 +01005057 return( 0 );
5058}
5059
5060static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5061{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005062 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005063 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005064 int ret = 0;
5065
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005066 if( hs == NULL )
5067 return( -1 );
5068
Hanno Beckere00ae372018-08-20 09:39:42 +01005069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5070
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005071 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5072 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5073 {
5074 /* Check if we have seen a ChangeCipherSpec before.
5075 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005076 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005077 {
5078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5079 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005080 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005081 }
5082
Hanno Becker39b8bc92018-08-28 17:17:13 +01005083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005084 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5085 ssl->in_msglen = 1;
5086 ssl->in_msg[0] = 1;
5087
5088 /* As long as they are equal, the exact value doesn't matter. */
5089 ssl->in_left = 0;
5090 ssl->next_record_offset = 0;
5091
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005092 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 goto exit;
5094 }
Hanno Becker37f95322018-08-16 13:55:32 +01005095
Hanno Beckerb8f50142018-08-28 10:01:34 +01005096#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005097 /* Debug only */
5098 {
5099 unsigned offset;
5100 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5101 {
5102 hs_buf = &hs->buffering.hs[offset];
5103 if( hs_buf->is_valid == 1 )
5104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5106 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005107 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005108 }
5109 }
5110 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005111#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005112
5113 /* Check if we have buffered and/or fully reassembled the
5114 * next handshake message. */
5115 hs_buf = &hs->buffering.hs[0];
5116 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5117 {
5118 /* Synthesize a record containing the buffered HS message. */
5119 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5120 ( hs_buf->data[2] << 8 ) |
5121 hs_buf->data[3];
5122
5123 /* Double-check that we haven't accidentally buffered
5124 * a message that doesn't fit into the input buffer. */
5125 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5126 {
5127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5129 }
5130
5131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5132 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5133 hs_buf->data, msg_len + 12 );
5134
5135 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5136 ssl->in_hslen = msg_len + 12;
5137 ssl->in_msglen = msg_len + 12;
5138 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5139
5140 ret = 0;
5141 goto exit;
5142 }
5143 else
5144 {
5145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5146 hs->in_msg_seq ) );
5147 }
5148
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005149 ret = -1;
5150
5151exit:
5152
5153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5154 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005155}
5156
Hanno Beckera02b0b42018-08-21 17:20:27 +01005157static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5158 size_t desired )
5159{
5160 int offset;
5161 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5163 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005164
Hanno Becker01315ea2018-08-21 17:22:17 +01005165 /* Get rid of future records epoch first, if such exist. */
5166 ssl_free_buffered_record( ssl );
5167
5168 /* Check if we have enough space available now. */
5169 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5170 hs->buffering.total_bytes_buffered ) )
5171 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005173 return( 0 );
5174 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005175
Hanno Becker4f432ad2018-08-28 10:02:32 +01005176 /* We don't have enough space to buffer the next expected handshake
5177 * message. Remove buffers used for future messages to gain space,
5178 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005179 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5180 offset >= 0; offset-- )
5181 {
5182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5183 offset ) );
5184
Hanno Beckerb309b922018-08-23 13:18:05 +01005185 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005186
5187 /* Check if we have enough space available now. */
5188 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5189 hs->buffering.total_bytes_buffered ) )
5190 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005192 return( 0 );
5193 }
5194 }
5195
5196 return( -1 );
5197}
5198
Hanno Becker40f50842018-08-15 14:48:01 +01005199static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5200{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005201 int ret = 0;
5202 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5203
5204 if( hs == NULL )
5205 return( 0 );
5206
5207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5208
5209 switch( ssl->in_msgtype )
5210 {
5211 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005213
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005214 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005215 break;
5216
5217 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005218 {
5219 unsigned recv_msg_seq_offset;
5220 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5221 mbedtls_ssl_hs_buffer *hs_buf;
5222 size_t msg_len = ssl->in_hslen - 12;
5223
5224 /* We should never receive an old handshake
5225 * message - double-check nonetheless. */
5226 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5227 {
5228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5230 }
5231
5232 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5233 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5234 {
5235 /* Silently ignore -- message too far in the future */
5236 MBEDTLS_SSL_DEBUG_MSG( 2,
5237 ( "Ignore future HS message with sequence number %u, "
5238 "buffering window %u - %u",
5239 recv_msg_seq, ssl->handshake->in_msg_seq,
5240 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5241
5242 goto exit;
5243 }
5244
5245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5246 recv_msg_seq, recv_msg_seq_offset ) );
5247
5248 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5249
5250 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005251 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005252 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005253 size_t reassembly_buf_sz;
5254
Hanno Becker37f95322018-08-16 13:55:32 +01005255 hs_buf->is_fragmented =
5256 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5257
5258 /* We copy the message back into the input buffer
5259 * after reassembly, so check that it's not too large.
5260 * This is an implementation-specific limitation
5261 * and not one from the standard, hence it is not
5262 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005263 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005264 {
5265 /* Ignore message */
5266 goto exit;
5267 }
5268
Hanno Beckere0b150f2018-08-21 15:51:03 +01005269 /* Check if we have enough space to buffer the message. */
5270 if( hs->buffering.total_bytes_buffered >
5271 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5272 {
5273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5275 }
5276
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005277 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5278 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005279
5280 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5281 hs->buffering.total_bytes_buffered ) )
5282 {
5283 if( recv_msg_seq_offset > 0 )
5284 {
5285 /* If we can't buffer a future message because
5286 * of space limitations -- ignore. */
5287 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",
5288 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5289 (unsigned) hs->buffering.total_bytes_buffered ) );
5290 goto exit;
5291 }
Hanno Beckere1801392018-08-21 16:51:05 +01005292 else
5293 {
5294 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",
5295 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5296 (unsigned) hs->buffering.total_bytes_buffered ) );
5297 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005298
Hanno Beckera02b0b42018-08-21 17:20:27 +01005299 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005300 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005301 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",
5302 (unsigned) msg_len,
5303 (unsigned) reassembly_buf_sz,
5304 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005305 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005306 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5307 goto exit;
5308 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005309 }
5310
5311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5312 msg_len ) );
5313
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005314 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5315 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005316 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005317 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005318 goto exit;
5319 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005320 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005321
5322 /* Prepare final header: copy msg_type, length and message_seq,
5323 * then add standardised fragment_offset and fragment_length */
5324 memcpy( hs_buf->data, ssl->in_msg, 6 );
5325 memset( hs_buf->data + 6, 0, 3 );
5326 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5327
5328 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005329
5330 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005331 }
5332 else
5333 {
5334 /* Make sure msg_type and length are consistent */
5335 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5336 {
5337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5338 /* Ignore */
5339 goto exit;
5340 }
5341 }
5342
Hanno Becker4422bbb2018-08-20 09:40:19 +01005343 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005344 {
5345 size_t frag_len, frag_off;
5346 unsigned char * const msg = hs_buf->data + 12;
5347
5348 /*
5349 * Check and copy current fragment
5350 */
5351
5352 /* Validation of header fields already done in
5353 * mbedtls_ssl_prepare_handshake_record(). */
5354 frag_off = ssl_get_hs_frag_off( ssl );
5355 frag_len = ssl_get_hs_frag_len( ssl );
5356
5357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5358 frag_off, frag_len ) );
5359 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5360
5361 if( hs_buf->is_fragmented )
5362 {
5363 unsigned char * const bitmask = msg + msg_len;
5364 ssl_bitmask_set( bitmask, frag_off, frag_len );
5365 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5366 msg_len ) == 0 );
5367 }
5368 else
5369 {
5370 hs_buf->is_complete = 1;
5371 }
5372
5373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5374 hs_buf->is_complete ? "" : "not yet " ) );
5375 }
5376
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005377 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005378 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005379
5380 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005381 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005382 break;
5383 }
5384
5385exit:
5386
5387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5388 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005389}
5390#endif /* MBEDTLS_SSL_PROTO_DTLS */
5391
Hanno Becker1097b342018-08-15 14:09:41 +01005392static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005393{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005394 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005395 * Consume last content-layer message and potentially
5396 * update in_msglen which keeps track of the contents'
5397 * consumption state.
5398 *
5399 * (1) Handshake messages:
5400 * Remove last handshake message, move content
5401 * and adapt in_msglen.
5402 *
5403 * (2) Alert messages:
5404 * Consume whole record content, in_msglen = 0.
5405 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005406 * (3) Change cipher spec:
5407 * Consume whole record content, in_msglen = 0.
5408 *
5409 * (4) Application data:
5410 * Don't do anything - the record layer provides
5411 * the application data as a stream transport
5412 * and consumes through mbedtls_ssl_read only.
5413 *
5414 */
5415
5416 /* Case (1): Handshake messages */
5417 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005418 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005419 /* Hard assertion to be sure that no application data
5420 * is in flight, as corrupting ssl->in_msglen during
5421 * ssl->in_offt != NULL is fatal. */
5422 if( ssl->in_offt != NULL )
5423 {
5424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5426 }
5427
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005428 /*
5429 * Get next Handshake message in the current record
5430 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005431
Hanno Becker4a810fb2017-05-24 16:27:30 +01005432 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005433 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434 * current handshake content: If DTLS handshake
5435 * fragmentation is used, that's the fragment
5436 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005437 * size here is faulty and should be changed at
5438 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005439 * (2) While it doesn't seem to cause problems, one
5440 * has to be very careful not to assume that in_hslen
5441 * is always <= in_msglen in a sensible communication.
5442 * Again, it's wrong for DTLS handshake fragmentation.
5443 * The following check is therefore mandatory, and
5444 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005445 * Additionally, ssl->in_hslen might be arbitrarily out of
5446 * bounds after handling a DTLS message with an unexpected
5447 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005448 */
5449 if( ssl->in_hslen < ssl->in_msglen )
5450 {
5451 ssl->in_msglen -= ssl->in_hslen;
5452 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5453 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005454
Hanno Becker4a810fb2017-05-24 16:27:30 +01005455 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5456 ssl->in_msg, ssl->in_msglen );
5457 }
5458 else
5459 {
5460 ssl->in_msglen = 0;
5461 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005462
Hanno Becker4a810fb2017-05-24 16:27:30 +01005463 ssl->in_hslen = 0;
5464 }
5465 /* Case (4): Application data */
5466 else if( ssl->in_offt != NULL )
5467 {
5468 return( 0 );
5469 }
5470 /* Everything else (CCS & Alerts) */
5471 else
5472 {
5473 ssl->in_msglen = 0;
5474 }
5475
Hanno Becker1097b342018-08-15 14:09:41 +01005476 return( 0 );
5477}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005478
Hanno Beckere74d5562018-08-15 14:26:08 +01005479static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5480{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005481 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005482 return( 1 );
5483
5484 return( 0 );
5485}
5486
Hanno Becker5f066e72018-08-16 14:56:31 +01005487#if defined(MBEDTLS_SSL_PROTO_DTLS)
5488
5489static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5490{
5491 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5492 if( hs == NULL )
5493 return;
5494
Hanno Becker01315ea2018-08-21 17:22:17 +01005495 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005496 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005497 hs->buffering.total_bytes_buffered -=
5498 hs->buffering.future_record.len;
5499
5500 mbedtls_free( hs->buffering.future_record.data );
5501 hs->buffering.future_record.data = NULL;
5502 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005503}
5504
5505static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5506{
5507 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5508 unsigned char * rec;
5509 size_t rec_len;
5510 unsigned rec_epoch;
5511
5512 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5513 return( 0 );
5514
5515 if( hs == NULL )
5516 return( 0 );
5517
Hanno Becker5f066e72018-08-16 14:56:31 +01005518 rec = hs->buffering.future_record.data;
5519 rec_len = hs->buffering.future_record.len;
5520 rec_epoch = hs->buffering.future_record.epoch;
5521
5522 if( rec == NULL )
5523 return( 0 );
5524
Hanno Becker4cb782d2018-08-20 11:19:05 +01005525 /* Only consider loading future records if the
5526 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005527 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005528 return( 0 );
5529
Hanno Becker5f066e72018-08-16 14:56:31 +01005530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5531
5532 if( rec_epoch != ssl->in_epoch )
5533 {
5534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5535 goto exit;
5536 }
5537
5538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5539
5540 /* Double-check that the record is not too large */
5541 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5542 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5543 {
5544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5546 }
5547
5548 memcpy( ssl->in_hdr, rec, rec_len );
5549 ssl->in_left = rec_len;
5550 ssl->next_record_offset = 0;
5551
5552 ssl_free_buffered_record( ssl );
5553
5554exit:
5555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5556 return( 0 );
5557}
5558
5559static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5560{
5561 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5562 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005563 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005564
5565 /* Don't buffer future records outside handshakes. */
5566 if( hs == NULL )
5567 return( 0 );
5568
5569 /* Only buffer handshake records (we are only interested
5570 * in Finished messages). */
5571 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5572 return( 0 );
5573
5574 /* Don't buffer more than one future epoch record. */
5575 if( hs->buffering.future_record.data != NULL )
5576 return( 0 );
5577
Hanno Becker01315ea2018-08-21 17:22:17 +01005578 /* Don't buffer record if there's not enough buffering space remaining. */
5579 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5580 hs->buffering.total_bytes_buffered ) )
5581 {
5582 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",
5583 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5584 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005585 return( 0 );
5586 }
5587
Hanno Becker5f066e72018-08-16 14:56:31 +01005588 /* Buffer record */
5589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5590 ssl->in_epoch + 1 ) );
5591 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5592 rec_hdr_len + ssl->in_msglen );
5593
5594 /* ssl_parse_record_header() only considers records
5595 * of the next epoch as candidates for buffering. */
5596 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005597 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005598
5599 hs->buffering.future_record.data =
5600 mbedtls_calloc( 1, hs->buffering.future_record.len );
5601 if( hs->buffering.future_record.data == NULL )
5602 {
5603 /* If we run out of RAM trying to buffer a
5604 * record from the next epoch, just ignore. */
5605 return( 0 );
5606 }
5607
Hanno Becker01315ea2018-08-21 17:22:17 +01005608 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005609
Hanno Becker01315ea2018-08-21 17:22:17 +01005610 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005611 return( 0 );
5612}
5613
5614#endif /* MBEDTLS_SSL_PROTO_DTLS */
5615
Hanno Beckere74d5562018-08-15 14:26:08 +01005616static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005617{
5618 int ret;
5619
Hanno Becker5f066e72018-08-16 14:56:31 +01005620#if defined(MBEDTLS_SSL_PROTO_DTLS)
5621 /* We might have buffered a future record; if so,
5622 * and if the epoch matches now, load it.
5623 * On success, this call will set ssl->in_left to
5624 * the length of the buffered record, so that
5625 * the calls to ssl_fetch_input() below will
5626 * essentially be no-ops. */
5627 ret = ssl_load_buffered_record( ssl );
5628 if( ret != 0 )
5629 return( ret );
5630#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005631
Hanno Becker8b09b732019-05-08 12:03:28 +01005632 /* Reset in pointers to default state for TLS/DTLS records,
5633 * assuming no CID and no offset between record content and
5634 * record plaintext. */
5635 ssl_update_in_pointers( ssl );
5636
5637 /* Ensure that we have enough space available for the default form
5638 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5639 * with no space for CIDs counted in). */
5640 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5641 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005644 return( ret );
5645 }
5646
5647 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5651 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005652 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005653 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5654 {
5655 ret = ssl_buffer_future_record( ssl );
5656 if( ret != 0 )
5657 return( ret );
5658
5659 /* Fall through to handling of unexpected records */
5660 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5661 }
5662
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005663 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5664 {
5665 /* Skip unexpected record (but not whole datagram) */
5666 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005667 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005668
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5670 "(header)" ) );
5671 }
5672 else
5673 {
5674 /* Skip invalid record and the rest of the datagram */
5675 ssl->next_record_offset = 0;
5676 ssl->in_left = 0;
5677
5678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5679 "(header)" ) );
5680 }
5681
5682 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005683 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005684 }
5685#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005686 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005687 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005688
5689 /*
5690 * Read and optionally decrypt the message contents
5691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005693 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005696 return( ret );
5697 }
5698
5699 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005701 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005702 {
Hanno Becker43395762019-05-03 14:46:38 +01005703 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005704 if( ssl->next_record_offset < ssl->in_left )
5705 {
5706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5707 }
5708 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005709 else
5710#endif
5711 ssl->in_left = 0;
5712
5713 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005716 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005717 {
5718 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005719 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005720 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005721 /* Except when waiting for Finished as a bad mac here
5722 * probably means something went wrong in the handshake
5723 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5724 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5725 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5726 {
5727#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5728 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5729 {
5730 mbedtls_ssl_send_alert_message( ssl,
5731 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5732 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5733 }
5734#endif
5735 return( ret );
5736 }
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005739 if( ssl->conf->badmac_limit != 0 &&
5740 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5743 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005744 }
5745#endif
5746
Hanno Becker4a810fb2017-05-24 16:27:30 +01005747 /* As above, invalid records cause
5748 * dismissal of the whole datagram. */
5749
5750 ssl->next_record_offset = 0;
5751 ssl->in_left = 0;
5752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005754 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005755 }
5756
5757 return( ret );
5758 }
5759 else
5760#endif
5761 {
5762 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5764 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 mbedtls_ssl_send_alert_message( ssl,
5767 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5768 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005769 }
5770#endif
5771 return( ret );
5772 }
5773 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005774
Simon Butcher99000142016-10-13 17:21:01 +01005775 return( 0 );
5776}
5777
5778int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5779{
5780 int ret;
5781
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005782 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005783 * Handle particular types of records
5784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005786 {
Simon Butcher99000142016-10-13 17:21:01 +01005787 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5788 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005789 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 }
5792
Hanno Beckere678eaa2018-08-21 14:57:46 +01005793 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005794 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005795 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005796 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5798 ssl->in_msglen ) );
5799 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005800 }
5801
Hanno Beckere678eaa2018-08-21 14:57:46 +01005802 if( ssl->in_msg[0] != 1 )
5803 {
5804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5805 ssl->in_msg[0] ) );
5806 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5807 }
5808
5809#if defined(MBEDTLS_SSL_PROTO_DTLS)
5810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5811 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5812 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5813 {
5814 if( ssl->handshake == NULL )
5815 {
5816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5817 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5818 }
5819
5820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5821 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5822 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005823#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005824 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005827 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005828 if( ssl->in_msglen != 2 )
5829 {
5830 /* Note: Standard allows for more than one 2 byte alert
5831 to be packed in a single message, but Mbed TLS doesn't
5832 currently support this. */
5833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5834 ssl->in_msglen ) );
5835 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5836 }
5837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 ssl->in_msg[0], ssl->in_msg[1] ) );
5840
5841 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005842 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005847 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005849 }
5850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5852 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5855 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005856 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005857
5858#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5859 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5860 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5861 {
Hanno Becker90333da2017-10-10 11:27:13 +01005862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005863 /* Will be handled when trying to parse ServerHello */
5864 return( 0 );
5865 }
5866#endif
5867
5868#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5869 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5870 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5871 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5872 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5875 /* Will be handled in mbedtls_ssl_parse_certificate() */
5876 return( 0 );
5877 }
5878#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5879
5880 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005881 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 }
5883
Hanno Beckerc76c6192017-06-06 10:03:17 +01005884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005885 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005886 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005887 /* Drop unexpected ApplicationData records,
5888 * except at the beginning of renegotiations */
5889 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5890 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5891#if defined(MBEDTLS_SSL_RENEGOTIATION)
5892 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5893 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005894#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005895 )
5896 {
5897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5898 return( MBEDTLS_ERR_SSL_NON_FATAL );
5899 }
5900
5901 if( ssl->handshake != NULL &&
5902 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5903 {
5904 ssl_handshake_wrapup_free_hs_transform( ssl );
5905 }
5906 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005907#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005908
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 return( 0 );
5910}
5911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005913{
5914 int ret;
5915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5917 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5918 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005919 {
5920 return( ret );
5921 }
5922
5923 return( 0 );
5924}
5925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005927 unsigned char level,
5928 unsigned char message )
5929{
5930 int ret;
5931
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005932 if( ssl == NULL || ssl->conf == NULL )
5933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005939 ssl->out_msglen = 2;
5940 ssl->out_msg[0] = level;
5941 ssl->out_msg[1] = message;
5942
Hanno Becker67bc7c32018-08-06 11:33:50 +01005943 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005946 return( ret );
5947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005949
5950 return( 0 );
5951}
5952
Paul Bakker5121ce52009-01-03 21:22:43 +00005953/*
5954 * Handshake functions
5955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5957 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5958 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5959 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5960 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5961 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5962 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005963/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005965{
Hanno Becker8759e162017-12-27 21:34:08 +00005966 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5971 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005972 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5973 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005976 ssl->state++;
5977 return( 0 );
5978 }
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005982}
5983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005985{
Hanno Becker8759e162017-12-27 21:34:08 +00005986 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5991 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005992 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5993 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005996 ssl->state++;
5997 return( 0 );
5998 }
5999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006002}
Gilles Peskinef9828522017-05-03 12:28:43 +02006003
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006004#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006005/* Some certificate support -> implement write and parse */
6006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006010 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006012 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6017 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006018 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6019 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006022 ssl->state++;
6023 return( 0 );
6024 }
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006027 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006028 {
6029 if( ssl->client_auth == 0 )
6030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006032 ssl->state++;
6033 return( 0 );
6034 }
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 /*
6038 * If using SSLv3 and got no cert, send an Alert message
6039 * (otherwise an empty Certificate message will be sent).
6040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6042 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006043 {
6044 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6046 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6047 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 goto write_msg;
6051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#endif /* MBEDTLS_SSL_CLI_C */
6055#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006056 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6061 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 }
6063 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
6068 /*
6069 * 0 . 0 handshake type
6070 * 1 . 3 handshake length
6071 * 4 . 6 length of all certs
6072 * 7 . 9 length of cert. 1
6073 * 10 . n-1 peer certificate
6074 * n . n+2 length of cert. 2
6075 * n+3 . ... upper level cert, etc.
6076 */
6077 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079
Paul Bakker29087132010-03-21 21:03:34 +00006080 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 {
6082 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006083 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006086 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006088 }
6089
6090 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6091 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6092 ssl->out_msg[i + 2] = (unsigned char)( n );
6093
6094 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6095 i += n; crt = crt->next;
6096 }
6097
6098 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6099 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6100 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6101
6102 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6104 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006105
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006106#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006107write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006108#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
6110 ssl->state++;
6111
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006112 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006113 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006115 return( ret );
6116 }
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006119
Paul Bakkered27a042013-04-18 22:46:23 +02006120 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006121}
6122
Hanno Becker285ff0c2019-01-31 07:44:03 +00006123#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006124static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6125 unsigned char *crt_buf,
6126 size_t crt_buf_len )
6127{
6128 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6129
6130 if( peer_crt == NULL )
6131 return( -1 );
6132
6133 if( peer_crt->raw.len != crt_buf_len )
6134 return( -1 );
6135
6136 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len) );
6137}
Hanno Becker285ff0c2019-01-31 07:44:03 +00006138#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006139
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006140/*
6141 * Once the certificate message is read, parse it into a cert chain and
6142 * perform basic checks, but leave actual verification to the caller
6143 */
6144static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006145{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006146 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006147 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006148 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150#if defined(MBEDTLS_SSL_SRV_C)
6151#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006152 /*
6153 * Check if the client sent an empty certificate
6154 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006155 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006157 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006158 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6160 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6161 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006164
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006165 /* The client was asked for a certificate but didn't send
6166 one. The client should know what's going on, so we
6167 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006168 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006169 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170 }
6171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6175 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006176 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6180 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6181 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6182 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006185
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006186 /* The client was asked for a certificate but didn't send
6187 one. The client should know what's going on, so we
6188 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006189 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006190 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006191 }
6192 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6194 MBEDTLS_SSL_PROTO_TLS1_2 */
6195#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006200 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6201 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 }
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6206 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006209 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6210 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006212 }
6213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006215
Paul Bakker5121ce52009-01-03 21:22:43 +00006216 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006218 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006219 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006220
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006221 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006225 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6226 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 }
6229
Hanno Becker33c3dc82019-01-30 14:46:46 +00006230 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6231 i += 3;
6232
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006233 /* In case we tried to reuse a session but it failed */
6234 if( ssl->session_negotiate->peer_cert != NULL )
6235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6237 mbedtls_free( ssl->session_negotiate->peer_cert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006238 ssl->session_negotiate->peer_cert = NULL;
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006239 }
6240
Hanno Becker33c3dc82019-01-30 14:46:46 +00006241 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 while( i < ssl->in_hslen )
6243 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006244 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006245 if ( i + 3 > ssl->in_hslen ) {
6246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006247 mbedtls_ssl_send_alert_message( ssl,
6248 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6249 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006250 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6251 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006252 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6253 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 if( ssl->in_msg[i] != 0 )
6255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006257 mbedtls_ssl_send_alert_message( ssl,
6258 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6259 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 }
6262
Hanno Becker33c3dc82019-01-30 14:46:46 +00006263 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006264 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6265 | (unsigned int) ssl->in_msg[i + 2];
6266 i += 3;
6267
6268 if( n < 128 || i + n > ssl->in_hslen )
6269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006271 mbedtls_ssl_send_alert_message( ssl,
6272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 }
6276
Hanno Becker33c3dc82019-01-30 14:46:46 +00006277 /* Check if we're handling the first CRT in the chain. */
6278 if( ssl->session_negotiate->peer_cert == NULL )
6279 {
6280 /* During client-side renegotiation, check the server's end-CRTs
6281 * hasn't changed compared to the initial handshake, mitigating
6282 * the triple handshake attack. On success, reuse the original
6283 * end-CRT instead of parsing it again. */
6284#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6285 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6286 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6287 {
6288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006289 if( ssl_check_peer_crt_unchanged( ssl,
6290 &ssl->in_msg[i],
6291 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006292 {
6293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006294 mbedtls_ssl_send_alert_message( ssl,
6295 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6296 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006297 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6298 }
6299
6300 /* Move CRT chain structure to new session instance. */
6301 ssl->session_negotiate->peer_cert = ssl->session->peer_cert;
6302 ssl->session->peer_cert = NULL;
6303
6304 /* Delete all remaining CRTs from the original CRT chain. */
Hanno Beckerfd399192019-01-31 07:44:17 +00006305 mbedtls_x509_crt_free(
6306 ssl->session_negotiate->peer_cert->next );
Hanno Beckerfe870272019-01-31 16:37:56 +00006307 mbedtls_free( ssl->session_negotiate->peer_cert->next );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006308 ssl->session_negotiate->peer_cert->next = NULL;
6309
6310 i += n;
6311 continue;
6312 }
6313#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6314
6315 /* Outside of client-side renegotiation, create a fresh X.509 CRT
6316 * instance to parse the end-CRT into. */
6317
6318 ssl->session_negotiate->peer_cert =
6319 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6320 if( ssl->session_negotiate->peer_cert == NULL )
6321 {
6322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6323 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006324 mbedtls_ssl_send_alert_message( ssl,
6325 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6326 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006327 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6328 }
6329
6330 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
6331
6332 /* Intentional fall through */
6333 }
6334
6335 /* Parse the next certificate in the chain. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Hanno Becker33c3dc82019-01-30 14:46:46 +00006337 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006338 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006340 case 0: /*ok*/
6341 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6342 /* Ignore certificate with an unknown algorithm: maybe a
6343 prior certificate was already trusted. */
6344 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006345
Hanno Becker33c3dc82019-01-30 14:46:46 +00006346 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6347 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6348 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006349
Hanno Becker33c3dc82019-01-30 14:46:46 +00006350 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6351 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6352 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006353
Hanno Becker33c3dc82019-01-30 14:46:46 +00006354 default:
6355 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6356 crt_parse_der_failed:
6357 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6358 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6359 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 }
6361
6362 i += n;
6363 }
6364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006366 return( 0 );
6367}
6368
6369int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6370{
6371 int ret;
6372 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006373 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006374#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6375 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6376 ? ssl->handshake->sni_authmode
6377 : ssl->conf->authmode;
6378#else
6379 const int authmode = ssl->conf->authmode;
6380#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006381 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006382
6383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6384
6385 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6386 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6387 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6388 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6389 {
6390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6391 ssl->state++;
6392 return( 0 );
6393 }
6394
6395#if defined(MBEDTLS_SSL_SRV_C)
6396 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6397 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6398 {
6399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6400 ssl->state++;
6401 return( 0 );
6402 }
6403
6404 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6405 authmode == MBEDTLS_SSL_VERIFY_NONE )
6406 {
6407 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006409
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006410 ssl->state++;
6411 return( 0 );
6412 }
6413#endif
6414
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006415#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6416 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006417 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006418 {
6419 goto crt_verify;
6420 }
6421#endif
6422
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006423 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006424 {
6425 /* mbedtls_ssl_read_record may have sent an alert already. We
6426 let it decide whether to alert. */
6427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6428 return( ret );
6429 }
6430
6431 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6432 {
6433#if defined(MBEDTLS_SSL_SRV_C)
6434 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6435 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6436 {
6437 ret = 0;
6438 }
6439#endif
6440
6441 ssl->state++;
6442 return( ret );
6443 }
6444
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006445#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6446 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006447 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006448
6449crt_verify:
6450 if( ssl->handshake->ecrs_enabled)
6451 rs_ctx = &ssl->handshake->ecrs_ctx;
6452#endif
6453
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006454 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006455 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006456 mbedtls_x509_crt *ca_chain;
6457 mbedtls_x509_crl *ca_crl;
6458
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006459#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006460 if( ssl->handshake->sni_ca_chain != NULL )
6461 {
6462 ca_chain = ssl->handshake->sni_ca_chain;
6463 ca_crl = ssl->handshake->sni_ca_crl;
6464 }
6465 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006466#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006467 {
6468 ca_chain = ssl->conf->ca_chain;
6469 ca_crl = ssl->conf->ca_crl;
6470 }
6471
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006472 /*
6473 * Main check: verify certificate
6474 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006475 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006476 ssl->session_negotiate->peer_cert,
6477 ca_chain, ca_crl,
6478 ssl->conf->cert_profile,
6479 ssl->hostname,
6480 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006481 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006482
6483 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006486 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006487
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006488#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6489 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006490 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006491#endif
6492
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006493 /*
6494 * Secondary checks: always done, but change 'ret' only if it was 0
6495 */
6496
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006497#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006500
6501 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006503 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006504 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006505 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006508 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006510 }
6511 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006512#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006515 ciphersuite_info,
6516 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006517 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006520 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006522 }
6523
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006524 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6525 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6526 * with details encoded in the verification flags. All other kinds
6527 * of error codes, including those from the user provided f_vrfy
6528 * functions, are treated as fatal and lead to a failure of
6529 * ssl_parse_certificate even if verification was optional. */
6530 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6531 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6532 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6533 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006534 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006535 }
6536
6537 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6538 {
6539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6540 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6541 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006542
6543 if( ret != 0 )
6544 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006545 uint8_t alert;
6546
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006547 /* The certificate may have been rejected for several reasons.
6548 Pick one and send the corresponding alert. Which alert to send
6549 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006550 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6551 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6552 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6553 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6554 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6555 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6556 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6557 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6558 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6559 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6560 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6561 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6562 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6563 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6564 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6565 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6566 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6567 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6569 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006570 else
6571 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006572 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6573 alert );
6574 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006575
Hanno Beckere6706e62017-05-15 16:05:15 +01006576#if defined(MBEDTLS_DEBUG_C)
6577 if( ssl->session_negotiate->verify_result != 0 )
6578 {
6579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6580 ssl->session_negotiate->verify_result ) );
6581 }
6582 else
6583 {
6584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6585 }
6586#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006587 }
6588
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006589 ssl->state++;
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006592
6593 return( ret );
6594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6596 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6597 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6598 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6599 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6600 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6601 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006604{
6605 int ret;
6606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006610 ssl->out_msglen = 1;
6611 ssl->out_msg[0] = 1;
6612
Paul Bakker5121ce52009-01-03 21:22:43 +00006613 ssl->state++;
6614
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006615 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006616 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 return( ret );
6619 }
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006622
6623 return( 0 );
6624}
6625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006627{
6628 int ret;
6629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006631
Hanno Becker327c93b2018-08-15 13:56:18 +01006632 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 return( ret );
6636 }
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006641 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6642 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 }
6645
Hanno Beckere678eaa2018-08-21 14:57:46 +01006646 /* CCS records are only accepted if they have length 1 and content '1',
6647 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006648
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006649 /*
6650 * Switch to our negotiated transform and session parameters for inbound
6651 * data.
6652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006654 ssl->transform_in = ssl->transform_negotiate;
6655 ssl->session_in = ssl->session_negotiate;
6656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006661 ssl_dtls_replay_reset( ssl );
6662#endif
6663
6664 /* Increment epoch */
6665 if( ++ssl->in_epoch == 0 )
6666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006668 /* This is highly unlikely to happen for legitimate reasons, so
6669 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006671 }
6672 }
6673 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006675 memset( ssl->in_ctr, 0, 8 );
6676
Hanno Beckerf5970a02019-05-08 09:38:41 +01006677 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6680 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006685 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6686 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006688 }
6689 }
6690#endif
6691
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 ssl->state++;
6693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
6696 return( 0 );
6697}
6698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6700 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006701{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006702 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6705 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6706 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006707 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006708 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6711#if defined(MBEDTLS_SHA512_C)
6712 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006713 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6714 else
6715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716#if defined(MBEDTLS_SHA256_C)
6717 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006718 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006719 else
6720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006724 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006725 }
Paul Bakker380da532012-04-18 16:10:25 +00006726}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006729{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6731 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006732 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6733 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006734#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6736#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006737 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006740 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006743}
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006746 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006747{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6749 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006750 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6751 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6754#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006755 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006758 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006761}
6762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6764 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6765static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006766 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006767{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006768 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6769 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006770}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006771#endif
Paul Bakker380da532012-04-18 16:10:25 +00006772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6774#if defined(MBEDTLS_SHA256_C)
6775static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006776 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006777{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006778 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006779}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006780#endif
Paul Bakker380da532012-04-18 16:10:25 +00006781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782#if defined(MBEDTLS_SHA512_C)
6783static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006784 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006785{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006786 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006787}
Paul Bakker769075d2012-11-24 11:26:46 +01006788#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006792static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006794{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006795 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006796 mbedtls_md5_context md5;
6797 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006798
Paul Bakker5121ce52009-01-03 21:22:43 +00006799 unsigned char padbuf[48];
6800 unsigned char md5sum[16];
6801 unsigned char sha1sum[20];
6802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006804 if( !session )
6805 session = ssl->session;
6806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006808
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006809 mbedtls_md5_init( &md5 );
6810 mbedtls_sha1_init( &sha1 );
6811
6812 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6813 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
6815 /*
6816 * SSLv3:
6817 * hash =
6818 * MD5( master + pad2 +
6819 * MD5( handshake + sender + master + pad1 ) )
6820 * + SHA1( master + pad2 +
6821 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 */
6823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006825 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6826 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006827#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006830 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6831 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006832#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006835 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006836
Paul Bakker1ef83d62012-04-11 12:09:53 +00006837 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006839 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6840 mbedtls_md5_update_ret( &md5, session->master, 48 );
6841 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6842 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006844 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6845 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6846 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6847 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006848
Paul Bakker1ef83d62012-04-11 12:09:53 +00006849 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006850
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006851 mbedtls_md5_starts_ret( &md5 );
6852 mbedtls_md5_update_ret( &md5, session->master, 48 );
6853 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6854 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6855 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006857 mbedtls_sha1_starts_ret( &sha1 );
6858 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6859 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6860 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6861 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006865 mbedtls_md5_free( &md5 );
6866 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006868 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6869 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6870 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006877static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006879{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006880 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006881 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006882 mbedtls_md5_context md5;
6883 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006884 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006887 if( !session )
6888 session = ssl->session;
6889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006891
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006892 mbedtls_md5_init( &md5 );
6893 mbedtls_sha1_init( &sha1 );
6894
6895 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6896 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006897
Paul Bakker1ef83d62012-04-11 12:09:53 +00006898 /*
6899 * TLSv1:
6900 * hash = PRF( master, finished_label,
6901 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6902 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006905 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6906 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006907#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006910 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6911 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006912#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006915 ? "client finished"
6916 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006917
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006918 mbedtls_md5_finish_ret( &md5, padbuf );
6919 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006920
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006921 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006922 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006925
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006926 mbedtls_md5_free( &md5 );
6927 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006929 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6936#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006937static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006939{
6940 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006941 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006942 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006943 unsigned char padbuf[32];
6944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006946 if( !session )
6947 session = ssl->session;
6948
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006949 mbedtls_sha256_init( &sha256 );
6950
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006952
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006953 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006954
6955 /*
6956 * TLSv1.2:
6957 * hash = PRF( master, finished_label,
6958 * Hash( handshake ) )[0.11]
6959 */
6960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if !defined(MBEDTLS_SHA256_ALT)
6962 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006963 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006964#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006967 ? "client finished"
6968 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006969
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006970 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006971
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006972 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006973 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006976
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006977 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006978
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006979 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006986static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006988{
6989 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006990 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006991 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006992 unsigned char padbuf[48];
6993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006995 if( !session )
6996 session = ssl->session;
6997
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006998 mbedtls_sha512_init( &sha512 );
6999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007001
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007002 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007003
7004 /*
7005 * TLSv1.2:
7006 * hash = PRF( master, finished_label,
7007 * Hash( handshake ) )[0.11]
7008 */
7009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007011 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7012 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007013#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007016 ? "client finished"
7017 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007018
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007019 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007020
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007021 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007022 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007025
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007026 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007027
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007028 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#endif /* MBEDTLS_SHA512_C */
7033#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007038
7039 /*
7040 * Free our handshake params
7041 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007042 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007044 ssl->handshake = NULL;
7045
7046 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007047 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007048 */
7049 if( ssl->transform )
7050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 mbedtls_ssl_transform_free( ssl->transform );
7052 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007053 }
7054 ssl->transform = ssl->transform_negotiate;
7055 ssl->transform_negotiate = NULL;
7056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007058}
7059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007061{
7062 int resume = ssl->handshake->resume;
7063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066#if defined(MBEDTLS_SSL_RENEGOTIATION)
7067 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007070 ssl->renego_records_seen = 0;
7071 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007072#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007073
7074 /*
7075 * Free the previous session and switch in the current one
7076 */
Paul Bakker0a597072012-09-25 21:55:46 +00007077 if( ssl->session )
7078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007080 /* RFC 7366 3.1: keep the EtM state */
7081 ssl->session_negotiate->encrypt_then_mac =
7082 ssl->session->encrypt_then_mac;
7083#endif
7084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 mbedtls_ssl_session_free( ssl->session );
7086 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007087 }
7088 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007089 ssl->session_negotiate = NULL;
7090
Paul Bakker0a597072012-09-25 21:55:46 +00007091 /*
7092 * Add cache entry
7093 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007094 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007095 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007096 resume == 0 )
7097 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007098 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007100 }
Paul Bakker0a597072012-09-25 21:55:46 +00007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007103 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007104 ssl->handshake->flight != NULL )
7105 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007106 /* Cancel handshake timer */
7107 ssl_set_timer( ssl, 0 );
7108
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007109 /* Keep last flight around in case we need to resend it:
7110 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007112 }
7113 else
7114#endif
7115 ssl_handshake_wrapup_free_hs_transform( ssl );
7116
Paul Bakker48916f92012-09-16 19:57:18 +00007117 ssl->state++;
7118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007120}
7121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007123{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007124 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007127
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007128 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007129
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007130 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007131
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007132 /*
7133 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7134 * may define some other value. Currently (early 2016), no defined
7135 * ciphersuite does this (and this is unlikely to change as activity has
7136 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007141 ssl->verify_data_len = hash_len;
7142 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007143#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007144
Paul Bakker5121ce52009-01-03 21:22:43 +00007145 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7147 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007148
7149 /*
7150 * In case of session resuming, invert the client and server
7151 * ChangeCipherSpec messages order.
7152 */
Paul Bakker0a597072012-09-25 21:55:46 +00007153 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007156 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007160 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007162#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007163 }
7164 else
7165 ssl->state++;
7166
Paul Bakker48916f92012-09-16 19:57:18 +00007167 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007168 * Switch to our negotiated transform and session parameters for outbound
7169 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007174 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007175 {
7176 unsigned char i;
7177
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007178 /* Remember current epoch settings for resending */
7179 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007180 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007181
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007182 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007183 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007184
7185 /* Increment epoch */
7186 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007187 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007188 break;
7189
7190 /* The loop goes to its end iff the counter is wrapping */
7191 if( i == 0 )
7192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7194 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007195 }
7196 }
7197 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007199 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007200
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007201 ssl->transform_out = ssl->transform_negotiate;
7202 ssl->session_out = ssl->session_negotiate;
7203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7205 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7210 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007211 }
7212 }
7213#endif
7214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007218#endif
7219
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007220 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007221 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007223 return( ret );
7224 }
7225
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007226#if defined(MBEDTLS_SSL_PROTO_DTLS)
7227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7228 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7229 {
7230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7231 return( ret );
7232 }
7233#endif
7234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007236
7237 return( 0 );
7238}
7239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007241#define SSL_MAX_HASH_LEN 36
7242#else
7243#define SSL_MAX_HASH_LEN 12
7244#endif
7245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007247{
Paul Bakker23986e52011-04-24 08:57:21 +00007248 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007249 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007250 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007253
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007254 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007255
Hanno Becker327c93b2018-08-15 13:56:18 +01007256 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007259 return( ret );
7260 }
7261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7266 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007268 }
7269
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007270 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#if defined(MBEDTLS_SSL_PROTO_SSL3)
7272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007273 hash_len = 36;
7274 else
7275#endif
7276 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7279 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7283 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007285 }
7286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007288 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007291 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7292 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007294 }
7295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007297 ssl->verify_data_len = hash_len;
7298 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007299#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007300
Paul Bakker0a597072012-09-25 21:55:46 +00007301 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007304 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007308 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007311 }
7312 else
7313 ssl->state++;
7314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007316 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007318#endif
7319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007321
7322 return( 0 );
7323}
7324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7330 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7331 mbedtls_md5_init( &handshake->fin_md5 );
7332 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007333 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7334 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007335#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7337#if defined(MBEDTLS_SHA256_C)
7338 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007339 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007340#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341#if defined(MBEDTLS_SHA512_C)
7342 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007343 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007344#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007346
7347 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007348
7349#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7350 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7351 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7352#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354#if defined(MBEDTLS_DHM_C)
7355 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#if defined(MBEDTLS_ECDH_C)
7358 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007359#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007360#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007361 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007362#if defined(MBEDTLS_SSL_CLI_C)
7363 handshake->ecjpake_cache = NULL;
7364 handshake->ecjpake_cache_len = 0;
7365#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007366#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007367
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007368#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007369 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007370#endif
7371
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007372#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7373 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7374#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007375}
7376
Hanno Becker611a83b2018-01-03 14:27:32 +00007377void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7382 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007383
Hanno Becker92231322018-01-03 15:32:51 +00007384#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 mbedtls_md_init( &transform->md_ctx_enc );
7386 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007387#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007388}
7389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007393}
7394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007396{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007397 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007398 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007400 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007402 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007403 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007404
7405 /*
7406 * Either the pointers are now NULL or cleared properly and can be freed.
7407 * Now allocate missing structures.
7408 */
7409 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007410 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007411 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007412 }
Paul Bakker48916f92012-09-16 19:57:18 +00007413
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007414 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007415 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007416 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007417 }
Paul Bakker48916f92012-09-16 19:57:18 +00007418
Paul Bakker82788fb2014-10-20 13:59:19 +02007419 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007420 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007421 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007422 }
Paul Bakker48916f92012-09-16 19:57:18 +00007423
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007424 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007425 if( ssl->handshake == NULL ||
7426 ssl->transform_negotiate == NULL ||
7427 ssl->session_negotiate == NULL )
7428 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 mbedtls_free( ssl->handshake );
7432 mbedtls_free( ssl->transform_negotiate );
7433 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007434
7435 ssl->handshake = NULL;
7436 ssl->transform_negotiate = NULL;
7437 ssl->session_negotiate = NULL;
7438
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007439 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007440 }
7441
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007442 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007444 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007445 ssl_handshake_params_init( ssl->handshake );
7446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007448 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7449 {
7450 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007451
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007452 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7453 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7454 else
7455 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007456
7457 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007458 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007459#endif
7460
Paul Bakker48916f92012-09-16 19:57:18 +00007461 return( 0 );
7462}
7463
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007464#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007465/* Dummy cookie callbacks for defaults */
7466static int ssl_cookie_write_dummy( void *ctx,
7467 unsigned char **p, unsigned char *end,
7468 const unsigned char *cli_id, size_t cli_id_len )
7469{
7470 ((void) ctx);
7471 ((void) p);
7472 ((void) end);
7473 ((void) cli_id);
7474 ((void) cli_id_len);
7475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007477}
7478
7479static int ssl_cookie_check_dummy( void *ctx,
7480 const unsigned char *cookie, size_t cookie_len,
7481 const unsigned char *cli_id, size_t cli_id_len )
7482{
7483 ((void) ctx);
7484 ((void) cookie);
7485 ((void) cookie_len);
7486 ((void) cli_id);
7487 ((void) cli_id_len);
7488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007490}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007491#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007492
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007493/* Once ssl->out_hdr as the address of the beginning of the
7494 * next outgoing record is set, deduce the other pointers.
7495 *
7496 * Note: For TLS, we save the implicit record sequence number
7497 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7498 * and the caller has to make sure there's space for this.
7499 */
7500
7501static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7502 mbedtls_ssl_transform *transform )
7503{
7504#if defined(MBEDTLS_SSL_PROTO_DTLS)
7505 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7506 {
7507 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007508#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007509 ssl->out_cid = ssl->out_ctr + 8;
7510 ssl->out_len = ssl->out_cid;
7511 if( transform != NULL )
7512 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007513#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007514 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007515#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007516 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007517 }
7518 else
7519#endif
7520 {
7521 ssl->out_ctr = ssl->out_hdr - 8;
7522 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007523#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007524 ssl->out_cid = ssl->out_len;
7525#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007526 ssl->out_iv = ssl->out_hdr + 5;
7527 }
7528
7529 /* Adjust out_msg to make space for explicit IV, if used. */
7530 if( transform != NULL &&
7531 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7532 {
7533 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7534 }
7535 else
7536 ssl->out_msg = ssl->out_iv;
7537}
7538
7539/* Once ssl->in_hdr as the address of the beginning of the
7540 * next incoming record is set, deduce the other pointers.
7541 *
7542 * Note: For TLS, we save the implicit record sequence number
7543 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7544 * and the caller has to make sure there's space for this.
7545 */
7546
Hanno Beckerf5970a02019-05-08 09:38:41 +01007547static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007548{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007549 /* This function sets the pointers to match the case
7550 * of unprotected TLS/DTLS records, with both ssl->in_iv
7551 * and ssl->in_msg pointing to the beginning of the record
7552 * content.
7553 *
7554 * When decrypting a protected record, ssl->in_msg
7555 * will be shifted to point to the beginning of the
7556 * record plaintext.
7557 */
7558
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007559#if defined(MBEDTLS_SSL_PROTO_DTLS)
7560 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7561 {
Hanno Becker70e79282019-05-03 14:34:53 +01007562 /* This sets the header pointers to match records
7563 * without CID. When we receive a record containing
7564 * a CID, the fields are shifted accordingly in
7565 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007566 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007567#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007568 ssl->in_cid = ssl->in_ctr + 8;
7569 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007570#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007571 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007572#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007573 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007574 }
7575 else
7576#endif
7577 {
7578 ssl->in_ctr = ssl->in_hdr - 8;
7579 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007580#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007581 ssl->in_cid = ssl->in_len;
7582#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007583 ssl->in_iv = ssl->in_hdr + 5;
7584 }
7585
Hanno Beckerf5970a02019-05-08 09:38:41 +01007586 /* This will be adjusted at record decryption time. */
7587 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007588}
7589
Paul Bakker5121ce52009-01-03 21:22:43 +00007590/*
7591 * Initialize an SSL context
7592 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007593void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7594{
7595 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7596}
7597
7598/*
7599 * Setup an SSL context
7600 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007601
7602static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7603{
7604 /* Set the incoming and outgoing record pointers. */
7605#if defined(MBEDTLS_SSL_PROTO_DTLS)
7606 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7607 {
7608 ssl->out_hdr = ssl->out_buf;
7609 ssl->in_hdr = ssl->in_buf;
7610 }
7611 else
7612#endif /* MBEDTLS_SSL_PROTO_DTLS */
7613 {
7614 ssl->out_hdr = ssl->out_buf + 8;
7615 ssl->in_hdr = ssl->in_buf + 8;
7616 }
7617
7618 /* Derive other internal pointers. */
7619 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007620 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007621}
7622
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007623int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007624 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007625{
Paul Bakker48916f92012-09-16 19:57:18 +00007626 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007627
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007628 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007629
7630 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007631 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007632 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007633
7634 /* Set to NULL in case of an error condition */
7635 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007636
Angus Grattond8213d02016-05-25 20:56:48 +10007637 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7638 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007639 {
Angus Grattond8213d02016-05-25 20:56:48 +10007640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007641 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007642 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007643 }
7644
7645 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7646 if( ssl->out_buf == NULL )
7647 {
7648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007649 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007650 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007651 }
7652
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007653 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007654
Paul Bakker48916f92012-09-16 19:57:18 +00007655 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007656 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007657
7658 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007659
7660error:
7661 mbedtls_free( ssl->in_buf );
7662 mbedtls_free( ssl->out_buf );
7663
7664 ssl->conf = NULL;
7665
7666 ssl->in_buf = NULL;
7667 ssl->out_buf = NULL;
7668
7669 ssl->in_hdr = NULL;
7670 ssl->in_ctr = NULL;
7671 ssl->in_len = NULL;
7672 ssl->in_iv = NULL;
7673 ssl->in_msg = NULL;
7674
7675 ssl->out_hdr = NULL;
7676 ssl->out_ctr = NULL;
7677 ssl->out_len = NULL;
7678 ssl->out_iv = NULL;
7679 ssl->out_msg = NULL;
7680
k-stachowiak9f7798e2018-07-31 16:52:32 +02007681 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007682}
7683
7684/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007685 * Reset an initialized and used SSL context for re-use while retaining
7686 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007687 *
7688 * If partial is non-zero, keep data in the input buffer and client ID.
7689 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007690 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007691static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007692{
Paul Bakker48916f92012-09-16 19:57:18 +00007693 int ret;
7694
Hanno Becker7e772132018-08-10 12:38:21 +01007695#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7696 !defined(MBEDTLS_SSL_SRV_C)
7697 ((void) partial);
7698#endif
7699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007701
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007702 /* Cancel any possibly running timer */
7703 ssl_set_timer( ssl, 0 );
7704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705#if defined(MBEDTLS_SSL_RENEGOTIATION)
7706 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007707 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007708
7709 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7711 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007714
Paul Bakker7eb013f2011-10-06 12:37:39 +00007715 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007716 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007717
7718 ssl->in_msgtype = 0;
7719 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007721 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007722 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007725 ssl_dtls_replay_reset( ssl );
7726#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007727
7728 ssl->in_hslen = 0;
7729 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007730
7731 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007732
7733 ssl->out_msgtype = 0;
7734 ssl->out_msglen = 0;
7735 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7737 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007738 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007739#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007740
Hanno Becker19859472018-08-06 09:40:20 +01007741 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7742
Paul Bakker48916f92012-09-16 19:57:18 +00007743 ssl->transform_in = NULL;
7744 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007745
Hanno Becker78640902018-08-13 16:35:15 +01007746 ssl->session_in = NULL;
7747 ssl->session_out = NULL;
7748
Angus Grattond8213d02016-05-25 20:56:48 +10007749 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007750
7751#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007752 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007753#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7754 {
7755 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007756 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007757 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7760 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7763 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7766 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007767 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007768 }
7769#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007770
Paul Bakker48916f92012-09-16 19:57:18 +00007771 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 mbedtls_ssl_transform_free( ssl->transform );
7774 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007775 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007776 }
Paul Bakker48916f92012-09-16 19:57:18 +00007777
Paul Bakkerc0463502013-02-14 11:19:38 +01007778 if( ssl->session )
7779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 mbedtls_ssl_session_free( ssl->session );
7781 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007782 ssl->session = NULL;
7783 }
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007786 ssl->alpn_chosen = NULL;
7787#endif
7788
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007789#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007790#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007791 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007792#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007793 {
7794 mbedtls_free( ssl->cli_id );
7795 ssl->cli_id = NULL;
7796 ssl->cli_id_len = 0;
7797 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007798#endif
7799
Paul Bakker48916f92012-09-16 19:57:18 +00007800 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7801 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007802
7803 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007804}
7805
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007806/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007807 * Reset an initialized and used SSL context for re-use while retaining
7808 * all application-set variables, function pointers and data.
7809 */
7810int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7811{
7812 return( ssl_session_reset_int( ssl, 0 ) );
7813}
7814
7815/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007816 * SSL set accessors
7817 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007818void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007820 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007821}
7822
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007823void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007824{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007825 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007826}
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007829void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007830{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007831 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007832}
7833#endif
7834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007836void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007837{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007838 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007839}
7840#endif
7841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007843
Hanno Becker1841b0a2018-08-24 11:13:57 +01007844void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7845 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007846{
7847 ssl->disable_datagram_packing = !allow_packing;
7848}
7849
7850void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7851 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007852{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007853 conf->hs_timeout_min = min;
7854 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007855}
7856#endif
7857
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007858void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007859{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007860 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007861}
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007864void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007865 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007866 void *p_vrfy )
7867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007868 conf->f_vrfy = f_vrfy;
7869 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007872
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007873void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007874 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007875 void *p_rng )
7876{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007877 conf->f_rng = f_rng;
7878 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007879}
7880
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007881void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007882 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 void *p_dbg )
7884{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007885 conf->f_dbg = f_dbg;
7886 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007887}
7888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007890 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007891 mbedtls_ssl_send_t *f_send,
7892 mbedtls_ssl_recv_t *f_recv,
7893 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007894{
7895 ssl->p_bio = p_bio;
7896 ssl->f_send = f_send;
7897 ssl->f_recv = f_recv;
7898 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007899}
7900
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007901#if defined(MBEDTLS_SSL_PROTO_DTLS)
7902void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7903{
7904 ssl->mtu = mtu;
7905}
7906#endif
7907
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007908void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007909{
7910 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007911}
7912
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007913void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7914 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007915 mbedtls_ssl_set_timer_t *f_set_timer,
7916 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007917{
7918 ssl->p_timer = p_timer;
7919 ssl->f_set_timer = f_set_timer;
7920 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007921
7922 /* Make sure we start with no timer running */
7923 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007924}
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007927void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007928 void *p_cache,
7929 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7930 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007931{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007932 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007933 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007934 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_CLI_C)
7939int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007940{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007941 int ret;
7942
7943 if( ssl == NULL ||
7944 session == NULL ||
7945 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007946 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007949 }
7950
7951 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7952 return( ret );
7953
Paul Bakker0a597072012-09-25 21:55:46 +00007954 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007955
7956 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007959
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007960void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007961 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007962{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007963 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7964 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7965 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7966 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007967}
7968
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007969void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007970 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007971 int major, int minor )
7972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007974 return;
7975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007977 return;
7978
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007979 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007980}
7981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007983void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007984 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007985{
7986 conf->cert_profile = profile;
7987}
7988
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007989/* Append a new keycert entry to a (possibly empty) list */
7990static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7991 mbedtls_x509_crt *cert,
7992 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007993{
niisato8ee24222018-06-25 19:05:48 +09007994 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007995
niisato8ee24222018-06-25 19:05:48 +09007996 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7997 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007998 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007999
niisato8ee24222018-06-25 19:05:48 +09008000 new_cert->cert = cert;
8001 new_cert->key = key;
8002 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008003
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008004 /* Update head is the list was null, else add to the end */
8005 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008006 {
niisato8ee24222018-06-25 19:05:48 +09008007 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008008 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008009 else
8010 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008011 mbedtls_ssl_key_cert *cur = *head;
8012 while( cur->next != NULL )
8013 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008014 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008015 }
8016
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008017 return( 0 );
8018}
8019
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008020int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008021 mbedtls_x509_crt *own_cert,
8022 mbedtls_pk_context *pk_key )
8023{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008024 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008025}
8026
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008027void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008028 mbedtls_x509_crt *ca_chain,
8029 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008030{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008031 conf->ca_chain = ca_chain;
8032 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008035
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008036#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8037int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8038 mbedtls_x509_crt *own_cert,
8039 mbedtls_pk_context *pk_key )
8040{
8041 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8042 own_cert, pk_key ) );
8043}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008044
8045void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8046 mbedtls_x509_crt *ca_chain,
8047 mbedtls_x509_crl *ca_crl )
8048{
8049 ssl->handshake->sni_ca_chain = ca_chain;
8050 ssl->handshake->sni_ca_crl = ca_crl;
8051}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008052
8053void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8054 int authmode )
8055{
8056 ssl->handshake->sni_authmode = authmode;
8057}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008058#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8059
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008060#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008061/*
8062 * Set EC J-PAKE password for current handshake
8063 */
8064int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8065 const unsigned char *pw,
8066 size_t pw_len )
8067{
8068 mbedtls_ecjpake_role role;
8069
Janos Follath8eb64132016-06-03 15:40:57 +01008070 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8072
8073 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8074 role = MBEDTLS_ECJPAKE_SERVER;
8075 else
8076 role = MBEDTLS_ECJPAKE_CLIENT;
8077
8078 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8079 role,
8080 MBEDTLS_MD_SHA256,
8081 MBEDTLS_ECP_DP_SECP256R1,
8082 pw, pw_len ) );
8083}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008084#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008087int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008088 const unsigned char *psk, size_t psk_len,
8089 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008090{
Paul Bakker6db455e2013-09-18 17:29:31 +02008091 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008096
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008097 /* Identity len will be encoded on two bytes */
8098 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008099 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008100 {
8101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8102 }
8103
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008104 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008105 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008106 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008107
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008108 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008109 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008110 conf->psk_len = 0;
8111 }
8112 if( conf->psk_identity != NULL )
8113 {
8114 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008115 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008116 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008117 }
8118
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008119 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8120 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008121 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008122 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008123 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008124 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008125 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008126 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008127 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008128
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008129 conf->psk_len = psk_len;
8130 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008131
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008132 memcpy( conf->psk, psk, conf->psk_len );
8133 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008134
8135 return( 0 );
8136}
8137
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008138int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8139 const unsigned char *psk, size_t psk_len )
8140{
8141 if( psk == NULL || ssl->handshake == NULL )
8142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8143
8144 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8146
8147 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008148 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008149 mbedtls_platform_zeroize( ssl->handshake->psk,
8150 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008151 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008152 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008153 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008154
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008155 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008156 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008157
8158 ssl->handshake->psk_len = psk_len;
8159 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8160
8161 return( 0 );
8162}
8163
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008164void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008166 size_t),
8167 void *p_psk )
8168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008169 conf->f_psk = f_psk;
8170 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008173
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008174#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008175
8176#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008177int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008178{
8179 int ret;
8180
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008181 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8182 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8183 {
8184 mbedtls_mpi_free( &conf->dhm_P );
8185 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008186 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008188
8189 return( 0 );
8190}
Hanno Becker470a8c42017-10-04 15:28:46 +01008191#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008192
Hanno Beckera90658f2017-10-04 15:29:08 +01008193int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8194 const unsigned char *dhm_P, size_t P_len,
8195 const unsigned char *dhm_G, size_t G_len )
8196{
8197 int ret;
8198
8199 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8200 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8201 {
8202 mbedtls_mpi_free( &conf->dhm_P );
8203 mbedtls_mpi_free( &conf->dhm_G );
8204 return( ret );
8205 }
8206
8207 return( 0 );
8208}
Paul Bakker5121ce52009-01-03 21:22:43 +00008209
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008210int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008211{
8212 int ret;
8213
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008214 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8215 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8216 {
8217 mbedtls_mpi_free( &conf->dhm_P );
8218 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008219 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008220 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008221
8222 return( 0 );
8223}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008224#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008225
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008226#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8227/*
8228 * Set the minimum length for Diffie-Hellman parameters
8229 */
8230void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8231 unsigned int bitlen )
8232{
8233 conf->dhm_min_bitlen = bitlen;
8234}
8235#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8236
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008237#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008238/*
8239 * Set allowed/preferred hashes for handshake signatures
8240 */
8241void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8242 const int *hashes )
8243{
8244 conf->sig_hashes = hashes;
8245}
Hanno Becker947194e2017-04-07 13:25:49 +01008246#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008247
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008248#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008249/*
8250 * Set the allowed elliptic curves
8251 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008252void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008253 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008255 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008256}
Hanno Becker947194e2017-04-07 13:25:49 +01008257#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008258
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008259#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008261{
Hanno Becker947194e2017-04-07 13:25:49 +01008262 /* Initialize to suppress unnecessary compiler warning */
8263 size_t hostname_len = 0;
8264
8265 /* Check if new hostname is valid before
8266 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008267 if( hostname != NULL )
8268 {
8269 hostname_len = strlen( hostname );
8270
8271 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8272 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8273 }
8274
8275 /* Now it's clear that we will overwrite the old hostname,
8276 * so we can free it safely */
8277
8278 if( ssl->hostname != NULL )
8279 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008280 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008281 mbedtls_free( ssl->hostname );
8282 }
8283
8284 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008285
Paul Bakker5121ce52009-01-03 21:22:43 +00008286 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008287 {
8288 ssl->hostname = NULL;
8289 }
8290 else
8291 {
8292 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008293 if( ssl->hostname == NULL )
8294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008295
Hanno Becker947194e2017-04-07 13:25:49 +01008296 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008297
Hanno Becker947194e2017-04-07 13:25:49 +01008298 ssl->hostname[hostname_len] = '\0';
8299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008300
8301 return( 0 );
8302}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008303#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008304
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008305#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008306void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008308 const unsigned char *, size_t),
8309 void *p_sni )
8310{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008311 conf->f_sni = f_sni;
8312 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008317int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008318{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008319 size_t cur_len, tot_len;
8320 const char **p;
8321
8322 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008323 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8324 * MUST NOT be truncated."
8325 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008326 */
8327 tot_len = 0;
8328 for( p = protos; *p != NULL; p++ )
8329 {
8330 cur_len = strlen( *p );
8331 tot_len += cur_len;
8332
8333 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008335 }
8336
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008337 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008338
8339 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008340}
8341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008343{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008344 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008347
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008348void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008349{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008350 conf->max_major_ver = major;
8351 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008352}
8353
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008354void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008355{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008356 conf->min_major_ver = major;
8357 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008358}
8359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008361void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008362{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008363 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008364}
8365#endif
8366
Janos Follath088ce432017-04-10 12:42:31 +01008367#if defined(MBEDTLS_SSL_SRV_C)
8368void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8369 char cert_req_ca_list )
8370{
8371 conf->cert_req_ca_list = cert_req_ca_list;
8372}
8373#endif
8374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008376void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008377{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008378 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008379}
8380#endif
8381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008383void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008384{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008385 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008386}
8387#endif
8388
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008389#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008390void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008392 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008393}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008394#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008397int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008400 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008403 }
8404
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008405 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008406
8407 return( 0 );
8408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008412void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008413{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008414 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008419void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008420{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008421 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008422}
8423#endif
8424
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008425void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008426{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008427 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008428}
8429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008431void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008434}
8435
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008436void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008437{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008438 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008439}
8440
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008441void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008442 const unsigned char period[8] )
8443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008444 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008449#if defined(MBEDTLS_SSL_CLI_C)
8450void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008451{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008452 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008453}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008454#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008455
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008456#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008457void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8458 mbedtls_ssl_ticket_write_t *f_ticket_write,
8459 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8460 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008461{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008462 conf->f_ticket_write = f_ticket_write;
8463 conf->f_ticket_parse = f_ticket_parse;
8464 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008465}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008468
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008469#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8470void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8471 mbedtls_ssl_export_keys_t *f_export_keys,
8472 void *p_export_keys )
8473{
8474 conf->f_export_keys = f_export_keys;
8475 conf->p_export_keys = p_export_keys;
8476}
8477#endif
8478
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008479#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008480void mbedtls_ssl_conf_async_private_cb(
8481 mbedtls_ssl_config *conf,
8482 mbedtls_ssl_async_sign_t *f_async_sign,
8483 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8484 mbedtls_ssl_async_resume_t *f_async_resume,
8485 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008486 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008487{
8488 conf->f_async_sign_start = f_async_sign;
8489 conf->f_async_decrypt_start = f_async_decrypt;
8490 conf->f_async_resume = f_async_resume;
8491 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008492 conf->p_async_config_data = async_config_data;
8493}
8494
Gilles Peskine8f97af72018-04-26 11:46:10 +02008495void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8496{
8497 return( conf->p_async_config_data );
8498}
8499
Gilles Peskine1febfef2018-04-30 11:54:39 +02008500void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008501{
8502 if( ssl->handshake == NULL )
8503 return( NULL );
8504 else
8505 return( ssl->handshake->user_async_ctx );
8506}
8507
Gilles Peskine1febfef2018-04-30 11:54:39 +02008508void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008509 void *ctx )
8510{
8511 if( ssl->handshake != NULL )
8512 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008513}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008514#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008515
Paul Bakker5121ce52009-01-03 21:22:43 +00008516/*
8517 * SSL get accessors
8518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008519size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008520{
8521 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8522}
8523
Hanno Becker8b170a02017-10-10 11:51:19 +01008524int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8525{
8526 /*
8527 * Case A: We're currently holding back
8528 * a message for further processing.
8529 */
8530
8531 if( ssl->keep_current_message == 1 )
8532 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008534 return( 1 );
8535 }
8536
8537 /*
8538 * Case B: Further records are pending in the current datagram.
8539 */
8540
8541#if defined(MBEDTLS_SSL_PROTO_DTLS)
8542 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8543 ssl->in_left > ssl->next_record_offset )
8544 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008546 return( 1 );
8547 }
8548#endif /* MBEDTLS_SSL_PROTO_DTLS */
8549
8550 /*
8551 * Case C: A handshake message is being processed.
8552 */
8553
Hanno Becker8b170a02017-10-10 11:51:19 +01008554 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8555 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008557 return( 1 );
8558 }
8559
8560 /*
8561 * Case D: An application data message is being processed
8562 */
8563 if( ssl->in_offt != NULL )
8564 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008566 return( 1 );
8567 }
8568
8569 /*
8570 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008571 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008572 * we implement support for multiple alerts in single records.
8573 */
8574
8575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8576 return( 0 );
8577}
8578
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008579uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008580{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008581 if( ssl->session != NULL )
8582 return( ssl->session->verify_result );
8583
8584 if( ssl->session_negotiate != NULL )
8585 return( ssl->session_negotiate->verify_result );
8586
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008587 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008588}
8589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008591{
Paul Bakker926c8e42013-03-06 10:23:34 +01008592 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008593 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008596}
8597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008601 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008602 {
8603 switch( ssl->minor_ver )
8604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008606 return( "DTLSv1.0" );
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008609 return( "DTLSv1.2" );
8610
8611 default:
8612 return( "unknown (DTLS)" );
8613 }
8614 }
8615#endif
8616
Paul Bakker43ca69c2011-01-15 17:35:19 +00008617 switch( ssl->minor_ver )
8618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008620 return( "SSLv3.0" );
8621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008623 return( "TLSv1.0" );
8624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008626 return( "TLSv1.1" );
8627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008629 return( "TLSv1.2" );
8630
Paul Bakker43ca69c2011-01-15 17:35:19 +00008631 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008632 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008633 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008634}
8635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008637{
Hanno Becker3136ede2018-08-17 15:28:19 +01008638 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008640 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008641
Hanno Becker43395762019-05-03 14:46:38 +01008642 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8643
Hanno Becker78640902018-08-13 16:35:15 +01008644 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008645 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647#if defined(MBEDTLS_ZLIB_SUPPORT)
8648 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8649 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008650#endif
8651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 case MBEDTLS_MODE_GCM:
8655 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008656 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008657 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008658 transform_expansion = transform->minlen;
8659 break;
8660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008662
8663 block_size = mbedtls_cipher_get_block_size(
8664 &transform->cipher_ctx_enc );
8665
Hanno Becker3136ede2018-08-17 15:28:19 +01008666 /* Expansion due to the addition of the MAC. */
8667 transform_expansion += transform->maclen;
8668
8669 /* Expansion due to the addition of CBC padding;
8670 * Theoretically up to 256 bytes, but we never use
8671 * more than the block size of the underlying cipher. */
8672 transform_expansion += block_size;
8673
8674 /* For TLS 1.1 or higher, an explicit IV is added
8675 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008676#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8677 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008678 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008679#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008680
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008681 break;
8682
8683 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008686 }
8687
Hanno Beckera5a2b082019-05-15 14:03:01 +01008688#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008689 if( transform->out_cid_len != 0 )
8690 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008691#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008692
Hanno Becker43395762019-05-03 14:46:38 +01008693 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008694}
8695
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008696#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8697size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8698{
8699 size_t max_len;
8700
8701 /*
8702 * Assume mfl_code is correct since it was checked when set
8703 */
Angus Grattond8213d02016-05-25 20:56:48 +10008704 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008705
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008706 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008707 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008708 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008709 {
Angus Grattond8213d02016-05-25 20:56:48 +10008710 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008711 }
8712
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008713 /* During a handshake, use the value being negotiated */
8714 if( ssl->session_negotiate != NULL &&
8715 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8716 {
8717 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8718 }
8719
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008720 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008721}
8722#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8723
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008724#if defined(MBEDTLS_SSL_PROTO_DTLS)
8725static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8726{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008727 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8728 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8729 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8730 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8731 return ( 0 );
8732
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008733 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8734 return( ssl->mtu );
8735
8736 if( ssl->mtu == 0 )
8737 return( ssl->handshake->mtu );
8738
8739 return( ssl->mtu < ssl->handshake->mtu ?
8740 ssl->mtu : ssl->handshake->mtu );
8741}
8742#endif /* MBEDTLS_SSL_PROTO_DTLS */
8743
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008744int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8745{
8746 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8747
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008748#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8749 !defined(MBEDTLS_SSL_PROTO_DTLS)
8750 (void) ssl;
8751#endif
8752
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008753#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8754 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8755
8756 if( max_len > mfl )
8757 max_len = mfl;
8758#endif
8759
8760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008761 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008762 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008763 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008764 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8765 const size_t overhead = (size_t) ret;
8766
8767 if( ret < 0 )
8768 return( ret );
8769
8770 if( mtu <= overhead )
8771 {
8772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8773 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8774 }
8775
8776 if( max_len > mtu - overhead )
8777 max_len = mtu - overhead;
8778 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008779#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008780
Hanno Becker0defedb2018-08-10 12:35:02 +01008781#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8782 !defined(MBEDTLS_SSL_PROTO_DTLS)
8783 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008784#endif
8785
8786 return( (int) max_len );
8787}
8788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789#if defined(MBEDTLS_X509_CRT_PARSE_C)
8790const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008791{
8792 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008793 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008794
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008795 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#if defined(MBEDTLS_SSL_CLI_C)
8800int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008801{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008802 if( ssl == NULL ||
8803 dst == NULL ||
8804 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008805 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008808 }
8809
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008810 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008811}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008813
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008814const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8815{
8816 if( ssl == NULL )
8817 return( NULL );
8818
8819 return( ssl->session );
8820}
8821
Paul Bakker5121ce52009-01-03 21:22:43 +00008822/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008823 * Define ticket header determining Mbed TLS version
8824 * and structure of the ticket.
8825 */
8826
Hanno Becker41527622019-05-16 12:50:45 +01008827/*
Hanno Becker26829e92019-05-28 14:30:45 +01008828 * Define bitflag determining compile-time settings influencing
8829 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008830 */
8831
Hanno Becker26829e92019-05-28 14:30:45 +01008832#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008833#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008834#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008835#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008836#endif /* MBEDTLS_HAVE_TIME */
8837
8838#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008839#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008840#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008841#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008842#endif /* MBEDTLS_X509_CRT_PARSE_C */
8843
8844#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008845#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008846#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008847#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008848#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8849
8850#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008851#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008852#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008853#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008854#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8855
8856#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008857#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008858#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008859#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008860#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8861
8862#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008863#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008864#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008865#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008866#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8867
Hanno Becker41527622019-05-16 12:50:45 +01008868#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8869#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8870#else
8871#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8872#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8873
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008874#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8875#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8876#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8877#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8878#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8879#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8880#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008881
Hanno Becker26829e92019-05-28 14:30:45 +01008882#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008883 ( (uint16_t) ( \
8884 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8885 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8886 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8887 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8888 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8889 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008890 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008891
Hanno Becker557fe9f2019-05-16 12:41:07 +01008892static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008893 MBEDTLS_VERSION_MAJOR,
8894 MBEDTLS_VERSION_MINOR,
8895 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008896 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8897 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008898};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008899
8900/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008901 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008902 * (in the presentation language of TLS, RFC 8446 section 3)
8903 *
Hanno Becker26829e92019-05-28 14:30:45 +01008904 * opaque mbedtls_version[3]; // major, minor, patch
8905 * opaque session_format[2]; // version-specific 16-bit field determining
8906 * // the format of the remaining
8907 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008908 *
8909 * Note: When updating the format, remember to keep
8910 * these version+format bytes.
8911 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008912 * // In this version, `session_format` determines
8913 * // the setting of those compile-time
8914 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008915 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008916 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008917 * uint8 ciphersuite[2]; // defined by the standard
8918 * uint8 compression; // 0 or 1
8919 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008920 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008921 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008922 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008923 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8924 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008925 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008926 * uint8 mfl_code; // up to 255 according to standard
8927 * uint8 trunc_hmac; // 0 or 1
8928 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008929 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008930 * The order is the same as in the definition of the structure, except
8931 * verify_result is put before peer_cert so that all mandatory fields come
8932 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008933 */
8934int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8935 unsigned char *buf,
8936 size_t buf_len,
8937 size_t *olen )
8938{
8939 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008940 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008941#if defined(MBEDTLS_HAVE_TIME)
8942 uint64_t start;
8943#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008944#if defined(MBEDTLS_X509_CRT_PARSE_C)
8945 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008946#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008947
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008948 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008949 * Add version identifier
8950 */
8951
8952 used += sizeof( ssl_serialized_session_header );
8953
8954 if( used <= buf_len )
8955 {
8956 memcpy( p, ssl_serialized_session_header,
8957 sizeof( ssl_serialized_session_header ) );
8958 p += sizeof( ssl_serialized_session_header );
8959 }
8960
8961 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008962 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008963 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008964#if defined(MBEDTLS_HAVE_TIME)
8965 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008966
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008967 if( used <= buf_len )
8968 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008969 start = (uint64_t) session->start;
8970
8971 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8972 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8973 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8974 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8975 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
8976 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
8977 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
8978 *p++ = (unsigned char)( ( start ) & 0xFF );
8979 }
8980#endif /* MBEDTLS_HAVE_TIME */
8981
8982 /*
8983 * Basic mandatory fields
8984 */
8985 used += 2 /* ciphersuite */
8986 + 1 /* compression */
8987 + 1 /* id_len */
8988 + sizeof( session->id )
8989 + sizeof( session->master )
8990 + 4; /* verify_result */
8991
8992 if( used <= buf_len )
8993 {
8994 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
8995 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
8996
8997 *p++ = (unsigned char)( session->compression & 0xFF );
8998
8999 *p++ = (unsigned char)( session->id_len & 0xFF );
9000 memcpy( p, session->id, 32 );
9001 p += 32;
9002
9003 memcpy( p, session->master, 48 );
9004 p += 48;
9005
9006 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9007 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9008 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9009 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009010 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009011
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009012 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009013 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009014 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009015#if defined(MBEDTLS_X509_CRT_PARSE_C)
9016 if( session->peer_cert == NULL )
9017 cert_len = 0;
9018 else
9019 cert_len = session->peer_cert->raw.len;
9020
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009021 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009022
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009023 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009024 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009025 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9026 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9027 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9028
9029 if( session->peer_cert != NULL )
9030 {
9031 memcpy( p, session->peer_cert->raw.p, cert_len );
9032 p += cert_len;
9033 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009034 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009035#endif /* MBEDTLS_X509_CRT_PARSE_C */
9036
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009037 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009038 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009039 */
9040#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009041 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009042
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009043 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009044 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009045 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9046 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9047 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9048
9049 if( session->ticket != NULL )
9050 {
9051 memcpy( p, session->ticket, session->ticket_len );
9052 p += session->ticket_len;
9053 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009054
9055 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9056 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9057 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9058 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009059 }
9060#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9061
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009062 /*
9063 * Misc extension-related info
9064 */
9065#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9066 used += 1;
9067
9068 if( used <= buf_len )
9069 *p++ = session->mfl_code;
9070#endif
9071
9072#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9073 used += 1;
9074
9075 if( used <= buf_len )
9076 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9077#endif
9078
9079#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9080 used += 1;
9081
9082 if( used <= buf_len )
9083 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9084#endif
9085
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009086 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009087 *olen = used;
9088
9089 if( used > buf_len )
9090 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009091
9092 return( 0 );
9093}
9094
9095/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009096 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009097 *
9098 * This internal version is wrapped by a public function that cleans up in
9099 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009100 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009101static int ssl_session_load( mbedtls_ssl_session *session,
9102 const unsigned char *buf,
9103 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009104{
9105 const unsigned char *p = buf;
9106 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009107#if defined(MBEDTLS_HAVE_TIME)
9108 uint64_t start;
9109#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009110#if defined(MBEDTLS_X509_CRT_PARSE_C)
9111 size_t cert_len;
9112#endif /* MBEDTLS_X509_CRT_PARSE_C */
9113
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009114 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009115 * Check version identifier
9116 */
9117
9118 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009120
9121 if( memcmp( p, ssl_serialized_session_header,
9122 sizeof( ssl_serialized_session_header ) ) != 0 )
9123 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009124 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009125 }
9126 p += sizeof( ssl_serialized_session_header );
9127
9128 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009129 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009130 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009131#if defined(MBEDTLS_HAVE_TIME)
9132 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9134
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009135 start = ( (uint64_t) p[0] << 56 ) |
9136 ( (uint64_t) p[1] << 48 ) |
9137 ( (uint64_t) p[2] << 40 ) |
9138 ( (uint64_t) p[3] << 32 ) |
9139 ( (uint64_t) p[4] << 24 ) |
9140 ( (uint64_t) p[5] << 16 ) |
9141 ( (uint64_t) p[6] << 8 ) |
9142 ( (uint64_t) p[7] );
9143 p += 8;
9144
9145 session->start = (time_t) start;
9146#endif /* MBEDTLS_HAVE_TIME */
9147
9148 /*
9149 * Basic mandatory fields
9150 */
9151 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9153
9154 session->ciphersuite = ( p[0] << 8 ) | p[1];
9155 p += 2;
9156
9157 session->compression = *p++;
9158
9159 session->id_len = *p++;
9160 memcpy( session->id, p, 32 );
9161 p += 32;
9162
9163 memcpy( session->master, p, 48 );
9164 p += 48;
9165
9166 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9167 ( (uint32_t) p[1] << 16 ) |
9168 ( (uint32_t) p[2] << 8 ) |
9169 ( (uint32_t) p[3] );
9170 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009171
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009172 /* Immediately clear invalid pointer values that have been read, in case
9173 * we exit early before we replaced them with valid ones. */
9174#if defined(MBEDTLS_X509_CRT_PARSE_C)
9175 session->peer_cert = NULL;
9176#endif
9177#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9178 session->ticket = NULL;
9179#endif
9180
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009181 /*
9182 * Peer certificate
9183 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009184#if defined(MBEDTLS_X509_CRT_PARSE_C)
9185 if( 3 > (size_t)( end - p ) )
9186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9187
9188 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9189 p += 3;
9190
9191 if( cert_len == 0 )
9192 {
9193 session->peer_cert = NULL;
9194 }
9195 else
9196 {
9197 int ret;
9198
9199 if( cert_len > (size_t)( end - p ) )
9200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9201
9202 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9203
9204 if( session->peer_cert == NULL )
9205 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9206
9207 mbedtls_x509_crt_init( session->peer_cert );
9208
9209 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9210 p, cert_len ) ) != 0 )
9211 {
9212 mbedtls_x509_crt_free( session->peer_cert );
9213 mbedtls_free( session->peer_cert );
9214 session->peer_cert = NULL;
9215 return( ret );
9216 }
9217
9218 p += cert_len;
9219 }
9220#endif /* MBEDTLS_X509_CRT_PARSE_C */
9221
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009222 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009223 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009224 */
9225#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9226 if( 3 > (size_t)( end - p ) )
9227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9228
9229 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9230 p += 3;
9231
9232 if( session->ticket_len != 0 )
9233 {
9234 if( session->ticket_len > (size_t)( end - p ) )
9235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9236
9237 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9238 if( session->ticket == NULL )
9239 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9240
9241 memcpy( session->ticket, p, session->ticket_len );
9242 p += session->ticket_len;
9243 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009244
9245 if( 4 > (size_t)( end - p ) )
9246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9247
9248 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9249 ( (uint32_t) p[1] << 16 ) |
9250 ( (uint32_t) p[2] << 8 ) |
9251 ( (uint32_t) p[3] );
9252 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009253#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9254
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009255 /*
9256 * Misc extension-related info
9257 */
9258#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9259 if( 1 > (size_t)( end - p ) )
9260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9261
9262 session->mfl_code = *p++;
9263#endif
9264
9265#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9266 if( 1 > (size_t)( end - p ) )
9267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9268
9269 session->trunc_hmac = *p++;
9270#endif
9271
9272#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9273 if( 1 > (size_t)( end - p ) )
9274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9275
9276 session->encrypt_then_mac = *p++;
9277#endif
9278
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009279 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009280 if( p != end )
9281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9282
9283 return( 0 );
9284}
9285
9286/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009287 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009288 */
9289int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9290 const unsigned char *buf,
9291 size_t len )
9292{
9293 int ret = ssl_session_load( session, buf, len );
9294
9295 if( ret != 0 )
9296 mbedtls_ssl_session_free( session );
9297
9298 return( ret );
9299}
9300
9301/*
Paul Bakker1961b702013-01-25 14:49:24 +01009302 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009307
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009308 if( ssl == NULL || ssl->conf == NULL )
9309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009312 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009316 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009318#endif
9319
Paul Bakker1961b702013-01-25 14:49:24 +01009320 return( ret );
9321}
9322
9323/*
9324 * Perform the SSL handshake
9325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009327{
9328 int ret = 0;
9329
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009330 if( ssl == NULL || ssl->conf == NULL )
9331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009338
9339 if( ret != 0 )
9340 break;
9341 }
9342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009344
9345 return( ret );
9346}
9347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#if defined(MBEDTLS_SSL_RENEGOTIATION)
9349#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009350/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009351 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009354{
9355 int ret;
9356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009358
9359 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9361 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009362
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009363 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009364 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009366 return( ret );
9367 }
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009370
9371 return( 0 );
9372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009374
9375/*
9376 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 * - any side: calling mbedtls_ssl_renegotiate(),
9378 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9379 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009380 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009381 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009385{
9386 int ret;
9387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009389
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009390 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9391 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009392
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009393 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9394 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009398 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009399 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009400 ssl->handshake->out_msg_seq = 1;
9401 else
9402 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009403 }
9404#endif
9405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9407 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009412 return( ret );
9413 }
9414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009416
9417 return( 0 );
9418}
9419
9420/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009421 * Renegotiate current connection on client,
9422 * or request renegotiation on server
9423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009427
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009428 if( ssl == NULL || ssl->conf == NULL )
9429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009432 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009439
9440 /* Did we already try/start sending HelloRequest? */
9441 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009443
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009444 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009449 /*
9450 * On client, either start the renegotiation process or,
9451 * if already in progress, continue the handshake
9452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009457
9458 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009461 return( ret );
9462 }
9463 }
9464 else
9465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009469 return( ret );
9470 }
9471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009473
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009474 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009475}
9476
9477/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009478 * Check record counters and renegotiate if they're above the limit.
9479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009481{
Andres AG2196c7f2016-12-15 17:01:16 +00009482 size_t ep_len = ssl_ep_len( ssl );
9483 int in_ctr_cmp;
9484 int out_ctr_cmp;
9485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9487 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009488 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009489 {
9490 return( 0 );
9491 }
9492
Andres AG2196c7f2016-12-15 17:01:16 +00009493 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9494 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009495 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009496 ssl->conf->renego_period + ep_len, 8 - ep_len );
9497
9498 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009499 {
9500 return( 0 );
9501 }
9502
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009507
9508/*
9509 * Receive application data decrypted from the SSL layer
9510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009512{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009513 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009514 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009515
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009516 if( ssl == NULL || ssl->conf == NULL )
9517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009522 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009525 return( ret );
9526
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009527 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009529 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009530 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009531 return( ret );
9532 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009533 }
9534#endif
9535
Hanno Becker4a810fb2017-05-24 16:27:30 +01009536 /*
9537 * Check if renegotiation is necessary and/or handshake is
9538 * in process. If yes, perform/continue, and fall through
9539 * if an unexpected packet is received while the client
9540 * is waiting for the ServerHello.
9541 *
9542 * (There is no equivalent to the last condition on
9543 * the server-side as it is not treated as within
9544 * a handshake while waiting for the ClientHello
9545 * after a renegotiation request.)
9546 */
9547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009549 ret = ssl_check_ctr_renegotiate( ssl );
9550 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9551 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009554 return( ret );
9555 }
9556#endif
9557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009561 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9562 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009565 return( ret );
9566 }
9567 }
9568
Hanno Beckere41158b2017-10-23 13:30:32 +01009569 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009570 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009571 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009572 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009573 if( ssl->f_get_timer != NULL &&
9574 ssl->f_get_timer( ssl->p_timer ) == -1 )
9575 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009576 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009577 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009578
Hanno Becker327c93b2018-08-15 13:56:18 +01009579 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009580 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009581 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9582 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009583
Hanno Becker4a810fb2017-05-24 16:27:30 +01009584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9585 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009586 }
9587
9588 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009590 {
9591 /*
9592 * OpenSSL sends empty messages to randomize the IV
9593 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009594 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009597 return( 0 );
9598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009600 return( ret );
9601 }
9602 }
9603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009607
Hanno Becker4a810fb2017-05-24 16:27:30 +01009608 /*
9609 * - For client-side, expect SERVER_HELLO_REQUEST.
9610 * - For server-side, expect CLIENT_HELLO.
9611 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9612 */
9613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009615 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009617 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009620
9621 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009623 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009624 {
9625 continue;
9626 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009629 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009630#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009631
Hanno Becker4a810fb2017-05-24 16:27:30 +01009632#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009633 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009637
9638 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009640 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009641 {
9642 continue;
9643 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009646 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009647#endif /* MBEDTLS_SSL_SRV_C */
9648
Hanno Becker21df7f92017-10-17 11:03:26 +01009649#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009650 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009651 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9652 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9653 ssl->conf->allow_legacy_renegotiation ==
9654 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9655 {
9656 /*
9657 * Accept renegotiation request
9658 */
Paul Bakker48916f92012-09-16 19:57:18 +00009659
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009660 /* DTLS clients need to know renego is server-initiated */
9661#if defined(MBEDTLS_SSL_PROTO_DTLS)
9662 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9663 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9664 {
9665 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9666 }
9667#endif
9668 ret = ssl_start_renegotiation( ssl );
9669 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9670 ret != 0 )
9671 {
9672 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9673 return( ret );
9674 }
9675 }
9676 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009677#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009678 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009679 /*
9680 * Refuse renegotiation
9681 */
9682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685#if defined(MBEDTLS_SSL_PROTO_SSL3)
9686 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009687 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009688 /* SSLv3 does not have a "no_renegotiation" warning, so
9689 we send a fatal alert and abort the connection. */
9690 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9691 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9692 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009693 }
9694 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9696#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9697 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9698 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9701 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9702 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009703 {
9704 return( ret );
9705 }
Paul Bakker48916f92012-09-16 19:57:18 +00009706 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009707 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9709 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009713 }
Paul Bakker48916f92012-09-16 19:57:18 +00009714 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009715
Hanno Becker90333da2017-10-10 11:27:13 +01009716 /* At this point, we don't know whether the renegotiation has been
9717 * completed or not. The cases to consider are the following:
9718 * 1) The renegotiation is complete. In this case, no new record
9719 * has been read yet.
9720 * 2) The renegotiation is incomplete because the client received
9721 * an application data record while awaiting the ServerHello.
9722 * 3) The renegotiation is incomplete because the client received
9723 * a non-handshake, non-application data message while awaiting
9724 * the ServerHello.
9725 * In each of these case, looping will be the proper action:
9726 * - For 1), the next iteration will read a new record and check
9727 * if it's application data.
9728 * - For 2), the loop condition isn't satisfied as application data
9729 * is present, hence continue is the same as break
9730 * - For 3), the loop condition is satisfied and read_record
9731 * will re-deliver the message that was held back by the client
9732 * when expecting the ServerHello.
9733 */
9734 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009735 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009736#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009738 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009739 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009740 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009741 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009744 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009746 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009747 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009748 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9752 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009755 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009756 }
9757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9761 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009762 }
9763
9764 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009765
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009766 /* We're going to return something now, cancel timer,
9767 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009769 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009770
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009772 /* If we requested renego but received AppData, resend HelloRequest.
9773 * Do it now, after setting in_offt, to avoid taking this branch
9774 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009776 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009778 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009779 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009782 return( ret );
9783 }
9784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009786#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 }
9788
9789 n = ( len < ssl->in_msglen )
9790 ? len : ssl->in_msglen;
9791
9792 memcpy( buf, ssl->in_offt, n );
9793 ssl->in_msglen -= n;
9794
9795 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009796 {
9797 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009798 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009799 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009800 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009801 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009802 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009803 /* more data available */
9804 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009808
Paul Bakker23986e52011-04-24 08:57:21 +00009809 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009810}
9811
9812/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009813 * Send application data to be encrypted by the SSL layer, taking care of max
9814 * fragment length and buffer size.
9815 *
9816 * According to RFC 5246 Section 6.2.1:
9817 *
9818 * Zero-length fragments of Application data MAY be sent as they are
9819 * potentially useful as a traffic analysis countermeasure.
9820 *
9821 * Therefore, it is possible that the input message length is 0 and the
9822 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009823 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009824static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009825 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009826{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009827 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9828 const size_t max_len = (size_t) ret;
9829
9830 if( ret < 0 )
9831 {
9832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9833 return( ret );
9834 }
9835
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009836 if( len > max_len )
9837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009839 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009842 "maximum fragment length: %d > %d",
9843 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009845 }
9846 else
9847#endif
9848 len = max_len;
9849 }
Paul Bakker887bd502011-06-08 13:10:54 +00009850
Paul Bakker5121ce52009-01-03 21:22:43 +00009851 if( ssl->out_left != 0 )
9852 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009853 /*
9854 * The user has previously tried to send the data and
9855 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9856 * written. In this case, we expect the high-level write function
9857 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009862 return( ret );
9863 }
9864 }
Paul Bakker887bd502011-06-08 13:10:54 +00009865 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009866 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009867 /*
9868 * The user is trying to send a message the first time, so we need to
9869 * copy the data into the internal buffers and setup the data structure
9870 * to keep track of partial writes
9871 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009872 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009874 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009875
Hanno Becker67bc7c32018-08-06 11:33:50 +01009876 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009879 return( ret );
9880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009881 }
9882
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009883 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009884}
9885
9886/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009887 * Write application data, doing 1/n-1 splitting if necessary.
9888 *
9889 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009890 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009891 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009894static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009895 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009896{
9897 int ret;
9898
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009899 if( ssl->conf->cbc_record_splitting ==
9900 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009901 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9903 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9904 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009905 {
9906 return( ssl_write_real( ssl, buf, len ) );
9907 }
9908
9909 if( ssl->split_done == 0 )
9910 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009911 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009912 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009913 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009914 }
9915
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009916 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9917 return( ret );
9918 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009919
9920 return( ret + 1 );
9921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009923
9924/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009925 * Write application data (public-facing wrapper)
9926 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009927int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009928{
9929 int ret;
9930
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009932
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009933 if( ssl == NULL || ssl->conf == NULL )
9934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9935
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009936#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009937 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9938 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009939 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009940 return( ret );
9941 }
9942#endif
9943
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009944 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009945 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009946 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009947 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009949 return( ret );
9950 }
9951 }
9952
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009953#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009954 ret = ssl_write_split( ssl, buf, len );
9955#else
9956 ret = ssl_write_real( ssl, buf, len );
9957#endif
9958
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009960
9961 return( ret );
9962}
9963
9964/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009965 * Notify the peer that the connection is being closed
9966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009968{
9969 int ret;
9970
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009971 if( ssl == NULL || ssl->conf == NULL )
9972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009975
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009976 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9982 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9983 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009986 return( ret );
9987 }
9988 }
9989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009991
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009992 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009993}
9994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009996{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009997 if( transform == NULL )
9998 return;
9999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010001 deflateEnd( &transform->ctx_deflate );
10002 inflateEnd( &transform->ctx_inflate );
10003#endif
10004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10006 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010007
Hanno Becker92231322018-01-03 15:32:51 +000010008#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009 mbedtls_md_free( &transform->md_ctx_enc );
10010 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010011#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010012
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010013 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010014}
10015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016#if defined(MBEDTLS_X509_CRT_PARSE_C)
10017static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010020
10021 while( cur != NULL )
10022 {
10023 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010024 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010025 cur = next;
10026 }
10027}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010029
Hanno Becker0271f962018-08-16 13:23:47 +010010030#if defined(MBEDTLS_SSL_PROTO_DTLS)
10031
10032static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10033{
10034 unsigned offset;
10035 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10036
10037 if( hs == NULL )
10038 return;
10039
Hanno Becker283f5ef2018-08-24 09:34:47 +010010040 ssl_free_buffered_record( ssl );
10041
Hanno Becker0271f962018-08-16 13:23:47 +010010042 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010043 ssl_buffering_free_slot( ssl, offset );
10044}
10045
10046static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10047 uint8_t slot )
10048{
10049 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10050 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010051
10052 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10053 return;
10054
Hanno Beckere605b192018-08-21 15:59:07 +010010055 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010056 {
Hanno Beckere605b192018-08-21 15:59:07 +010010057 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010058 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010059 mbedtls_free( hs_buf->data );
10060 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010061 }
10062}
10063
10064#endif /* MBEDTLS_SSL_PROTO_DTLS */
10065
Gilles Peskine9b562d52018-04-25 20:32:43 +020010066void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010067{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010068 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10069
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010070 if( handshake == NULL )
10071 return;
10072
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010073#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10074 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10075 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010076 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010077 handshake->async_in_progress = 0;
10078 }
10079#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10080
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010081#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10082 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10083 mbedtls_md5_free( &handshake->fin_md5 );
10084 mbedtls_sha1_free( &handshake->fin_sha1 );
10085#endif
10086#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10087#if defined(MBEDTLS_SHA256_C)
10088 mbedtls_sha256_free( &handshake->fin_sha256 );
10089#endif
10090#if defined(MBEDTLS_SHA512_C)
10091 mbedtls_sha512_free( &handshake->fin_sha512 );
10092#endif
10093#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095#if defined(MBEDTLS_DHM_C)
10096 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098#if defined(MBEDTLS_ECDH_C)
10099 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010100#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010101#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010102 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010103#if defined(MBEDTLS_SSL_CLI_C)
10104 mbedtls_free( handshake->ecjpake_cache );
10105 handshake->ecjpake_cache = NULL;
10106 handshake->ecjpake_cache_len = 0;
10107#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010108#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010109
Janos Follath4ae5c292016-02-10 11:27:43 +000010110#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10111 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010112 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010114#endif
10115
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010116#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10117 if( handshake->psk != NULL )
10118 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010119 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010120 mbedtls_free( handshake->psk );
10121 }
10122#endif
10123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10125 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010126 /*
10127 * Free only the linked list wrapper, not the keys themselves
10128 * since the belong to the SNI callback
10129 */
10130 if( handshake->sni_key_cert != NULL )
10131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010133
10134 while( cur != NULL )
10135 {
10136 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010138 cur = next;
10139 }
10140 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010141#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010142
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010143#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010144 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010145#endif
10146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147#if defined(MBEDTLS_SSL_PROTO_DTLS)
10148 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010149 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010150 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010151#endif
10152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010153 mbedtls_platform_zeroize( handshake,
10154 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010155}
10156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010157void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010158{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010159 if( session == NULL )
10160 return;
10161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010162#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000010163 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +000010164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165 mbedtls_x509_crt_free( session->peer_cert );
10166 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +000010167 }
Paul Bakkered27a042013-04-18 22:46:23 +020010168#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010169
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010170#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010172#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010173
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010174 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010175}
10176
Paul Bakker5121ce52009-01-03 21:22:43 +000010177/*
10178 * Free an SSL context
10179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010181{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010182 if( ssl == NULL )
10183 return;
10184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010186
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010187 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010188 {
Angus Grattond8213d02016-05-25 20:56:48 +100010189 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010191 }
10192
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010193 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010194 {
Angus Grattond8213d02016-05-25 20:56:48 +100010195 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010197 }
10198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010200 if( ssl->compress_buf != NULL )
10201 {
Angus Grattond8213d02016-05-25 20:56:48 +100010202 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010204 }
10205#endif
10206
Paul Bakker48916f92012-09-16 19:57:18 +000010207 if( ssl->transform )
10208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 mbedtls_ssl_transform_free( ssl->transform );
10210 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010211 }
10212
10213 if( ssl->handshake )
10214 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010215 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10217 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 mbedtls_free( ssl->handshake );
10220 mbedtls_free( ssl->transform_negotiate );
10221 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010222 }
10223
Paul Bakkerc0463502013-02-14 11:19:38 +010010224 if( ssl->session )
10225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 mbedtls_ssl_session_free( ssl->session );
10227 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010228 }
10229
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010230#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010231 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010232 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010233 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010235 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10239 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10242 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010243 }
10244#endif
10245
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010246#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010248#endif
10249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010251
Paul Bakker86f04f42013-02-14 11:20:09 +010010252 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010253 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010254}
10255
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010256/*
10257 * Initialze mbedtls_ssl_config
10258 */
10259void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10260{
10261 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10262}
10263
Simon Butcherc97b6972015-12-27 23:48:17 +000010264#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010265static int ssl_preset_default_hashes[] = {
10266#if defined(MBEDTLS_SHA512_C)
10267 MBEDTLS_MD_SHA512,
10268 MBEDTLS_MD_SHA384,
10269#endif
10270#if defined(MBEDTLS_SHA256_C)
10271 MBEDTLS_MD_SHA256,
10272 MBEDTLS_MD_SHA224,
10273#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010274#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010275 MBEDTLS_MD_SHA1,
10276#endif
10277 MBEDTLS_MD_NONE
10278};
Simon Butcherc97b6972015-12-27 23:48:17 +000010279#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010280
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010281static int ssl_preset_suiteb_ciphersuites[] = {
10282 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10283 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10284 0
10285};
10286
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010287#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010288static int ssl_preset_suiteb_hashes[] = {
10289 MBEDTLS_MD_SHA256,
10290 MBEDTLS_MD_SHA384,
10291 MBEDTLS_MD_NONE
10292};
10293#endif
10294
10295#if defined(MBEDTLS_ECP_C)
10296static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10297 MBEDTLS_ECP_DP_SECP256R1,
10298 MBEDTLS_ECP_DP_SECP384R1,
10299 MBEDTLS_ECP_DP_NONE
10300};
10301#endif
10302
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010303/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010304 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010305 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010306int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010307 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010308{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010309#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010310 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010311#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010312
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010313 /* Use the functions here so that they are covered in tests,
10314 * but otherwise access member directly for efficiency */
10315 mbedtls_ssl_conf_endpoint( conf, endpoint );
10316 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010317
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010318 /*
10319 * Things that are common to all presets
10320 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010321#if defined(MBEDTLS_SSL_CLI_C)
10322 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10323 {
10324 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10325#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10326 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10327#endif
10328 }
10329#endif
10330
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010331#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010332 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010333#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010334
10335#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10336 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10337#endif
10338
10339#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10340 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10341#endif
10342
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010343#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10344 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10345#endif
10346
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010347#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010348 conf->f_cookie_write = ssl_cookie_write_dummy;
10349 conf->f_cookie_check = ssl_cookie_check_dummy;
10350#endif
10351
10352#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10353 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10354#endif
10355
Janos Follath088ce432017-04-10 12:42:31 +010010356#if defined(MBEDTLS_SSL_SRV_C)
10357 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10358#endif
10359
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010360#if defined(MBEDTLS_SSL_PROTO_DTLS)
10361 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10362 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10363#endif
10364
10365#if defined(MBEDTLS_SSL_RENEGOTIATION)
10366 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010367 memset( conf->renego_period, 0x00, 2 );
10368 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010369#endif
10370
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010371#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10372 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10373 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010374 const unsigned char dhm_p[] =
10375 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10376 const unsigned char dhm_g[] =
10377 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10378
Hanno Beckera90658f2017-10-04 15:29:08 +010010379 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10380 dhm_p, sizeof( dhm_p ),
10381 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010382 {
10383 return( ret );
10384 }
10385 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010386#endif
10387
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010388 /*
10389 * Preset-specific defaults
10390 */
10391 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010392 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010393 /*
10394 * NSA Suite B
10395 */
10396 case MBEDTLS_SSL_PRESET_SUITEB:
10397 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10398 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10399 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10400 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10401
10402 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10403 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10404 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10405 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10406 ssl_preset_suiteb_ciphersuites;
10407
10408#if defined(MBEDTLS_X509_CRT_PARSE_C)
10409 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010410#endif
10411
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010412#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010413 conf->sig_hashes = ssl_preset_suiteb_hashes;
10414#endif
10415
10416#if defined(MBEDTLS_ECP_C)
10417 conf->curve_list = ssl_preset_suiteb_curves;
10418#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010419 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010420
10421 /*
10422 * Default
10423 */
10424 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010425 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10426 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10427 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10428 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10429 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10430 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10431 MBEDTLS_SSL_MIN_MINOR_VERSION :
10432 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010433 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10434 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10435
10436#if defined(MBEDTLS_SSL_PROTO_DTLS)
10437 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10438 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10439#endif
10440
10441 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10442 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10443 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10444 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10445 mbedtls_ssl_list_ciphersuites();
10446
10447#if defined(MBEDTLS_X509_CRT_PARSE_C)
10448 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10449#endif
10450
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010451#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010452 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010453#endif
10454
10455#if defined(MBEDTLS_ECP_C)
10456 conf->curve_list = mbedtls_ecp_grp_id_list();
10457#endif
10458
10459#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10460 conf->dhm_min_bitlen = 1024;
10461#endif
10462 }
10463
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010464 return( 0 );
10465}
10466
10467/*
10468 * Free mbedtls_ssl_config
10469 */
10470void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10471{
10472#if defined(MBEDTLS_DHM_C)
10473 mbedtls_mpi_free( &conf->dhm_P );
10474 mbedtls_mpi_free( &conf->dhm_G );
10475#endif
10476
10477#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10478 if( conf->psk != NULL )
10479 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010480 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010481 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010482 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010483 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010484 }
10485
10486 if( conf->psk_identity != NULL )
10487 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010488 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010489 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010490 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010491 conf->psk_identity_len = 0;
10492 }
10493#endif
10494
10495#if defined(MBEDTLS_X509_CRT_PARSE_C)
10496 ssl_key_cert_free( conf->key_cert );
10497#endif
10498
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010499 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010500}
10501
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010502#if defined(MBEDTLS_PK_C) && \
10503 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010504/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010507unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509#if defined(MBEDTLS_RSA_C)
10510 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10511 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010512#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513#if defined(MBEDTLS_ECDSA_C)
10514 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10515 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010518}
10519
Hanno Becker7e5437a2017-04-28 17:15:26 +010010520unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10521{
10522 switch( type ) {
10523 case MBEDTLS_PK_RSA:
10524 return( MBEDTLS_SSL_SIG_RSA );
10525 case MBEDTLS_PK_ECDSA:
10526 case MBEDTLS_PK_ECKEY:
10527 return( MBEDTLS_SSL_SIG_ECDSA );
10528 default:
10529 return( MBEDTLS_SSL_SIG_ANON );
10530 }
10531}
10532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010534{
10535 switch( sig )
10536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537#if defined(MBEDTLS_RSA_C)
10538 case MBEDTLS_SSL_SIG_RSA:
10539 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010541#if defined(MBEDTLS_ECDSA_C)
10542 case MBEDTLS_SSL_SIG_ECDSA:
10543 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010544#endif
10545 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010546 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010547 }
10548}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010549#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010550
Hanno Becker7e5437a2017-04-28 17:15:26 +010010551#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10552 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10553
10554/* Find an entry in a signature-hash set matching a given hash algorithm. */
10555mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10556 mbedtls_pk_type_t sig_alg )
10557{
10558 switch( sig_alg )
10559 {
10560 case MBEDTLS_PK_RSA:
10561 return( set->rsa );
10562 case MBEDTLS_PK_ECDSA:
10563 return( set->ecdsa );
10564 default:
10565 return( MBEDTLS_MD_NONE );
10566 }
10567}
10568
10569/* Add a signature-hash-pair to a signature-hash set */
10570void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10571 mbedtls_pk_type_t sig_alg,
10572 mbedtls_md_type_t md_alg )
10573{
10574 switch( sig_alg )
10575 {
10576 case MBEDTLS_PK_RSA:
10577 if( set->rsa == MBEDTLS_MD_NONE )
10578 set->rsa = md_alg;
10579 break;
10580
10581 case MBEDTLS_PK_ECDSA:
10582 if( set->ecdsa == MBEDTLS_MD_NONE )
10583 set->ecdsa = md_alg;
10584 break;
10585
10586 default:
10587 break;
10588 }
10589}
10590
10591/* Allow exactly one hash algorithm for each signature. */
10592void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10593 mbedtls_md_type_t md_alg )
10594{
10595 set->rsa = md_alg;
10596 set->ecdsa = md_alg;
10597}
10598
10599#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10600 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10601
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010602/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010603 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010606{
10607 switch( hash )
10608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609#if defined(MBEDTLS_MD5_C)
10610 case MBEDTLS_SSL_HASH_MD5:
10611 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010612#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613#if defined(MBEDTLS_SHA1_C)
10614 case MBEDTLS_SSL_HASH_SHA1:
10615 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617#if defined(MBEDTLS_SHA256_C)
10618 case MBEDTLS_SSL_HASH_SHA224:
10619 return( MBEDTLS_MD_SHA224 );
10620 case MBEDTLS_SSL_HASH_SHA256:
10621 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010622#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623#if defined(MBEDTLS_SHA512_C)
10624 case MBEDTLS_SSL_HASH_SHA384:
10625 return( MBEDTLS_MD_SHA384 );
10626 case MBEDTLS_SSL_HASH_SHA512:
10627 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010628#endif
10629 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010631 }
10632}
10633
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010634/*
10635 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10636 */
10637unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10638{
10639 switch( md )
10640 {
10641#if defined(MBEDTLS_MD5_C)
10642 case MBEDTLS_MD_MD5:
10643 return( MBEDTLS_SSL_HASH_MD5 );
10644#endif
10645#if defined(MBEDTLS_SHA1_C)
10646 case MBEDTLS_MD_SHA1:
10647 return( MBEDTLS_SSL_HASH_SHA1 );
10648#endif
10649#if defined(MBEDTLS_SHA256_C)
10650 case MBEDTLS_MD_SHA224:
10651 return( MBEDTLS_SSL_HASH_SHA224 );
10652 case MBEDTLS_MD_SHA256:
10653 return( MBEDTLS_SSL_HASH_SHA256 );
10654#endif
10655#if defined(MBEDTLS_SHA512_C)
10656 case MBEDTLS_MD_SHA384:
10657 return( MBEDTLS_SSL_HASH_SHA384 );
10658 case MBEDTLS_MD_SHA512:
10659 return( MBEDTLS_SSL_HASH_SHA512 );
10660#endif
10661 default:
10662 return( MBEDTLS_SSL_HASH_NONE );
10663 }
10664}
10665
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010666#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010667/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010668 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010669 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010670 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010671int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010672{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010674
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010675 if( ssl->conf->curve_list == NULL )
10676 return( -1 );
10677
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010678 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010679 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010680 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010681
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010682 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010683}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010684#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010685
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010686#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010687/*
10688 * Check if a hash proposed by the peer is in our list.
10689 * Return 0 if we're willing to use it, -1 otherwise.
10690 */
10691int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10692 mbedtls_md_type_t md )
10693{
10694 const int *cur;
10695
10696 if( ssl->conf->sig_hashes == NULL )
10697 return( -1 );
10698
10699 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10700 if( *cur == (int) md )
10701 return( 0 );
10702
10703 return( -1 );
10704}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010705#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707#if defined(MBEDTLS_X509_CRT_PARSE_C)
10708int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10709 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010710 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010711 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010712{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010713 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010715 int usage = 0;
10716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010717#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010718 const char *ext_oid;
10719 size_t ext_len;
10720#endif
10721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10723 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010724 ((void) cert);
10725 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010726 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010727#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10730 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010731 {
10732 /* Server part of the key exchange */
10733 switch( ciphersuite->key_exchange )
10734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735 case MBEDTLS_KEY_EXCHANGE_RSA:
10736 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010737 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010738 break;
10739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10741 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10742 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10743 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010744 break;
10745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10747 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010748 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010749 break;
10750
10751 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 case MBEDTLS_KEY_EXCHANGE_NONE:
10753 case MBEDTLS_KEY_EXCHANGE_PSK:
10754 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10755 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010756 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010757 usage = 0;
10758 }
10759 }
10760 else
10761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10763 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010764 }
10765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010766 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010767 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010768 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010769 ret = -1;
10770 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010771#else
10772 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10776 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10779 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010780 }
10781 else
10782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10784 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010785 }
10786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010788 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010789 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010790 ret = -1;
10791 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010793
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010794 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010797
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010798/*
10799 * Convert version numbers to/from wire format
10800 * and, for DTLS, to/from TLS equivalent.
10801 *
10802 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010803 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010804 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10805 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010808 unsigned char ver[2] )
10809{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010810#if defined(MBEDTLS_SSL_PROTO_DTLS)
10811 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010814 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10815
10816 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10817 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10818 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010819 else
10820#else
10821 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010822#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010823 {
10824 ver[0] = (unsigned char) major;
10825 ver[1] = (unsigned char) minor;
10826 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010827}
10828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010829void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010830 const unsigned char ver[2] )
10831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010832#if defined(MBEDTLS_SSL_PROTO_DTLS)
10833 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010834 {
10835 *major = 255 - ver[0] + 2;
10836 *minor = 255 - ver[1] + 1;
10837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010838 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010839 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10840 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010841 else
10842#else
10843 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010844#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010845 {
10846 *major = ver[0];
10847 *minor = ver[1];
10848 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010849}
10850
Simon Butcher99000142016-10-13 17:21:01 +010010851int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10852{
10853#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10854 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10855 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10856
10857 switch( md )
10858 {
10859#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10860#if defined(MBEDTLS_MD5_C)
10861 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010862 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010863#endif
10864#if defined(MBEDTLS_SHA1_C)
10865 case MBEDTLS_SSL_HASH_SHA1:
10866 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10867 break;
10868#endif
10869#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10870#if defined(MBEDTLS_SHA512_C)
10871 case MBEDTLS_SSL_HASH_SHA384:
10872 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10873 break;
10874#endif
10875#if defined(MBEDTLS_SHA256_C)
10876 case MBEDTLS_SSL_HASH_SHA256:
10877 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10878 break;
10879#endif
10880 default:
10881 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10882 }
10883
10884 return 0;
10885#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10886 (void) ssl;
10887 (void) md;
10888
10889 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10891}
10892
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010893#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10894 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10895int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10896 unsigned char *output,
10897 unsigned char *data, size_t data_len )
10898{
10899 int ret = 0;
10900 mbedtls_md5_context mbedtls_md5;
10901 mbedtls_sha1_context mbedtls_sha1;
10902
10903 mbedtls_md5_init( &mbedtls_md5 );
10904 mbedtls_sha1_init( &mbedtls_sha1 );
10905
10906 /*
10907 * digitally-signed struct {
10908 * opaque md5_hash[16];
10909 * opaque sha_hash[20];
10910 * };
10911 *
10912 * md5_hash
10913 * MD5(ClientHello.random + ServerHello.random
10914 * + ServerParams);
10915 * sha_hash
10916 * SHA(ClientHello.random + ServerHello.random
10917 * + ServerParams);
10918 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010919 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010920 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010922 goto exit;
10923 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010924 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010925 ssl->handshake->randbytes, 64 ) ) != 0 )
10926 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010928 goto exit;
10929 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010930 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010931 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010933 goto exit;
10934 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010935 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010936 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010938 goto exit;
10939 }
10940
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010941 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010942 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010944 goto exit;
10945 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010946 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010947 ssl->handshake->randbytes, 64 ) ) != 0 )
10948 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010950 goto exit;
10951 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010952 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010953 data_len ) ) != 0 )
10954 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010956 goto exit;
10957 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010958 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010959 output + 16 ) ) != 0 )
10960 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010962 goto exit;
10963 }
10964
10965exit:
10966 mbedtls_md5_free( &mbedtls_md5 );
10967 mbedtls_sha1_free( &mbedtls_sha1 );
10968
10969 if( ret != 0 )
10970 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10971 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10972
10973 return( ret );
10974
10975}
10976#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10977 MBEDTLS_SSL_PROTO_TLS1_1 */
10978
10979#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10980 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10981int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010982 unsigned char *hash, size_t *hashlen,
10983 unsigned char *data, size_t data_len,
10984 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010985{
10986 int ret = 0;
10987 mbedtls_md_context_t ctx;
10988 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010989 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010990
10991 mbedtls_md_init( &ctx );
10992
10993 /*
10994 * digitally-signed struct {
10995 * opaque client_random[32];
10996 * opaque server_random[32];
10997 * ServerDHParams params;
10998 * };
10999 */
11000 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11001 {
11002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11003 goto exit;
11004 }
11005 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11006 {
11007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11008 goto exit;
11009 }
11010 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11011 {
11012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11013 goto exit;
11014 }
11015 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11016 {
11017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11018 goto exit;
11019 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011020 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011021 {
11022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11023 goto exit;
11024 }
11025
11026exit:
11027 mbedtls_md_free( &ctx );
11028
11029 if( ret != 0 )
11030 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11031 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11032
11033 return( ret );
11034}
11035#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11036 MBEDTLS_SSL_PROTO_TLS1_2 */
11037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038#endif /* MBEDTLS_SSL_TLS_C */