blob: cc4217171f27192b4d39830472688ffcb920648d [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
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006123/*
6124 * Once the certificate message is read, parse it into a cert chain and
6125 * perform basic checks, but leave actual verification to the caller
6126 */
6127static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006128{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006129 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006130 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006131 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133#if defined(MBEDTLS_SSL_SRV_C)
6134#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006135 /*
6136 * Check if the client sent an empty certificate
6137 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006138 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006141 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6143 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6144 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006148 /* The client was asked for a certificate but didn't send
6149 one. The client should know what's going on, so we
6150 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006151 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006152 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006153 }
6154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6158 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006159 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6163 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6164 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6165 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006169 /* The client was asked for a certificate but didn't send
6170 one. The client should know what's going on, so we
6171 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006172 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006173 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006174 }
6175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6177 MBEDTLS_SSL_PROTO_TLS1_2 */
6178#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006183 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6184 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006186 }
6187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6189 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006192 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6193 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195 }
6196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006198
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006202 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006203
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006204 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006208 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6209 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006211 }
6212
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006213 /* In case we tried to reuse a session but it failed */
6214 if( ssl->session_negotiate->peer_cert != NULL )
6215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6217 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006218 }
6219
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006220 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006225 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6226 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006227 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 }
6229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006231
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006232 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
6234 while( i < ssl->in_hslen )
6235 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006236 if ( i + 3 > ssl->in_hslen ) {
6237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6238 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6239 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6240 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 if( ssl->in_msg[i] != 0 )
6243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006245 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6246 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006248 }
6249
6250 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6251 | (unsigned int) ssl->in_msg[i + 2];
6252 i += 3;
6253
6254 if( n < 128 || i + n > ssl->in_hslen )
6255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006257 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6258 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 }
6261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006263 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006264 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006266 case 0: /*ok*/
6267 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6268 /* Ignore certificate with an unknown algorithm: maybe a
6269 prior certificate was already trusted. */
6270 break;
6271
6272 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6273 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6274 goto crt_parse_der_failed;
6275
6276 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6277 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6278 goto crt_parse_der_failed;
6279
6280 default:
6281 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6282 crt_parse_der_failed:
6283 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006285 return( ret );
6286 }
6287
6288 i += n;
6289 }
6290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006292
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006293 /*
6294 * On client, make sure the server cert doesn't change during renego to
6295 * avoid "triple handshake" attack: https://secure-resumption.com/
6296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006300 {
6301 if( ssl->session->peer_cert == NULL )
6302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006304 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6305 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006307 }
6308
6309 if( ssl->session->peer_cert->raw.len !=
6310 ssl->session_negotiate->peer_cert->raw.len ||
6311 memcmp( ssl->session->peer_cert->raw.p,
6312 ssl->session_negotiate->peer_cert->raw.p,
6313 ssl->session->peer_cert->raw.len ) != 0 )
6314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6317 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006319 }
6320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006322
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006323 return( 0 );
6324}
6325
6326int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6327{
6328 int ret;
6329 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006330 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006331#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6332 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6333 ? ssl->handshake->sni_authmode
6334 : ssl->conf->authmode;
6335#else
6336 const int authmode = ssl->conf->authmode;
6337#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006338 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006339
6340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6341
6342 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6343 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6344 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6345 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6346 {
6347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6348 ssl->state++;
6349 return( 0 );
6350 }
6351
6352#if defined(MBEDTLS_SSL_SRV_C)
6353 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6354 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6355 {
6356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6357 ssl->state++;
6358 return( 0 );
6359 }
6360
6361 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6362 authmode == MBEDTLS_SSL_VERIFY_NONE )
6363 {
6364 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006366
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006367 ssl->state++;
6368 return( 0 );
6369 }
6370#endif
6371
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006372#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6373 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006374 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006375 {
6376 goto crt_verify;
6377 }
6378#endif
6379
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006380 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006381 {
6382 /* mbedtls_ssl_read_record may have sent an alert already. We
6383 let it decide whether to alert. */
6384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6385 return( ret );
6386 }
6387
6388 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6389 {
6390#if defined(MBEDTLS_SSL_SRV_C)
6391 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6392 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6393 {
6394 ret = 0;
6395 }
6396#endif
6397
6398 ssl->state++;
6399 return( ret );
6400 }
6401
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006402#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6403 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006404 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006405
6406crt_verify:
6407 if( ssl->handshake->ecrs_enabled)
6408 rs_ctx = &ssl->handshake->ecrs_ctx;
6409#endif
6410
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006411 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006412 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006413 mbedtls_x509_crt *ca_chain;
6414 mbedtls_x509_crl *ca_crl;
6415
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006416#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006417 if( ssl->handshake->sni_ca_chain != NULL )
6418 {
6419 ca_chain = ssl->handshake->sni_ca_chain;
6420 ca_crl = ssl->handshake->sni_ca_crl;
6421 }
6422 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006423#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006424 {
6425 ca_chain = ssl->conf->ca_chain;
6426 ca_crl = ssl->conf->ca_crl;
6427 }
6428
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006429 /*
6430 * Main check: verify certificate
6431 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006432 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006433 ssl->session_negotiate->peer_cert,
6434 ca_chain, ca_crl,
6435 ssl->conf->cert_profile,
6436 ssl->hostname,
6437 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006438 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439
6440 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006443 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006444
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006445#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6446 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006447 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006448#endif
6449
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006450 /*
6451 * Secondary checks: always done, but change 'ret' only if it was 0
6452 */
6453
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006454#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006457
6458 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006460 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006461 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006462 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006465 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006467 }
6468 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006469#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006472 ciphersuite_info,
6473 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006474 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006477 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006479 }
6480
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006481 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6482 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6483 * with details encoded in the verification flags. All other kinds
6484 * of error codes, including those from the user provided f_vrfy
6485 * functions, are treated as fatal and lead to a failure of
6486 * ssl_parse_certificate even if verification was optional. */
6487 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6488 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6489 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6490 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006492 }
6493
6494 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6495 {
6496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6497 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6498 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006499
6500 if( ret != 0 )
6501 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006502 uint8_t alert;
6503
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006504 /* The certificate may have been rejected for several reasons.
6505 Pick one and send the corresponding alert. Which alert to send
6506 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006507 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6508 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6509 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6510 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6511 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6512 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6513 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6514 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6515 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6516 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6517 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6519 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6520 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6521 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6522 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6523 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6524 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6525 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6526 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006527 else
6528 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6530 alert );
6531 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006532
Hanno Beckere6706e62017-05-15 16:05:15 +01006533#if defined(MBEDTLS_DEBUG_C)
6534 if( ssl->session_negotiate->verify_result != 0 )
6535 {
6536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6537 ssl->session_negotiate->verify_result ) );
6538 }
6539 else
6540 {
6541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6542 }
6543#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 }
6545
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006546 ssl->state++;
6547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006549
6550 return( ret );
6551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6553 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6554 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6555 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6556 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6557 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6558 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006561{
6562 int ret;
6563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 ssl->out_msglen = 1;
6568 ssl->out_msg[0] = 1;
6569
Paul Bakker5121ce52009-01-03 21:22:43 +00006570 ssl->state++;
6571
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006572 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006573 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 return( ret );
6576 }
6577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006579
6580 return( 0 );
6581}
6582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006584{
6585 int ret;
6586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
Hanno Becker327c93b2018-08-15 13:56:18 +01006589 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006592 return( ret );
6593 }
6594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006598 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6599 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 }
6602
Hanno Beckere678eaa2018-08-21 14:57:46 +01006603 /* CCS records are only accepted if they have length 1 and content '1',
6604 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006605
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006606 /*
6607 * Switch to our negotiated transform and session parameters for inbound
6608 * data.
6609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006611 ssl->transform_in = ssl->transform_negotiate;
6612 ssl->session_in = ssl->session_negotiate;
6613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006618 ssl_dtls_replay_reset( ssl );
6619#endif
6620
6621 /* Increment epoch */
6622 if( ++ssl->in_epoch == 0 )
6623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006625 /* This is highly unlikely to happen for legitimate reasons, so
6626 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006628 }
6629 }
6630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006632 memset( ssl->in_ctr, 0, 8 );
6633
Hanno Beckerf5970a02019-05-08 09:38:41 +01006634 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6637 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006642 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6643 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006645 }
6646 }
6647#endif
6648
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 ssl->state++;
6650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006652
6653 return( 0 );
6654}
6655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6657 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006658{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006659 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6662 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6663 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006664 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006665 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006666#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6668#if defined(MBEDTLS_SHA512_C)
6669 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006670 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6671 else
6672#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673#if defined(MBEDTLS_SHA256_C)
6674 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006675 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006676 else
6677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006681 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006682 }
Paul Bakker380da532012-04-18 16:10:25 +00006683}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6688 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006689 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6690 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6693#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006694 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006695#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006697 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006700}
6701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006703 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006704{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6706 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006707 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6708 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
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_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006712 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006713#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006715 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006718}
6719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6721 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6722static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006723 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006724{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006725 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6726 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006727}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006728#endif
Paul Bakker380da532012-04-18 16:10:25 +00006729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6731#if defined(MBEDTLS_SHA256_C)
6732static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006733 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006734{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006735 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006736}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006737#endif
Paul Bakker380da532012-04-18 16:10:25 +00006738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SHA512_C)
6740static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006741 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006742{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006743 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006744}
Paul Bakker769075d2012-11-24 11:26:46 +01006745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006749static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006751{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006752 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006753 mbedtls_md5_context md5;
6754 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006755
Paul Bakker5121ce52009-01-03 21:22:43 +00006756 unsigned char padbuf[48];
6757 unsigned char md5sum[16];
6758 unsigned char sha1sum[20];
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006761 if( !session )
6762 session = ssl->session;
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006765
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006766 mbedtls_md5_init( &md5 );
6767 mbedtls_sha1_init( &sha1 );
6768
6769 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6770 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006771
6772 /*
6773 * SSLv3:
6774 * hash =
6775 * MD5( master + pad2 +
6776 * MD5( handshake + sender + master + pad1 ) )
6777 * + SHA1( master + pad2 +
6778 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 */
6780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006782 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6783 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006784#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006787 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6788 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006792 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006793
Paul Bakker1ef83d62012-04-11 12:09:53 +00006794 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006795
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006796 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6797 mbedtls_md5_update_ret( &md5, session->master, 48 );
6798 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6799 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006801 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6802 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6803 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6804 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805
Paul Bakker1ef83d62012-04-11 12:09:53 +00006806 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006807
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006808 mbedtls_md5_starts_ret( &md5 );
6809 mbedtls_md5_update_ret( &md5, session->master, 48 );
6810 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6811 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6812 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006813
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006814 mbedtls_sha1_starts_ret( &sha1 );
6815 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6816 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6817 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6818 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006821
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006822 mbedtls_md5_free( &md5 );
6823 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006825 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6826 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6827 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006834static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006836{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006837 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006838 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006839 mbedtls_md5_context md5;
6840 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006841 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006844 if( !session )
6845 session = ssl->session;
6846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006848
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006849 mbedtls_md5_init( &md5 );
6850 mbedtls_sha1_init( &sha1 );
6851
6852 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6853 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006854
Paul Bakker1ef83d62012-04-11 12:09:53 +00006855 /*
6856 * TLSv1:
6857 * hash = PRF( master, finished_label,
6858 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6859 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006862 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6863 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006864#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006867 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6868 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006869#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006872 ? "client finished"
6873 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006874
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006875 mbedtls_md5_finish_ret( &md5, padbuf );
6876 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006877
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006878 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006879 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006882
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006883 mbedtls_md5_free( &md5 );
6884 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006885
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006886 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6893#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006894static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006896{
6897 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006898 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006899 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006900 unsigned char padbuf[32];
6901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006903 if( !session )
6904 session = ssl->session;
6905
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006906 mbedtls_sha256_init( &sha256 );
6907
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006909
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006910 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006911
6912 /*
6913 * TLSv1.2:
6914 * hash = PRF( master, finished_label,
6915 * Hash( handshake ) )[0.11]
6916 */
6917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918#if !defined(MBEDTLS_SHA256_ALT)
6919 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006920 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006921#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006924 ? "client finished"
6925 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006926
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006927 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006929 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006930 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006934 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006935
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006936 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006943static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006945{
6946 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006947 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006948 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006949 unsigned char padbuf[48];
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006952 if( !session )
6953 session = ssl->session;
6954
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006955 mbedtls_sha512_init( &sha512 );
6956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006958
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006959 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006960
6961 /*
6962 * TLSv1.2:
6963 * hash = PRF( master, finished_label,
6964 * Hash( handshake ) )[0.11]
6965 */
6966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006968 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6969 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006970#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006973 ? "client finished"
6974 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006975
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006976 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006977
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006978 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006979 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006982
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006983 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006984
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006985 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989#endif /* MBEDTLS_SHA512_C */
6990#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006995
6996 /*
6997 * Free our handshake params
6998 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006999 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007001 ssl->handshake = NULL;
7002
7003 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007004 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007005 */
7006 if( ssl->transform )
7007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 mbedtls_ssl_transform_free( ssl->transform );
7009 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007010 }
7011 ssl->transform = ssl->transform_negotiate;
7012 ssl->transform_negotiate = NULL;
7013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007015}
7016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007018{
7019 int resume = ssl->handshake->resume;
7020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_SSL_RENEGOTIATION)
7024 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007027 ssl->renego_records_seen = 0;
7028 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007029#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007030
7031 /*
7032 * Free the previous session and switch in the current one
7033 */
Paul Bakker0a597072012-09-25 21:55:46 +00007034 if( ssl->session )
7035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007037 /* RFC 7366 3.1: keep the EtM state */
7038 ssl->session_negotiate->encrypt_then_mac =
7039 ssl->session->encrypt_then_mac;
7040#endif
7041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042 mbedtls_ssl_session_free( ssl->session );
7043 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007044 }
7045 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007046 ssl->session_negotiate = NULL;
7047
Paul Bakker0a597072012-09-25 21:55:46 +00007048 /*
7049 * Add cache entry
7050 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007051 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007052 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007053 resume == 0 )
7054 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007055 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007057 }
Paul Bakker0a597072012-09-25 21:55:46 +00007058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007061 ssl->handshake->flight != NULL )
7062 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007063 /* Cancel handshake timer */
7064 ssl_set_timer( ssl, 0 );
7065
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007066 /* Keep last flight around in case we need to resend it:
7067 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007069 }
7070 else
7071#endif
7072 ssl_handshake_wrapup_free_hs_transform( ssl );
7073
Paul Bakker48916f92012-09-16 19:57:18 +00007074 ssl->state++;
7075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007077}
7078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007080{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007081 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007084
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007085 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007086
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007087 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007088
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007089 /*
7090 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7091 * may define some other value. Currently (early 2016), no defined
7092 * ciphersuite does this (and this is unlikely to change as activity has
7093 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007098 ssl->verify_data_len = hash_len;
7099 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007100#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007101
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7104 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
7106 /*
7107 * In case of session resuming, invert the client and server
7108 * ChangeCipherSpec messages order.
7109 */
Paul Bakker0a597072012-09-25 21:55:46 +00007110 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007113 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007120 }
7121 else
7122 ssl->state++;
7123
Paul Bakker48916f92012-09-16 19:57:18 +00007124 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007125 * Switch to our negotiated transform and session parameters for outbound
7126 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007132 {
7133 unsigned char i;
7134
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007135 /* Remember current epoch settings for resending */
7136 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007137 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007138
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007139 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007140 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007141
7142 /* Increment epoch */
7143 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007144 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007145 break;
7146
7147 /* The loop goes to its end iff the counter is wrapping */
7148 if( i == 0 )
7149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7151 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007152 }
7153 }
7154 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007156 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007157
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007158 ssl->transform_out = ssl->transform_negotiate;
7159 ssl->session_out = ssl->session_negotiate;
7160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7162 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7167 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007168 }
7169 }
7170#endif
7171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007175#endif
7176
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007177 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007180 return( ret );
7181 }
7182
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007183#if defined(MBEDTLS_SSL_PROTO_DTLS)
7184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7185 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7186 {
7187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7188 return( ret );
7189 }
7190#endif
7191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007193
7194 return( 0 );
7195}
7196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007198#define SSL_MAX_HASH_LEN 36
7199#else
7200#define SSL_MAX_HASH_LEN 12
7201#endif
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007204{
Paul Bakker23986e52011-04-24 08:57:21 +00007205 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007206 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007207 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007210
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007211 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007212
Hanno Becker327c93b2018-08-15 13:56:18 +01007213 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007216 return( ret );
7217 }
7218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007222 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7223 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007225 }
7226
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007227 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SSL_PROTO_SSL3)
7229 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007230 hash_len = 36;
7231 else
7232#endif
7233 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7236 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007239 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7240 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007242 }
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007245 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007248 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7249 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251 }
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007254 ssl->verify_data_len = hash_len;
7255 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007256#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007257
Paul Bakker0a597072012-09-25 21:55:46 +00007258 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007268 }
7269 else
7270 ssl->state++;
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007273 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007275#endif
7276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
7279 return( 0 );
7280}
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7287 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7288 mbedtls_md5_init( &handshake->fin_md5 );
7289 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007290 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7291 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7294#if defined(MBEDTLS_SHA256_C)
7295 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007296 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#if defined(MBEDTLS_SHA512_C)
7299 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007300 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007303
7304 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007305
7306#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7307 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7308 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7309#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_DHM_C)
7312 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_ECDH_C)
7315 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007316#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007317#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007318 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007319#if defined(MBEDTLS_SSL_CLI_C)
7320 handshake->ecjpake_cache = NULL;
7321 handshake->ecjpake_cache_len = 0;
7322#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007323#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007324
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007325#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007326 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007327#endif
7328
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007329#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7330 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7331#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007332}
7333
Hanno Becker611a83b2018-01-03 14:27:32 +00007334void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7339 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007340
Hanno Becker92231322018-01-03 15:32:51 +00007341#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 mbedtls_md_init( &transform->md_ctx_enc );
7343 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007344#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007345}
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007350}
7351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007353{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007354 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007355 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007357 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007359 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007360 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007361
7362 /*
7363 * Either the pointers are now NULL or cleared properly and can be freed.
7364 * Now allocate missing structures.
7365 */
7366 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007367 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007368 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007369 }
Paul Bakker48916f92012-09-16 19:57:18 +00007370
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007371 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007372 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007373 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007374 }
Paul Bakker48916f92012-09-16 19:57:18 +00007375
Paul Bakker82788fb2014-10-20 13:59:19 +02007376 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007377 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007378 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007379 }
Paul Bakker48916f92012-09-16 19:57:18 +00007380
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007381 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007382 if( ssl->handshake == NULL ||
7383 ssl->transform_negotiate == NULL ||
7384 ssl->session_negotiate == NULL )
7385 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 mbedtls_free( ssl->handshake );
7389 mbedtls_free( ssl->transform_negotiate );
7390 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007391
7392 ssl->handshake = NULL;
7393 ssl->transform_negotiate = NULL;
7394 ssl->session_negotiate = NULL;
7395
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007396 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007397 }
7398
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007399 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007401 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007402 ssl_handshake_params_init( ssl->handshake );
7403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007405 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7406 {
7407 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007408
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007409 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7410 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7411 else
7412 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007413
7414 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007415 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007416#endif
7417
Paul Bakker48916f92012-09-16 19:57:18 +00007418 return( 0 );
7419}
7420
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007421#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007422/* Dummy cookie callbacks for defaults */
7423static int ssl_cookie_write_dummy( void *ctx,
7424 unsigned char **p, unsigned char *end,
7425 const unsigned char *cli_id, size_t cli_id_len )
7426{
7427 ((void) ctx);
7428 ((void) p);
7429 ((void) end);
7430 ((void) cli_id);
7431 ((void) cli_id_len);
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007434}
7435
7436static int ssl_cookie_check_dummy( void *ctx,
7437 const unsigned char *cookie, size_t cookie_len,
7438 const unsigned char *cli_id, size_t cli_id_len )
7439{
7440 ((void) ctx);
7441 ((void) cookie);
7442 ((void) cookie_len);
7443 ((void) cli_id);
7444 ((void) cli_id_len);
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007447}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007448#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007449
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007450/* Once ssl->out_hdr as the address of the beginning of the
7451 * next outgoing record is set, deduce the other pointers.
7452 *
7453 * Note: For TLS, we save the implicit record sequence number
7454 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7455 * and the caller has to make sure there's space for this.
7456 */
7457
7458static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7459 mbedtls_ssl_transform *transform )
7460{
7461#if defined(MBEDTLS_SSL_PROTO_DTLS)
7462 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7463 {
7464 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007465#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007466 ssl->out_cid = ssl->out_ctr + 8;
7467 ssl->out_len = ssl->out_cid;
7468 if( transform != NULL )
7469 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007470#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007471 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007472#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007473 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007474 }
7475 else
7476#endif
7477 {
7478 ssl->out_ctr = ssl->out_hdr - 8;
7479 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007480#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007481 ssl->out_cid = ssl->out_len;
7482#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007483 ssl->out_iv = ssl->out_hdr + 5;
7484 }
7485
7486 /* Adjust out_msg to make space for explicit IV, if used. */
7487 if( transform != NULL &&
7488 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7489 {
7490 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7491 }
7492 else
7493 ssl->out_msg = ssl->out_iv;
7494}
7495
7496/* Once ssl->in_hdr as the address of the beginning of the
7497 * next incoming record is set, deduce the other pointers.
7498 *
7499 * Note: For TLS, we save the implicit record sequence number
7500 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7501 * and the caller has to make sure there's space for this.
7502 */
7503
Hanno Beckerf5970a02019-05-08 09:38:41 +01007504static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007505{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007506 /* This function sets the pointers to match the case
7507 * of unprotected TLS/DTLS records, with both ssl->in_iv
7508 * and ssl->in_msg pointing to the beginning of the record
7509 * content.
7510 *
7511 * When decrypting a protected record, ssl->in_msg
7512 * will be shifted to point to the beginning of the
7513 * record plaintext.
7514 */
7515
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007516#if defined(MBEDTLS_SSL_PROTO_DTLS)
7517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7518 {
Hanno Becker70e79282019-05-03 14:34:53 +01007519 /* This sets the header pointers to match records
7520 * without CID. When we receive a record containing
7521 * a CID, the fields are shifted accordingly in
7522 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007523 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007524#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007525 ssl->in_cid = ssl->in_ctr + 8;
7526 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007527#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007528 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007529#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007530 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007531 }
7532 else
7533#endif
7534 {
7535 ssl->in_ctr = ssl->in_hdr - 8;
7536 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007537#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007538 ssl->in_cid = ssl->in_len;
7539#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007540 ssl->in_iv = ssl->in_hdr + 5;
7541 }
7542
Hanno Beckerf5970a02019-05-08 09:38:41 +01007543 /* This will be adjusted at record decryption time. */
7544 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007545}
7546
Paul Bakker5121ce52009-01-03 21:22:43 +00007547/*
7548 * Initialize an SSL context
7549 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007550void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7551{
7552 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7553}
7554
7555/*
7556 * Setup an SSL context
7557 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007558
7559static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7560{
7561 /* Set the incoming and outgoing record pointers. */
7562#if defined(MBEDTLS_SSL_PROTO_DTLS)
7563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7564 {
7565 ssl->out_hdr = ssl->out_buf;
7566 ssl->in_hdr = ssl->in_buf;
7567 }
7568 else
7569#endif /* MBEDTLS_SSL_PROTO_DTLS */
7570 {
7571 ssl->out_hdr = ssl->out_buf + 8;
7572 ssl->in_hdr = ssl->in_buf + 8;
7573 }
7574
7575 /* Derive other internal pointers. */
7576 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007577 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007578}
7579
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007580int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007581 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007582{
Paul Bakker48916f92012-09-16 19:57:18 +00007583 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007584
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007585 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007586
7587 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007588 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007589 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007590
7591 /* Set to NULL in case of an error condition */
7592 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007593
Angus Grattond8213d02016-05-25 20:56:48 +10007594 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7595 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007596 {
Angus Grattond8213d02016-05-25 20:56:48 +10007597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007598 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007599 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007600 }
7601
7602 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7603 if( ssl->out_buf == NULL )
7604 {
7605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007606 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007607 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007608 }
7609
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007610 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007611
Paul Bakker48916f92012-09-16 19:57:18 +00007612 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007613 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007614
7615 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007616
7617error:
7618 mbedtls_free( ssl->in_buf );
7619 mbedtls_free( ssl->out_buf );
7620
7621 ssl->conf = NULL;
7622
7623 ssl->in_buf = NULL;
7624 ssl->out_buf = NULL;
7625
7626 ssl->in_hdr = NULL;
7627 ssl->in_ctr = NULL;
7628 ssl->in_len = NULL;
7629 ssl->in_iv = NULL;
7630 ssl->in_msg = NULL;
7631
7632 ssl->out_hdr = NULL;
7633 ssl->out_ctr = NULL;
7634 ssl->out_len = NULL;
7635 ssl->out_iv = NULL;
7636 ssl->out_msg = NULL;
7637
k-stachowiak9f7798e2018-07-31 16:52:32 +02007638 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007639}
7640
7641/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007642 * Reset an initialized and used SSL context for re-use while retaining
7643 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007644 *
7645 * If partial is non-zero, keep data in the input buffer and client ID.
7646 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007647 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007648static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007649{
Paul Bakker48916f92012-09-16 19:57:18 +00007650 int ret;
7651
Hanno Becker7e772132018-08-10 12:38:21 +01007652#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7653 !defined(MBEDTLS_SSL_SRV_C)
7654 ((void) partial);
7655#endif
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007658
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007659 /* Cancel any possibly running timer */
7660 ssl_set_timer( ssl, 0 );
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_SSL_RENEGOTIATION)
7663 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007664 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007665
7666 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7668 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007669#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007671
Paul Bakker7eb013f2011-10-06 12:37:39 +00007672 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007673 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007674
7675 ssl->in_msgtype = 0;
7676 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007678 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007679 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007682 ssl_dtls_replay_reset( ssl );
7683#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007684
7685 ssl->in_hslen = 0;
7686 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007687
7688 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007689
7690 ssl->out_msgtype = 0;
7691 ssl->out_msglen = 0;
7692 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7694 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007695 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007696#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007697
Hanno Becker19859472018-08-06 09:40:20 +01007698 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7699
Paul Bakker48916f92012-09-16 19:57:18 +00007700 ssl->transform_in = NULL;
7701 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007702
Hanno Becker78640902018-08-13 16:35:15 +01007703 ssl->session_in = NULL;
7704 ssl->session_out = NULL;
7705
Angus Grattond8213d02016-05-25 20:56:48 +10007706 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007707
7708#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007709 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007710#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7711 {
7712 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007713 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007714 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7717 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7720 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7723 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007724 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007725 }
7726#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007727
Paul Bakker48916f92012-09-16 19:57:18 +00007728 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 mbedtls_ssl_transform_free( ssl->transform );
7731 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007732 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007733 }
Paul Bakker48916f92012-09-16 19:57:18 +00007734
Paul Bakkerc0463502013-02-14 11:19:38 +01007735 if( ssl->session )
7736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 mbedtls_ssl_session_free( ssl->session );
7738 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007739 ssl->session = NULL;
7740 }
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007743 ssl->alpn_chosen = NULL;
7744#endif
7745
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007746#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007747#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007748 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007749#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007750 {
7751 mbedtls_free( ssl->cli_id );
7752 ssl->cli_id = NULL;
7753 ssl->cli_id_len = 0;
7754 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007755#endif
7756
Paul Bakker48916f92012-09-16 19:57:18 +00007757 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7758 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007759
7760 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007761}
7762
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007763/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007764 * Reset an initialized and used SSL context for re-use while retaining
7765 * all application-set variables, function pointers and data.
7766 */
7767int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7768{
7769 return( ssl_session_reset_int( ssl, 0 ) );
7770}
7771
7772/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007773 * SSL set accessors
7774 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007775void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007776{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007777 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007778}
7779
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007780void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007781{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007782 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007783}
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007786void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007787{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007788 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007789}
7790#endif
7791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007793void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007794{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007795 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007796}
7797#endif
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007800
Hanno Becker1841b0a2018-08-24 11:13:57 +01007801void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7802 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007803{
7804 ssl->disable_datagram_packing = !allow_packing;
7805}
7806
7807void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7808 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007809{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007810 conf->hs_timeout_min = min;
7811 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007812}
7813#endif
7814
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007815void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007816{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007817 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007818}
7819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007821void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007822 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007823 void *p_vrfy )
7824{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007825 conf->f_vrfy = f_vrfy;
7826 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007829
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007830void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007831 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007832 void *p_rng )
7833{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007834 conf->f_rng = f_rng;
7835 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007836}
7837
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007838void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007839 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007840 void *p_dbg )
7841{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007842 conf->f_dbg = f_dbg;
7843 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007844}
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007847 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007848 mbedtls_ssl_send_t *f_send,
7849 mbedtls_ssl_recv_t *f_recv,
7850 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007851{
7852 ssl->p_bio = p_bio;
7853 ssl->f_send = f_send;
7854 ssl->f_recv = f_recv;
7855 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007856}
7857
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007858#if defined(MBEDTLS_SSL_PROTO_DTLS)
7859void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7860{
7861 ssl->mtu = mtu;
7862}
7863#endif
7864
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007865void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007866{
7867 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007868}
7869
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007870void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7871 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007872 mbedtls_ssl_set_timer_t *f_set_timer,
7873 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007874{
7875 ssl->p_timer = p_timer;
7876 ssl->f_set_timer = f_set_timer;
7877 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007878
7879 /* Make sure we start with no timer running */
7880 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007881}
7882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007884void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007885 void *p_cache,
7886 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7887 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007888{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007889 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007890 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007891 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007892}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#if defined(MBEDTLS_SSL_CLI_C)
7896int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007897{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007898 int ret;
7899
7900 if( ssl == NULL ||
7901 session == NULL ||
7902 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007903 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007906 }
7907
7908 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7909 return( ret );
7910
Paul Bakker0a597072012-09-25 21:55:46 +00007911 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007912
7913 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007916
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007917void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007918 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007919{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007920 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7921 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7922 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7923 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007924}
7925
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007926void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007927 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007928 int major, int minor )
7929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007931 return;
7932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007934 return;
7935
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007936 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007937}
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007940void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007941 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007942{
7943 conf->cert_profile = profile;
7944}
7945
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007946/* Append a new keycert entry to a (possibly empty) list */
7947static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7948 mbedtls_x509_crt *cert,
7949 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007950{
niisato8ee24222018-06-25 19:05:48 +09007951 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007952
niisato8ee24222018-06-25 19:05:48 +09007953 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7954 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007955 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007956
niisato8ee24222018-06-25 19:05:48 +09007957 new_cert->cert = cert;
7958 new_cert->key = key;
7959 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007960
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007961 /* Update head is the list was null, else add to the end */
7962 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007963 {
niisato8ee24222018-06-25 19:05:48 +09007964 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007965 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007966 else
7967 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007968 mbedtls_ssl_key_cert *cur = *head;
7969 while( cur->next != NULL )
7970 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007971 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007972 }
7973
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007974 return( 0 );
7975}
7976
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007977int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007978 mbedtls_x509_crt *own_cert,
7979 mbedtls_pk_context *pk_key )
7980{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007981 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007982}
7983
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007984void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007985 mbedtls_x509_crt *ca_chain,
7986 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007987{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007988 conf->ca_chain = ca_chain;
7989 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007992
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007993#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7994int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7995 mbedtls_x509_crt *own_cert,
7996 mbedtls_pk_context *pk_key )
7997{
7998 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7999 own_cert, pk_key ) );
8000}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008001
8002void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8003 mbedtls_x509_crt *ca_chain,
8004 mbedtls_x509_crl *ca_crl )
8005{
8006 ssl->handshake->sni_ca_chain = ca_chain;
8007 ssl->handshake->sni_ca_crl = ca_crl;
8008}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008009
8010void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8011 int authmode )
8012{
8013 ssl->handshake->sni_authmode = authmode;
8014}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008015#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8016
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008017#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008018/*
8019 * Set EC J-PAKE password for current handshake
8020 */
8021int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8022 const unsigned char *pw,
8023 size_t pw_len )
8024{
8025 mbedtls_ecjpake_role role;
8026
Janos Follath8eb64132016-06-03 15:40:57 +01008027 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8029
8030 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8031 role = MBEDTLS_ECJPAKE_SERVER;
8032 else
8033 role = MBEDTLS_ECJPAKE_CLIENT;
8034
8035 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8036 role,
8037 MBEDTLS_MD_SHA256,
8038 MBEDTLS_ECP_DP_SECP256R1,
8039 pw, pw_len ) );
8040}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008041#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008044int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008045 const unsigned char *psk, size_t psk_len,
8046 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008047{
Paul Bakker6db455e2013-09-18 17:29:31 +02008048 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008053
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008054 /* Identity len will be encoded on two bytes */
8055 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008056 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008057 {
8058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8059 }
8060
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008061 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008062 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008063 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008064
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008065 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008066 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008067 conf->psk_len = 0;
8068 }
8069 if( conf->psk_identity != NULL )
8070 {
8071 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008072 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008073 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008074 }
8075
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008076 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8077 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008078 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008079 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008080 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008081 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008082 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008084 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008085
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008086 conf->psk_len = psk_len;
8087 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008088
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008089 memcpy( conf->psk, psk, conf->psk_len );
8090 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008091
8092 return( 0 );
8093}
8094
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008095int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8096 const unsigned char *psk, size_t psk_len )
8097{
8098 if( psk == NULL || ssl->handshake == NULL )
8099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8100
8101 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8103
8104 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008105 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008106 mbedtls_platform_zeroize( ssl->handshake->psk,
8107 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008108 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008109 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008110 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008111
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008112 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008113 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008114
8115 ssl->handshake->psk_len = psk_len;
8116 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8117
8118 return( 0 );
8119}
8120
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008121void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008123 size_t),
8124 void *p_psk )
8125{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008126 conf->f_psk = f_psk;
8127 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008130
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008131#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008132
8133#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008134int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008135{
8136 int ret;
8137
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008138 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8139 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8140 {
8141 mbedtls_mpi_free( &conf->dhm_P );
8142 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008143 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008145
8146 return( 0 );
8147}
Hanno Becker470a8c42017-10-04 15:28:46 +01008148#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008149
Hanno Beckera90658f2017-10-04 15:29:08 +01008150int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8151 const unsigned char *dhm_P, size_t P_len,
8152 const unsigned char *dhm_G, size_t G_len )
8153{
8154 int ret;
8155
8156 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8157 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8158 {
8159 mbedtls_mpi_free( &conf->dhm_P );
8160 mbedtls_mpi_free( &conf->dhm_G );
8161 return( ret );
8162 }
8163
8164 return( 0 );
8165}
Paul Bakker5121ce52009-01-03 21:22:43 +00008166
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008167int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008168{
8169 int ret;
8170
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008171 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8172 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8173 {
8174 mbedtls_mpi_free( &conf->dhm_P );
8175 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008176 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008177 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008178
8179 return( 0 );
8180}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008181#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008182
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008183#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8184/*
8185 * Set the minimum length for Diffie-Hellman parameters
8186 */
8187void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8188 unsigned int bitlen )
8189{
8190 conf->dhm_min_bitlen = bitlen;
8191}
8192#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8193
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008194#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008195/*
8196 * Set allowed/preferred hashes for handshake signatures
8197 */
8198void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8199 const int *hashes )
8200{
8201 conf->sig_hashes = hashes;
8202}
Hanno Becker947194e2017-04-07 13:25:49 +01008203#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008204
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008205#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008206/*
8207 * Set the allowed elliptic curves
8208 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008209void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008210 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008212 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008213}
Hanno Becker947194e2017-04-07 13:25:49 +01008214#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008215
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008216#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008218{
Hanno Becker947194e2017-04-07 13:25:49 +01008219 /* Initialize to suppress unnecessary compiler warning */
8220 size_t hostname_len = 0;
8221
8222 /* Check if new hostname is valid before
8223 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008224 if( hostname != NULL )
8225 {
8226 hostname_len = strlen( hostname );
8227
8228 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8230 }
8231
8232 /* Now it's clear that we will overwrite the old hostname,
8233 * so we can free it safely */
8234
8235 if( ssl->hostname != NULL )
8236 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008237 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008238 mbedtls_free( ssl->hostname );
8239 }
8240
8241 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008242
Paul Bakker5121ce52009-01-03 21:22:43 +00008243 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008244 {
8245 ssl->hostname = NULL;
8246 }
8247 else
8248 {
8249 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008250 if( ssl->hostname == NULL )
8251 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008252
Hanno Becker947194e2017-04-07 13:25:49 +01008253 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008254
Hanno Becker947194e2017-04-07 13:25:49 +01008255 ssl->hostname[hostname_len] = '\0';
8256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008257
8258 return( 0 );
8259}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008260#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008261
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008262#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008263void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008265 const unsigned char *, size_t),
8266 void *p_sni )
8267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008268 conf->f_sni = f_sni;
8269 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008274int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008275{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008276 size_t cur_len, tot_len;
8277 const char **p;
8278
8279 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008280 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8281 * MUST NOT be truncated."
8282 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008283 */
8284 tot_len = 0;
8285 for( p = protos; *p != NULL; p++ )
8286 {
8287 cur_len = strlen( *p );
8288 tot_len += cur_len;
8289
8290 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008292 }
8293
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008294 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008295
8296 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008297}
8298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008300{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008301 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008304
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008305void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008307 conf->max_major_ver = major;
8308 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008309}
8310
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008311void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008312{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008313 conf->min_major_ver = major;
8314 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008315}
8316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008318void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008319{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008320 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008321}
8322#endif
8323
Janos Follath088ce432017-04-10 12:42:31 +01008324#if defined(MBEDTLS_SSL_SRV_C)
8325void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8326 char cert_req_ca_list )
8327{
8328 conf->cert_req_ca_list = cert_req_ca_list;
8329}
8330#endif
8331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008333void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008334{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008335 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008336}
8337#endif
8338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008340void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008341{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008342 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008343}
8344#endif
8345
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008346#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008347void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008348{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008349 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008350}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008351#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008354int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008357 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008360 }
8361
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008362 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008363
8364 return( 0 );
8365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008369void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008371 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008376void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008377{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008378 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008379}
8380#endif
8381
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008382void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008383{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008384 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008385}
8386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008388void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008389{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008390 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008391}
8392
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008395 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008396}
8397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008399 const unsigned char period[8] )
8400{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008401 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008406#if defined(MBEDTLS_SSL_CLI_C)
8407void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008408{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008409 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008410}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008411#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008412
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008413#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008414void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8415 mbedtls_ssl_ticket_write_t *f_ticket_write,
8416 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8417 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008418{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008419 conf->f_ticket_write = f_ticket_write;
8420 conf->f_ticket_parse = f_ticket_parse;
8421 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008422}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008425
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008426#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8427void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8428 mbedtls_ssl_export_keys_t *f_export_keys,
8429 void *p_export_keys )
8430{
8431 conf->f_export_keys = f_export_keys;
8432 conf->p_export_keys = p_export_keys;
8433}
8434#endif
8435
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008436#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008437void mbedtls_ssl_conf_async_private_cb(
8438 mbedtls_ssl_config *conf,
8439 mbedtls_ssl_async_sign_t *f_async_sign,
8440 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8441 mbedtls_ssl_async_resume_t *f_async_resume,
8442 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008443 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008444{
8445 conf->f_async_sign_start = f_async_sign;
8446 conf->f_async_decrypt_start = f_async_decrypt;
8447 conf->f_async_resume = f_async_resume;
8448 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008449 conf->p_async_config_data = async_config_data;
8450}
8451
Gilles Peskine8f97af72018-04-26 11:46:10 +02008452void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8453{
8454 return( conf->p_async_config_data );
8455}
8456
Gilles Peskine1febfef2018-04-30 11:54:39 +02008457void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008458{
8459 if( ssl->handshake == NULL )
8460 return( NULL );
8461 else
8462 return( ssl->handshake->user_async_ctx );
8463}
8464
Gilles Peskine1febfef2018-04-30 11:54:39 +02008465void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008466 void *ctx )
8467{
8468 if( ssl->handshake != NULL )
8469 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008470}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008471#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008472
Paul Bakker5121ce52009-01-03 21:22:43 +00008473/*
8474 * SSL get accessors
8475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008477{
8478 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8479}
8480
Hanno Becker8b170a02017-10-10 11:51:19 +01008481int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8482{
8483 /*
8484 * Case A: We're currently holding back
8485 * a message for further processing.
8486 */
8487
8488 if( ssl->keep_current_message == 1 )
8489 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008491 return( 1 );
8492 }
8493
8494 /*
8495 * Case B: Further records are pending in the current datagram.
8496 */
8497
8498#if defined(MBEDTLS_SSL_PROTO_DTLS)
8499 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8500 ssl->in_left > ssl->next_record_offset )
8501 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008502 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008503 return( 1 );
8504 }
8505#endif /* MBEDTLS_SSL_PROTO_DTLS */
8506
8507 /*
8508 * Case C: A handshake message is being processed.
8509 */
8510
Hanno Becker8b170a02017-10-10 11:51:19 +01008511 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8512 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008514 return( 1 );
8515 }
8516
8517 /*
8518 * Case D: An application data message is being processed
8519 */
8520 if( ssl->in_offt != NULL )
8521 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008523 return( 1 );
8524 }
8525
8526 /*
8527 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008528 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008529 * we implement support for multiple alerts in single records.
8530 */
8531
8532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8533 return( 0 );
8534}
8535
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008536uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008537{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008538 if( ssl->session != NULL )
8539 return( ssl->session->verify_result );
8540
8541 if( ssl->session_negotiate != NULL )
8542 return( ssl->session_negotiate->verify_result );
8543
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008544 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008545}
8546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008548{
Paul Bakker926c8e42013-03-06 10:23:34 +01008549 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008550 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008553}
8554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008558 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008559 {
8560 switch( ssl->minor_ver )
8561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008563 return( "DTLSv1.0" );
8564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008566 return( "DTLSv1.2" );
8567
8568 default:
8569 return( "unknown (DTLS)" );
8570 }
8571 }
8572#endif
8573
Paul Bakker43ca69c2011-01-15 17:35:19 +00008574 switch( ssl->minor_ver )
8575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008577 return( "SSLv3.0" );
8578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008580 return( "TLSv1.0" );
8581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008583 return( "TLSv1.1" );
8584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008586 return( "TLSv1.2" );
8587
Paul Bakker43ca69c2011-01-15 17:35:19 +00008588 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008589 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008590 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008591}
8592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008594{
Hanno Becker3136ede2018-08-17 15:28:19 +01008595 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008597 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008598
Hanno Becker43395762019-05-03 14:46:38 +01008599 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8600
Hanno Becker78640902018-08-13 16:35:15 +01008601 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008602 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604#if defined(MBEDTLS_ZLIB_SUPPORT)
8605 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8606 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008607#endif
8608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 case MBEDTLS_MODE_GCM:
8612 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008613 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008615 transform_expansion = transform->minlen;
8616 break;
8617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008619
8620 block_size = mbedtls_cipher_get_block_size(
8621 &transform->cipher_ctx_enc );
8622
Hanno Becker3136ede2018-08-17 15:28:19 +01008623 /* Expansion due to the addition of the MAC. */
8624 transform_expansion += transform->maclen;
8625
8626 /* Expansion due to the addition of CBC padding;
8627 * Theoretically up to 256 bytes, but we never use
8628 * more than the block size of the underlying cipher. */
8629 transform_expansion += block_size;
8630
8631 /* For TLS 1.1 or higher, an explicit IV is added
8632 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008633#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8634 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008635 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008636#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008637
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008638 break;
8639
8640 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008643 }
8644
Hanno Beckera5a2b082019-05-15 14:03:01 +01008645#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008646 if( transform->out_cid_len != 0 )
8647 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008648#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008649
Hanno Becker43395762019-05-03 14:46:38 +01008650 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008651}
8652
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008653#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8654size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8655{
8656 size_t max_len;
8657
8658 /*
8659 * Assume mfl_code is correct since it was checked when set
8660 */
Angus Grattond8213d02016-05-25 20:56:48 +10008661 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008662
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008663 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008664 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008665 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008666 {
Angus Grattond8213d02016-05-25 20:56:48 +10008667 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008668 }
8669
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008670 /* During a handshake, use the value being negotiated */
8671 if( ssl->session_negotiate != NULL &&
8672 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8673 {
8674 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8675 }
8676
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008677 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008678}
8679#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8680
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008681#if defined(MBEDTLS_SSL_PROTO_DTLS)
8682static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8683{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008684 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8686 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8687 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8688 return ( 0 );
8689
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008690 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8691 return( ssl->mtu );
8692
8693 if( ssl->mtu == 0 )
8694 return( ssl->handshake->mtu );
8695
8696 return( ssl->mtu < ssl->handshake->mtu ?
8697 ssl->mtu : ssl->handshake->mtu );
8698}
8699#endif /* MBEDTLS_SSL_PROTO_DTLS */
8700
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008701int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8702{
8703 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8704
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008705#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8706 !defined(MBEDTLS_SSL_PROTO_DTLS)
8707 (void) ssl;
8708#endif
8709
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008710#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8711 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8712
8713 if( max_len > mfl )
8714 max_len = mfl;
8715#endif
8716
8717#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008718 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008719 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008720 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008721 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8722 const size_t overhead = (size_t) ret;
8723
8724 if( ret < 0 )
8725 return( ret );
8726
8727 if( mtu <= overhead )
8728 {
8729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8730 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8731 }
8732
8733 if( max_len > mtu - overhead )
8734 max_len = mtu - overhead;
8735 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008736#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008737
Hanno Becker0defedb2018-08-10 12:35:02 +01008738#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8739 !defined(MBEDTLS_SSL_PROTO_DTLS)
8740 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008741#endif
8742
8743 return( (int) max_len );
8744}
8745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746#if defined(MBEDTLS_X509_CRT_PARSE_C)
8747const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008748{
8749 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008750 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008751
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008752 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#if defined(MBEDTLS_SSL_CLI_C)
8757int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008758{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008759 if( ssl == NULL ||
8760 dst == NULL ||
8761 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008762 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008765 }
8766
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008767 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008770
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008771const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8772{
8773 if( ssl == NULL )
8774 return( NULL );
8775
8776 return( ssl->session );
8777}
8778
Paul Bakker5121ce52009-01-03 21:22:43 +00008779/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008780 * Define ticket header determining Mbed TLS version
8781 * and structure of the ticket.
8782 */
8783
Hanno Becker41527622019-05-16 12:50:45 +01008784/*
Hanno Becker26829e92019-05-28 14:30:45 +01008785 * Define bitflag determining compile-time settings influencing
8786 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008787 */
8788
Hanno Becker26829e92019-05-28 14:30:45 +01008789#if defined(MBEDTLS_SSL_SERIALIZED_STRUCTURES_LOCAL_ONLY)
8790#define SSL_SERIALIZED_SESSION_CONFIG_LOCAL 1
Hanno Becker41527622019-05-16 12:50:45 +01008791#else
Hanno Becker26829e92019-05-28 14:30:45 +01008792#define SSL_SERIALIZED_SESSION_CONFIG_LOCAL 0
8793#endif /* MBEDTLS_SSL_SERIALIZED_STRUCTURES_LOCAL_ONLY */
8794
8795#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008796#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008797#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008798#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008799#endif /* MBEDTLS_HAVE_TIME */
8800
8801#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008802#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008803#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008804#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008805#endif /* MBEDTLS_X509_CRT_PARSE_C */
8806
8807#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008808#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008809#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008810#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008811#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8812
8813#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008814#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008815#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008816#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008817#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8818
8819#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008820#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008821#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008822#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008823#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8824
8825#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008826#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008827#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008828#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008829#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8830
Hanno Becker41527622019-05-16 12:50:45 +01008831#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8832#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8833#else
8834#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8835#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8836
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008837#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8838#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8839#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8840#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8841#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8842#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8843#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
8844#define SSL_SERIALIZED_SESSION_CONFIG_LOCAL_BIT 7
8845
Hanno Becker26829e92019-05-28 14:30:45 +01008846#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008847 ( (uint16_t) ( \
8848 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8849 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8850 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8851 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8852 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8853 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
8854 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
8855 ( SSL_SERIALIZED_SESSION_CONFIG_LOCAL << SSL_SERIALIZED_SESSION_CONFIG_LOCAL_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008856
Hanno Becker557fe9f2019-05-16 12:41:07 +01008857static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008858 MBEDTLS_VERSION_MAJOR,
8859 MBEDTLS_VERSION_MINOR,
8860 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008861 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8862 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008863};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008864
8865/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008866 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008867 * (in the presentation language of TLS, RFC 8446 section 3)
8868 *
Hanno Becker26829e92019-05-28 14:30:45 +01008869 * opaque mbedtls_version[3]; // major, minor, patch
8870 * opaque session_format[2]; // version-specific 16-bit field determining
8871 * // the format of the remaining
8872 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008873 *
8874 * Note: When updating the format, remember to keep
8875 * these version+format bytes.
8876 *
8877 * // In this version, `session_format`
8878 * // indicates whether
Hanno Becker26829e92019-05-28 14:30:45 +01008879 * // MBEDTLS_SSL_SERIALIZED_STRUCTURES_LOCAL_ONLY
8880 * // is set, plus the setting of those compile-
8881 * // time configuration options which influence
8882 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008883 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008884 * uint8 ciphersuite[2]; // defined by the standard
8885 * uint8 compression; // 0 or 1
8886 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008887 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008888 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008889 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008890 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8891 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008892 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008893 * uint8 mfl_code; // up to 255 according to standard
8894 * uint8 trunc_hmac; // 0 or 1
8895 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008896 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008897 * The order is the same as in the definition of the structure, except
8898 * verify_result is put before peer_cert so that all mandatory fields come
8899 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008900 */
8901int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8902 unsigned char *buf,
8903 size_t buf_len,
8904 size_t *olen )
8905{
8906 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008907 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008908#if defined(MBEDTLS_HAVE_TIME)
8909 uint64_t start;
8910#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008911#if defined(MBEDTLS_X509_CRT_PARSE_C)
8912 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008913#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008914
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008915 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008916 * Add version identifier
8917 */
8918
8919 used += sizeof( ssl_serialized_session_header );
8920
8921 if( used <= buf_len )
8922 {
8923 memcpy( p, ssl_serialized_session_header,
8924 sizeof( ssl_serialized_session_header ) );
8925 p += sizeof( ssl_serialized_session_header );
8926 }
8927
8928 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008929 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008930 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008931#if defined(MBEDTLS_HAVE_TIME)
8932 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008933
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008934 if( used <= buf_len )
8935 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008936 start = (uint64_t) session->start;
8937
8938 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8939 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8940 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8941 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8942 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
8943 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
8944 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
8945 *p++ = (unsigned char)( ( start ) & 0xFF );
8946 }
8947#endif /* MBEDTLS_HAVE_TIME */
8948
8949 /*
8950 * Basic mandatory fields
8951 */
8952 used += 2 /* ciphersuite */
8953 + 1 /* compression */
8954 + 1 /* id_len */
8955 + sizeof( session->id )
8956 + sizeof( session->master )
8957 + 4; /* verify_result */
8958
8959 if( used <= buf_len )
8960 {
8961 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
8962 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
8963
8964 *p++ = (unsigned char)( session->compression & 0xFF );
8965
8966 *p++ = (unsigned char)( session->id_len & 0xFF );
8967 memcpy( p, session->id, 32 );
8968 p += 32;
8969
8970 memcpy( p, session->master, 48 );
8971 p += 48;
8972
8973 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
8974 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
8975 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
8976 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008977 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008978
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008979 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008980 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008981 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008982#if defined(MBEDTLS_X509_CRT_PARSE_C)
8983 if( session->peer_cert == NULL )
8984 cert_len = 0;
8985 else
8986 cert_len = session->peer_cert->raw.len;
8987
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008988 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008989
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008990 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008991 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008992 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
8993 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
8994 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
8995
8996 if( session->peer_cert != NULL )
8997 {
8998 memcpy( p, session->peer_cert->raw.p, cert_len );
8999 p += cert_len;
9000 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009001 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009002#endif /* MBEDTLS_X509_CRT_PARSE_C */
9003
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009004 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009005 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009006 */
9007#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009008 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009009
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009010 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009011 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009012 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9013 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9014 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9015
9016 if( session->ticket != NULL )
9017 {
9018 memcpy( p, session->ticket, session->ticket_len );
9019 p += session->ticket_len;
9020 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009021
9022 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9023 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9024 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9025 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009026 }
9027#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9028
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009029 /*
9030 * Misc extension-related info
9031 */
9032#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9033 used += 1;
9034
9035 if( used <= buf_len )
9036 *p++ = session->mfl_code;
9037#endif
9038
9039#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9040 used += 1;
9041
9042 if( used <= buf_len )
9043 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9044#endif
9045
9046#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9047 used += 1;
9048
9049 if( used <= buf_len )
9050 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9051#endif
9052
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009053 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009054 *olen = used;
9055
9056 if( used > buf_len )
9057 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009058
9059 return( 0 );
9060}
9061
9062/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009063 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009064 *
9065 * This internal version is wrapped by a public function that cleans up in
9066 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009067 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009068static int ssl_session_load( mbedtls_ssl_session *session,
9069 const unsigned char *buf,
9070 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009071{
9072 const unsigned char *p = buf;
9073 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009074#if defined(MBEDTLS_HAVE_TIME)
9075 uint64_t start;
9076#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009077#if defined(MBEDTLS_X509_CRT_PARSE_C)
9078 size_t cert_len;
9079#endif /* MBEDTLS_X509_CRT_PARSE_C */
9080
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009081 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009082 * Check version identifier
9083 */
9084
9085 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009087
9088 if( memcmp( p, ssl_serialized_session_header,
9089 sizeof( ssl_serialized_session_header ) ) != 0 )
9090 {
9091 /* A more specific error code might be used here. */
9092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9093 }
9094 p += sizeof( ssl_serialized_session_header );
9095
9096 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009097 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009098 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009099#if defined(MBEDTLS_HAVE_TIME)
9100 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9102
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009103 start = ( (uint64_t) p[0] << 56 ) |
9104 ( (uint64_t) p[1] << 48 ) |
9105 ( (uint64_t) p[2] << 40 ) |
9106 ( (uint64_t) p[3] << 32 ) |
9107 ( (uint64_t) p[4] << 24 ) |
9108 ( (uint64_t) p[5] << 16 ) |
9109 ( (uint64_t) p[6] << 8 ) |
9110 ( (uint64_t) p[7] );
9111 p += 8;
9112
9113 session->start = (time_t) start;
9114#endif /* MBEDTLS_HAVE_TIME */
9115
9116 /*
9117 * Basic mandatory fields
9118 */
9119 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9121
9122 session->ciphersuite = ( p[0] << 8 ) | p[1];
9123 p += 2;
9124
9125 session->compression = *p++;
9126
9127 session->id_len = *p++;
9128 memcpy( session->id, p, 32 );
9129 p += 32;
9130
9131 memcpy( session->master, p, 48 );
9132 p += 48;
9133
9134 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9135 ( (uint32_t) p[1] << 16 ) |
9136 ( (uint32_t) p[2] << 8 ) |
9137 ( (uint32_t) p[3] );
9138 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009139
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009140 /* Immediately clear invalid pointer values that have been read, in case
9141 * we exit early before we replaced them with valid ones. */
9142#if defined(MBEDTLS_X509_CRT_PARSE_C)
9143 session->peer_cert = NULL;
9144#endif
9145#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9146 session->ticket = NULL;
9147#endif
9148
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009149 /*
9150 * Peer certificate
9151 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009152#if defined(MBEDTLS_X509_CRT_PARSE_C)
9153 if( 3 > (size_t)( end - p ) )
9154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9155
9156 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9157 p += 3;
9158
9159 if( cert_len == 0 )
9160 {
9161 session->peer_cert = NULL;
9162 }
9163 else
9164 {
9165 int ret;
9166
9167 if( cert_len > (size_t)( end - p ) )
9168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9169
9170 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9171
9172 if( session->peer_cert == NULL )
9173 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9174
9175 mbedtls_x509_crt_init( session->peer_cert );
9176
9177 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9178 p, cert_len ) ) != 0 )
9179 {
9180 mbedtls_x509_crt_free( session->peer_cert );
9181 mbedtls_free( session->peer_cert );
9182 session->peer_cert = NULL;
9183 return( ret );
9184 }
9185
9186 p += cert_len;
9187 }
9188#endif /* MBEDTLS_X509_CRT_PARSE_C */
9189
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009190 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009191 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009192 */
9193#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9194 if( 3 > (size_t)( end - p ) )
9195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9196
9197 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9198 p += 3;
9199
9200 if( session->ticket_len != 0 )
9201 {
9202 if( session->ticket_len > (size_t)( end - p ) )
9203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9204
9205 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9206 if( session->ticket == NULL )
9207 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9208
9209 memcpy( session->ticket, p, session->ticket_len );
9210 p += session->ticket_len;
9211 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009212
9213 if( 4 > (size_t)( end - p ) )
9214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9215
9216 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9217 ( (uint32_t) p[1] << 16 ) |
9218 ( (uint32_t) p[2] << 8 ) |
9219 ( (uint32_t) p[3] );
9220 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009221#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9222
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009223 /*
9224 * Misc extension-related info
9225 */
9226#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9227 if( 1 > (size_t)( end - p ) )
9228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9229
9230 session->mfl_code = *p++;
9231#endif
9232
9233#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9234 if( 1 > (size_t)( end - p ) )
9235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9236
9237 session->trunc_hmac = *p++;
9238#endif
9239
9240#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9241 if( 1 > (size_t)( end - p ) )
9242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9243
9244 session->encrypt_then_mac = *p++;
9245#endif
9246
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009247 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009248 if( p != end )
9249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9250
9251 return( 0 );
9252}
9253
9254/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009255 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009256 */
9257int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9258 const unsigned char *buf,
9259 size_t len )
9260{
9261 int ret = ssl_session_load( session, buf, len );
9262
9263 if( ret != 0 )
9264 mbedtls_ssl_session_free( session );
9265
9266 return( ret );
9267}
9268
9269/*
Paul Bakker1961b702013-01-25 14:49:24 +01009270 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009275
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009276 if( ssl == NULL || ssl->conf == NULL )
9277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009284 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009286#endif
9287
Paul Bakker1961b702013-01-25 14:49:24 +01009288 return( ret );
9289}
9290
9291/*
9292 * Perform the SSL handshake
9293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009295{
9296 int ret = 0;
9297
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009298 if( ssl == NULL || ssl->conf == NULL )
9299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009306
9307 if( ret != 0 )
9308 break;
9309 }
9310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009312
9313 return( ret );
9314}
9315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316#if defined(MBEDTLS_SSL_RENEGOTIATION)
9317#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009318/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009319 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009322{
9323 int ret;
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009326
9327 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9329 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009330
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009331 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009332 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009334 return( ret );
9335 }
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009338
9339 return( 0 );
9340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009342
9343/*
9344 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345 * - any side: calling mbedtls_ssl_renegotiate(),
9346 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9347 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009348 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009349 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009353{
9354 int ret;
9355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009357
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009358 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9359 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009360
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009361 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9362 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009364 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009366 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009368 ssl->handshake->out_msg_seq = 1;
9369 else
9370 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009371 }
9372#endif
9373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9375 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009380 return( ret );
9381 }
9382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009384
9385 return( 0 );
9386}
9387
9388/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009389 * Renegotiate current connection on client,
9390 * or request renegotiation on server
9391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009395
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009396 if( ssl == NULL || ssl->conf == NULL )
9397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009400 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009401 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009407
9408 /* Did we already try/start sending HelloRequest? */
9409 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009411
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009412 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009417 /*
9418 * On client, either start the renegotiation process or,
9419 * if already in progress, continue the handshake
9420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009425
9426 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009429 return( ret );
9430 }
9431 }
9432 else
9433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009437 return( ret );
9438 }
9439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009441
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009442 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009443}
9444
9445/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009446 * Check record counters and renegotiate if they're above the limit.
9447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009449{
Andres AG2196c7f2016-12-15 17:01:16 +00009450 size_t ep_len = ssl_ep_len( ssl );
9451 int in_ctr_cmp;
9452 int out_ctr_cmp;
9453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9455 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009456 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009457 {
9458 return( 0 );
9459 }
9460
Andres AG2196c7f2016-12-15 17:01:16 +00009461 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9462 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009463 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009464 ssl->conf->renego_period + ep_len, 8 - ep_len );
9465
9466 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009467 {
9468 return( 0 );
9469 }
9470
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009475
9476/*
9477 * Receive application data decrypted from the SSL layer
9478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009480{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009481 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009482 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009483
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009484 if( ssl == NULL || ssl->conf == NULL )
9485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009493 return( ret );
9494
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009495 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009497 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009498 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009499 return( ret );
9500 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009501 }
9502#endif
9503
Hanno Becker4a810fb2017-05-24 16:27:30 +01009504 /*
9505 * Check if renegotiation is necessary and/or handshake is
9506 * in process. If yes, perform/continue, and fall through
9507 * if an unexpected packet is received while the client
9508 * is waiting for the ServerHello.
9509 *
9510 * (There is no equivalent to the last condition on
9511 * the server-side as it is not treated as within
9512 * a handshake while waiting for the ClientHello
9513 * after a renegotiation request.)
9514 */
9515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009517 ret = ssl_check_ctr_renegotiate( ssl );
9518 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9519 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009522 return( ret );
9523 }
9524#endif
9525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009529 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9530 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009533 return( ret );
9534 }
9535 }
9536
Hanno Beckere41158b2017-10-23 13:30:32 +01009537 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009538 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009539 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009540 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009541 if( ssl->f_get_timer != NULL &&
9542 ssl->f_get_timer( ssl->p_timer ) == -1 )
9543 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009544 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009545 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009546
Hanno Becker327c93b2018-08-15 13:56:18 +01009547 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009548 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009549 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9550 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009551
Hanno Becker4a810fb2017-05-24 16:27:30 +01009552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9553 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009554 }
9555
9556 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009558 {
9559 /*
9560 * OpenSSL sends empty messages to randomize the IV
9561 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009562 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009565 return( 0 );
9566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009568 return( ret );
9569 }
9570 }
9571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009575
Hanno Becker4a810fb2017-05-24 16:27:30 +01009576 /*
9577 * - For client-side, expect SERVER_HELLO_REQUEST.
9578 * - For server-side, expect CLIENT_HELLO.
9579 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9580 */
9581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009583 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009585 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009588
9589 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009591 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009592 {
9593 continue;
9594 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009595#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009597 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009598#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009599
Hanno Becker4a810fb2017-05-24 16:27:30 +01009600#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009601 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009605
9606 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009609 {
9610 continue;
9611 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009612#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009614 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009615#endif /* MBEDTLS_SSL_SRV_C */
9616
Hanno Becker21df7f92017-10-17 11:03:26 +01009617#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009618 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009619 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9620 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9621 ssl->conf->allow_legacy_renegotiation ==
9622 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9623 {
9624 /*
9625 * Accept renegotiation request
9626 */
Paul Bakker48916f92012-09-16 19:57:18 +00009627
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009628 /* DTLS clients need to know renego is server-initiated */
9629#if defined(MBEDTLS_SSL_PROTO_DTLS)
9630 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9631 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9632 {
9633 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9634 }
9635#endif
9636 ret = ssl_start_renegotiation( ssl );
9637 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9638 ret != 0 )
9639 {
9640 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9641 return( ret );
9642 }
9643 }
9644 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009645#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009646 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009647 /*
9648 * Refuse renegotiation
9649 */
9650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653#if defined(MBEDTLS_SSL_PROTO_SSL3)
9654 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009655 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009656 /* SSLv3 does not have a "no_renegotiation" warning, so
9657 we send a fatal alert and abort the connection. */
9658 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9659 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9660 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009661 }
9662 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9664#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9665 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9666 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9669 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9670 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009671 {
9672 return( ret );
9673 }
Paul Bakker48916f92012-09-16 19:57:18 +00009674 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9677 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009681 }
Paul Bakker48916f92012-09-16 19:57:18 +00009682 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009683
Hanno Becker90333da2017-10-10 11:27:13 +01009684 /* At this point, we don't know whether the renegotiation has been
9685 * completed or not. The cases to consider are the following:
9686 * 1) The renegotiation is complete. In this case, no new record
9687 * has been read yet.
9688 * 2) The renegotiation is incomplete because the client received
9689 * an application data record while awaiting the ServerHello.
9690 * 3) The renegotiation is incomplete because the client received
9691 * a non-handshake, non-application data message while awaiting
9692 * the ServerHello.
9693 * In each of these case, looping will be the proper action:
9694 * - For 1), the next iteration will read a new record and check
9695 * if it's application data.
9696 * - For 2), the loop condition isn't satisfied as application data
9697 * is present, hence continue is the same as break
9698 * - For 3), the loop condition is satisfied and read_record
9699 * will re-deliver the message that was held back by the client
9700 * when expecting the ServerHello.
9701 */
9702 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009703 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009704#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009706 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009707 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009708 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009709 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009712 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009714 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009715 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9720 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009723 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009724 }
9725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9729 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009730 }
9731
9732 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009733
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009734 /* We're going to return something now, cancel timer,
9735 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009737 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009738
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009740 /* If we requested renego but received AppData, resend HelloRequest.
9741 * Do it now, after setting in_offt, to avoid taking this branch
9742 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009744 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009746 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009747 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009750 return( ret );
9751 }
9752 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009753#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009754#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009755 }
9756
9757 n = ( len < ssl->in_msglen )
9758 ? len : ssl->in_msglen;
9759
9760 memcpy( buf, ssl->in_offt, n );
9761 ssl->in_msglen -= n;
9762
9763 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009764 {
9765 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009766 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009767 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009769 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009770 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009771 /* more data available */
9772 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009776
Paul Bakker23986e52011-04-24 08:57:21 +00009777 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009778}
9779
9780/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009781 * Send application data to be encrypted by the SSL layer, taking care of max
9782 * fragment length and buffer size.
9783 *
9784 * According to RFC 5246 Section 6.2.1:
9785 *
9786 * Zero-length fragments of Application data MAY be sent as they are
9787 * potentially useful as a traffic analysis countermeasure.
9788 *
9789 * Therefore, it is possible that the input message length is 0 and the
9790 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009791 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009792static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009793 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009794{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009795 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9796 const size_t max_len = (size_t) ret;
9797
9798 if( ret < 0 )
9799 {
9800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9801 return( ret );
9802 }
9803
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009804 if( len > max_len )
9805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009807 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009810 "maximum fragment length: %d > %d",
9811 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009813 }
9814 else
9815#endif
9816 len = max_len;
9817 }
Paul Bakker887bd502011-06-08 13:10:54 +00009818
Paul Bakker5121ce52009-01-03 21:22:43 +00009819 if( ssl->out_left != 0 )
9820 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009821 /*
9822 * The user has previously tried to send the data and
9823 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9824 * written. In this case, we expect the high-level write function
9825 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009830 return( ret );
9831 }
9832 }
Paul Bakker887bd502011-06-08 13:10:54 +00009833 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009834 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009835 /*
9836 * The user is trying to send a message the first time, so we need to
9837 * copy the data into the internal buffers and setup the data structure
9838 * to keep track of partial writes
9839 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009840 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009842 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009843
Hanno Becker67bc7c32018-08-06 11:33:50 +01009844 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009847 return( ret );
9848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009849 }
9850
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009851 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009852}
9853
9854/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009855 * Write application data, doing 1/n-1 splitting if necessary.
9856 *
9857 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009858 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009859 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009862static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009863 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009864{
9865 int ret;
9866
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009867 if( ssl->conf->cbc_record_splitting ==
9868 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009869 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9871 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9872 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009873 {
9874 return( ssl_write_real( ssl, buf, len ) );
9875 }
9876
9877 if( ssl->split_done == 0 )
9878 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009879 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009880 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009881 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009882 }
9883
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009884 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9885 return( ret );
9886 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009887
9888 return( ret + 1 );
9889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009891
9892/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009893 * Write application data (public-facing wrapper)
9894 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009895int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009896{
9897 int ret;
9898
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009900
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009901 if( ssl == NULL || ssl->conf == NULL )
9902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9903
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009904#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009905 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9906 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009907 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009908 return( ret );
9909 }
9910#endif
9911
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009912 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009913 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009914 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009915 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009917 return( ret );
9918 }
9919 }
9920
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009921#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009922 ret = ssl_write_split( ssl, buf, len );
9923#else
9924 ret = ssl_write_real( ssl, buf, len );
9925#endif
9926
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009928
9929 return( ret );
9930}
9931
9932/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009933 * Notify the peer that the connection is being closed
9934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009936{
9937 int ret;
9938
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009939 if( ssl == NULL || ssl->conf == NULL )
9940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009943
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009944 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9950 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9951 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 return( ret );
9955 }
9956 }
9957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009959
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009960 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009961}
9962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009964{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009965 if( transform == NULL )
9966 return;
9967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009969 deflateEnd( &transform->ctx_deflate );
9970 inflateEnd( &transform->ctx_inflate );
9971#endif
9972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9974 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009975
Hanno Becker92231322018-01-03 15:32:51 +00009976#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 mbedtls_md_free( &transform->md_ctx_enc );
9978 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009979#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009980
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009981 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009982}
9983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984#if defined(MBEDTLS_X509_CRT_PARSE_C)
9985static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009988
9989 while( cur != NULL )
9990 {
9991 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009993 cur = next;
9994 }
9995}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009997
Hanno Becker0271f962018-08-16 13:23:47 +01009998#if defined(MBEDTLS_SSL_PROTO_DTLS)
9999
10000static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10001{
10002 unsigned offset;
10003 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10004
10005 if( hs == NULL )
10006 return;
10007
Hanno Becker283f5ef2018-08-24 09:34:47 +010010008 ssl_free_buffered_record( ssl );
10009
Hanno Becker0271f962018-08-16 13:23:47 +010010010 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010011 ssl_buffering_free_slot( ssl, offset );
10012}
10013
10014static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10015 uint8_t slot )
10016{
10017 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10018 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010019
10020 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10021 return;
10022
Hanno Beckere605b192018-08-21 15:59:07 +010010023 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010024 {
Hanno Beckere605b192018-08-21 15:59:07 +010010025 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010026 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010027 mbedtls_free( hs_buf->data );
10028 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010029 }
10030}
10031
10032#endif /* MBEDTLS_SSL_PROTO_DTLS */
10033
Gilles Peskine9b562d52018-04-25 20:32:43 +020010034void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010035{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010036 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10037
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010038 if( handshake == NULL )
10039 return;
10040
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010041#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10042 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10043 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010044 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010045 handshake->async_in_progress = 0;
10046 }
10047#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10048
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010049#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10050 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10051 mbedtls_md5_free( &handshake->fin_md5 );
10052 mbedtls_sha1_free( &handshake->fin_sha1 );
10053#endif
10054#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10055#if defined(MBEDTLS_SHA256_C)
10056 mbedtls_sha256_free( &handshake->fin_sha256 );
10057#endif
10058#if defined(MBEDTLS_SHA512_C)
10059 mbedtls_sha512_free( &handshake->fin_sha512 );
10060#endif
10061#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063#if defined(MBEDTLS_DHM_C)
10064 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066#if defined(MBEDTLS_ECDH_C)
10067 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010068#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010069#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010070 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010071#if defined(MBEDTLS_SSL_CLI_C)
10072 mbedtls_free( handshake->ecjpake_cache );
10073 handshake->ecjpake_cache = NULL;
10074 handshake->ecjpake_cache_len = 0;
10075#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010076#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010077
Janos Follath4ae5c292016-02-10 11:27:43 +000010078#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10079 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010080 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010082#endif
10083
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010084#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10085 if( handshake->psk != NULL )
10086 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010087 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010088 mbedtls_free( handshake->psk );
10089 }
10090#endif
10091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10093 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010094 /*
10095 * Free only the linked list wrapper, not the keys themselves
10096 * since the belong to the SNI callback
10097 */
10098 if( handshake->sni_key_cert != NULL )
10099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010101
10102 while( cur != NULL )
10103 {
10104 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010106 cur = next;
10107 }
10108 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010110
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010111#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010112 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010113#endif
10114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115#if defined(MBEDTLS_SSL_PROTO_DTLS)
10116 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010117 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010118 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010119#endif
10120
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010121 mbedtls_platform_zeroize( handshake,
10122 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010123}
10124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010126{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010127 if( session == NULL )
10128 return;
10129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000010131 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +000010132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133 mbedtls_x509_crt_free( session->peer_cert );
10134 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +000010135 }
Paul Bakkered27a042013-04-18 22:46:23 +020010136#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010137
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010138#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010140#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010141
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010142 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010143}
10144
Paul Bakker5121ce52009-01-03 21:22:43 +000010145/*
10146 * Free an SSL context
10147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010149{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010150 if( ssl == NULL )
10151 return;
10152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010154
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010155 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010156 {
Angus Grattond8213d02016-05-25 20:56:48 +100010157 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010158 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010159 }
10160
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010161 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010162 {
Angus Grattond8213d02016-05-25 20:56:48 +100010163 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010165 }
10166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010167#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010168 if( ssl->compress_buf != NULL )
10169 {
Angus Grattond8213d02016-05-25 20:56:48 +100010170 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010172 }
10173#endif
10174
Paul Bakker48916f92012-09-16 19:57:18 +000010175 if( ssl->transform )
10176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177 mbedtls_ssl_transform_free( ssl->transform );
10178 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010179 }
10180
10181 if( ssl->handshake )
10182 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010183 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10185 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 mbedtls_free( ssl->handshake );
10188 mbedtls_free( ssl->transform_negotiate );
10189 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010190 }
10191
Paul Bakkerc0463502013-02-14 11:19:38 +010010192 if( ssl->session )
10193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194 mbedtls_ssl_session_free( ssl->session );
10195 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010196 }
10197
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010198#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010199 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010200 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010201 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010203 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010204#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10207 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10210 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010211 }
10212#endif
10213
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010214#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010216#endif
10217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010219
Paul Bakker86f04f42013-02-14 11:20:09 +010010220 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010221 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010222}
10223
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010224/*
10225 * Initialze mbedtls_ssl_config
10226 */
10227void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10228{
10229 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10230}
10231
Simon Butcherc97b6972015-12-27 23:48:17 +000010232#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010233static int ssl_preset_default_hashes[] = {
10234#if defined(MBEDTLS_SHA512_C)
10235 MBEDTLS_MD_SHA512,
10236 MBEDTLS_MD_SHA384,
10237#endif
10238#if defined(MBEDTLS_SHA256_C)
10239 MBEDTLS_MD_SHA256,
10240 MBEDTLS_MD_SHA224,
10241#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010242#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010243 MBEDTLS_MD_SHA1,
10244#endif
10245 MBEDTLS_MD_NONE
10246};
Simon Butcherc97b6972015-12-27 23:48:17 +000010247#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010248
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010249static int ssl_preset_suiteb_ciphersuites[] = {
10250 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10251 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10252 0
10253};
10254
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010255#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010256static int ssl_preset_suiteb_hashes[] = {
10257 MBEDTLS_MD_SHA256,
10258 MBEDTLS_MD_SHA384,
10259 MBEDTLS_MD_NONE
10260};
10261#endif
10262
10263#if defined(MBEDTLS_ECP_C)
10264static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10265 MBEDTLS_ECP_DP_SECP256R1,
10266 MBEDTLS_ECP_DP_SECP384R1,
10267 MBEDTLS_ECP_DP_NONE
10268};
10269#endif
10270
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010271/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010272 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010273 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010274int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010275 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010276{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010277#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010278 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010279#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010280
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010281 /* Use the functions here so that they are covered in tests,
10282 * but otherwise access member directly for efficiency */
10283 mbedtls_ssl_conf_endpoint( conf, endpoint );
10284 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010285
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010286 /*
10287 * Things that are common to all presets
10288 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010289#if defined(MBEDTLS_SSL_CLI_C)
10290 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10291 {
10292 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10293#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10294 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10295#endif
10296 }
10297#endif
10298
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010299#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010300 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010301#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010302
10303#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10304 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10305#endif
10306
10307#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10308 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10309#endif
10310
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010311#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10312 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10313#endif
10314
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010315#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010316 conf->f_cookie_write = ssl_cookie_write_dummy;
10317 conf->f_cookie_check = ssl_cookie_check_dummy;
10318#endif
10319
10320#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10321 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10322#endif
10323
Janos Follath088ce432017-04-10 12:42:31 +010010324#if defined(MBEDTLS_SSL_SRV_C)
10325 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10326#endif
10327
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010328#if defined(MBEDTLS_SSL_PROTO_DTLS)
10329 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10330 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10331#endif
10332
10333#if defined(MBEDTLS_SSL_RENEGOTIATION)
10334 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010335 memset( conf->renego_period, 0x00, 2 );
10336 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010337#endif
10338
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010339#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10340 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10341 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010342 const unsigned char dhm_p[] =
10343 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10344 const unsigned char dhm_g[] =
10345 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10346
Hanno Beckera90658f2017-10-04 15:29:08 +010010347 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10348 dhm_p, sizeof( dhm_p ),
10349 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010350 {
10351 return( ret );
10352 }
10353 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010354#endif
10355
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010356 /*
10357 * Preset-specific defaults
10358 */
10359 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010360 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010361 /*
10362 * NSA Suite B
10363 */
10364 case MBEDTLS_SSL_PRESET_SUITEB:
10365 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10366 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10367 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10368 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10369
10370 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10371 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10372 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10373 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10374 ssl_preset_suiteb_ciphersuites;
10375
10376#if defined(MBEDTLS_X509_CRT_PARSE_C)
10377 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010378#endif
10379
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010380#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010381 conf->sig_hashes = ssl_preset_suiteb_hashes;
10382#endif
10383
10384#if defined(MBEDTLS_ECP_C)
10385 conf->curve_list = ssl_preset_suiteb_curves;
10386#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010387 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010388
10389 /*
10390 * Default
10391 */
10392 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010393 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10394 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10395 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10396 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10397 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10398 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10399 MBEDTLS_SSL_MIN_MINOR_VERSION :
10400 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010401 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10402 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10403
10404#if defined(MBEDTLS_SSL_PROTO_DTLS)
10405 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10406 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10407#endif
10408
10409 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10410 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10411 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10412 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10413 mbedtls_ssl_list_ciphersuites();
10414
10415#if defined(MBEDTLS_X509_CRT_PARSE_C)
10416 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10417#endif
10418
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010419#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010420 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010421#endif
10422
10423#if defined(MBEDTLS_ECP_C)
10424 conf->curve_list = mbedtls_ecp_grp_id_list();
10425#endif
10426
10427#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10428 conf->dhm_min_bitlen = 1024;
10429#endif
10430 }
10431
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010432 return( 0 );
10433}
10434
10435/*
10436 * Free mbedtls_ssl_config
10437 */
10438void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10439{
10440#if defined(MBEDTLS_DHM_C)
10441 mbedtls_mpi_free( &conf->dhm_P );
10442 mbedtls_mpi_free( &conf->dhm_G );
10443#endif
10444
10445#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10446 if( conf->psk != NULL )
10447 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010448 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010449 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010450 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010451 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010452 }
10453
10454 if( conf->psk_identity != NULL )
10455 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010456 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010457 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010458 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010459 conf->psk_identity_len = 0;
10460 }
10461#endif
10462
10463#if defined(MBEDTLS_X509_CRT_PARSE_C)
10464 ssl_key_cert_free( conf->key_cert );
10465#endif
10466
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010467 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010468}
10469
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010470#if defined(MBEDTLS_PK_C) && \
10471 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010472/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477#if defined(MBEDTLS_RSA_C)
10478 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10479 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481#if defined(MBEDTLS_ECDSA_C)
10482 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10483 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010486}
10487
Hanno Becker7e5437a2017-04-28 17:15:26 +010010488unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10489{
10490 switch( type ) {
10491 case MBEDTLS_PK_RSA:
10492 return( MBEDTLS_SSL_SIG_RSA );
10493 case MBEDTLS_PK_ECDSA:
10494 case MBEDTLS_PK_ECKEY:
10495 return( MBEDTLS_SSL_SIG_ECDSA );
10496 default:
10497 return( MBEDTLS_SSL_SIG_ANON );
10498 }
10499}
10500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010502{
10503 switch( sig )
10504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505#if defined(MBEDTLS_RSA_C)
10506 case MBEDTLS_SSL_SIG_RSA:
10507 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509#if defined(MBEDTLS_ECDSA_C)
10510 case MBEDTLS_SSL_SIG_ECDSA:
10511 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010512#endif
10513 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010515 }
10516}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010517#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010518
Hanno Becker7e5437a2017-04-28 17:15:26 +010010519#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10520 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10521
10522/* Find an entry in a signature-hash set matching a given hash algorithm. */
10523mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10524 mbedtls_pk_type_t sig_alg )
10525{
10526 switch( sig_alg )
10527 {
10528 case MBEDTLS_PK_RSA:
10529 return( set->rsa );
10530 case MBEDTLS_PK_ECDSA:
10531 return( set->ecdsa );
10532 default:
10533 return( MBEDTLS_MD_NONE );
10534 }
10535}
10536
10537/* Add a signature-hash-pair to a signature-hash set */
10538void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10539 mbedtls_pk_type_t sig_alg,
10540 mbedtls_md_type_t md_alg )
10541{
10542 switch( sig_alg )
10543 {
10544 case MBEDTLS_PK_RSA:
10545 if( set->rsa == MBEDTLS_MD_NONE )
10546 set->rsa = md_alg;
10547 break;
10548
10549 case MBEDTLS_PK_ECDSA:
10550 if( set->ecdsa == MBEDTLS_MD_NONE )
10551 set->ecdsa = md_alg;
10552 break;
10553
10554 default:
10555 break;
10556 }
10557}
10558
10559/* Allow exactly one hash algorithm for each signature. */
10560void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10561 mbedtls_md_type_t md_alg )
10562{
10563 set->rsa = md_alg;
10564 set->ecdsa = md_alg;
10565}
10566
10567#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10568 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10569
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010570/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010571 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010573mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010574{
10575 switch( hash )
10576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#if defined(MBEDTLS_MD5_C)
10578 case MBEDTLS_SSL_HASH_MD5:
10579 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010580#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581#if defined(MBEDTLS_SHA1_C)
10582 case MBEDTLS_SSL_HASH_SHA1:
10583 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585#if defined(MBEDTLS_SHA256_C)
10586 case MBEDTLS_SSL_HASH_SHA224:
10587 return( MBEDTLS_MD_SHA224 );
10588 case MBEDTLS_SSL_HASH_SHA256:
10589 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591#if defined(MBEDTLS_SHA512_C)
10592 case MBEDTLS_SSL_HASH_SHA384:
10593 return( MBEDTLS_MD_SHA384 );
10594 case MBEDTLS_SSL_HASH_SHA512:
10595 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010596#endif
10597 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010599 }
10600}
10601
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010602/*
10603 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10604 */
10605unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10606{
10607 switch( md )
10608 {
10609#if defined(MBEDTLS_MD5_C)
10610 case MBEDTLS_MD_MD5:
10611 return( MBEDTLS_SSL_HASH_MD5 );
10612#endif
10613#if defined(MBEDTLS_SHA1_C)
10614 case MBEDTLS_MD_SHA1:
10615 return( MBEDTLS_SSL_HASH_SHA1 );
10616#endif
10617#if defined(MBEDTLS_SHA256_C)
10618 case MBEDTLS_MD_SHA224:
10619 return( MBEDTLS_SSL_HASH_SHA224 );
10620 case MBEDTLS_MD_SHA256:
10621 return( MBEDTLS_SSL_HASH_SHA256 );
10622#endif
10623#if defined(MBEDTLS_SHA512_C)
10624 case MBEDTLS_MD_SHA384:
10625 return( MBEDTLS_SSL_HASH_SHA384 );
10626 case MBEDTLS_MD_SHA512:
10627 return( MBEDTLS_SSL_HASH_SHA512 );
10628#endif
10629 default:
10630 return( MBEDTLS_SSL_HASH_NONE );
10631 }
10632}
10633
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010634#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010635/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010636 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010637 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010638 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010639int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010642
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010643 if( ssl->conf->curve_list == NULL )
10644 return( -1 );
10645
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010646 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010647 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010648 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010649
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010650 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010651}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010652#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010653
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010654#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010655/*
10656 * Check if a hash proposed by the peer is in our list.
10657 * Return 0 if we're willing to use it, -1 otherwise.
10658 */
10659int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10660 mbedtls_md_type_t md )
10661{
10662 const int *cur;
10663
10664 if( ssl->conf->sig_hashes == NULL )
10665 return( -1 );
10666
10667 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10668 if( *cur == (int) md )
10669 return( 0 );
10670
10671 return( -1 );
10672}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010673#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675#if defined(MBEDTLS_X509_CRT_PARSE_C)
10676int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10677 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010678 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010679 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010680{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010681 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010683 int usage = 0;
10684#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010686 const char *ext_oid;
10687 size_t ext_len;
10688#endif
10689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10691 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010692 ((void) cert);
10693 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010694 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010695#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10698 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010699 {
10700 /* Server part of the key exchange */
10701 switch( ciphersuite->key_exchange )
10702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 case MBEDTLS_KEY_EXCHANGE_RSA:
10704 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010705 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010706 break;
10707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010708 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10709 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10710 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10711 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010712 break;
10713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10715 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010716 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010717 break;
10718
10719 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 case MBEDTLS_KEY_EXCHANGE_NONE:
10721 case MBEDTLS_KEY_EXCHANGE_PSK:
10722 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10723 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010724 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010725 usage = 0;
10726 }
10727 }
10728 else
10729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10731 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010732 }
10733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010735 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010736 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010737 ret = -1;
10738 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010739#else
10740 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010743#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10744 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10747 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010748 }
10749 else
10750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10752 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010753 }
10754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010756 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010757 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010758 ret = -1;
10759 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010760#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010761
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010762 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010765
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010766/*
10767 * Convert version numbers to/from wire format
10768 * and, for DTLS, to/from TLS equivalent.
10769 *
10770 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010771 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010772 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10773 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010776 unsigned char ver[2] )
10777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778#if defined(MBEDTLS_SSL_PROTO_DTLS)
10779 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010782 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10783
10784 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10785 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10786 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010787 else
10788#else
10789 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010790#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010791 {
10792 ver[0] = (unsigned char) major;
10793 ver[1] = (unsigned char) minor;
10794 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010795}
10796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010798 const unsigned char ver[2] )
10799{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010800#if defined(MBEDTLS_SSL_PROTO_DTLS)
10801 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010802 {
10803 *major = 255 - ver[0] + 2;
10804 *minor = 255 - ver[1] + 1;
10805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010806 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010807 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10808 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010809 else
10810#else
10811 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010812#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010813 {
10814 *major = ver[0];
10815 *minor = ver[1];
10816 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010817}
10818
Simon Butcher99000142016-10-13 17:21:01 +010010819int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10820{
10821#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10822 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10823 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10824
10825 switch( md )
10826 {
10827#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10828#if defined(MBEDTLS_MD5_C)
10829 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010830 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010831#endif
10832#if defined(MBEDTLS_SHA1_C)
10833 case MBEDTLS_SSL_HASH_SHA1:
10834 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10835 break;
10836#endif
10837#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10838#if defined(MBEDTLS_SHA512_C)
10839 case MBEDTLS_SSL_HASH_SHA384:
10840 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10841 break;
10842#endif
10843#if defined(MBEDTLS_SHA256_C)
10844 case MBEDTLS_SSL_HASH_SHA256:
10845 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10846 break;
10847#endif
10848 default:
10849 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10850 }
10851
10852 return 0;
10853#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10854 (void) ssl;
10855 (void) md;
10856
10857 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10858#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10859}
10860
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010861#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10862 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10863int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10864 unsigned char *output,
10865 unsigned char *data, size_t data_len )
10866{
10867 int ret = 0;
10868 mbedtls_md5_context mbedtls_md5;
10869 mbedtls_sha1_context mbedtls_sha1;
10870
10871 mbedtls_md5_init( &mbedtls_md5 );
10872 mbedtls_sha1_init( &mbedtls_sha1 );
10873
10874 /*
10875 * digitally-signed struct {
10876 * opaque md5_hash[16];
10877 * opaque sha_hash[20];
10878 * };
10879 *
10880 * md5_hash
10881 * MD5(ClientHello.random + ServerHello.random
10882 * + ServerParams);
10883 * sha_hash
10884 * SHA(ClientHello.random + ServerHello.random
10885 * + ServerParams);
10886 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010887 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010888 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010890 goto exit;
10891 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010892 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010893 ssl->handshake->randbytes, 64 ) ) != 0 )
10894 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010896 goto exit;
10897 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010898 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010899 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010901 goto exit;
10902 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010903 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010904 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010906 goto exit;
10907 }
10908
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010909 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010910 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010912 goto exit;
10913 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010914 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010915 ssl->handshake->randbytes, 64 ) ) != 0 )
10916 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010918 goto exit;
10919 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010920 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010921 data_len ) ) != 0 )
10922 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010924 goto exit;
10925 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010926 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010927 output + 16 ) ) != 0 )
10928 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010930 goto exit;
10931 }
10932
10933exit:
10934 mbedtls_md5_free( &mbedtls_md5 );
10935 mbedtls_sha1_free( &mbedtls_sha1 );
10936
10937 if( ret != 0 )
10938 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10939 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10940
10941 return( ret );
10942
10943}
10944#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10945 MBEDTLS_SSL_PROTO_TLS1_1 */
10946
10947#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10948 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10949int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010950 unsigned char *hash, size_t *hashlen,
10951 unsigned char *data, size_t data_len,
10952 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010953{
10954 int ret = 0;
10955 mbedtls_md_context_t ctx;
10956 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010957 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010958
10959 mbedtls_md_init( &ctx );
10960
10961 /*
10962 * digitally-signed struct {
10963 * opaque client_random[32];
10964 * opaque server_random[32];
10965 * ServerDHParams params;
10966 * };
10967 */
10968 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10969 {
10970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10971 goto exit;
10972 }
10973 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10974 {
10975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10976 goto exit;
10977 }
10978 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10979 {
10980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10981 goto exit;
10982 }
10983 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10984 {
10985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10986 goto exit;
10987 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010988 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010989 {
10990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10991 goto exit;
10992 }
10993
10994exit:
10995 mbedtls_md_free( &ctx );
10996
10997 if( ret != 0 )
10998 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10999 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11000
11001 return( ret );
11002}
11003#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11004 MBEDTLS_SSL_PROTO_TLS1_2 */
11005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006#endif /* MBEDTLS_SSL_TLS_C */