blob: dc52024646db7f5441f4f7c009c057c0444ea065 [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],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100733#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200734#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
735 int encrypt_then_mac,
736#endif
737#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
738 int trunc_hmac,
739#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100740#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200741#if defined(MBEDTLS_ZLIB_SUPPORT)
742 int compression,
743#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200744 ssl_tls_prf_t tls_prf,
745 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200746 int minor_ver,
747 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200748 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000749{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200750 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 unsigned char keyblk[256];
752 unsigned char *key1;
753 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100754 unsigned char *mac_enc;
755 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000756 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200757 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000758 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000759 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 const mbedtls_cipher_info_t *cipher_info;
761 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100762
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200763#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
764 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
765 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200766 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000768#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000769
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200770 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100771#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
772 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200773 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000774#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200775 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200777 /*
778 * Get various info structures
779 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200780 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200781 if( ciphersuite_info == NULL )
782 {
783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200784 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
786 }
787
Hanno Becker8759e162017-12-27 21:34:08 +0000788 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100789 if( cipher_info == NULL )
790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000792 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100794 }
795
Hanno Becker8759e162017-12-27 21:34:08 +0000796 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100797 if( md_info == NULL )
798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000800 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100802 }
803
Hanno Beckera5a2b082019-05-15 14:03:01 +0100804#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100805 /* Copy own and peer's CID if the use of the CID
806 * extension has been negotiated. */
807 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
808 {
809 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100810
Hanno Becker4932f9f2019-05-03 15:23:51 +0100811 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100812 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100813 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100814 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100815
816 transform->out_cid_len = ssl->handshake->peer_cid_len;
817 memcpy( transform->out_cid, ssl->handshake->peer_cid,
818 ssl->handshake->peer_cid_len );
819 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
820 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100821 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100822#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100823
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200825 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200827 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100828 if( ret != 0 )
829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100831 return( ret );
832 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200835 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200836 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200837 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 /*
841 * Determine the appropriate key, IV and MAC length.
842 */
Paul Bakker68884e32013-01-07 18:20:04 +0100843
Hanno Beckere7f2df02017-12-27 08:17:40 +0000844 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200845
Hanno Beckerf1229442018-01-03 15:32:31 +0000846#if defined(MBEDTLS_GCM_C) || \
847 defined(MBEDTLS_CCM_C) || \
848 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200850 cipher_info->mode == MBEDTLS_MODE_CCM ||
851 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 {
Hanno Becker8759e162017-12-27 21:34:08 +0000853 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200854
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200855 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000856 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000857 transform->taglen =
858 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200859
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200860 /* All modes haves 96-bit IVs;
861 * GCM and CCM has 4 implicit and 8 explicit bytes
862 * ChachaPoly has all 12 bytes implicit
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200865 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
866 transform->fixed_ivlen = 12;
867 else
868 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200869
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200870 /* Minimum length of encrypted record */
871 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000872 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100873 }
874 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000875#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
876#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
877 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
878 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100879 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200880 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
882 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200885 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100886 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000887
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200888 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000889 mac_key_len = mbedtls_md_get_size( md_info );
890 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200893 /*
894 * If HMAC is to be truncated, we shall keep the leftmost bytes,
895 * (rfc 6066 page 13 or rfc 2104 section 4),
896 * so we only need to adjust the length here.
897 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200898 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000901
902#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
903 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000904 * HMAC implementation which also truncates the key
905 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000906 mac_key_len = transform->maclen;
907#endif
908 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200910
911 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100912 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200914 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200916 transform->minlen = transform->maclen;
917 else
Paul Bakker68884e32013-01-07 18:20:04 +0100918 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200919 /*
920 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100921 * 1. if EtM is in use: one block plus MAC
922 * otherwise: * first multiple of blocklen greater than maclen
923 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200926 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100927 {
928 transform->minlen = transform->maclen
929 + cipher_info->block_size;
930 }
931 else
932#endif
933 {
934 transform->minlen = transform->maclen
935 + cipher_info->block_size
936 - transform->maclen % cipher_info->block_size;
937 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200940 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
941 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200942 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100943 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200946 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
947 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200948 {
949 transform->minlen += transform->ivlen;
950 }
951 else
952#endif
953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200956 }
Paul Bakker68884e32013-01-07 18:20:04 +0100957 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000959 else
960#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
961 {
962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
963 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
964 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Hanno Beckere7f2df02017-12-27 08:17:40 +0000966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
967 (unsigned) keylen,
968 (unsigned) transform->minlen,
969 (unsigned) transform->ivlen,
970 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972 /*
973 * Finally setup the cipher contexts, IVs and MAC secrets.
974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200976 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000978 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000979 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker68884e32013-01-07 18:20:04 +0100981 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000982 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000984 /*
985 * This is not used in TLS v1.1.
986 */
Paul Bakker48916f92012-09-16 19:57:18 +0000987 iv_copy_len = ( transform->fixed_ivlen ) ?
988 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000989 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
990 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000991 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 }
993 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#endif /* MBEDTLS_SSL_CLI_C */
995#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200996 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000997 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000998 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001002 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001004 /*
1005 * This is not used in TLS v1.1.
1006 */
Paul Bakker48916f92012-09-16 19:57:18 +00001007 iv_copy_len = ( transform->fixed_ivlen ) ?
1008 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001009 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1010 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001011 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001013 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001018 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Hanno Becker92231322018-01-03 15:32:51 +00001020#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001022 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001023 {
Hanno Becker92231322018-01-03 15:32:51 +00001024 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001028 }
1029
Hanno Becker81c7b182017-11-09 18:39:33 +00001030 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1031 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
1033 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1035#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1036 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001037 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001038 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001039 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1040 For AEAD-based ciphersuites, there is nothing to do here. */
1041 if( mac_key_len != 0 )
1042 {
1043 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1044 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1045 }
Paul Bakker68884e32013-01-07 18:20:04 +01001046 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001047 else
1048#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001052 }
Hanno Becker92231322018-01-03 15:32:51 +00001053#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1056 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001057 {
1058 int ret = 0;
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001061
Hanno Beckere7f2df02017-12-27 08:17:40 +00001062 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001063 transform->iv_enc, transform->iv_dec,
1064 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001065 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001066 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1069 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001070 }
1071 }
Hanno Becker92231322018-01-03 15:32:51 +00001072#else
1073 ((void) mac_dec);
1074 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001076
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001077#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1078 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001079 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001080 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001081 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001082 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001083 iv_copy_len );
1084 }
1085#endif
1086
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001087 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001088 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001091 return( ret );
1092 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001093
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001094 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095 cipher_info ) ) != 0 )
1096 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098 return( ret );
1099 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001102 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106 return( ret );
1107 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001110 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001114 return( ret );
1115 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_CIPHER_MODE_CBC)
1118 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1121 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001124 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001125 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1128 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 return( ret );
1132 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001136 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001138 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001140 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001143
Paul Bakker48916f92012-09-16 19:57:18 +00001144 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1145 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001146
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001147 if( deflateInit( &transform->ctx_deflate,
1148 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001149 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1152 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001153 }
1154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001156
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001160/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001161 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001162 *
1163 * Inputs:
1164 * - SSL/TLS minor version
1165 * - hash associated with the ciphersuite (only used by TLS 1.2)
1166 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001167 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001168 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1169 */
1170static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1171 int minor_ver,
1172 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001173{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001174#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1175 (void) hash;
1176#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001177
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001178#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001179 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001180 {
1181 handshake->tls_prf = ssl3_prf;
1182 handshake->calc_verify = ssl_calc_verify_ssl;
1183 handshake->calc_finished = ssl_calc_finished_ssl;
1184 }
1185 else
1186#endif
1187#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001188 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001189 {
1190 handshake->tls_prf = tls1_prf;
1191 handshake->calc_verify = ssl_calc_verify_tls;
1192 handshake->calc_finished = ssl_calc_finished_tls;
1193 }
1194 else
1195#endif
1196#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1197#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001198 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1199 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001200 {
1201 handshake->tls_prf = tls_prf_sha384;
1202 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1203 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1204 }
1205 else
1206#endif
1207#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001208 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001209 {
1210 handshake->tls_prf = tls_prf_sha256;
1211 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1212 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1213 }
1214 else
1215#endif
1216#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1217 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1219 }
1220
1221 return( 0 );
1222}
1223
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001224/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001225 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001226 *
1227 * Parameters:
1228 * [in/out] handshake
1229 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1230 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001231 * [out] master
1232 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001233 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001234static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001235 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001236 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001237{
1238 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001239
1240#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001241 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001242 (void) ssl;
1243#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001244
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001245 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001246 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1248 return( 0 );
1249 }
1250
1251 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1252 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001253
1254#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001255 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001256 {
1257 unsigned char session_hash[48];
1258 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001259
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001260 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001261
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001262 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1263 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001264
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266 "extended master secret",
1267 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001268 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001269 }
1270 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001271#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001272 {
1273 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1274 "master secret",
1275 handshake->randbytes, 64,
1276 master, 48 );
1277 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001278 if( ret != 0 )
1279 {
1280 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1281 return( ret );
1282 }
1283
1284 mbedtls_platform_zeroize( handshake->premaster,
1285 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001286
1287 return( 0 );
1288}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001289
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001290int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1291{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001292 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001293 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1294 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001295
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1297
1298 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001299 ret = ssl_set_handshake_prfs( ssl->handshake,
1300 ssl->minor_ver,
1301 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001302 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001303 {
1304 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001305 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001306 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001307
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001308 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001309 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001310 ssl->session_negotiate->master,
1311 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001312 if( ret != 0 )
1313 {
1314 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1315 return( ret );
1316 }
1317
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001318 /* Swap the client and server random values:
1319 * - MS derivation wanted client+server (RFC 5246 8.1)
1320 * - key derivation wants server+client (RFC 5246 6.3) */
1321 {
1322 unsigned char tmp[64];
1323 memcpy( tmp, ssl->handshake->randbytes, 64 );
1324 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1325 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1326 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1327 }
1328
1329 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001330 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001331 ssl->session_negotiate->ciphersuite,
1332 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001333#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001334#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1335 ssl->session_negotiate->encrypt_then_mac,
1336#endif
1337#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1338 ssl->session_negotiate->trunc_hmac,
1339#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001340#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001341#if defined(MBEDTLS_ZLIB_SUPPORT)
1342 ssl->session_negotiate->compression,
1343#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001344 ssl->handshake->tls_prf,
1345 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001346 ssl->minor_ver,
1347 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001348 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001349 if( ret != 0 )
1350 {
1351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1352 return( ret );
1353 }
1354
1355 /* We no longer need Server/ClientHello.random values */
1356 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1357 sizeof( ssl->handshake->randbytes ) );
1358
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001359 /* Allocate compression buffer */
1360#if defined(MBEDTLS_ZLIB_SUPPORT)
1361 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1362 ssl->compress_buf == NULL )
1363 {
1364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1365 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1366 if( ssl->compress_buf == NULL )
1367 {
1368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001369 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1371 }
1372 }
1373#endif
1374
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1376
1377 return( 0 );
1378}
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001381void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1382 unsigned char hash[36],
1383 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001385 mbedtls_md5_context md5;
1386 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 unsigned char pad_1[48];
1388 unsigned char pad_2[48];
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001392 mbedtls_md5_init( &md5 );
1393 mbedtls_sha1_init( &sha1 );
1394
1395 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1396 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker380da532012-04-18 16:10:25 +00001398 memset( pad_1, 0x36, 48 );
1399 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1403 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001405 mbedtls_md5_starts_ret( &md5 );
1406 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1407 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1408 mbedtls_md5_update_ret( &md5, hash, 16 );
1409 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1413 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001414
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001415 mbedtls_sha1_starts_ret( &sha1 );
1416 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1417 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1418 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1419 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001421 *hlen = 36;
1422
1423 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001425
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001426 mbedtls_md5_free( &md5 );
1427 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001428
Paul Bakker380da532012-04-18 16:10:25 +00001429 return;
1430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001434void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1435 unsigned char hash[36],
1436 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001437{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001438 mbedtls_md5_context md5;
1439 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001443 mbedtls_md5_init( &md5 );
1444 mbedtls_sha1_init( &sha1 );
1445
1446 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1447 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001449 mbedtls_md5_finish_ret( &md5, hash );
1450 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001452 *hlen = 36;
1453
1454 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001457 mbedtls_md5_free( &md5 );
1458 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001459
Paul Bakker380da532012-04-18 16:10:25 +00001460 return;
1461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1465#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001466void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1467 unsigned char hash[32],
1468 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001469{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001470 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001472 mbedtls_sha256_init( &sha256 );
1473
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001476 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001477 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001479 *hlen = 32;
1480
1481 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001484 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001485
Paul Bakker380da532012-04-18 16:10:25 +00001486 return;
1487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001491void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1492 unsigned char hash[48],
1493 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001494{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001497 mbedtls_sha512_init( &sha512 );
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001501 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001502 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001504 *hlen = 48;
1505
1506 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001509 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 return;
1512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SHA512_C */
1514#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1517int 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 +02001518{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001519 unsigned char *p = ssl->handshake->premaster;
1520 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001521 const unsigned char *psk = ssl->conf->psk;
1522 size_t psk_len = ssl->conf->psk_len;
1523
1524 /* If the psk callback was called, use its result */
1525 if( ssl->handshake->psk != NULL )
1526 {
1527 psk = ssl->handshake->psk;
1528 psk_len = ssl->handshake->psk_len;
1529 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001530
1531 /*
1532 * PMS = struct {
1533 * opaque other_secret<0..2^16-1>;
1534 * opaque psk<0..2^16-1>;
1535 * };
1536 * with "other_secret" depending on the particular key exchange
1537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1539 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001541 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001544 *(p++) = (unsigned char)( psk_len >> 8 );
1545 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001546
1547 if( end < p || (size_t)( end - p ) < psk_len )
1548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1549
1550 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001551 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552 }
1553 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1555#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1556 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001557 {
1558 /*
1559 * other_secret already set by the ClientKeyExchange message,
1560 * and is 48 bytes long
1561 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001562 if( end - p < 2 )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001565 *p++ = 0;
1566 *p++ = 48;
1567 p += 48;
1568 }
1569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1571#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1572 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001574 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001575 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001577 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001579 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001580 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001583 return( ret );
1584 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001585 *(p++) = (unsigned char)( len >> 8 );
1586 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001587 p += len;
1588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 }
1591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1593#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1594 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001595 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001596 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 size_t zlen;
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001600 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001601 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604 return( ret );
1605 }
1606
1607 *(p++) = (unsigned char)( zlen >> 8 );
1608 *(p++) = (unsigned char)( zlen );
1609 p += zlen;
1610
Janos Follath3fbdada2018-08-15 10:26:53 +01001611 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1612 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 }
1614 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 }
1620
1621 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001622 if( end - p < 2 )
1623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001624
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001625 *(p++) = (unsigned char)( psk_len >> 8 );
1626 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001627
1628 if( end < p || (size_t)( end - p ) < psk_len )
1629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1630
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001631 memcpy( p, psk, psk_len );
1632 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633
1634 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1635
1636 return( 0 );
1637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001641/*
1642 * SSLv3.0 MAC functions
1643 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001644#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001645static void ssl_mac( mbedtls_md_context_t *md_ctx,
1646 const unsigned char *secret,
1647 const unsigned char *buf, size_t len,
1648 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001649 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
1651 unsigned char header[11];
1652 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001653 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1655 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001656
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001657 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001659 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001660 else
Paul Bakker68884e32013-01-07 18:20:04 +01001661 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663 memcpy( header, ctr, 8 );
1664 header[ 8] = (unsigned char) type;
1665 header[ 9] = (unsigned char)( len >> 8 );
1666 header[10] = (unsigned char)( len );
1667
Paul Bakker68884e32013-01-07 18:20:04 +01001668 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_md_starts( md_ctx );
1670 mbedtls_md_update( md_ctx, secret, md_size );
1671 mbedtls_md_update( md_ctx, padding, padlen );
1672 mbedtls_md_update( md_ctx, header, 11 );
1673 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Paul Bakker68884e32013-01-07 18:20:04 +01001676 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_md_starts( md_ctx );
1678 mbedtls_md_update( md_ctx, secret, md_size );
1679 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001680 mbedtls_md_update( md_ctx, out, md_size );
1681 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001684
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001685/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001686 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001687#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001688 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1689 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1690 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1691/* This function makes sure every byte in the memory region is accessed
1692 * (in ascending addresses order) */
1693static void ssl_read_memory( unsigned char *p, size_t len )
1694{
1695 unsigned char acc = 0;
1696 volatile unsigned char force;
1697
1698 for( ; len != 0; p++, len-- )
1699 acc ^= *p;
1700
1701 force = acc;
1702 (void) force;
1703}
1704#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1705
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001706/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001708 */
Hanno Becker3307b532017-12-27 21:37:21 +00001709
Hanno Beckera5a2b082019-05-15 14:03:01 +01001710#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001711/* This functions transforms a DTLS plaintext fragment and a record content
1712 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001713 *
1714 * struct {
1715 * opaque content[DTLSPlaintext.length];
1716 * ContentType real_type;
1717 * uint8 zeros[length_of_padding];
1718 * } DTLSInnerPlaintext;
1719 *
1720 * Input:
1721 * - `content`: The beginning of the buffer holding the
1722 * plaintext to be wrapped.
1723 * - `*content_size`: The length of the plaintext in Bytes.
1724 * - `max_len`: The number of Bytes available starting from
1725 * `content`. This must be `>= *content_size`.
1726 * - `rec_type`: The desired record content type.
1727 *
1728 * Output:
1729 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1730 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1731 *
1732 * Returns:
1733 * - `0` on success.
1734 * - A negative error code if `max_len` didn't offer enough space
1735 * for the expansion.
1736 */
1737static int ssl_cid_build_inner_plaintext( unsigned char *content,
1738 size_t *content_size,
1739 size_t remaining,
1740 uint8_t rec_type )
1741{
1742 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001743 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1744 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1745 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001746
1747 /* Write real content type */
1748 if( remaining == 0 )
1749 return( -1 );
1750 content[ len ] = rec_type;
1751 len++;
1752 remaining--;
1753
1754 if( remaining < pad )
1755 return( -1 );
1756 memset( content + len, 0, pad );
1757 len += pad;
1758 remaining -= pad;
1759
1760 *content_size = len;
1761 return( 0 );
1762}
1763
Hanno Becker7dc25772019-05-20 15:08:01 +01001764/* This function parses a DTLSInnerPlaintext structure.
1765 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001766static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1767 size_t *content_size,
1768 uint8_t *rec_type )
1769{
1770 size_t remaining = *content_size;
1771
1772 /* Determine length of padding by skipping zeroes from the back. */
1773 do
1774 {
1775 if( remaining == 0 )
1776 return( -1 );
1777 remaining--;
1778 } while( content[ remaining ] == 0 );
1779
1780 *content_size = remaining;
1781 *rec_type = content[ remaining ];
1782
1783 return( 0 );
1784}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001785#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001786
Hanno Becker99abf512019-05-20 14:50:53 +01001787/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001788 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001789static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001790 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001791 mbedtls_record *rec )
1792{
Hanno Becker99abf512019-05-20 14:50:53 +01001793 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001794 *
1795 * additional_data = seq_num + TLSCompressed.type +
1796 * TLSCompressed.version + TLSCompressed.length;
1797 *
Hanno Becker99abf512019-05-20 14:50:53 +01001798 * For the CID extension, this is extended as follows
1799 * (quoting draft-ietf-tls-dtls-connection-id-05,
1800 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001801 *
1802 * additional_data = seq_num + DTLSPlaintext.type +
1803 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001804 * cid +
1805 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001806 * length_of_DTLSInnerPlaintext;
1807 */
1808
Hanno Becker3307b532017-12-27 21:37:21 +00001809 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1810 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001811 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001812
Hanno Beckera5a2b082019-05-15 14:03:01 +01001813#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001814 if( rec->cid_len != 0 )
1815 {
1816 memcpy( add_data + 11, rec->cid, rec->cid_len );
1817 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1818 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1819 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1820 *add_data_len = 13 + 1 + rec->cid_len;
1821 }
1822 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001823#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001824 {
1825 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1826 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1827 *add_data_len = 13;
1828 }
Hanno Becker3307b532017-12-27 21:37:21 +00001829}
1830
Hanno Becker611a83b2018-01-03 14:27:32 +00001831int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1832 mbedtls_ssl_transform *transform,
1833 mbedtls_record *rec,
1834 int (*f_rng)(void *, unsigned char *, size_t),
1835 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001838 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001839 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001840 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001841 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001842 size_t post_avail;
1843
1844 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001845#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001846 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001847 ((void) ssl);
1848#endif
1849
1850 /* The PRNG is used for dynamic IV generation that's used
1851 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1852#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1853 ( defined(MBEDTLS_AES_C) || \
1854 defined(MBEDTLS_ARIA_C) || \
1855 defined(MBEDTLS_CAMELLIA_C) ) && \
1856 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1857 ((void) f_rng);
1858 ((void) p_rng);
1859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Hanno Becker3307b532017-12-27 21:37:21 +00001863 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001864 {
Hanno Becker3307b532017-12-27 21:37:21 +00001865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1867 }
Hanno Becker505089d2019-05-01 09:45:57 +01001868 if( rec == NULL
1869 || rec->buf == NULL
1870 || rec->buf_len < rec->data_offset
1871 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001872#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001873 || rec->cid_len != 0
1874#endif
1875 )
Hanno Becker3307b532017-12-27 21:37:21 +00001876 {
1877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 }
1880
Hanno Becker3307b532017-12-27 21:37:21 +00001881 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001882 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001884 data, rec->data_len );
1885
1886 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1887
1888 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1889 {
1890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1891 (unsigned) rec->data_len,
1892 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1894 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001895
Hanno Beckera5a2b082019-05-15 14:03:01 +01001896#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001897 /*
1898 * Add CID information
1899 */
1900 rec->cid_len = transform->out_cid_len;
1901 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1902 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001903
1904 if( rec->cid_len != 0 )
1905 {
1906 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001907 * Wrap plaintext into DTLSInnerPlaintext structure.
1908 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001909 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001910 * Note that this changes `rec->data_len`, and hence
1911 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001912 */
1913 if( ssl_cid_build_inner_plaintext( data,
1914 &rec->data_len,
1915 post_avail,
1916 rec->type ) != 0 )
1917 {
1918 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1919 }
1920
1921 rec->type = MBEDTLS_SSL_MSG_CID;
1922 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001923#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001924
Hanno Becker92c930f2019-04-29 17:31:37 +01001925 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001928 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001930#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 if( mode == MBEDTLS_MODE_STREAM ||
1932 ( mode == MBEDTLS_MODE_CBC
1933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001934 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001935#endif
1936 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 {
Hanno Becker3307b532017-12-27 21:37:21 +00001938 if( post_avail < transform->maclen )
1939 {
1940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1941 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1942 }
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001945 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001946 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001947 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001948 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1949 data, rec->data_len, rec->ctr, rec->type, mac );
1950 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001951 }
1952 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1955 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001956 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001957 {
Hanno Becker992b6872017-11-09 18:57:39 +00001958 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1959
Hanno Beckere83efe62019-04-29 13:52:53 +01001960 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001961
Hanno Becker3307b532017-12-27 21:37:21 +00001962 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001963 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001964 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1965 data, rec->data_len );
1966 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1967 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1968
1969 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 }
1971 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001972#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001976 }
1977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1979 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001980
Hanno Becker3307b532017-12-27 21:37:21 +00001981 rec->data_len += transform->maclen;
1982 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001984 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001985#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001987 /*
1988 * Encrypt
1989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1991 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001993 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001994 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001996 "including %d bytes of padding",
1997 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Hanno Becker3307b532017-12-27 21:37:21 +00001999 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2000 transform->iv_enc, transform->ivlen,
2001 data, rec->data_len,
2002 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002005 return( ret );
2006 }
2007
Hanno Becker3307b532017-12-27 21:37:21 +00002008 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
Paul Bakker68884e32013-01-07 18:20:04 +01002014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002017#if defined(MBEDTLS_GCM_C) || \
2018 defined(MBEDTLS_CCM_C) || \
2019 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002021 mode == MBEDTLS_MODE_CCM ||
2022 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002023 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002024 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002025 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002026 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002027
Hanno Becker3307b532017-12-27 21:37:21 +00002028 /* Check that there's space for both the authentication tag
2029 * and the explicit IV before and after the record content. */
2030 if( post_avail < transform->taglen ||
2031 rec->data_offset < explicit_iv_len )
2032 {
2033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2034 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2035 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002036
Paul Bakker68884e32013-01-07 18:20:04 +01002037 /*
2038 * Generate IV
2039 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2041 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002042 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002044 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2045 explicit_iv_len );
2046 /* Prefix record content with explicit IV. */
2047 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002048 }
2049 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2050 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002051 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002052 unsigned char i;
2053
2054 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2055
2056 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002057 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 }
2059 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002060 {
2061 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002064 }
2065
Hanno Beckere83efe62019-04-29 13:52:53 +01002066 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002067
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2069 iv, transform->ivlen );
2070 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002071 data - explicit_iv_len, explicit_iv_len );
2072 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002073 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002075 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002076 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002077
Paul Bakker68884e32013-01-07 18:20:04 +01002078 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002079 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002080 */
Hanno Becker3307b532017-12-27 21:37:21 +00002081
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002083 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002084 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002085 data, rec->data_len, /* source */
2086 data, &rec->data_len, /* destination */
2087 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002090 return( ret );
2091 }
2092
Hanno Becker3307b532017-12-27 21:37:21 +00002093 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2094 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095
Hanno Becker3307b532017-12-27 21:37:21 +00002096 rec->data_len += transform->taglen + explicit_iv_len;
2097 rec->data_offset -= explicit_iv_len;
2098 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002099 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2103#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002104 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002107 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002108 size_t padlen, i;
2109 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 /* Currently we're always using minimal padding
2112 * (up to 255 bytes would be allowed). */
2113 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2114 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 padlen = 0;
2116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 /* Check there's enough space in the buffer for the padding. */
2118 if( post_avail < padlen + 1 )
2119 {
2120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2121 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2122 }
2123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002125 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Hanno Becker3307b532017-12-27 21:37:21 +00002127 rec->data_len += padlen + 1;
2128 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002132 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2133 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002134 */
Hanno Becker3307b532017-12-27 21:37:21 +00002135 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002136 {
Hanno Becker3307b532017-12-27 21:37:21 +00002137 if( f_rng == NULL )
2138 {
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2141 }
2142
2143 if( rec->data_offset < transform->ivlen )
2144 {
2145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2146 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2147 }
2148
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 /*
2150 * Generate IV
2151 */
Hanno Becker3307b532017-12-27 21:37:21 +00002152 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002153 if( ret != 0 )
2154 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155
Hanno Becker3307b532017-12-27 21:37:21 +00002156 memcpy( data - transform->ivlen, transform->iv_enc,
2157 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002163 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002164 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002165 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Hanno Becker3307b532017-12-27 21:37:21 +00002167 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2168 transform->iv_enc,
2169 transform->ivlen,
2170 data, rec->data_len,
2171 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002174 return( ret );
2175 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002176
Hanno Becker3307b532017-12-27 21:37:21 +00002177 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002181 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002184 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002185 {
2186 /*
2187 * Save IV in SSL3 and TLS1
2188 */
Hanno Becker3307b532017-12-27 21:37:21 +00002189 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2190 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 }
Hanno Becker3307b532017-12-27 21:37:21 +00002192 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002193#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002194 {
2195 data -= transform->ivlen;
2196 rec->data_offset -= transform->ivlen;
2197 rec->data_len += transform->ivlen;
2198 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002201 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002202 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002203 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2204
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002205 /*
2206 * MAC(MAC_write_key, seq_num +
2207 * TLSCipherText.type +
2208 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002209 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002210 * IV + // except for TLS 1.0
2211 * ENC(content + padding + padding_length));
2212 */
Hanno Becker3307b532017-12-27 21:37:21 +00002213
2214 if( post_avail < transform->maclen)
2215 {
2216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2217 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2218 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002219
Hanno Beckere83efe62019-04-29 13:52:53 +01002220 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002223 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002224 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225
Hanno Becker3307b532017-12-27 21:37:21 +00002226 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002227 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002228 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2229 data, rec->data_len );
2230 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2231 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002232
Hanno Becker3307b532017-12-27 21:37:21 +00002233 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Becker3307b532017-12-27 21:37:21 +00002235 rec->data_len += transform->maclen;
2236 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002237 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002243 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249 /* Make extra sure authentication was performed, exactly once */
2250 if( auth_done != 1 )
2251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002254 }
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
2258 return( 0 );
2259}
2260
Hanno Becker611a83b2018-01-03 14:27:32 +00002261int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2262 mbedtls_ssl_transform *transform,
2263 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002265 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002267 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002268#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002269 size_t padlen = 0, correct = 1;
2270#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002271 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002272 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002273 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002274
Hanno Becker611a83b2018-01-03 14:27:32 +00002275#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002276 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002277 ((void) ssl);
2278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002281 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2285 }
2286 if( rec == NULL ||
2287 rec->buf == NULL ||
2288 rec->buf_len < rec->data_offset ||
2289 rec->buf_len - rec->data_offset < rec->data_len )
2290 {
2291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002293 }
2294
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295 data = rec->buf + rec->data_offset;
2296 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Hanno Beckera5a2b082019-05-15 14:03:01 +01002298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002299 /*
2300 * Match record's CID with incoming CID.
2301 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002302 if( rec->cid_len != transform->in_cid_len ||
2303 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2304 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002305 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002306 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2310 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002311 {
2312 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002313 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2314 transform->iv_dec,
2315 transform->ivlen,
2316 data, rec->data_len,
2317 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002320 return( ret );
2321 }
2322
Hanno Becker4c6876b2017-12-27 21:28:58 +00002323 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
Paul Bakker68884e32013-01-07 18:20:04 +01002329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331#if defined(MBEDTLS_GCM_C) || \
2332 defined(MBEDTLS_CCM_C) || \
2333 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 mode == MBEDTLS_MODE_CCM ||
2336 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002337 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002340
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002341 /*
2342 * Compute and update sizes
2343 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002344 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002347 "+ taglen (%d)", rec->data_len,
2348 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002350 }
Paul Bakker68884e32013-01-07 18:20:04 +01002351
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352 /*
2353 * Prepare IV
2354 */
2355 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2356 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002357 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002358 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002360
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002361 }
2362 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2363 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002364 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 unsigned char i;
2366
2367 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2368
2369 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002370 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002371 }
2372 else
2373 {
2374 /* Reminder if we ever add an AEAD mode with a different size */
2375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2377 }
2378
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 data += explicit_iv_len;
2380 rec->data_offset += explicit_iv_len;
2381 rec->data_len -= explicit_iv_len + transform->taglen;
2382
Hanno Beckere83efe62019-04-29 13:52:53 +01002383 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002384 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002385 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386
2387 memcpy( transform->iv_dec + transform->fixed_ivlen,
2388 data - explicit_iv_len, explicit_iv_len );
2389
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002391 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002392 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002393
Paul Bakker68884e32013-01-07 18:20:04 +01002394
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002395 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002396 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002397 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002398 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2399 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401 data, rec->data_len,
2402 data, &olen,
2403 data + rec->data_len,
2404 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2409 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 return( ret );
2412 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002413 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002414
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2423#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002424 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002427 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 /*
Paul Bakker45829992013-01-03 14:52:21 +01002430 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002433 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2434 {
2435 /* The ciphertext is prefixed with the CBC IV. */
2436 minlen += transform->ivlen;
2437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438#endif
Paul Bakker45829992013-01-03 14:52:21 +01002439
Hanno Becker4c6876b2017-12-27 21:28:58 +00002440 /* Size considerations:
2441 *
2442 * - The CBC cipher text must not be empty and hence
2443 * at least of size transform->ivlen.
2444 *
2445 * Together with the potential IV-prefix, this explains
2446 * the first of the two checks below.
2447 *
2448 * - The record must contain a MAC, either in plain or
2449 * encrypted, depending on whether Encrypt-then-MAC
2450 * is used or not.
2451 * - If it is, the message contains the IV-prefix,
2452 * the CBC ciphertext, and the MAC.
2453 * - If it is not, the padded plaintext, and hence
2454 * the CBC ciphertext, has at least length maclen + 1
2455 * because there is at least the padding length byte.
2456 *
2457 * As the CBC ciphertext is not empty, both cases give the
2458 * lower bound minlen + maclen + 1 on the record size, which
2459 * we test for in the second check below.
2460 */
2461 if( rec->data_len < minlen + transform->ivlen ||
2462 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002465 "+ 1 ) ( + expl IV )", rec->data_len,
2466 transform->ivlen,
2467 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002469 }
2470
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 /*
2472 * Authenticate before decrypt if enabled
2473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002475 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002480
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2482 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002483
Hanno Beckere83efe62019-04-29 13:52:53 +01002484 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002485
Hanno Beckere83efe62019-04-29 13:52:53 +01002486 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2487 add_data_len );
2488 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2489 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2491 data, rec->data_len );
2492 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2493 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002494
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2496 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002497 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002498 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Becker4c6876b2017-12-27 21:28:58 +00002500 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2501 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509
2510 /*
2511 * Check length sanity
2512 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518 }
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002521 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002522 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002523 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002524 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002526 /* This is safe because data_len >= minlen + maclen + 1 initially,
2527 * and at this point we have at most subtracted maclen (note that
2528 * minlen == transform->ivlen here). */
2529 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002530
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 data += transform->ivlen;
2532 rec->data_offset += transform->ivlen;
2533 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2538 transform->iv_dec, transform->ivlen,
2539 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002542 return( ret );
2543 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002544
Hanno Becker4c6876b2017-12-27 21:28:58 +00002545 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002553 {
2554 /*
2555 * Save IV in SSL3 and TLS1
2556 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2558 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Hanno Becker4c6876b2017-12-27 21:28:58 +00002562 /* Safe since data_len >= minlen + maclen + 1, so after having
2563 * subtracted at most minlen and maclen up to this point,
2564 * data_len > 0. */
2565 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002566
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( auth_done == 1 )
2568 {
2569 correct *= ( rec->data_len >= padlen + 1 );
2570 padlen *= ( rec->data_len >= padlen + 1 );
2571 }
2572 else
Paul Bakker45829992013-01-03 14:52:21 +01002573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002575 if( rec->data_len < transform->maclen + padlen + 1 )
2576 {
2577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2578 rec->data_len,
2579 transform->maclen,
2580 padlen + 1 ) );
2581 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002582#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583
2584 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2585 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Becker4c6876b2017-12-27 21:28:58 +00002588 padlen++;
2589
2590 /* Regardless of the validity of the padding,
2591 * we have data_len >= padlen here. */
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002594 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_DEBUG_ALL)
2599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002600 "should be no more than %d",
2601 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002602#endif
Paul Bakker45829992013-01-03 14:52:21 +01002603 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605 }
2606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2608#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2609 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 /* The padding check involves a series of up to 256
2613 * consecutive memory reads at the end of the record
2614 * plaintext buffer. In order to hide the length and
2615 * validity of the padding, always perform exactly
2616 * `min(256,plaintext_len)` reads (but take into account
2617 * only the last `padlen` bytes for the padding check). */
2618 size_t pad_count = 0;
2619 size_t real_count = 0;
2620 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002621
Hanno Becker4c6876b2017-12-27 21:28:58 +00002622 /* Index of first padding byte; it has been ensured above
2623 * that the subtraction is safe. */
2624 size_t const padding_idx = rec->data_len - padlen;
2625 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2626 size_t const start_idx = rec->data_len - num_checks;
2627 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002628
Hanno Becker4c6876b2017-12-27 21:28:58 +00002629 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002630 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002631 real_count |= ( idx >= padding_idx );
2632 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002633 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002637 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002639#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002640 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2644 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002648 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002649
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 /* If the padding was found to be invalid, padlen == 0
2651 * and the subtraction is safe. If the padding was found valid,
2652 * padlen hasn't been changed and the previous assertion
2653 * data_len >= padlen still holds. */
2654 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002656 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002658 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002664#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002667#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
2669 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670 * Authenticate if not done yet.
2671 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002673#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002674 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002675 {
Hanno Becker992b6872017-11-09 18:57:39 +00002676 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002677
Hanno Becker4c6876b2017-12-27 21:28:58 +00002678 /* If the initial value of padlen was such that
2679 * data_len < maclen + padlen + 1, then padlen
2680 * got reset to 1, and the initial check
2681 * data_len >= minlen + maclen + 1
2682 * guarantees that at this point we still
2683 * have at least data_len >= maclen.
2684 *
2685 * If the initial value of padlen was such that
2686 * data_len >= maclen + padlen + 1, then we have
2687 * subtracted either padlen + 1 (if the padding was correct)
2688 * or 0 (if the padding was incorrect) since then,
2689 * hence data_len >= maclen in any case.
2690 */
2691 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Hanno Beckere83efe62019-04-29 13:52:53 +01002693 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002696 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002698 ssl_mac( &transform->md_ctx_dec,
2699 transform->mac_dec,
2700 data, rec->data_len,
2701 rec->ctr, rec->type,
2702 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002703 }
2704 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2706#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2707 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002708 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002709 {
2710 /*
2711 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002712 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002713 *
2714 * Known timing attacks:
2715 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2716 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002717 * To compensate for different timings for the MAC calculation
2718 * depending on how much padding was removed (which is determined
2719 * by padlen), process extra_run more blocks through the hash
2720 * function.
2721 *
2722 * The formula in the paper is
2723 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2724 * where L1 is the size of the header plus the decrypted message
2725 * plus CBC padding and L2 is the size of the header plus the
2726 * decrypted message. This is for an underlying hash function
2727 * with 64-byte blocks.
2728 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2729 * correctly. We round down instead of up, so -56 is the correct
2730 * value for our calculations instead of -55.
2731 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002732 * Repeat the formula rather than defining a block_size variable.
2733 * This avoids requiring division by a variable at runtime
2734 * (which would be marginally less efficient and would require
2735 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002736 */
2737 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002738 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002739
2740 /*
2741 * The next two sizes are the minimum and maximum values of
2742 * in_msglen over all padlen values.
2743 *
2744 * They're independent of padlen, since we previously did
2745 * in_msglen -= padlen.
2746 *
2747 * Note that max_len + maclen is never more than the buffer
2748 * length, as we previously did in_msglen -= maclen too.
2749 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002750 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002751 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2752
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 memset( tmp, 0, sizeof( tmp ) );
2754
2755 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002757#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2758 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002759 case MBEDTLS_MD_MD5:
2760 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002762 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002763 extra_run =
2764 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2765 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002766 break;
2767#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002768#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002770 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002771 extra_run =
2772 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2773 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 break;
2775#endif
2776 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2779 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002780
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002781 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002782
Hanno Beckere83efe62019-04-29 13:52:53 +01002783 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2784 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002785 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2786 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002787 /* Make sure we access everything even when padlen > 0. This
2788 * makes the synchronisation requirements for just-in-time
2789 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002790 ssl_read_memory( data + rec->data_len, padlen );
2791 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002792
2793 /* Call mbedtls_md_process at least once due to cache attacks
2794 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002795 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002796 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Becker4c6876b2017-12-27 21:28:58 +00002798 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002799
2800 /* Make sure we access all the memory that could contain the MAC,
2801 * before we check it in the next code block. This makes the
2802 * synchronisation requirements for just-in-time Prime+Probe
2803 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002804 ssl_read_memory( data + min_len,
2805 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002806 }
2807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2809 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002816 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2817 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002818#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2821 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_SSL_DEBUG_ALL)
2824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002825#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 correct = 0;
2827 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002828 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002830
2831 /*
2832 * Finally check the correct flag
2833 */
2834 if( correct == 0 )
2835 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002836#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837
2838 /* Make extra sure authentication was performed, exactly once */
2839 if( auth_done != 1 )
2840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Hanno Beckera5a2b082019-05-15 14:03:01 +01002845#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002846 if( rec->cid_len != 0 )
2847 {
2848 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2849 &rec->type );
2850 if( ret != 0 )
2851 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2852 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002853#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
2857 return( 0 );
2858}
2859
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002860#undef MAC_NONE
2861#undef MAC_PLAINTEXT
2862#undef MAC_CIPHERTEXT
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002865/*
2866 * Compression/decompression functions
2867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002869{
2870 int ret;
2871 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002872 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002873 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002874 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002877
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002878 if( len_pre == 0 )
2879 return( 0 );
2880
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881 memcpy( msg_pre, ssl->out_msg, len_pre );
2882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884 ssl->out_msglen ) );
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887 ssl->out_msg, ssl->out_msglen );
2888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2890 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2891 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002892 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Paul Bakker48916f92012-09-16 19:57:18 +00002894 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895 if( ret != Z_OK )
2896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2898 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 }
2900
Angus Grattond8213d02016-05-25 20:56:48 +10002901 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002902 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 ssl->out_msglen ) );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msg, ssl->out_msglen );
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911
2912 return( 0 );
2913}
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916{
2917 int ret;
2918 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002919 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002921 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002925 if( len_pre == 0 )
2926 return( 0 );
2927
Paul Bakker2770fbd2012-07-03 13:30:23 +00002928 memcpy( msg_pre, ssl->in_msg, len_pre );
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931 ssl->in_msglen ) );
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002934 ssl->in_msg, ssl->in_msglen );
2935
Paul Bakker48916f92012-09-16 19:57:18 +00002936 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2937 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2938 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002939 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002940 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941
Paul Bakker48916f92012-09-16 19:57:18 +00002942 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 if( ret != Z_OK )
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2946 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 }
2948
Angus Grattond8213d02016-05-25 20:56:48 +10002949 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002950 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->in_msglen ) );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956 ssl->in_msg, ssl->in_msglen );
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959
2960 return( 0 );
2961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2965static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_SSL_PROTO_DTLS)
2968static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002969{
2970 /* If renegotiation is not enforced, retransmit until we would reach max
2971 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002972 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002973 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002974 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002975 unsigned char doublings = 1;
2976
2977 while( ratio != 0 )
2978 {
2979 ++doublings;
2980 ratio >>= 1;
2981 }
2982
2983 if( ++ssl->renego_records_seen > doublings )
2984 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002986 return( 0 );
2987 }
2988 }
2989
2990 return( ssl_write_hello_request( ssl ) );
2991}
2992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002994
Paul Bakker5121ce52009-01-03 21:22:43 +00002995/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002996 * Fill the input message buffer by appending data to it.
2997 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002998 *
2999 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3000 * available (from this read and/or a previous one). Otherwise, an error code
3001 * is returned (possibly EOF or WANT_READ).
3002 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003003 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3004 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3005 * since we always read a whole datagram at once.
3006 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003007 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003008 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003011{
Paul Bakker23986e52011-04-24 08:57:21 +00003012 int ret;
3013 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003020 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003022 }
3023
Angus Grattond8213d02016-05-25 20:56:48 +10003024 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003028 }
3029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003033 uint32_t timeout;
3034
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003035 /* Just to be sure */
3036 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3037 {
3038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3039 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3041 }
3042
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003043 /*
3044 * The point is, we need to always read a full datagram at once, so we
3045 * sometimes read more then requested, and handle the additional data.
3046 * It could be the rest of the current record (while fetching the
3047 * header) and/or some other records in the same datagram.
3048 */
3049
3050 /*
3051 * Move to the next record in the already read datagram if applicable
3052 */
3053 if( ssl->next_record_offset != 0 )
3054 {
3055 if( ssl->in_left < ssl->next_record_offset )
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059 }
3060
3061 ssl->in_left -= ssl->next_record_offset;
3062
3063 if( ssl->in_left != 0 )
3064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003066 ssl->next_record_offset ) );
3067 memmove( ssl->in_hdr,
3068 ssl->in_hdr + ssl->next_record_offset,
3069 ssl->in_left );
3070 }
3071
3072 ssl->next_record_offset = 0;
3073 }
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003077
3078 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003079 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080 */
3081 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003084 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003085 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003086
3087 /*
3088 * A record can't be split accross datagrams. If we need to read but
3089 * are not at the beginning of a new record, the caller did something
3090 * wrong.
3091 */
3092 if( ssl->in_left != 0 )
3093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003096 }
3097
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003098 /*
3099 * Don't even try to read if time's out already.
3100 * This avoids by-passing the timer when repeatedly receiving messages
3101 * that will end up being dropped.
3102 */
3103 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003104 {
3105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003106 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003107 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003108 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003109 {
Angus Grattond8213d02016-05-25 20:56:48 +10003110 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 timeout = ssl->handshake->retransmit_timeout;
3114 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003115 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003118
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003119 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003120 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3121 timeout );
3122 else
3123 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
3127 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 }
3130
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003131 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003137 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003138 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003141 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 }
3143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003147 return( ret );
3148 }
3149
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003150 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003153 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003155 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003156 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003159 return( ret );
3160 }
3161
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003162 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003165 }
3166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 if( ret < 0 )
3168 return( ret );
3169
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003170 ssl->in_left = ret;
3171 }
3172 else
3173#endif
3174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003176 ssl->in_left, nb_want ) );
3177
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003178 while( ssl->in_left < nb_want )
3179 {
3180 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003181
3182 if( ssl_check_timer( ssl ) != 0 )
3183 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3184 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003185 {
3186 if( ssl->f_recv_timeout != NULL )
3187 {
3188 ret = ssl->f_recv_timeout( ssl->p_bio,
3189 ssl->in_hdr + ssl->in_left, len,
3190 ssl->conf->read_timeout );
3191 }
3192 else
3193 {
3194 ret = ssl->f_recv( ssl->p_bio,
3195 ssl->in_hdr + ssl->in_left, len );
3196 }
3197 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003200 ssl->in_left, nb_want ) );
3201 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003202
3203 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003205
3206 if( ret < 0 )
3207 return( ret );
3208
mohammad160352aecb92018-03-28 23:41:40 -07003209 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003210 {
Darryl Green11999bb2018-03-13 15:22:58 +00003211 MBEDTLS_SSL_DEBUG_MSG( 1,
3212 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003213 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3215 }
3216
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003217 ssl->in_left += ret;
3218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 }
3220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
3223 return( 0 );
3224}
3225
3226/*
3227 * Flush any data not yet written
3228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003230{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003231 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003232 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003236 if( ssl->f_send == NULL )
3237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003239 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003241 }
3242
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003243 /* Avoid incrementing counter if data is flushed */
3244 if( ssl->out_left == 0 )
3245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003247 return( 0 );
3248 }
3249
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 while( ssl->out_left > 0 )
3251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003253 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
Hanno Becker2b1e3542018-08-06 11:19:13 +01003255 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003256 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
3260 if( ret <= 0 )
3261 return( ret );
3262
mohammad160352aecb92018-03-28 23:41:40 -07003263 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003264 {
Darryl Green11999bb2018-03-13 15:22:58 +00003265 MBEDTLS_SSL_DEBUG_MSG( 1,
3266 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003267 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3269 }
3270
Paul Bakker5121ce52009-01-03 21:22:43 +00003271 ssl->out_left -= ret;
3272 }
3273
Hanno Becker2b1e3542018-08-06 11:19:13 +01003274#if defined(MBEDTLS_SSL_PROTO_DTLS)
3275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003276 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003277 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003278 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003279 else
3280#endif
3281 {
3282 ssl->out_hdr = ssl->out_buf + 8;
3283 }
3284 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
3288 return( 0 );
3289}
3290
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003291/*
3292 * Functions to handle the DTLS retransmission state machine
3293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003295/*
3296 * Append current handshake message to current outgoing flight
3297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3302 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3303 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003304
3305 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003306 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003307 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003310 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311 }
3312
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003313 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003314 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003317 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318 }
3319
3320 /* Copy current handshake message with headers */
3321 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3322 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003323 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324 msg->next = NULL;
3325
3326 /* Append to the current flight */
3327 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003328 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003329 else
3330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332 while( cur->next != NULL )
3333 cur = cur->next;
3334 cur->next = msg;
3335 }
3336
Hanno Becker3b235902018-08-06 09:54:53 +01003337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003338 return( 0 );
3339}
3340
3341/*
3342 * Free the current flight of handshake messages
3343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 mbedtls_ssl_flight_item *cur = flight;
3347 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348
3349 while( cur != NULL )
3350 {
3351 next = cur->next;
3352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 mbedtls_free( cur->p );
3354 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003355
3356 cur = next;
3357 }
3358}
3359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3361static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003362#endif
3363
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003365 * Swap transform_out and out_ctr with the alternative ones
3366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003370 unsigned char tmp_out_ctr[8];
3371
3372 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003375 return;
3376 }
3377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003379
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003380 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003381 tmp_transform = ssl->transform_out;
3382 ssl->transform_out = ssl->handshake->alt_transform_out;
3383 ssl->handshake->alt_transform_out = tmp_transform;
3384
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003385 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003386 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3387 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003388 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003389
3390 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003391 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3394 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3399 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003400 }
3401 }
3402#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003403}
3404
3405/*
3406 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003407 */
3408int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3409{
3410 int ret = 0;
3411
3412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3413
3414 ret = mbedtls_ssl_flight_transmit( ssl );
3415
3416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3417
3418 return( ret );
3419}
3420
3421/*
3422 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003423 *
3424 * Need to remember the current message in case flush_output returns
3425 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003426 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003428int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003430 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003434 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003436
3437 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003438 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003439 ssl_swap_epochs( ssl );
3440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003442 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003443
3444 while( ssl->handshake->cur_msg != NULL )
3445 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003446 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003447 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003448
Hanno Beckere1dcb032018-08-17 16:47:58 +01003449 int const is_finished =
3450 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3451 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3452
Hanno Becker04da1892018-08-14 13:22:10 +01003453 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3454 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3455
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003456 /* Swap epochs before sending Finished: we can't do it after
3457 * sending ChangeCipherSpec, in case write returns WANT_READ.
3458 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003459 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003460 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003462 ssl_swap_epochs( ssl );
3463 }
3464
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 ret = ssl_get_remaining_payload_in_datagram( ssl );
3466 if( ret < 0 )
3467 return( ret );
3468 max_frag_len = (size_t) ret;
3469
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003470 /* CCS is copied as is, while HS messages may need fragmentation */
3471 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3472 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003473 if( max_frag_len == 0 )
3474 {
3475 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3476 return( ret );
3477
3478 continue;
3479 }
3480
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003481 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003482 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003483 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003484
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003485 /* Update position inside current message */
3486 ssl->handshake->cur_msg_p += cur->len;
3487 }
3488 else
3489 {
3490 const unsigned char * const p = ssl->handshake->cur_msg_p;
3491 const size_t hs_len = cur->len - 12;
3492 const size_t frag_off = p - ( cur->p + 12 );
3493 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003494 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003495
Hanno Beckere1dcb032018-08-17 16:47:58 +01003496 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003497 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003498 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003499 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003500
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3502 return( ret );
3503
3504 continue;
3505 }
3506 max_hs_frag_len = max_frag_len - 12;
3507
3508 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3509 max_hs_frag_len : rem_len;
3510
3511 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003512 {
3513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003514 (unsigned) cur_hs_frag_len,
3515 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003516 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003517
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003518 /* Messages are stored with handshake headers as if not fragmented,
3519 * copy beginning of headers then fill fragmentation fields.
3520 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3521 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003522
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003523 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3524 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3525 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3526
Hanno Becker67bc7c32018-08-06 11:33:50 +01003527 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3528 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3529 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003530
3531 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3532
Hanno Becker3f7b9732018-08-28 09:53:25 +01003533 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003534 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3535 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003536 ssl->out_msgtype = cur->type;
3537
3538 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003539 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003540 }
3541
3542 /* If done with the current message move to the next one if any */
3543 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3544 {
3545 if( cur->next != NULL )
3546 {
3547 ssl->handshake->cur_msg = cur->next;
3548 ssl->handshake->cur_msg_p = cur->next->p + 12;
3549 }
3550 else
3551 {
3552 ssl->handshake->cur_msg = NULL;
3553 ssl->handshake->cur_msg_p = NULL;
3554 }
3555 }
3556
3557 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003558 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003561 return( ret );
3562 }
3563 }
3564
Hanno Becker67bc7c32018-08-06 11:33:50 +01003565 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3566 return( ret );
3567
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003568 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3570 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003571 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003574 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3575 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003576
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578
3579 return( 0 );
3580}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003581
3582/*
3583 * To be called when the last message of an incoming flight is received.
3584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003586{
3587 /* We won't need to resend that one any more */
3588 ssl_flight_free( ssl->handshake->flight );
3589 ssl->handshake->flight = NULL;
3590 ssl->handshake->cur_msg = NULL;
3591
3592 /* The next incoming flight will start with this msg_seq */
3593 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3594
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003595 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003596 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003597
Hanno Becker0271f962018-08-16 13:23:47 +01003598 /* Clear future message buffering structure. */
3599 ssl_buffering_free( ssl );
3600
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003601 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003602 ssl_set_timer( ssl, 0 );
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3605 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003608 }
3609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003611}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003612
3613/*
3614 * To be called when the last message of an outgoing flight is send.
3615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003617{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003618 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003619 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3622 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003625 }
3626 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003630
Paul Bakker5121ce52009-01-03 21:22:43 +00003631/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003632 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003634
3635/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003636 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003637 *
3638 * - fill in handshake headers
3639 * - update handshake checksum
3640 * - DTLS: save message for resending
3641 * - then pass to the record layer
3642 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003643 * DTLS: except for HelloRequest, messages are only queued, and will only be
3644 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003645 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003646 * Inputs:
3647 * - ssl->out_msglen: 4 + actual handshake message len
3648 * (4 is the size of handshake headers for TLS)
3649 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3650 * - ssl->out_msg + 4: the handshake message body
3651 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003652 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003653 * - ssl->out_msglen: the length of the record contents
3654 * (including handshake headers but excluding record headers)
3655 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003656 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003659 int ret;
3660 const size_t hs_len = ssl->out_msglen - 4;
3661 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003662
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3664
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003665 /*
3666 * Sanity checks
3667 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003668 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003669 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3670 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003671 /* In SSLv3, the client might send a NoCertificate alert. */
3672#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3673 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3674 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3675 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3676#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3677 {
3678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3679 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3680 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003683 /* Whenever we send anything different from a
3684 * HelloRequest we should be in a handshake - double check. */
3685 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3686 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003687 ssl->handshake == NULL )
3688 {
3689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3690 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003695 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003697 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003700 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003702
Hanno Beckerb50a2532018-08-06 11:52:54 +01003703 /* Double-check that we did not exceed the bounds
3704 * of the outgoing record buffer.
3705 * This should never fail as the various message
3706 * writing functions must obey the bounds of the
3707 * outgoing record buffer, but better be safe.
3708 *
3709 * Note: We deliberately do not check for the MTU or MFL here.
3710 */
3711 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3712 {
3713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3714 "size %u, maximum %u",
3715 (unsigned) ssl->out_msglen,
3716 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3718 }
3719
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003720 /*
3721 * Fill handshake headers
3722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003724 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003725 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3726 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3727 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003728
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003729 /*
3730 * DTLS has additional fields in the Handshake layer,
3731 * between the length field and the actual payload:
3732 * uint16 message_seq;
3733 * uint24 fragment_offset;
3734 * uint24 fragment_length;
3735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003737 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003738 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003739 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003740 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003741 {
3742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3743 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003745 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3747 }
3748
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003749 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003750 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003751
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003752 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003753 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003754 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003755 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3756 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3757 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003758 }
3759 else
3760 {
3761 ssl->out_msg[4] = 0;
3762 ssl->out_msg[5] = 0;
3763 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003764
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003765 /* Handshake hashes are computed without fragmentation,
3766 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003767 memset( ssl->out_msg + 6, 0x00, 3 );
3768 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003771
Hanno Becker0207e532018-08-28 10:28:28 +01003772 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003773 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3774 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 }
3776
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003777 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003779 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003780 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3781 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782 {
3783 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786 return( ret );
3787 }
3788 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003789 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003790#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003791 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003792 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003793 {
3794 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3795 return( ret );
3796 }
3797 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003798
3799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3800
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003801 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003802}
3803
3804/*
3805 * Record layer functions
3806 */
3807
3808/*
3809 * Write current record.
3810 *
3811 * Uses:
3812 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3813 * - ssl->out_msglen: length of the record content (excl headers)
3814 * - ssl->out_msg: record content
3815 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003816int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003817{
3818 int ret, done = 0;
3819 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821
3822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003825 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003827 {
3828 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003831 return( ret );
3832 }
3833
3834 len = ssl->out_msglen;
3835 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3839 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 ret = mbedtls_ssl_hw_record_write( ssl );
3844 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3847 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003848 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003849
3850 if( ret == 0 )
3851 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003852 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003854 if( !done )
3855 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003856 unsigned i;
3857 size_t protected_record_size;
3858
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003859 /* Skip writing the record content type to after the encryption,
3860 * as it may change when using the CID extension. */
3861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003863 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003864
Hanno Becker19859472018-08-06 09:40:20 +01003865 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003866 ssl->out_len[0] = (unsigned char)( len >> 8 );
3867 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003868
Paul Bakker48916f92012-09-16 19:57:18 +00003869 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003870 {
Hanno Becker3307b532017-12-27 21:37:21 +00003871 mbedtls_record rec;
3872
3873 rec.buf = ssl->out_iv;
3874 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3875 ( ssl->out_iv - ssl->out_buf );
3876 rec.data_len = ssl->out_msglen;
3877 rec.data_offset = ssl->out_msg - rec.buf;
3878
3879 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3880 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3881 ssl->conf->transport, rec.ver );
3882 rec.type = ssl->out_msgtype;
3883
Hanno Beckera5a2b082019-05-15 14:03:01 +01003884#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003885 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003886 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003887#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003888
Hanno Becker611a83b2018-01-03 14:27:32 +00003889 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003890 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003893 return( ret );
3894 }
3895
Hanno Becker3307b532017-12-27 21:37:21 +00003896 if( rec.data_offset != 0 )
3897 {
3898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3900 }
3901
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003902 /* Update the record content type and CID. */
3903 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003904#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003905 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003906#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003907 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003908 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3909 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003910 }
3911
Hanno Becker43395762019-05-03 14:46:38 +01003912 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003913
3914#if defined(MBEDTLS_SSL_PROTO_DTLS)
3915 /* In case of DTLS, double-check that we don't exceed
3916 * the remaining space in the datagram. */
3917 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3918 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003919 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003920 if( ret < 0 )
3921 return( ret );
3922
3923 if( protected_record_size > (size_t) ret )
3924 {
3925 /* Should never happen */
3926 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3927 }
3928 }
3929#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003930
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003931 /* Now write the potentially updated record content type. */
3932 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003935 "version = [%d:%d], msglen = %d",
3936 ssl->out_hdr[0], ssl->out_hdr[1],
3937 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003940 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003941
3942 ssl->out_left += protected_record_size;
3943 ssl->out_hdr += protected_record_size;
3944 ssl_update_out_pointers( ssl, ssl->transform_out );
3945
Hanno Becker04484622018-08-06 09:49:38 +01003946 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3947 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3948 break;
3949
3950 /* The loop goes to its end iff the counter is wrapping */
3951 if( i == ssl_ep_len( ssl ) )
3952 {
3953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3954 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003956 }
3957
Hanno Becker67bc7c32018-08-06 11:33:50 +01003958#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003959 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3960 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003961 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003962 size_t remaining;
3963 ret = ssl_get_remaining_payload_in_datagram( ssl );
3964 if( ret < 0 )
3965 {
3966 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3967 ret );
3968 return( ret );
3969 }
3970
3971 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003972 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003973 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003974 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003975 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003976 else
3977 {
Hanno Becker513815a2018-08-20 11:56:09 +01003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003979 }
3980 }
3981#endif /* MBEDTLS_SSL_PROTO_DTLS */
3982
3983 if( ( flush == SSL_FORCE_FLUSH ) &&
3984 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 return( ret );
3988 }
3989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
3992 return( 0 );
3993}
3994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003996
3997static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3998{
3999 if( ssl->in_msglen < ssl->in_hslen ||
4000 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4001 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4002 {
4003 return( 1 );
4004 }
4005 return( 0 );
4006}
Hanno Becker44650b72018-08-16 12:51:11 +01004007
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004008static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004009{
4010 return( ( ssl->in_msg[9] << 16 ) |
4011 ( ssl->in_msg[10] << 8 ) |
4012 ssl->in_msg[11] );
4013}
4014
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004015static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004016{
4017 return( ( ssl->in_msg[6] << 16 ) |
4018 ( ssl->in_msg[7] << 8 ) |
4019 ssl->in_msg[8] );
4020}
4021
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004022static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004023{
4024 uint32_t msg_len, frag_off, frag_len;
4025
4026 msg_len = ssl_get_hs_total_len( ssl );
4027 frag_off = ssl_get_hs_frag_off( ssl );
4028 frag_len = ssl_get_hs_frag_len( ssl );
4029
4030 if( frag_off > msg_len )
4031 return( -1 );
4032
4033 if( frag_len > msg_len - frag_off )
4034 return( -1 );
4035
4036 if( frag_len + 12 > ssl->in_msglen )
4037 return( -1 );
4038
4039 return( 0 );
4040}
4041
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004042/*
4043 * Mark bits in bitmask (used for DTLS HS reassembly)
4044 */
4045static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4046{
4047 unsigned int start_bits, end_bits;
4048
4049 start_bits = 8 - ( offset % 8 );
4050 if( start_bits != 8 )
4051 {
4052 size_t first_byte_idx = offset / 8;
4053
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004054 /* Special case */
4055 if( len <= start_bits )
4056 {
4057 for( ; len != 0; len-- )
4058 mask[first_byte_idx] |= 1 << ( start_bits - len );
4059
4060 /* Avoid potential issues with offset or len becoming invalid */
4061 return;
4062 }
4063
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004064 offset += start_bits; /* Now offset % 8 == 0 */
4065 len -= start_bits;
4066
4067 for( ; start_bits != 0; start_bits-- )
4068 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4069 }
4070
4071 end_bits = len % 8;
4072 if( end_bits != 0 )
4073 {
4074 size_t last_byte_idx = ( offset + len ) / 8;
4075
4076 len -= end_bits; /* Now len % 8 == 0 */
4077
4078 for( ; end_bits != 0; end_bits-- )
4079 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4080 }
4081
4082 memset( mask + offset / 8, 0xFF, len / 8 );
4083}
4084
4085/*
4086 * Check that bitmask is full
4087 */
4088static int ssl_bitmask_check( unsigned char *mask, size_t len )
4089{
4090 size_t i;
4091
4092 for( i = 0; i < len / 8; i++ )
4093 if( mask[i] != 0xFF )
4094 return( -1 );
4095
4096 for( i = 0; i < len % 8; i++ )
4097 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4098 return( -1 );
4099
4100 return( 0 );
4101}
4102
Hanno Becker56e205e2018-08-16 09:06:12 +01004103/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004104static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004105 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004106{
Hanno Becker56e205e2018-08-16 09:06:12 +01004107 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004108
Hanno Becker56e205e2018-08-16 09:06:12 +01004109 alloc_len = 12; /* Handshake header */
4110 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004111
Hanno Beckerd07df862018-08-16 09:14:58 +01004112 if( add_bitmap )
4113 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004114
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004115 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004116}
Hanno Becker56e205e2018-08-16 09:06:12 +01004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004119
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004120static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004121{
4122 return( ( ssl->in_msg[1] << 16 ) |
4123 ( ssl->in_msg[2] << 8 ) |
4124 ssl->in_msg[3] );
4125}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004126
Simon Butcher99000142016-10-13 17:21:01 +01004127int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004132 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004134 }
4135
Hanno Becker12555c62018-08-16 12:47:53 +01004136 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004139 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004140 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004144 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004145 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004146 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004147
Hanno Becker44650b72018-08-16 12:51:11 +01004148 if( ssl_check_hs_header( ssl ) != 0 )
4149 {
4150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4151 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4152 }
4153
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004154 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004155 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4156 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4157 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4158 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004159 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004160 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4161 {
4162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4163 recv_msg_seq,
4164 ssl->handshake->in_msg_seq ) );
4165 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4166 }
4167
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004168 /* Retransmit only on last message from previous flight, to avoid
4169 * too many retransmissions.
4170 * Besides, No sane server ever retransmits HelloVerifyRequest */
4171 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004175 "message_seq = %d, start_of_flight = %d",
4176 recv_msg_seq,
4177 ssl->handshake->in_flight_start_seq ) );
4178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004182 return( ret );
4183 }
4184 }
4185 else
4186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004188 "message_seq = %d, expected = %d",
4189 recv_msg_seq,
4190 ssl->handshake->in_msg_seq ) );
4191 }
4192
Hanno Becker90333da2017-10-10 11:27:13 +01004193 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004194 }
4195 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004196
Hanno Becker6d97ef52018-08-16 13:09:04 +01004197 /* Message reassembly is handled alongside buffering of future
4198 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004199 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004200 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004201 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004204 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004205 }
4206 }
4207 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004209 /* With TLS we don't handle fragmentation (for now) */
4210 if( ssl->in_msglen < ssl->in_hslen )
4211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4213 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004214 }
4215
Simon Butcher99000142016-10-13 17:21:01 +01004216 return( 0 );
4217}
4218
4219void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4220{
Hanno Becker0271f962018-08-16 13:23:47 +01004221 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004222
Hanno Becker0271f962018-08-16 13:23:47 +01004223 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004224 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004225 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004226 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004227
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004228 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004231 ssl->handshake != NULL )
4232 {
Hanno Becker0271f962018-08-16 13:23:47 +01004233 unsigned offset;
4234 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004235
Hanno Becker0271f962018-08-16 13:23:47 +01004236 /* Increment handshake sequence number */
4237 hs->in_msg_seq++;
4238
4239 /*
4240 * Clear up handshake buffering and reassembly structure.
4241 */
4242
4243 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004244 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004245
4246 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004247 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4248 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004249 offset++, hs_buf++ )
4250 {
4251 *hs_buf = *(hs_buf + 1);
4252 }
4253
4254 /* Create a fresh last entry */
4255 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004256 }
4257#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004258}
4259
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004260/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004261 * DTLS anti-replay: RFC 6347 4.1.2.6
4262 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004263 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4264 * Bit n is set iff record number in_window_top - n has been seen.
4265 *
4266 * Usually, in_window_top is the last record number seen and the lsb of
4267 * in_window is set. The only exception is the initial state (record number 0
4268 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4271static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004272{
4273 ssl->in_window_top = 0;
4274 ssl->in_window = 0;
4275}
4276
4277static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4278{
4279 return( ( (uint64_t) buf[0] << 40 ) |
4280 ( (uint64_t) buf[1] << 32 ) |
4281 ( (uint64_t) buf[2] << 24 ) |
4282 ( (uint64_t) buf[3] << 16 ) |
4283 ( (uint64_t) buf[4] << 8 ) |
4284 ( (uint64_t) buf[5] ) );
4285}
4286
4287/*
4288 * Return 0 if sequence number is acceptable, -1 otherwise
4289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004291{
4292 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4293 uint64_t bit;
4294
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004295 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004296 return( 0 );
4297
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004298 if( rec_seqnum > ssl->in_window_top )
4299 return( 0 );
4300
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004301 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004302
4303 if( bit >= 64 )
4304 return( -1 );
4305
4306 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4307 return( -1 );
4308
4309 return( 0 );
4310}
4311
4312/*
4313 * Update replay window on new validated record
4314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004316{
4317 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4318
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004319 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004320 return;
4321
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004322 if( rec_seqnum > ssl->in_window_top )
4323 {
4324 /* Update window_top and the contents of the window */
4325 uint64_t shift = rec_seqnum - ssl->in_window_top;
4326
4327 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004328 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004329 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004330 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004331 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004332 ssl->in_window |= 1;
4333 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004334
4335 ssl->in_window_top = rec_seqnum;
4336 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004337 else
4338 {
4339 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004340 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004341
4342 if( bit < 64 ) /* Always true, but be extra sure */
4343 ssl->in_window |= (uint64_t) 1 << bit;
4344 }
4345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004347
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004348#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004349/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004350static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4351
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004352/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004353 * Without any SSL context, check if a datagram looks like a ClientHello with
4354 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004355 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004356 *
4357 * - if cookie is valid, return 0
4358 * - if ClientHello looks superficially valid but cookie is not,
4359 * fill obuf and set olen, then
4360 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4361 * - otherwise return a specific error code
4362 */
4363static int ssl_check_dtls_clihlo_cookie(
4364 mbedtls_ssl_cookie_write_t *f_cookie_write,
4365 mbedtls_ssl_cookie_check_t *f_cookie_check,
4366 void *p_cookie,
4367 const unsigned char *cli_id, size_t cli_id_len,
4368 const unsigned char *in, size_t in_len,
4369 unsigned char *obuf, size_t buf_len, size_t *olen )
4370{
4371 size_t sid_len, cookie_len;
4372 unsigned char *p;
4373
4374 if( f_cookie_write == NULL || f_cookie_check == NULL )
4375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4376
4377 /*
4378 * Structure of ClientHello with record and handshake headers,
4379 * and expected values. We don't need to check a lot, more checks will be
4380 * done when actually parsing the ClientHello - skipping those checks
4381 * avoids code duplication and does not make cookie forging any easier.
4382 *
4383 * 0-0 ContentType type; copied, must be handshake
4384 * 1-2 ProtocolVersion version; copied
4385 * 3-4 uint16 epoch; copied, must be 0
4386 * 5-10 uint48 sequence_number; copied
4387 * 11-12 uint16 length; (ignored)
4388 *
4389 * 13-13 HandshakeType msg_type; (ignored)
4390 * 14-16 uint24 length; (ignored)
4391 * 17-18 uint16 message_seq; copied
4392 * 19-21 uint24 fragment_offset; copied, must be 0
4393 * 22-24 uint24 fragment_length; (ignored)
4394 *
4395 * 25-26 ProtocolVersion client_version; (ignored)
4396 * 27-58 Random random; (ignored)
4397 * 59-xx SessionID session_id; 1 byte len + sid_len content
4398 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4399 * ...
4400 *
4401 * Minimum length is 61 bytes.
4402 */
4403 if( in_len < 61 ||
4404 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4405 in[3] != 0 || in[4] != 0 ||
4406 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4407 {
4408 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4409 }
4410
4411 sid_len = in[59];
4412 if( sid_len > in_len - 61 )
4413 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4414
4415 cookie_len = in[60 + sid_len];
4416 if( cookie_len > in_len - 60 )
4417 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4418
4419 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4420 cli_id, cli_id_len ) == 0 )
4421 {
4422 /* Valid cookie */
4423 return( 0 );
4424 }
4425
4426 /*
4427 * If we get here, we've got an invalid cookie, let's prepare HVR.
4428 *
4429 * 0-0 ContentType type; copied
4430 * 1-2 ProtocolVersion version; copied
4431 * 3-4 uint16 epoch; copied
4432 * 5-10 uint48 sequence_number; copied
4433 * 11-12 uint16 length; olen - 13
4434 *
4435 * 13-13 HandshakeType msg_type; hello_verify_request
4436 * 14-16 uint24 length; olen - 25
4437 * 17-18 uint16 message_seq; copied
4438 * 19-21 uint24 fragment_offset; copied
4439 * 22-24 uint24 fragment_length; olen - 25
4440 *
4441 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4442 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4443 *
4444 * Minimum length is 28.
4445 */
4446 if( buf_len < 28 )
4447 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4448
4449 /* Copy most fields and adapt others */
4450 memcpy( obuf, in, 25 );
4451 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4452 obuf[25] = 0xfe;
4453 obuf[26] = 0xff;
4454
4455 /* Generate and write actual cookie */
4456 p = obuf + 28;
4457 if( f_cookie_write( p_cookie,
4458 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4459 {
4460 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4461 }
4462
4463 *olen = p - obuf;
4464
4465 /* Go back and fill length fields */
4466 obuf[27] = (unsigned char)( *olen - 28 );
4467
4468 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4469 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4470 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4471
4472 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4473 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4474
4475 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4476}
4477
4478/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004479 * Handle possible client reconnect with the same UDP quadruplet
4480 * (RFC 6347 Section 4.2.8).
4481 *
4482 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4483 * that looks like a ClientHello.
4484 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004485 * - if the input looks like a ClientHello without cookies,
4486 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004487 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004488 * - if the input looks like a ClientHello with a valid cookie,
4489 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004490 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004491 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004492 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004493 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004494 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4495 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004496 */
4497static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4498{
4499 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004500 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004501
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004502 ret = ssl_check_dtls_clihlo_cookie(
4503 ssl->conf->f_cookie_write,
4504 ssl->conf->f_cookie_check,
4505 ssl->conf->p_cookie,
4506 ssl->cli_id, ssl->cli_id_len,
4507 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004508 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4511
4512 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004513 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004514 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004515 * If the error is permanent we'll catch it later,
4516 * if it's not, then hopefully it'll work next time. */
4517 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4518
4519 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 }
4521
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004522 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004523 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004524 /* Got a valid cookie, partially reset context */
4525 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4526 {
4527 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4528 return( ret );
4529 }
4530
4531 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532 }
4533
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004534 return( ret );
4535}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004536#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004537
Hanno Becker46483f12019-05-03 13:25:54 +01004538static int ssl_check_record_type( uint8_t record_type )
4539{
4540 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4541 record_type != MBEDTLS_SSL_MSG_ALERT &&
4542 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4543 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4544 {
4545 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4546 }
4547
4548 return( 0 );
4549}
4550
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004551/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004552 * ContentType type;
4553 * ProtocolVersion version;
4554 * uint16 epoch; // DTLS only
4555 * uint48 sequence_number; // DTLS only
4556 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004557 *
4558 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004559 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004560 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4561 *
4562 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004563 * 1. proceed with the record if this function returns 0
4564 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4565 * 3. return CLIENT_RECONNECT if this function return that value
4566 * 4. drop the whole datagram if this function returns anything else.
4567 * Point 2 is needed when the peer is resending, and we have already received
4568 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004571{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004572 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004573 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004574
Hanno Becker8b09b732019-05-08 12:03:28 +01004575 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004576
Paul Bakker5121ce52009-01-03 21:22:43 +00004577 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004578 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004579
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004580 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004581#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004582 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4583 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4584 ssl->conf->cid_len != 0 )
4585 {
4586 /* Shift pointers to account for record header including CID
4587 * struct {
4588 * ContentType special_type = tls12_cid;
4589 * ProtocolVersion version;
4590 * uint16 epoch;
4591 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004592 * opaque cid[cid_length]; // Additional field compared to
4593 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004594 * uint16 length;
4595 * opaque enc_content[DTLSCiphertext.length];
4596 * } DTLSCiphertext;
4597 */
4598
4599 /* So far, we only support static CID lengths
4600 * fixed in the configuration. */
4601 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4602 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4603 }
4604 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004605#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004606 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004609
4610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004611 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4612 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004613 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4614#endif /* MBEDTLS_SSL_PROTO_DTLS */
4615 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4616 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004619 }
4620
4621 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004622 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4625 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 }
4627
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004628 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4631 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004632 }
4633
Hanno Becker8b09b732019-05-08 12:03:28 +01004634 /* Now that the total length of the record header is known, ensure
4635 * that the current datagram is large enough to hold it.
4636 * This would fail, for example, if we received a datagram of
4637 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4638 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4639 if( ret != 0 )
4640 {
4641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4642 return( ret );
4643 }
4644 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4645
4646 /* Parse and validate record length
4647 * This must happen after the CID parsing because
4648 * its position in the record header depends on
4649 * the presence of a CID. */
4650
4651 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004652 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004653 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004657 }
4658
Hanno Becker8b09b732019-05-08 12:03:28 +01004659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004660 "version = [%d:%d], msglen = %d",
4661 ssl->in_msgtype,
4662 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004663
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004664 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004665 * DTLS-related tests.
4666 * Check epoch before checking length constraint because
4667 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4668 * message gets duplicated before the corresponding Finished message,
4669 * the second ChangeCipherSpec should be discarded because it belongs
4670 * to an old epoch, but not because its length is shorter than
4671 * the minimum record length for packets using the new record transform.
4672 * Note that these two kinds of failures are handled differently,
4673 * as an unexpected record is silently skipped but an invalid
4674 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004675 */
4676#if defined(MBEDTLS_SSL_PROTO_DTLS)
4677 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4678 {
4679 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4680
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004681 /* Check epoch (and sequence number) with DTLS */
4682 if( rec_epoch != ssl->in_epoch )
4683 {
4684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4685 "expected %d, received %d",
4686 ssl->in_epoch, rec_epoch ) );
4687
4688#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4689 /*
4690 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4691 * access the first byte of record content (handshake type), as we
4692 * have an active transform (possibly iv_len != 0), so use the
4693 * fact that the record header len is 13 instead.
4694 */
4695 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4696 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4697 rec_epoch == 0 &&
4698 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4699 ssl->in_left > 13 &&
4700 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4701 {
4702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4703 "from the same port" ) );
4704 return( ssl_handle_possible_reconnect( ssl ) );
4705 }
4706 else
4707#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004708 {
4709 /* Consider buffering the record. */
4710 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4711 {
4712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4713 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4714 }
4715
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004716 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004717 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004718 }
4719
4720#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4721 /* Replay detection only works for the current epoch */
4722 if( rec_epoch == ssl->in_epoch &&
4723 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4724 {
4725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4726 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4727 }
4728#endif
4729 }
4730#endif /* MBEDTLS_SSL_PROTO_DTLS */
4731
Hanno Becker52c6dc62017-05-26 16:07:36 +01004732
4733 /* Check length against bounds of the current transform and version */
4734 if( ssl->transform_in == NULL )
4735 {
4736 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004737 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004738 {
4739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4740 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4741 }
4742 }
4743 else
4744 {
4745 if( ssl->in_msglen < ssl->transform_in->minlen )
4746 {
4747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4748 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4749 }
4750
4751#if defined(MBEDTLS_SSL_PROTO_SSL3)
4752 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004753 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004754 {
4755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4756 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4757 }
4758#endif
4759#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4760 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4761 /*
4762 * TLS encrypted messages can have up to 256 bytes of padding
4763 */
4764 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4765 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004766 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004767 {
4768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4769 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4770 }
4771#endif
4772 }
4773
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004774 return( 0 );
4775}
Paul Bakker5121ce52009-01-03 21:22:43 +00004776
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004777/*
4778 * If applicable, decrypt (and decompress) record content
4779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004781{
4782 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004785 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4788 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 ret = mbedtls_ssl_hw_record_read( ssl );
4793 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4796 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004797 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004798
4799 if( ret == 0 )
4800 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004803 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004804 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004805 mbedtls_record rec;
4806
4807 rec.buf = ssl->in_iv;
4808 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4809 - ( ssl->in_iv - ssl->in_buf );
4810 rec.data_len = ssl->in_msglen;
4811 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004812#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004813 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004814 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004815#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004816
4817 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4818 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4819 ssl->conf->transport, rec.ver );
4820 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004821 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4822 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004825
Hanno Beckera5a2b082019-05-15 14:03:01 +01004826#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004827 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4828 ssl->conf->ignore_unexpected_cid
4829 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4830 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004832 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004833 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004834#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004835
Paul Bakker5121ce52009-01-03 21:22:43 +00004836 return( ret );
4837 }
4838
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004839 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004840 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004841 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4842 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004844
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004845 /* The record content type may change during decryption,
4846 * so re-read it. */
4847 ssl->in_msgtype = rec.type;
4848 /* Also update the input buffer, because unfortunately
4849 * the server-side ssl_parse_client_hello() reparses the
4850 * record header when receiving a ClientHello initiating
4851 * a renegotiation. */
4852 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004853 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004854 ssl->in_msglen = rec.data_len;
4855 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4856 ssl->in_len[1] = (unsigned char)( rec.data_len );
4857
Paul Bakker5121ce52009-01-03 21:22:43 +00004858 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4859 ssl->in_msg, ssl->in_msglen );
4860
Hanno Beckera5a2b082019-05-15 14:03:01 +01004861#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004862 /* We have already checked the record content type
4863 * in ssl_parse_record_header(), failing or silently
4864 * dropping the record in the case of an unknown type.
4865 *
4866 * Since with the use of CIDs, the record content type
4867 * might change during decryption, re-check the record
4868 * content type, but treat a failure as fatal this time. */
4869 if( ssl_check_record_type( ssl->in_msgtype ) )
4870 {
4871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4872 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4873 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004874#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004875
Angus Grattond8213d02016-05-25 20:56:48 +10004876 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4879 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004881 else if( ssl->in_msglen == 0 )
4882 {
4883#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4884 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4885 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4886 {
4887 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4889 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4890 }
4891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4892
4893 ssl->nb_zero++;
4894
4895 /*
4896 * Three or more empty messages may be a DoS attack
4897 * (excessive CPU consumption).
4898 */
4899 if( ssl->nb_zero > 3 )
4900 {
4901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004902 "messages, possible DoS attack" ) );
4903 /* Treat the records as if they were not properly authenticated,
4904 * thereby failing the connection if we see more than allowed
4905 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004906 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4907 }
4908 }
4909 else
4910 ssl->nb_zero = 0;
4911
4912#if defined(MBEDTLS_SSL_PROTO_DTLS)
4913 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4914 {
4915 ; /* in_ctr read from peer, not maintained internally */
4916 }
4917 else
4918#endif
4919 {
4920 unsigned i;
4921 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4922 if( ++ssl->in_ctr[i - 1] != 0 )
4923 break;
4924
4925 /* The loop goes to its end iff the counter is wrapping */
4926 if( i == ssl_ep_len( ssl ) )
4927 {
4928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4929 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4930 }
4931 }
4932
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 }
4934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004936 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004938 {
4939 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004942 return( ret );
4943 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004944 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004948 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004951 }
4952#endif
4953
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004954 return( 0 );
4955}
4956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004958
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004959/*
4960 * Read a record.
4961 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004962 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4963 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4964 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004965 */
Hanno Becker1097b342018-08-15 14:09:41 +01004966
4967/* Helper functions for mbedtls_ssl_read_record(). */
4968static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004969static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4970static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004971
Hanno Becker327c93b2018-08-15 13:56:18 +01004972int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004973 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004974{
4975 int ret;
4976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004978
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004979 if( ssl->keep_current_message == 0 )
4980 {
4981 do {
Simon Butcher99000142016-10-13 17:21:01 +01004982
Hanno Becker26994592018-08-15 14:14:59 +01004983 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004984 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004985 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004986
Hanno Beckere74d5562018-08-15 14:26:08 +01004987 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004988 {
Hanno Becker40f50842018-08-15 14:48:01 +01004989#if defined(MBEDTLS_SSL_PROTO_DTLS)
4990 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004991
Hanno Becker40f50842018-08-15 14:48:01 +01004992 /* We only check for buffered messages if the
4993 * current datagram is fully consumed. */
4994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004995 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004996 {
Hanno Becker40f50842018-08-15 14:48:01 +01004997 if( ssl_load_buffered_message( ssl ) == 0 )
4998 have_buffered = 1;
4999 }
5000
5001 if( have_buffered == 0 )
5002#endif /* MBEDTLS_SSL_PROTO_DTLS */
5003 {
5004 ret = ssl_get_next_record( ssl );
5005 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5006 continue;
5007
5008 if( ret != 0 )
5009 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005010 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005011 return( ret );
5012 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005013 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005014 }
5015
5016 ret = mbedtls_ssl_handle_message_type( ssl );
5017
Hanno Becker40f50842018-08-15 14:48:01 +01005018#if defined(MBEDTLS_SSL_PROTO_DTLS)
5019 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5020 {
5021 /* Buffer future message */
5022 ret = ssl_buffer_message( ssl );
5023 if( ret != 0 )
5024 return( ret );
5025
5026 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5027 }
5028#endif /* MBEDTLS_SSL_PROTO_DTLS */
5029
Hanno Becker90333da2017-10-10 11:27:13 +01005030 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5031 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005032
5033 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005034 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005035 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005036 return( ret );
5037 }
5038
Hanno Becker327c93b2018-08-15 13:56:18 +01005039 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005040 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005041 {
5042 mbedtls_ssl_update_handshake_status( ssl );
5043 }
Simon Butcher99000142016-10-13 17:21:01 +01005044 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005045 else
Simon Butcher99000142016-10-13 17:21:01 +01005046 {
Hanno Becker02f59072018-08-15 14:00:24 +01005047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005048 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005049 }
5050
5051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5052
5053 return( 0 );
5054}
5055
Hanno Becker40f50842018-08-15 14:48:01 +01005056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005057static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005058{
Hanno Becker40f50842018-08-15 14:48:01 +01005059 if( ssl->in_left > ssl->next_record_offset )
5060 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005061
Hanno Becker40f50842018-08-15 14:48:01 +01005062 return( 0 );
5063}
5064
5065static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5066{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005067 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005068 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005069 int ret = 0;
5070
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005071 if( hs == NULL )
5072 return( -1 );
5073
Hanno Beckere00ae372018-08-20 09:39:42 +01005074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5075
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005076 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5077 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5078 {
5079 /* Check if we have seen a ChangeCipherSpec before.
5080 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005081 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005082 {
5083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5084 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005085 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005086 }
5087
Hanno Becker39b8bc92018-08-28 17:17:13 +01005088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5090 ssl->in_msglen = 1;
5091 ssl->in_msg[0] = 1;
5092
5093 /* As long as they are equal, the exact value doesn't matter. */
5094 ssl->in_left = 0;
5095 ssl->next_record_offset = 0;
5096
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005097 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 goto exit;
5099 }
Hanno Becker37f95322018-08-16 13:55:32 +01005100
Hanno Beckerb8f50142018-08-28 10:01:34 +01005101#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005102 /* Debug only */
5103 {
5104 unsigned offset;
5105 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5106 {
5107 hs_buf = &hs->buffering.hs[offset];
5108 if( hs_buf->is_valid == 1 )
5109 {
5110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5111 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005112 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005113 }
5114 }
5115 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005116#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005117
5118 /* Check if we have buffered and/or fully reassembled the
5119 * next handshake message. */
5120 hs_buf = &hs->buffering.hs[0];
5121 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5122 {
5123 /* Synthesize a record containing the buffered HS message. */
5124 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5125 ( hs_buf->data[2] << 8 ) |
5126 hs_buf->data[3];
5127
5128 /* Double-check that we haven't accidentally buffered
5129 * a message that doesn't fit into the input buffer. */
5130 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5131 {
5132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5134 }
5135
5136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5137 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5138 hs_buf->data, msg_len + 12 );
5139
5140 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5141 ssl->in_hslen = msg_len + 12;
5142 ssl->in_msglen = msg_len + 12;
5143 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5144
5145 ret = 0;
5146 goto exit;
5147 }
5148 else
5149 {
5150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5151 hs->in_msg_seq ) );
5152 }
5153
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005154 ret = -1;
5155
5156exit:
5157
5158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5159 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005160}
5161
Hanno Beckera02b0b42018-08-21 17:20:27 +01005162static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5163 size_t desired )
5164{
5165 int offset;
5166 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5168 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005169
Hanno Becker01315ea2018-08-21 17:22:17 +01005170 /* Get rid of future records epoch first, if such exist. */
5171 ssl_free_buffered_record( ssl );
5172
5173 /* Check if we have enough space available now. */
5174 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5175 hs->buffering.total_bytes_buffered ) )
5176 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005178 return( 0 );
5179 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005180
Hanno Becker4f432ad2018-08-28 10:02:32 +01005181 /* We don't have enough space to buffer the next expected handshake
5182 * message. Remove buffers used for future messages to gain space,
5183 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005184 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5185 offset >= 0; offset-- )
5186 {
5187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5188 offset ) );
5189
Hanno Beckerb309b922018-08-23 13:18:05 +01005190 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191
5192 /* Check if we have enough space available now. */
5193 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5194 hs->buffering.total_bytes_buffered ) )
5195 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005197 return( 0 );
5198 }
5199 }
5200
5201 return( -1 );
5202}
5203
Hanno Becker40f50842018-08-15 14:48:01 +01005204static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5205{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005206 int ret = 0;
5207 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5208
5209 if( hs == NULL )
5210 return( 0 );
5211
5212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5213
5214 switch( ssl->in_msgtype )
5215 {
5216 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005218
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005219 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005220 break;
5221
5222 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005223 {
5224 unsigned recv_msg_seq_offset;
5225 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5226 mbedtls_ssl_hs_buffer *hs_buf;
5227 size_t msg_len = ssl->in_hslen - 12;
5228
5229 /* We should never receive an old handshake
5230 * message - double-check nonetheless. */
5231 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5232 {
5233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5235 }
5236
5237 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5238 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5239 {
5240 /* Silently ignore -- message too far in the future */
5241 MBEDTLS_SSL_DEBUG_MSG( 2,
5242 ( "Ignore future HS message with sequence number %u, "
5243 "buffering window %u - %u",
5244 recv_msg_seq, ssl->handshake->in_msg_seq,
5245 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5246
5247 goto exit;
5248 }
5249
5250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5251 recv_msg_seq, recv_msg_seq_offset ) );
5252
5253 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5254
5255 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005256 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005257 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005258 size_t reassembly_buf_sz;
5259
Hanno Becker37f95322018-08-16 13:55:32 +01005260 hs_buf->is_fragmented =
5261 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5262
5263 /* We copy the message back into the input buffer
5264 * after reassembly, so check that it's not too large.
5265 * This is an implementation-specific limitation
5266 * and not one from the standard, hence it is not
5267 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005268 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005269 {
5270 /* Ignore message */
5271 goto exit;
5272 }
5273
Hanno Beckere0b150f2018-08-21 15:51:03 +01005274 /* Check if we have enough space to buffer the message. */
5275 if( hs->buffering.total_bytes_buffered >
5276 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5277 {
5278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5280 }
5281
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005282 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5283 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005284
5285 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5286 hs->buffering.total_bytes_buffered ) )
5287 {
5288 if( recv_msg_seq_offset > 0 )
5289 {
5290 /* If we can't buffer a future message because
5291 * of space limitations -- ignore. */
5292 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",
5293 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5294 (unsigned) hs->buffering.total_bytes_buffered ) );
5295 goto exit;
5296 }
Hanno Beckere1801392018-08-21 16:51:05 +01005297 else
5298 {
5299 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",
5300 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5301 (unsigned) hs->buffering.total_bytes_buffered ) );
5302 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005303
Hanno Beckera02b0b42018-08-21 17:20:27 +01005304 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005305 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005306 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",
5307 (unsigned) msg_len,
5308 (unsigned) reassembly_buf_sz,
5309 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005310 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005311 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5312 goto exit;
5313 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005314 }
5315
5316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5317 msg_len ) );
5318
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005319 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5320 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005321 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005322 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005323 goto exit;
5324 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005325 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005326
5327 /* Prepare final header: copy msg_type, length and message_seq,
5328 * then add standardised fragment_offset and fragment_length */
5329 memcpy( hs_buf->data, ssl->in_msg, 6 );
5330 memset( hs_buf->data + 6, 0, 3 );
5331 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5332
5333 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005334
5335 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005336 }
5337 else
5338 {
5339 /* Make sure msg_type and length are consistent */
5340 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5341 {
5342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5343 /* Ignore */
5344 goto exit;
5345 }
5346 }
5347
Hanno Becker4422bbb2018-08-20 09:40:19 +01005348 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005349 {
5350 size_t frag_len, frag_off;
5351 unsigned char * const msg = hs_buf->data + 12;
5352
5353 /*
5354 * Check and copy current fragment
5355 */
5356
5357 /* Validation of header fields already done in
5358 * mbedtls_ssl_prepare_handshake_record(). */
5359 frag_off = ssl_get_hs_frag_off( ssl );
5360 frag_len = ssl_get_hs_frag_len( ssl );
5361
5362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5363 frag_off, frag_len ) );
5364 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5365
5366 if( hs_buf->is_fragmented )
5367 {
5368 unsigned char * const bitmask = msg + msg_len;
5369 ssl_bitmask_set( bitmask, frag_off, frag_len );
5370 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5371 msg_len ) == 0 );
5372 }
5373 else
5374 {
5375 hs_buf->is_complete = 1;
5376 }
5377
5378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5379 hs_buf->is_complete ? "" : "not yet " ) );
5380 }
5381
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005382 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005383 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005384
5385 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005386 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005387 break;
5388 }
5389
5390exit:
5391
5392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5393 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005394}
5395#endif /* MBEDTLS_SSL_PROTO_DTLS */
5396
Hanno Becker1097b342018-08-15 14:09:41 +01005397static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005398{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005399 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005400 * Consume last content-layer message and potentially
5401 * update in_msglen which keeps track of the contents'
5402 * consumption state.
5403 *
5404 * (1) Handshake messages:
5405 * Remove last handshake message, move content
5406 * and adapt in_msglen.
5407 *
5408 * (2) Alert messages:
5409 * Consume whole record content, in_msglen = 0.
5410 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005411 * (3) Change cipher spec:
5412 * Consume whole record content, in_msglen = 0.
5413 *
5414 * (4) Application data:
5415 * Don't do anything - the record layer provides
5416 * the application data as a stream transport
5417 * and consumes through mbedtls_ssl_read only.
5418 *
5419 */
5420
5421 /* Case (1): Handshake messages */
5422 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005423 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005424 /* Hard assertion to be sure that no application data
5425 * is in flight, as corrupting ssl->in_msglen during
5426 * ssl->in_offt != NULL is fatal. */
5427 if( ssl->in_offt != NULL )
5428 {
5429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5430 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5431 }
5432
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005433 /*
5434 * Get next Handshake message in the current record
5435 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005436
Hanno Becker4a810fb2017-05-24 16:27:30 +01005437 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005438 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005439 * current handshake content: If DTLS handshake
5440 * fragmentation is used, that's the fragment
5441 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005442 * size here is faulty and should be changed at
5443 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005444 * (2) While it doesn't seem to cause problems, one
5445 * has to be very careful not to assume that in_hslen
5446 * is always <= in_msglen in a sensible communication.
5447 * Again, it's wrong for DTLS handshake fragmentation.
5448 * The following check is therefore mandatory, and
5449 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005450 * Additionally, ssl->in_hslen might be arbitrarily out of
5451 * bounds after handling a DTLS message with an unexpected
5452 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005453 */
5454 if( ssl->in_hslen < ssl->in_msglen )
5455 {
5456 ssl->in_msglen -= ssl->in_hslen;
5457 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5458 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5461 ssl->in_msg, ssl->in_msglen );
5462 }
5463 else
5464 {
5465 ssl->in_msglen = 0;
5466 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005467
Hanno Becker4a810fb2017-05-24 16:27:30 +01005468 ssl->in_hslen = 0;
5469 }
5470 /* Case (4): Application data */
5471 else if( ssl->in_offt != NULL )
5472 {
5473 return( 0 );
5474 }
5475 /* Everything else (CCS & Alerts) */
5476 else
5477 {
5478 ssl->in_msglen = 0;
5479 }
5480
Hanno Becker1097b342018-08-15 14:09:41 +01005481 return( 0 );
5482}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005483
Hanno Beckere74d5562018-08-15 14:26:08 +01005484static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5485{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005486 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005487 return( 1 );
5488
5489 return( 0 );
5490}
5491
Hanno Becker5f066e72018-08-16 14:56:31 +01005492#if defined(MBEDTLS_SSL_PROTO_DTLS)
5493
5494static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5495{
5496 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5497 if( hs == NULL )
5498 return;
5499
Hanno Becker01315ea2018-08-21 17:22:17 +01005500 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005501 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005502 hs->buffering.total_bytes_buffered -=
5503 hs->buffering.future_record.len;
5504
5505 mbedtls_free( hs->buffering.future_record.data );
5506 hs->buffering.future_record.data = NULL;
5507 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005508}
5509
5510static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5511{
5512 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5513 unsigned char * rec;
5514 size_t rec_len;
5515 unsigned rec_epoch;
5516
5517 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5518 return( 0 );
5519
5520 if( hs == NULL )
5521 return( 0 );
5522
Hanno Becker5f066e72018-08-16 14:56:31 +01005523 rec = hs->buffering.future_record.data;
5524 rec_len = hs->buffering.future_record.len;
5525 rec_epoch = hs->buffering.future_record.epoch;
5526
5527 if( rec == NULL )
5528 return( 0 );
5529
Hanno Becker4cb782d2018-08-20 11:19:05 +01005530 /* Only consider loading future records if the
5531 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005532 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005533 return( 0 );
5534
Hanno Becker5f066e72018-08-16 14:56:31 +01005535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5536
5537 if( rec_epoch != ssl->in_epoch )
5538 {
5539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5540 goto exit;
5541 }
5542
5543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5544
5545 /* Double-check that the record is not too large */
5546 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5547 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5548 {
5549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5551 }
5552
5553 memcpy( ssl->in_hdr, rec, rec_len );
5554 ssl->in_left = rec_len;
5555 ssl->next_record_offset = 0;
5556
5557 ssl_free_buffered_record( ssl );
5558
5559exit:
5560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5561 return( 0 );
5562}
5563
5564static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5565{
5566 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5567 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005568 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005569
5570 /* Don't buffer future records outside handshakes. */
5571 if( hs == NULL )
5572 return( 0 );
5573
5574 /* Only buffer handshake records (we are only interested
5575 * in Finished messages). */
5576 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5577 return( 0 );
5578
5579 /* Don't buffer more than one future epoch record. */
5580 if( hs->buffering.future_record.data != NULL )
5581 return( 0 );
5582
Hanno Becker01315ea2018-08-21 17:22:17 +01005583 /* Don't buffer record if there's not enough buffering space remaining. */
5584 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5585 hs->buffering.total_bytes_buffered ) )
5586 {
5587 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",
5588 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5589 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005590 return( 0 );
5591 }
5592
Hanno Becker5f066e72018-08-16 14:56:31 +01005593 /* Buffer record */
5594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5595 ssl->in_epoch + 1 ) );
5596 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5597 rec_hdr_len + ssl->in_msglen );
5598
5599 /* ssl_parse_record_header() only considers records
5600 * of the next epoch as candidates for buffering. */
5601 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005602 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005603
5604 hs->buffering.future_record.data =
5605 mbedtls_calloc( 1, hs->buffering.future_record.len );
5606 if( hs->buffering.future_record.data == NULL )
5607 {
5608 /* If we run out of RAM trying to buffer a
5609 * record from the next epoch, just ignore. */
5610 return( 0 );
5611 }
5612
Hanno Becker01315ea2018-08-21 17:22:17 +01005613 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005614
Hanno Becker01315ea2018-08-21 17:22:17 +01005615 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005616 return( 0 );
5617}
5618
5619#endif /* MBEDTLS_SSL_PROTO_DTLS */
5620
Hanno Beckere74d5562018-08-15 14:26:08 +01005621static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005622{
5623 int ret;
5624
Hanno Becker5f066e72018-08-16 14:56:31 +01005625#if defined(MBEDTLS_SSL_PROTO_DTLS)
5626 /* We might have buffered a future record; if so,
5627 * and if the epoch matches now, load it.
5628 * On success, this call will set ssl->in_left to
5629 * the length of the buffered record, so that
5630 * the calls to ssl_fetch_input() below will
5631 * essentially be no-ops. */
5632 ret = ssl_load_buffered_record( ssl );
5633 if( ret != 0 )
5634 return( ret );
5635#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005636
Hanno Becker8b09b732019-05-08 12:03:28 +01005637 /* Reset in pointers to default state for TLS/DTLS records,
5638 * assuming no CID and no offset between record content and
5639 * record plaintext. */
5640 ssl_update_in_pointers( ssl );
5641
5642 /* Ensure that we have enough space available for the default form
5643 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5644 * with no space for CIDs counted in). */
5645 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5646 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005649 return( ret );
5650 }
5651
5652 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005655 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5656 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005657 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005658 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5659 {
5660 ret = ssl_buffer_future_record( ssl );
5661 if( ret != 0 )
5662 return( ret );
5663
5664 /* Fall through to handling of unexpected records */
5665 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5666 }
5667
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005668 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5669 {
5670 /* Skip unexpected record (but not whole datagram) */
5671 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005672 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005673
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5675 "(header)" ) );
5676 }
5677 else
5678 {
5679 /* Skip invalid record and the rest of the datagram */
5680 ssl->next_record_offset = 0;
5681 ssl->in_left = 0;
5682
5683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5684 "(header)" ) );
5685 }
5686
5687 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005688 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005689 }
5690#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005691 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005692 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005693
5694 /*
5695 * Read and optionally decrypt the message contents
5696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005698 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005701 return( ret );
5702 }
5703
5704 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005706 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005707 {
Hanno Becker43395762019-05-03 14:46:38 +01005708 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005709 if( ssl->next_record_offset < ssl->in_left )
5710 {
5711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5712 }
5713 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005714 else
5715#endif
5716 ssl->in_left = 0;
5717
5718 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005722 {
5723 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005724 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005725 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005726 /* Except when waiting for Finished as a bad mac here
5727 * probably means something went wrong in the handshake
5728 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5729 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5730 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5731 {
5732#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5733 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5734 {
5735 mbedtls_ssl_send_alert_message( ssl,
5736 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5737 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5738 }
5739#endif
5740 return( ret );
5741 }
5742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005744 if( ssl->conf->badmac_limit != 0 &&
5745 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5748 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005749 }
5750#endif
5751
Hanno Becker4a810fb2017-05-24 16:27:30 +01005752 /* As above, invalid records cause
5753 * dismissal of the whole datagram. */
5754
5755 ssl->next_record_offset = 0;
5756 ssl->in_left = 0;
5757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005759 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005760 }
5761
5762 return( ret );
5763 }
5764 else
5765#endif
5766 {
5767 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5769 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 mbedtls_ssl_send_alert_message( ssl,
5772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5773 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005774 }
5775#endif
5776 return( ret );
5777 }
5778 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005779
Simon Butcher99000142016-10-13 17:21:01 +01005780 return( 0 );
5781}
5782
5783int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5784{
5785 int ret;
5786
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005787 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005788 * Handle particular types of records
5789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 {
Simon Butcher99000142016-10-13 17:21:01 +01005792 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5793 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005794 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 }
5797
Hanno Beckere678eaa2018-08-21 14:57:46 +01005798 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005799 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005800 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005801 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5803 ssl->in_msglen ) );
5804 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005805 }
5806
Hanno Beckere678eaa2018-08-21 14:57:46 +01005807 if( ssl->in_msg[0] != 1 )
5808 {
5809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5810 ssl->in_msg[0] ) );
5811 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5812 }
5813
5814#if defined(MBEDTLS_SSL_PROTO_DTLS)
5815 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5816 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5817 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5818 {
5819 if( ssl->handshake == NULL )
5820 {
5821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5822 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5823 }
5824
5825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5826 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5827 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005828#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005829 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005832 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005833 if( ssl->in_msglen != 2 )
5834 {
5835 /* Note: Standard allows for more than one 2 byte alert
5836 to be packed in a single message, but Mbed TLS doesn't
5837 currently support this. */
5838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5839 ssl->in_msglen ) );
5840 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5841 }
5842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005844 ssl->in_msg[0], ssl->in_msg[1] ) );
5845
5846 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005847 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005852 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 }
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5857 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5860 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005862
5863#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5864 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5865 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5866 {
Hanno Becker90333da2017-10-10 11:27:13 +01005867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005868 /* Will be handled when trying to parse ServerHello */
5869 return( 0 );
5870 }
5871#endif
5872
5873#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5874 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5875 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5876 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5877 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5878 {
5879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5880 /* Will be handled in mbedtls_ssl_parse_certificate() */
5881 return( 0 );
5882 }
5883#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5884
5885 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005886 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005887 }
5888
Hanno Beckerc76c6192017-06-06 10:03:17 +01005889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005890 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005891 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005892 /* Drop unexpected ApplicationData records,
5893 * except at the beginning of renegotiations */
5894 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5895 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5896#if defined(MBEDTLS_SSL_RENEGOTIATION)
5897 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5898 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005899#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005900 )
5901 {
5902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5903 return( MBEDTLS_ERR_SSL_NON_FATAL );
5904 }
5905
5906 if( ssl->handshake != NULL &&
5907 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5908 {
5909 ssl_handshake_wrapup_free_hs_transform( ssl );
5910 }
5911 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005912#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005913
Paul Bakker5121ce52009-01-03 21:22:43 +00005914 return( 0 );
5915}
5916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005918{
5919 int ret;
5920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5923 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005924 {
5925 return( ret );
5926 }
5927
5928 return( 0 );
5929}
5930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005932 unsigned char level,
5933 unsigned char message )
5934{
5935 int ret;
5936
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005937 if( ssl == NULL || ssl->conf == NULL )
5938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005944 ssl->out_msglen = 2;
5945 ssl->out_msg[0] = level;
5946 ssl->out_msg[1] = message;
5947
Hanno Becker67bc7c32018-08-06 11:33:50 +01005948 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005951 return( ret );
5952 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005954
5955 return( 0 );
5956}
5957
Paul Bakker5121ce52009-01-03 21:22:43 +00005958/*
5959 * Handshake functions
5960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5962 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5963 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5964 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5965 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5966 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5967 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005968/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005970{
Hanno Becker8759e162017-12-27 21:34:08 +00005971 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5976 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005977 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5978 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005981 ssl->state++;
5982 return( 0 );
5983 }
5984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005987}
5988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005990{
Hanno Becker8759e162017-12-27 21:34:08 +00005991 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5996 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005997 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5998 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006001 ssl->state++;
6002 return( 0 );
6003 }
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006007}
Gilles Peskinef9828522017-05-03 12:28:43 +02006008
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006009#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006010/* Some certificate support -> implement write and parse */
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006017 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6022 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006023 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6024 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006027 ssl->state++;
6028 return( 0 );
6029 }
6030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 {
6034 if( ssl->client_auth == 0 )
6035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 ssl->state++;
6038 return( 0 );
6039 }
6040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006042 /*
6043 * If using SSLv3 and got no cert, send an Alert message
6044 * (otherwise an empty Certificate message will be sent).
6045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6047 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006048 {
6049 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6051 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6052 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 goto write_msg;
6056 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059#endif /* MBEDTLS_SSL_CLI_C */
6060#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006061 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6066 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006067 }
6068 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006069#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006072
6073 /*
6074 * 0 . 0 handshake type
6075 * 1 . 3 handshake length
6076 * 4 . 6 length of all certs
6077 * 7 . 9 length of cert. 1
6078 * 10 . n-1 peer certificate
6079 * n . n+2 length of cert. 2
6080 * n+3 . ... upper level cert, etc.
6081 */
6082 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006084
Paul Bakker29087132010-03-21 21:03:34 +00006085 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 {
6087 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006088 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006091 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 }
6094
6095 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6096 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6097 ssl->out_msg[i + 2] = (unsigned char)( n );
6098
6099 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6100 i += n; crt = crt->next;
6101 }
6102
6103 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6104 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6105 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6106
6107 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6109 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006110
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006111#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006112write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006114
6115 ssl->state++;
6116
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006117 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006118 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006120 return( ret );
6121 }
6122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006124
Paul Bakkered27a042013-04-18 22:46:23 +02006125 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006126}
6127
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006128/*
6129 * Once the certificate message is read, parse it into a cert chain and
6130 * perform basic checks, but leave actual verification to the caller
6131 */
6132static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006134 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006135 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006136 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138#if defined(MBEDTLS_SSL_SRV_C)
6139#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 /*
6141 * Check if the client sent an empty certificate
6142 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006143 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006146 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6148 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6149 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006152
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006153 /* The client was asked for a certificate but didn't send
6154 one. The client should know what's going on, so we
6155 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006156 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006157 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006158 }
6159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6163 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006164 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6168 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6169 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6170 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006173
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006174 /* The client was asked for a certificate but didn't send
6175 one. The client should know what's going on, so we
6176 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006177 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006178 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006179 }
6180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6182 MBEDTLS_SSL_PROTO_TLS1_2 */
6183#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006188 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6189 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006191 }
6192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6194 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006197 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6198 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006200 }
6201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006203
Paul Bakker5121ce52009-01-03 21:22:43 +00006204 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006206 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006207 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006208
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006209 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006213 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6214 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006216 }
6217
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006218 /* In case we tried to reuse a session but it failed */
6219 if( ssl->session_negotiate->peer_cert != NULL )
6220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6222 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006223 }
6224
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006225 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006227 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006230 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6231 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006232 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233 }
6234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006236
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006237 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006238
6239 while( i < ssl->in_hslen )
6240 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006241 if ( i + 3 > ssl->in_hslen ) {
6242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6243 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6244 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6245 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6246 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 if( ssl->in_msg[i] != 0 )
6248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006250 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6251 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 }
6254
6255 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6256 | (unsigned int) ssl->in_msg[i + 2];
6257 i += 3;
6258
6259 if( n < 128 || i + n > ssl->in_hslen )
6260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006262 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6263 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 }
6266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006268 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006269 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006270 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006271 case 0: /*ok*/
6272 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6273 /* Ignore certificate with an unknown algorithm: maybe a
6274 prior certificate was already trusted. */
6275 break;
6276
6277 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6278 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6279 goto crt_parse_der_failed;
6280
6281 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6282 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6283 goto crt_parse_der_failed;
6284
6285 default:
6286 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6287 crt_parse_der_failed:
6288 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 return( ret );
6291 }
6292
6293 i += n;
6294 }
6295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006298 /*
6299 * On client, make sure the server cert doesn't change during renego to
6300 * avoid "triple handshake" attack: https://secure-resumption.com/
6301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006303 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006305 {
6306 if( ssl->session->peer_cert == NULL )
6307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006309 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6310 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006312 }
6313
6314 if( ssl->session->peer_cert->raw.len !=
6315 ssl->session_negotiate->peer_cert->raw.len ||
6316 memcmp( ssl->session->peer_cert->raw.p,
6317 ssl->session_negotiate->peer_cert->raw.p,
6318 ssl->session->peer_cert->raw.len ) != 0 )
6319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006321 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6322 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006324 }
6325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006327
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006328 return( 0 );
6329}
6330
6331int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6332{
6333 int ret;
6334 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006335 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006336#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6337 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6338 ? ssl->handshake->sni_authmode
6339 : ssl->conf->authmode;
6340#else
6341 const int authmode = ssl->conf->authmode;
6342#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006343 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006344
6345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6346
6347 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6348 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6349 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6350 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6351 {
6352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6353 ssl->state++;
6354 return( 0 );
6355 }
6356
6357#if defined(MBEDTLS_SSL_SRV_C)
6358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6359 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6360 {
6361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6362 ssl->state++;
6363 return( 0 );
6364 }
6365
6366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6367 authmode == MBEDTLS_SSL_VERIFY_NONE )
6368 {
6369 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006371
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006372 ssl->state++;
6373 return( 0 );
6374 }
6375#endif
6376
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006377#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6378 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006379 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006380 {
6381 goto crt_verify;
6382 }
6383#endif
6384
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006385 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006386 {
6387 /* mbedtls_ssl_read_record may have sent an alert already. We
6388 let it decide whether to alert. */
6389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6390 return( ret );
6391 }
6392
6393 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6394 {
6395#if defined(MBEDTLS_SSL_SRV_C)
6396 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6397 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6398 {
6399 ret = 0;
6400 }
6401#endif
6402
6403 ssl->state++;
6404 return( ret );
6405 }
6406
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006407#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6408 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006409 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006410
6411crt_verify:
6412 if( ssl->handshake->ecrs_enabled)
6413 rs_ctx = &ssl->handshake->ecrs_ctx;
6414#endif
6415
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006416 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006417 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006418 mbedtls_x509_crt *ca_chain;
6419 mbedtls_x509_crl *ca_crl;
6420
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006421#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006422 if( ssl->handshake->sni_ca_chain != NULL )
6423 {
6424 ca_chain = ssl->handshake->sni_ca_chain;
6425 ca_crl = ssl->handshake->sni_ca_crl;
6426 }
6427 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006428#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006429 {
6430 ca_chain = ssl->conf->ca_chain;
6431 ca_crl = ssl->conf->ca_crl;
6432 }
6433
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006434 /*
6435 * Main check: verify certificate
6436 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006437 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006438 ssl->session_negotiate->peer_cert,
6439 ca_chain, ca_crl,
6440 ssl->conf->cert_profile,
6441 ssl->hostname,
6442 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006443 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006444
6445 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006448 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006449
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006450#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6451 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006452 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006453#endif
6454
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006455 /*
6456 * Secondary checks: always done, but change 'ret' only if it was 0
6457 */
6458
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006459#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006462
6463 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006465 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006466 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006467 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006470 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006472 }
6473 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006474#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006477 ciphersuite_info,
6478 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006479 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006482 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006484 }
6485
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006486 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6487 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6488 * with details encoded in the verification flags. All other kinds
6489 * of error codes, including those from the user provided f_vrfy
6490 * functions, are treated as fatal and lead to a failure of
6491 * ssl_parse_certificate even if verification was optional. */
6492 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6493 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6494 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6495 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006497 }
6498
6499 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6500 {
6501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6502 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6503 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006504
6505 if( ret != 0 )
6506 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006507 uint8_t alert;
6508
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006509 /* The certificate may have been rejected for several reasons.
6510 Pick one and send the corresponding alert. Which alert to send
6511 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006512 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6513 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6514 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6515 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6516 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6517 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6518 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6519 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6520 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6521 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6522 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6523 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6524 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6525 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6526 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6527 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6528 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6529 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6530 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6531 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006532 else
6533 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006534 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6535 alert );
6536 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006537
Hanno Beckere6706e62017-05-15 16:05:15 +01006538#if defined(MBEDTLS_DEBUG_C)
6539 if( ssl->session_negotiate->verify_result != 0 )
6540 {
6541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6542 ssl->session_negotiate->verify_result ) );
6543 }
6544 else
6545 {
6546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6547 }
6548#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006549 }
6550
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006551 ssl->state++;
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006554
6555 return( ret );
6556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6558 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6559 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6560 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6561 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6562 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6563 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006566{
6567 int ret;
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006572 ssl->out_msglen = 1;
6573 ssl->out_msg[0] = 1;
6574
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 ssl->state++;
6576
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006577 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006578 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580 return( ret );
6581 }
6582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584
6585 return( 0 );
6586}
6587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006589{
6590 int ret;
6591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006593
Hanno Becker327c93b2018-08-15 13:56:18 +01006594 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006597 return( ret );
6598 }
6599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006603 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6604 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006606 }
6607
Hanno Beckere678eaa2018-08-21 14:57:46 +01006608 /* CCS records are only accepted if they have length 1 and content '1',
6609 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006610
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006611 /*
6612 * Switch to our negotiated transform and session parameters for inbound
6613 * data.
6614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006616 ssl->transform_in = ssl->transform_negotiate;
6617 ssl->session_in = ssl->session_negotiate;
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006623 ssl_dtls_replay_reset( ssl );
6624#endif
6625
6626 /* Increment epoch */
6627 if( ++ssl->in_epoch == 0 )
6628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006630 /* This is highly unlikely to happen for legitimate reasons, so
6631 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006633 }
6634 }
6635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006637 memset( ssl->in_ctr, 0, 8 );
6638
Hanno Beckerf5970a02019-05-08 09:38:41 +01006639 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6642 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006647 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6648 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006650 }
6651 }
6652#endif
6653
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 ssl->state++;
6655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006657
6658 return( 0 );
6659}
6660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6662 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006663{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006664 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6667 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6668 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006669 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006670 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006671#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6673#if defined(MBEDTLS_SHA512_C)
6674 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006675 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6676 else
6677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678#if defined(MBEDTLS_SHA256_C)
6679 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006680 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006681 else
6682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006686 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006687 }
Paul Bakker380da532012-04-18 16:10:25 +00006688}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6693 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006694 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6695 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6698#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006699 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006702 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006705}
6706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006708 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006709{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6711 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006712 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6713 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6716#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006717 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006720 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006723}
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6726 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6727static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006728 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006729{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006730 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6731 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006732}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006733#endif
Paul Bakker380da532012-04-18 16:10:25 +00006734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6736#if defined(MBEDTLS_SHA256_C)
6737static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006738 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006739{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006740 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006741}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006742#endif
Paul Bakker380da532012-04-18 16:10:25 +00006743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744#if defined(MBEDTLS_SHA512_C)
6745static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006746 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006747{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006748 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006749}
Paul Bakker769075d2012-11-24 11:26:46 +01006750#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006754static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006756{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006757 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006758 mbedtls_md5_context md5;
6759 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006760
Paul Bakker5121ce52009-01-03 21:22:43 +00006761 unsigned char padbuf[48];
6762 unsigned char md5sum[16];
6763 unsigned char sha1sum[20];
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006766 if( !session )
6767 session = ssl->session;
6768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006770
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006771 mbedtls_md5_init( &md5 );
6772 mbedtls_sha1_init( &sha1 );
6773
6774 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6775 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776
6777 /*
6778 * SSLv3:
6779 * hash =
6780 * MD5( master + pad2 +
6781 * MD5( handshake + sender + master + pad1 ) )
6782 * + SHA1( master + pad2 +
6783 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 */
6785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006787 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6788 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006792 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6793 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006794#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006797 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006798
Paul Bakker1ef83d62012-04-11 12:09:53 +00006799 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006801 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6802 mbedtls_md5_update_ret( &md5, session->master, 48 );
6803 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6804 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006806 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6807 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6808 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6809 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
Paul Bakker1ef83d62012-04-11 12:09:53 +00006811 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006813 mbedtls_md5_starts_ret( &md5 );
6814 mbedtls_md5_update_ret( &md5, session->master, 48 );
6815 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6816 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6817 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006819 mbedtls_sha1_starts_ret( &sha1 );
6820 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6821 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6822 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6823 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006827 mbedtls_md5_free( &md5 );
6828 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006829
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006830 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6831 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6832 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006841{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006842 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006843 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006844 mbedtls_md5_context md5;
6845 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006846 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006849 if( !session )
6850 session = ssl->session;
6851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006854 mbedtls_md5_init( &md5 );
6855 mbedtls_sha1_init( &sha1 );
6856
6857 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6858 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006859
Paul Bakker1ef83d62012-04-11 12:09:53 +00006860 /*
6861 * TLSv1:
6862 * hash = PRF( master, finished_label,
6863 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6864 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006867 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6868 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006872 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6873 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006874#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006877 ? "client finished"
6878 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006879
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006880 mbedtls_md5_finish_ret( &md5, padbuf );
6881 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006882
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006883 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006884 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006887
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006888 mbedtls_md5_free( &md5 );
6889 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006890
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006891 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6898#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006899static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006901{
6902 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006903 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006904 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006905 unsigned char padbuf[32];
6906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006908 if( !session )
6909 session = ssl->session;
6910
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006911 mbedtls_sha256_init( &sha256 );
6912
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006914
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006915 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006916
6917 /*
6918 * TLSv1.2:
6919 * hash = PRF( master, finished_label,
6920 * Hash( handshake ) )[0.11]
6921 */
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if !defined(MBEDTLS_SHA256_ALT)
6924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006925 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006926#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006929 ? "client finished"
6930 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006931
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006932 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006934 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006935 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006938
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006939 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006940
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006941 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006948static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006950{
6951 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006952 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006953 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006954 unsigned char padbuf[48];
6955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006957 if( !session )
6958 session = ssl->session;
6959
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006960 mbedtls_sha512_init( &sha512 );
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006963
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006964 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006965
6966 /*
6967 * TLSv1.2:
6968 * hash = PRF( master, finished_label,
6969 * Hash( handshake ) )[0.11]
6970 */
6971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006973 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6974 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006975#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006978 ? "client finished"
6979 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006980
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006981 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006982
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006983 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006984 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006987
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006988 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006989
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006990 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006993}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994#endif /* MBEDTLS_SHA512_C */
6995#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007000
7001 /*
7002 * Free our handshake params
7003 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007004 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007006 ssl->handshake = NULL;
7007
7008 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007009 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007010 */
7011 if( ssl->transform )
7012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 mbedtls_ssl_transform_free( ssl->transform );
7014 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007015 }
7016 ssl->transform = ssl->transform_negotiate;
7017 ssl->transform_negotiate = NULL;
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007020}
7021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007023{
7024 int resume = ssl->handshake->resume;
7025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028#if defined(MBEDTLS_SSL_RENEGOTIATION)
7029 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007032 ssl->renego_records_seen = 0;
7033 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007034#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007035
7036 /*
7037 * Free the previous session and switch in the current one
7038 */
Paul Bakker0a597072012-09-25 21:55:46 +00007039 if( ssl->session )
7040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007042 /* RFC 7366 3.1: keep the EtM state */
7043 ssl->session_negotiate->encrypt_then_mac =
7044 ssl->session->encrypt_then_mac;
7045#endif
7046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 mbedtls_ssl_session_free( ssl->session );
7048 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007049 }
7050 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007051 ssl->session_negotiate = NULL;
7052
Paul Bakker0a597072012-09-25 21:55:46 +00007053 /*
7054 * Add cache entry
7055 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007056 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007057 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007058 resume == 0 )
7059 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007060 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007062 }
Paul Bakker0a597072012-09-25 21:55:46 +00007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007066 ssl->handshake->flight != NULL )
7067 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007068 /* Cancel handshake timer */
7069 ssl_set_timer( ssl, 0 );
7070
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007071 /* Keep last flight around in case we need to resend it:
7072 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007074 }
7075 else
7076#endif
7077 ssl_handshake_wrapup_free_hs_transform( ssl );
7078
Paul Bakker48916f92012-09-16 19:57:18 +00007079 ssl->state++;
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007082}
7083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007085{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007086 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007089
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007090 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007091
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007092 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007093
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007094 /*
7095 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7096 * may define some other value. Currently (early 2016), no defined
7097 * ciphersuite does this (and this is unlikely to change as activity has
7098 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007103 ssl->verify_data_len = hash_len;
7104 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007105#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007106
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7109 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007110
7111 /*
7112 * In case of session resuming, invert the client and server
7113 * ChangeCipherSpec messages order.
7114 */
Paul Bakker0a597072012-09-25 21:55:46 +00007115 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007118 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007122 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007124#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007125 }
7126 else
7127 ssl->state++;
7128
Paul Bakker48916f92012-09-16 19:57:18 +00007129 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007130 * Switch to our negotiated transform and session parameters for outbound
7131 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007137 {
7138 unsigned char i;
7139
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007140 /* Remember current epoch settings for resending */
7141 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007142 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007143
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007144 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007145 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007146
7147 /* Increment epoch */
7148 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007149 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007150 break;
7151
7152 /* The loop goes to its end iff the counter is wrapping */
7153 if( i == 0 )
7154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7156 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007157 }
7158 }
7159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007161 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007162
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007163 ssl->transform_out = ssl->transform_negotiate;
7164 ssl->session_out = ssl->session_negotiate;
7165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7167 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7172 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007173 }
7174 }
7175#endif
7176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007180#endif
7181
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007182 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007183 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007185 return( ret );
7186 }
7187
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007188#if defined(MBEDTLS_SSL_PROTO_DTLS)
7189 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7190 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7191 {
7192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7193 return( ret );
7194 }
7195#endif
7196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007198
7199 return( 0 );
7200}
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007203#define SSL_MAX_HASH_LEN 36
7204#else
7205#define SSL_MAX_HASH_LEN 12
7206#endif
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007209{
Paul Bakker23986e52011-04-24 08:57:21 +00007210 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007211 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007212 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007215
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007216 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007217
Hanno Becker327c93b2018-08-15 13:56:18 +01007218 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007221 return( ret );
7222 }
7223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007227 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7228 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007230 }
7231
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007232 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233#if defined(MBEDTLS_SSL_PROTO_SSL3)
7234 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007235 hash_len = 36;
7236 else
7237#endif
7238 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7241 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007244 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7245 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007247 }
7248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007250 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007253 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007256 }
7257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007259 ssl->verify_data_len = hash_len;
7260 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007261#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007262
Paul Bakker0a597072012-09-25 21:55:46 +00007263 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007266 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007273 }
7274 else
7275 ssl->state++;
7276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007278 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007280#endif
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007283
7284 return( 0 );
7285}
7286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7292 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7293 mbedtls_md5_init( &handshake->fin_md5 );
7294 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007295 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7296 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7299#if defined(MBEDTLS_SHA256_C)
7300 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007301 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SHA512_C)
7304 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007305 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007308
7309 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007310
7311#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7312 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7313 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7314#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if defined(MBEDTLS_DHM_C)
7317 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319#if defined(MBEDTLS_ECDH_C)
7320 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007321#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007322#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007323 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007324#if defined(MBEDTLS_SSL_CLI_C)
7325 handshake->ecjpake_cache = NULL;
7326 handshake->ecjpake_cache_len = 0;
7327#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007328#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007329
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007330#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007331 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007332#endif
7333
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007334#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7335 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7336#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007337}
7338
Hanno Becker611a83b2018-01-03 14:27:32 +00007339void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7344 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007345
Hanno Becker92231322018-01-03 15:32:51 +00007346#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 mbedtls_md_init( &transform->md_ctx_enc );
7348 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007349#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007350}
7351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007353{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007355}
7356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007358{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007359 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007360 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007362 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007364 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007365 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007366
7367 /*
7368 * Either the pointers are now NULL or cleared properly and can be freed.
7369 * Now allocate missing structures.
7370 */
7371 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007372 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007373 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007374 }
Paul Bakker48916f92012-09-16 19:57:18 +00007375
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007376 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007377 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007378 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007379 }
Paul Bakker48916f92012-09-16 19:57:18 +00007380
Paul Bakker82788fb2014-10-20 13:59:19 +02007381 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007382 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007383 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007384 }
Paul Bakker48916f92012-09-16 19:57:18 +00007385
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007386 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007387 if( ssl->handshake == NULL ||
7388 ssl->transform_negotiate == NULL ||
7389 ssl->session_negotiate == NULL )
7390 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 mbedtls_free( ssl->handshake );
7394 mbedtls_free( ssl->transform_negotiate );
7395 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007396
7397 ssl->handshake = NULL;
7398 ssl->transform_negotiate = NULL;
7399 ssl->session_negotiate = NULL;
7400
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007401 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007402 }
7403
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007404 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007406 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007407 ssl_handshake_params_init( ssl->handshake );
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7411 {
7412 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007413
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007414 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7415 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7416 else
7417 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007418
7419 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007420 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007421#endif
7422
Paul Bakker48916f92012-09-16 19:57:18 +00007423 return( 0 );
7424}
7425
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007426#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007427/* Dummy cookie callbacks for defaults */
7428static int ssl_cookie_write_dummy( void *ctx,
7429 unsigned char **p, unsigned char *end,
7430 const unsigned char *cli_id, size_t cli_id_len )
7431{
7432 ((void) ctx);
7433 ((void) p);
7434 ((void) end);
7435 ((void) cli_id);
7436 ((void) cli_id_len);
7437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007439}
7440
7441static int ssl_cookie_check_dummy( void *ctx,
7442 const unsigned char *cookie, size_t cookie_len,
7443 const unsigned char *cli_id, size_t cli_id_len )
7444{
7445 ((void) ctx);
7446 ((void) cookie);
7447 ((void) cookie_len);
7448 ((void) cli_id);
7449 ((void) cli_id_len);
7450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007452}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007453#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007454
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007455/* Once ssl->out_hdr as the address of the beginning of the
7456 * next outgoing record is set, deduce the other pointers.
7457 *
7458 * Note: For TLS, we save the implicit record sequence number
7459 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7460 * and the caller has to make sure there's space for this.
7461 */
7462
7463static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7464 mbedtls_ssl_transform *transform )
7465{
7466#if defined(MBEDTLS_SSL_PROTO_DTLS)
7467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7468 {
7469 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007470#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007471 ssl->out_cid = ssl->out_ctr + 8;
7472 ssl->out_len = ssl->out_cid;
7473 if( transform != NULL )
7474 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007475#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007476 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007477#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007478 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007479 }
7480 else
7481#endif
7482 {
7483 ssl->out_ctr = ssl->out_hdr - 8;
7484 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007485#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007486 ssl->out_cid = ssl->out_len;
7487#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007488 ssl->out_iv = ssl->out_hdr + 5;
7489 }
7490
7491 /* Adjust out_msg to make space for explicit IV, if used. */
7492 if( transform != NULL &&
7493 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7494 {
7495 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7496 }
7497 else
7498 ssl->out_msg = ssl->out_iv;
7499}
7500
7501/* Once ssl->in_hdr as the address of the beginning of the
7502 * next incoming record is set, deduce the other pointers.
7503 *
7504 * Note: For TLS, we save the implicit record sequence number
7505 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7506 * and the caller has to make sure there's space for this.
7507 */
7508
Hanno Beckerf5970a02019-05-08 09:38:41 +01007509static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007510{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007511 /* This function sets the pointers to match the case
7512 * of unprotected TLS/DTLS records, with both ssl->in_iv
7513 * and ssl->in_msg pointing to the beginning of the record
7514 * content.
7515 *
7516 * When decrypting a protected record, ssl->in_msg
7517 * will be shifted to point to the beginning of the
7518 * record plaintext.
7519 */
7520
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007521#if defined(MBEDTLS_SSL_PROTO_DTLS)
7522 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7523 {
Hanno Becker70e79282019-05-03 14:34:53 +01007524 /* This sets the header pointers to match records
7525 * without CID. When we receive a record containing
7526 * a CID, the fields are shifted accordingly in
7527 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007528 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007529#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007530 ssl->in_cid = ssl->in_ctr + 8;
7531 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007532#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007533 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007534#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007535 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007536 }
7537 else
7538#endif
7539 {
7540 ssl->in_ctr = ssl->in_hdr - 8;
7541 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007543 ssl->in_cid = ssl->in_len;
7544#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007545 ssl->in_iv = ssl->in_hdr + 5;
7546 }
7547
Hanno Beckerf5970a02019-05-08 09:38:41 +01007548 /* This will be adjusted at record decryption time. */
7549 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007550}
7551
Paul Bakker5121ce52009-01-03 21:22:43 +00007552/*
7553 * Initialize an SSL context
7554 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007555void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7556{
7557 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7558}
7559
7560/*
7561 * Setup an SSL context
7562 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007563
7564static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7565{
7566 /* Set the incoming and outgoing record pointers. */
7567#if defined(MBEDTLS_SSL_PROTO_DTLS)
7568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7569 {
7570 ssl->out_hdr = ssl->out_buf;
7571 ssl->in_hdr = ssl->in_buf;
7572 }
7573 else
7574#endif /* MBEDTLS_SSL_PROTO_DTLS */
7575 {
7576 ssl->out_hdr = ssl->out_buf + 8;
7577 ssl->in_hdr = ssl->in_buf + 8;
7578 }
7579
7580 /* Derive other internal pointers. */
7581 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007582 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007583}
7584
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007585int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007586 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007587{
Paul Bakker48916f92012-09-16 19:57:18 +00007588 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007589
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007590 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007591
7592 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007593 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007594 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007595
7596 /* Set to NULL in case of an error condition */
7597 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007598
Angus Grattond8213d02016-05-25 20:56:48 +10007599 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7600 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007601 {
Angus Grattond8213d02016-05-25 20:56:48 +10007602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007603 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007604 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007605 }
7606
7607 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7608 if( ssl->out_buf == NULL )
7609 {
7610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007611 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007612 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007613 }
7614
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007615 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007616
Paul Bakker48916f92012-09-16 19:57:18 +00007617 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007618 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007619
7620 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007621
7622error:
7623 mbedtls_free( ssl->in_buf );
7624 mbedtls_free( ssl->out_buf );
7625
7626 ssl->conf = NULL;
7627
7628 ssl->in_buf = NULL;
7629 ssl->out_buf = NULL;
7630
7631 ssl->in_hdr = NULL;
7632 ssl->in_ctr = NULL;
7633 ssl->in_len = NULL;
7634 ssl->in_iv = NULL;
7635 ssl->in_msg = NULL;
7636
7637 ssl->out_hdr = NULL;
7638 ssl->out_ctr = NULL;
7639 ssl->out_len = NULL;
7640 ssl->out_iv = NULL;
7641 ssl->out_msg = NULL;
7642
k-stachowiak9f7798e2018-07-31 16:52:32 +02007643 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007644}
7645
7646/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007647 * Reset an initialized and used SSL context for re-use while retaining
7648 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007649 *
7650 * If partial is non-zero, keep data in the input buffer and client ID.
7651 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007652 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007653static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007654{
Paul Bakker48916f92012-09-16 19:57:18 +00007655 int ret;
7656
Hanno Becker7e772132018-08-10 12:38:21 +01007657#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7658 !defined(MBEDTLS_SSL_SRV_C)
7659 ((void) partial);
7660#endif
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007663
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007664 /* Cancel any possibly running timer */
7665 ssl_set_timer( ssl, 0 );
7666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if defined(MBEDTLS_SSL_RENEGOTIATION)
7668 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007669 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007670
7671 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7673 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007676
Paul Bakker7eb013f2011-10-06 12:37:39 +00007677 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007678 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007679
7680 ssl->in_msgtype = 0;
7681 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007683 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007684 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007687 ssl_dtls_replay_reset( ssl );
7688#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007689
7690 ssl->in_hslen = 0;
7691 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007692
7693 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007694
7695 ssl->out_msgtype = 0;
7696 ssl->out_msglen = 0;
7697 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7699 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007700 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007701#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007702
Hanno Becker19859472018-08-06 09:40:20 +01007703 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7704
Paul Bakker48916f92012-09-16 19:57:18 +00007705 ssl->transform_in = NULL;
7706 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007707
Hanno Becker78640902018-08-13 16:35:15 +01007708 ssl->session_in = NULL;
7709 ssl->session_out = NULL;
7710
Angus Grattond8213d02016-05-25 20:56:48 +10007711 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007712
7713#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007714 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007715#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7716 {
7717 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007718 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007719 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7722 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7725 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007729 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007730 }
7731#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007732
Paul Bakker48916f92012-09-16 19:57:18 +00007733 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 mbedtls_ssl_transform_free( ssl->transform );
7736 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007737 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007738 }
Paul Bakker48916f92012-09-16 19:57:18 +00007739
Paul Bakkerc0463502013-02-14 11:19:38 +01007740 if( ssl->session )
7741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 mbedtls_ssl_session_free( ssl->session );
7743 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007744 ssl->session = NULL;
7745 }
7746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007748 ssl->alpn_chosen = NULL;
7749#endif
7750
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007751#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007752#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007753 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007754#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007755 {
7756 mbedtls_free( ssl->cli_id );
7757 ssl->cli_id = NULL;
7758 ssl->cli_id_len = 0;
7759 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007760#endif
7761
Paul Bakker48916f92012-09-16 19:57:18 +00007762 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7763 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007764
7765 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007766}
7767
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007768/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007769 * Reset an initialized and used SSL context for re-use while retaining
7770 * all application-set variables, function pointers and data.
7771 */
7772int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7773{
7774 return( ssl_session_reset_int( ssl, 0 ) );
7775}
7776
7777/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007778 * SSL set accessors
7779 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007780void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007781{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007782 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007783}
7784
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007785void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007787 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007788}
7789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007791void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007792{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007793 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007794}
7795#endif
7796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007798void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007799{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007800 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007801}
7802#endif
7803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007805
Hanno Becker1841b0a2018-08-24 11:13:57 +01007806void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7807 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007808{
7809 ssl->disable_datagram_packing = !allow_packing;
7810}
7811
7812void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7813 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007814{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007815 conf->hs_timeout_min = min;
7816 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007817}
7818#endif
7819
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007820void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007821{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007822 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007823}
7824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007826void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007827 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007828 void *p_vrfy )
7829{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007830 conf->f_vrfy = f_vrfy;
7831 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007834
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007835void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007836 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007837 void *p_rng )
7838{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007839 conf->f_rng = f_rng;
7840 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007841}
7842
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007843void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007844 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 void *p_dbg )
7846{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007847 conf->f_dbg = f_dbg;
7848 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007849}
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007852 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007853 mbedtls_ssl_send_t *f_send,
7854 mbedtls_ssl_recv_t *f_recv,
7855 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007856{
7857 ssl->p_bio = p_bio;
7858 ssl->f_send = f_send;
7859 ssl->f_recv = f_recv;
7860 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007861}
7862
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007863#if defined(MBEDTLS_SSL_PROTO_DTLS)
7864void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7865{
7866 ssl->mtu = mtu;
7867}
7868#endif
7869
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007870void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007871{
7872 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007873}
7874
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007875void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7876 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007877 mbedtls_ssl_set_timer_t *f_set_timer,
7878 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007879{
7880 ssl->p_timer = p_timer;
7881 ssl->f_set_timer = f_set_timer;
7882 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007883
7884 /* Make sure we start with no timer running */
7885 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007886}
7887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007889void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007890 void *p_cache,
7891 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7892 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007893{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007894 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007895 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007896 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SSL_CLI_C)
7901int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007902{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007903 int ret;
7904
7905 if( ssl == NULL ||
7906 session == NULL ||
7907 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007908 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007911 }
7912
7913 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7914 return( ret );
7915
Paul Bakker0a597072012-09-25 21:55:46 +00007916 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007917
7918 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007921
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007922void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007923 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007925 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7926 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7927 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7928 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007929}
7930
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007931void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007932 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007933 int major, int minor )
7934{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007936 return;
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007939 return;
7940
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007941 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007942}
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007945void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007946 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007947{
7948 conf->cert_profile = profile;
7949}
7950
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007951/* Append a new keycert entry to a (possibly empty) list */
7952static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7953 mbedtls_x509_crt *cert,
7954 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007955{
niisato8ee24222018-06-25 19:05:48 +09007956 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007957
niisato8ee24222018-06-25 19:05:48 +09007958 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7959 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007960 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007961
niisato8ee24222018-06-25 19:05:48 +09007962 new_cert->cert = cert;
7963 new_cert->key = key;
7964 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007965
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007966 /* Update head is the list was null, else add to the end */
7967 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007968 {
niisato8ee24222018-06-25 19:05:48 +09007969 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007970 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007971 else
7972 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007973 mbedtls_ssl_key_cert *cur = *head;
7974 while( cur->next != NULL )
7975 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007976 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007977 }
7978
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007979 return( 0 );
7980}
7981
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007982int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007983 mbedtls_x509_crt *own_cert,
7984 mbedtls_pk_context *pk_key )
7985{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007986 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007987}
7988
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007989void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007990 mbedtls_x509_crt *ca_chain,
7991 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007992{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007993 conf->ca_chain = ca_chain;
7994 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007995}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007997
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007998#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7999int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8000 mbedtls_x509_crt *own_cert,
8001 mbedtls_pk_context *pk_key )
8002{
8003 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8004 own_cert, pk_key ) );
8005}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008006
8007void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8008 mbedtls_x509_crt *ca_chain,
8009 mbedtls_x509_crl *ca_crl )
8010{
8011 ssl->handshake->sni_ca_chain = ca_chain;
8012 ssl->handshake->sni_ca_crl = ca_crl;
8013}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008014
8015void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8016 int authmode )
8017{
8018 ssl->handshake->sni_authmode = authmode;
8019}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008020#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8021
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008022#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008023/*
8024 * Set EC J-PAKE password for current handshake
8025 */
8026int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8027 const unsigned char *pw,
8028 size_t pw_len )
8029{
8030 mbedtls_ecjpake_role role;
8031
Janos Follath8eb64132016-06-03 15:40:57 +01008032 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8034
8035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8036 role = MBEDTLS_ECJPAKE_SERVER;
8037 else
8038 role = MBEDTLS_ECJPAKE_CLIENT;
8039
8040 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8041 role,
8042 MBEDTLS_MD_SHA256,
8043 MBEDTLS_ECP_DP_SECP256R1,
8044 pw, pw_len ) );
8045}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008046#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008049int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008050 const unsigned char *psk, size_t psk_len,
8051 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008052{
Paul Bakker6db455e2013-09-18 17:29:31 +02008053 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008058
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008059 /* Identity len will be encoded on two bytes */
8060 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008061 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008062 {
8063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8064 }
8065
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008066 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008067 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008068 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008069
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008070 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008071 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008072 conf->psk_len = 0;
8073 }
8074 if( conf->psk_identity != NULL )
8075 {
8076 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008077 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008078 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008079 }
8080
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008081 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8082 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008083 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008084 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008085 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008086 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008087 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008088 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008089 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008090
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008091 conf->psk_len = psk_len;
8092 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008093
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008094 memcpy( conf->psk, psk, conf->psk_len );
8095 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008096
8097 return( 0 );
8098}
8099
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008100int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8101 const unsigned char *psk, size_t psk_len )
8102{
8103 if( psk == NULL || ssl->handshake == NULL )
8104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8105
8106 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8108
8109 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008110 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008111 mbedtls_platform_zeroize( ssl->handshake->psk,
8112 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008113 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008114 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008115 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008116
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008117 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008118 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008119
8120 ssl->handshake->psk_len = psk_len;
8121 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8122
8123 return( 0 );
8124}
8125
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008126void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008128 size_t),
8129 void *p_psk )
8130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008131 conf->f_psk = f_psk;
8132 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008135
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008136#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008137
8138#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008139int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008140{
8141 int ret;
8142
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008143 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8144 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8145 {
8146 mbedtls_mpi_free( &conf->dhm_P );
8147 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008148 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008150
8151 return( 0 );
8152}
Hanno Becker470a8c42017-10-04 15:28:46 +01008153#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008154
Hanno Beckera90658f2017-10-04 15:29:08 +01008155int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8156 const unsigned char *dhm_P, size_t P_len,
8157 const unsigned char *dhm_G, size_t G_len )
8158{
8159 int ret;
8160
8161 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8162 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8163 {
8164 mbedtls_mpi_free( &conf->dhm_P );
8165 mbedtls_mpi_free( &conf->dhm_G );
8166 return( ret );
8167 }
8168
8169 return( 0 );
8170}
Paul Bakker5121ce52009-01-03 21:22:43 +00008171
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008172int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008173{
8174 int ret;
8175
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008176 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8177 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8178 {
8179 mbedtls_mpi_free( &conf->dhm_P );
8180 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008181 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008182 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008183
8184 return( 0 );
8185}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008186#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008187
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008188#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8189/*
8190 * Set the minimum length for Diffie-Hellman parameters
8191 */
8192void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8193 unsigned int bitlen )
8194{
8195 conf->dhm_min_bitlen = bitlen;
8196}
8197#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8198
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008199#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008200/*
8201 * Set allowed/preferred hashes for handshake signatures
8202 */
8203void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8204 const int *hashes )
8205{
8206 conf->sig_hashes = hashes;
8207}
Hanno Becker947194e2017-04-07 13:25:49 +01008208#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008209
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008210#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008211/*
8212 * Set the allowed elliptic curves
8213 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008214void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008215 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008216{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008217 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008218}
Hanno Becker947194e2017-04-07 13:25:49 +01008219#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008220
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008221#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008223{
Hanno Becker947194e2017-04-07 13:25:49 +01008224 /* Initialize to suppress unnecessary compiler warning */
8225 size_t hostname_len = 0;
8226
8227 /* Check if new hostname is valid before
8228 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008229 if( hostname != NULL )
8230 {
8231 hostname_len = strlen( hostname );
8232
8233 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8235 }
8236
8237 /* Now it's clear that we will overwrite the old hostname,
8238 * so we can free it safely */
8239
8240 if( ssl->hostname != NULL )
8241 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008242 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008243 mbedtls_free( ssl->hostname );
8244 }
8245
8246 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008247
Paul Bakker5121ce52009-01-03 21:22:43 +00008248 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008249 {
8250 ssl->hostname = NULL;
8251 }
8252 else
8253 {
8254 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008255 if( ssl->hostname == NULL )
8256 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008257
Hanno Becker947194e2017-04-07 13:25:49 +01008258 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008259
Hanno Becker947194e2017-04-07 13:25:49 +01008260 ssl->hostname[hostname_len] = '\0';
8261 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008262
8263 return( 0 );
8264}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008265#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008266
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008267#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008268void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008270 const unsigned char *, size_t),
8271 void *p_sni )
8272{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008273 conf->f_sni = f_sni;
8274 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008275}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008279int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008280{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008281 size_t cur_len, tot_len;
8282 const char **p;
8283
8284 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008285 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8286 * MUST NOT be truncated."
8287 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008288 */
8289 tot_len = 0;
8290 for( p = protos; *p != NULL; p++ )
8291 {
8292 cur_len = strlen( *p );
8293 tot_len += cur_len;
8294
8295 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008297 }
8298
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008299 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008300
8301 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008302}
8303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008305{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008306 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008309
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008310void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008311{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008312 conf->max_major_ver = major;
8313 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008314}
8315
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008316void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008317{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008318 conf->min_major_ver = major;
8319 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008320}
8321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008323void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008324{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008325 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008326}
8327#endif
8328
Janos Follath088ce432017-04-10 12:42:31 +01008329#if defined(MBEDTLS_SSL_SRV_C)
8330void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8331 char cert_req_ca_list )
8332{
8333 conf->cert_req_ca_list = cert_req_ca_list;
8334}
8335#endif
8336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008338void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008339{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008340 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008341}
8342#endif
8343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008345void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008346{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008347 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008348}
8349#endif
8350
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008351#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008352void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008353{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008354 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008355}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008356#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008359int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008362 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008365 }
8366
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008367 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008368
8369 return( 0 );
8370}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008374void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008376 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008381void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008382{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008383 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008384}
8385#endif
8386
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008387void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008388{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008389 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008390}
8391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008395 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008396}
8397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008401}
8402
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008403void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008404 const unsigned char period[8] )
8405{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008406 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008411#if defined(MBEDTLS_SSL_CLI_C)
8412void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008413{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008414 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008415}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008416#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008417
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008418#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008419void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8420 mbedtls_ssl_ticket_write_t *f_ticket_write,
8421 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8422 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008423{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008424 conf->f_ticket_write = f_ticket_write;
8425 conf->f_ticket_parse = f_ticket_parse;
8426 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008427}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008430
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008431#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8432void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8433 mbedtls_ssl_export_keys_t *f_export_keys,
8434 void *p_export_keys )
8435{
8436 conf->f_export_keys = f_export_keys;
8437 conf->p_export_keys = p_export_keys;
8438}
8439#endif
8440
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008441#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008442void mbedtls_ssl_conf_async_private_cb(
8443 mbedtls_ssl_config *conf,
8444 mbedtls_ssl_async_sign_t *f_async_sign,
8445 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8446 mbedtls_ssl_async_resume_t *f_async_resume,
8447 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008448 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008449{
8450 conf->f_async_sign_start = f_async_sign;
8451 conf->f_async_decrypt_start = f_async_decrypt;
8452 conf->f_async_resume = f_async_resume;
8453 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008454 conf->p_async_config_data = async_config_data;
8455}
8456
Gilles Peskine8f97af72018-04-26 11:46:10 +02008457void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8458{
8459 return( conf->p_async_config_data );
8460}
8461
Gilles Peskine1febfef2018-04-30 11:54:39 +02008462void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008463{
8464 if( ssl->handshake == NULL )
8465 return( NULL );
8466 else
8467 return( ssl->handshake->user_async_ctx );
8468}
8469
Gilles Peskine1febfef2018-04-30 11:54:39 +02008470void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008471 void *ctx )
8472{
8473 if( ssl->handshake != NULL )
8474 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008475}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008476#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008477
Paul Bakker5121ce52009-01-03 21:22:43 +00008478/*
8479 * SSL get accessors
8480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008482{
8483 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8484}
8485
Hanno Becker8b170a02017-10-10 11:51:19 +01008486int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8487{
8488 /*
8489 * Case A: We're currently holding back
8490 * a message for further processing.
8491 */
8492
8493 if( ssl->keep_current_message == 1 )
8494 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008496 return( 1 );
8497 }
8498
8499 /*
8500 * Case B: Further records are pending in the current datagram.
8501 */
8502
8503#if defined(MBEDTLS_SSL_PROTO_DTLS)
8504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8505 ssl->in_left > ssl->next_record_offset )
8506 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008508 return( 1 );
8509 }
8510#endif /* MBEDTLS_SSL_PROTO_DTLS */
8511
8512 /*
8513 * Case C: A handshake message is being processed.
8514 */
8515
Hanno Becker8b170a02017-10-10 11:51:19 +01008516 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8517 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008519 return( 1 );
8520 }
8521
8522 /*
8523 * Case D: An application data message is being processed
8524 */
8525 if( ssl->in_offt != NULL )
8526 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008528 return( 1 );
8529 }
8530
8531 /*
8532 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008533 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008534 * we implement support for multiple alerts in single records.
8535 */
8536
8537 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8538 return( 0 );
8539}
8540
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008541uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008542{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008543 if( ssl->session != NULL )
8544 return( ssl->session->verify_result );
8545
8546 if( ssl->session_negotiate != NULL )
8547 return( ssl->session_negotiate->verify_result );
8548
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008549 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008550}
8551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008553{
Paul Bakker926c8e42013-03-06 10:23:34 +01008554 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008555 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008558}
8559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008564 {
8565 switch( ssl->minor_ver )
8566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008568 return( "DTLSv1.0" );
8569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008571 return( "DTLSv1.2" );
8572
8573 default:
8574 return( "unknown (DTLS)" );
8575 }
8576 }
8577#endif
8578
Paul Bakker43ca69c2011-01-15 17:35:19 +00008579 switch( ssl->minor_ver )
8580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008582 return( "SSLv3.0" );
8583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008585 return( "TLSv1.0" );
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008588 return( "TLSv1.1" );
8589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008591 return( "TLSv1.2" );
8592
Paul Bakker43ca69c2011-01-15 17:35:19 +00008593 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008594 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008595 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008596}
8597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008599{
Hanno Becker3136ede2018-08-17 15:28:19 +01008600 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008602 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008603
Hanno Becker43395762019-05-03 14:46:38 +01008604 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8605
Hanno Becker78640902018-08-13 16:35:15 +01008606 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008607 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#if defined(MBEDTLS_ZLIB_SUPPORT)
8610 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8611 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008612#endif
8613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 case MBEDTLS_MODE_GCM:
8617 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008618 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008620 transform_expansion = transform->minlen;
8621 break;
8622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008624
8625 block_size = mbedtls_cipher_get_block_size(
8626 &transform->cipher_ctx_enc );
8627
Hanno Becker3136ede2018-08-17 15:28:19 +01008628 /* Expansion due to the addition of the MAC. */
8629 transform_expansion += transform->maclen;
8630
8631 /* Expansion due to the addition of CBC padding;
8632 * Theoretically up to 256 bytes, but we never use
8633 * more than the block size of the underlying cipher. */
8634 transform_expansion += block_size;
8635
8636 /* For TLS 1.1 or higher, an explicit IV is added
8637 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008638#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8639 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008640 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008641#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008642
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008643 break;
8644
8645 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008648 }
8649
Hanno Beckera5a2b082019-05-15 14:03:01 +01008650#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008651 if( transform->out_cid_len != 0 )
8652 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008653#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008654
Hanno Becker43395762019-05-03 14:46:38 +01008655 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008656}
8657
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008658#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8659size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8660{
8661 size_t max_len;
8662
8663 /*
8664 * Assume mfl_code is correct since it was checked when set
8665 */
Angus Grattond8213d02016-05-25 20:56:48 +10008666 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008667
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008668 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008669 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008670 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008671 {
Angus Grattond8213d02016-05-25 20:56:48 +10008672 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008673 }
8674
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008675 /* During a handshake, use the value being negotiated */
8676 if( ssl->session_negotiate != NULL &&
8677 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8678 {
8679 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8680 }
8681
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008682 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008683}
8684#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8685
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008686#if defined(MBEDTLS_SSL_PROTO_DTLS)
8687static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8688{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008689 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8690 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8691 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8692 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8693 return ( 0 );
8694
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008695 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8696 return( ssl->mtu );
8697
8698 if( ssl->mtu == 0 )
8699 return( ssl->handshake->mtu );
8700
8701 return( ssl->mtu < ssl->handshake->mtu ?
8702 ssl->mtu : ssl->handshake->mtu );
8703}
8704#endif /* MBEDTLS_SSL_PROTO_DTLS */
8705
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008706int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8707{
8708 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8709
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008710#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8711 !defined(MBEDTLS_SSL_PROTO_DTLS)
8712 (void) ssl;
8713#endif
8714
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008715#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8716 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8717
8718 if( max_len > mfl )
8719 max_len = mfl;
8720#endif
8721
8722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008723 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008724 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008725 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008726 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8727 const size_t overhead = (size_t) ret;
8728
8729 if( ret < 0 )
8730 return( ret );
8731
8732 if( mtu <= overhead )
8733 {
8734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8735 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8736 }
8737
8738 if( max_len > mtu - overhead )
8739 max_len = mtu - overhead;
8740 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008741#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008742
Hanno Becker0defedb2018-08-10 12:35:02 +01008743#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8744 !defined(MBEDTLS_SSL_PROTO_DTLS)
8745 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008746#endif
8747
8748 return( (int) max_len );
8749}
8750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008751#if defined(MBEDTLS_X509_CRT_PARSE_C)
8752const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008753{
8754 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008755 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008756
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008757 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761#if defined(MBEDTLS_SSL_CLI_C)
8762int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008763{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008764 if( ssl == NULL ||
8765 dst == NULL ||
8766 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008767 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008770 }
8771
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008772 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008775
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008776const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8777{
8778 if( ssl == NULL )
8779 return( NULL );
8780
8781 return( ssl->session );
8782}
8783
Paul Bakker5121ce52009-01-03 21:22:43 +00008784/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008785 * Define ticket header determining Mbed TLS version
8786 * and structure of the ticket.
8787 */
8788
Hanno Becker41527622019-05-16 12:50:45 +01008789/*
Hanno Becker26829e92019-05-28 14:30:45 +01008790 * Define bitflag determining compile-time settings influencing
8791 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008792 */
8793
Hanno Becker26829e92019-05-28 14:30:45 +01008794#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008795#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008796#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008797#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008798#endif /* MBEDTLS_HAVE_TIME */
8799
8800#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008801#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008802#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008803#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008804#endif /* MBEDTLS_X509_CRT_PARSE_C */
8805
8806#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008807#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008808#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008809#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008810#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8811
8812#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008813#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008814#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008815#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008816#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8817
8818#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008819#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008820#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008821#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008822#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8823
8824#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008825#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008826#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008827#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008828#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8829
Hanno Becker41527622019-05-16 12:50:45 +01008830#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8831#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8832#else
8833#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8834#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8835
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008836#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8837#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8838#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8839#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8840#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8841#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8842#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008843
Hanno Becker26829e92019-05-28 14:30:45 +01008844#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008845 ( (uint16_t) ( \
8846 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8847 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8848 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8849 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8850 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8851 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008852 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008853
Hanno Becker557fe9f2019-05-16 12:41:07 +01008854static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008855 MBEDTLS_VERSION_MAJOR,
8856 MBEDTLS_VERSION_MINOR,
8857 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008858 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8859 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008860};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008861
8862/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008863 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008864 * (in the presentation language of TLS, RFC 8446 section 3)
8865 *
Hanno Becker26829e92019-05-28 14:30:45 +01008866 * opaque mbedtls_version[3]; // major, minor, patch
8867 * opaque session_format[2]; // version-specific 16-bit field determining
8868 * // the format of the remaining
8869 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008870 *
8871 * Note: When updating the format, remember to keep
8872 * these version+format bytes.
8873 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008874 * // In this version, `session_format` determines
8875 * // the setting of those compile-time
8876 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008877 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008878 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008879 * uint8 ciphersuite[2]; // defined by the standard
8880 * uint8 compression; // 0 or 1
8881 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008882 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008883 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008884 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008885 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8886 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008887 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008888 * uint8 mfl_code; // up to 255 according to standard
8889 * uint8 trunc_hmac; // 0 or 1
8890 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008891 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008892 * The order is the same as in the definition of the structure, except
8893 * verify_result is put before peer_cert so that all mandatory fields come
8894 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008895 */
8896int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8897 unsigned char *buf,
8898 size_t buf_len,
8899 size_t *olen )
8900{
8901 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008902 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008903#if defined(MBEDTLS_HAVE_TIME)
8904 uint64_t start;
8905#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008906#if defined(MBEDTLS_X509_CRT_PARSE_C)
8907 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008908#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008909
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008910 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008911 * Add version identifier
8912 */
8913
8914 used += sizeof( ssl_serialized_session_header );
8915
8916 if( used <= buf_len )
8917 {
8918 memcpy( p, ssl_serialized_session_header,
8919 sizeof( ssl_serialized_session_header ) );
8920 p += sizeof( ssl_serialized_session_header );
8921 }
8922
8923 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008924 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008925 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008926#if defined(MBEDTLS_HAVE_TIME)
8927 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008928
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008929 if( used <= buf_len )
8930 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008931 start = (uint64_t) session->start;
8932
8933 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8934 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8935 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8936 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8937 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
8938 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
8939 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
8940 *p++ = (unsigned char)( ( start ) & 0xFF );
8941 }
8942#endif /* MBEDTLS_HAVE_TIME */
8943
8944 /*
8945 * Basic mandatory fields
8946 */
8947 used += 2 /* ciphersuite */
8948 + 1 /* compression */
8949 + 1 /* id_len */
8950 + sizeof( session->id )
8951 + sizeof( session->master )
8952 + 4; /* verify_result */
8953
8954 if( used <= buf_len )
8955 {
8956 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
8957 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
8958
8959 *p++ = (unsigned char)( session->compression & 0xFF );
8960
8961 *p++ = (unsigned char)( session->id_len & 0xFF );
8962 memcpy( p, session->id, 32 );
8963 p += 32;
8964
8965 memcpy( p, session->master, 48 );
8966 p += 48;
8967
8968 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
8969 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
8970 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
8971 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008972 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008973
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008974 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008975 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008976 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008977#if defined(MBEDTLS_X509_CRT_PARSE_C)
8978 if( session->peer_cert == NULL )
8979 cert_len = 0;
8980 else
8981 cert_len = session->peer_cert->raw.len;
8982
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008983 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008984
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008985 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008986 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008987 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
8988 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
8989 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
8990
8991 if( session->peer_cert != NULL )
8992 {
8993 memcpy( p, session->peer_cert->raw.p, cert_len );
8994 p += cert_len;
8995 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008996 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008997#endif /* MBEDTLS_X509_CRT_PARSE_C */
8998
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008999 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009000 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009001 */
9002#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009003 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009004
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009005 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009006 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009007 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9008 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9009 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9010
9011 if( session->ticket != NULL )
9012 {
9013 memcpy( p, session->ticket, session->ticket_len );
9014 p += session->ticket_len;
9015 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009016
9017 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9018 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9019 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9020 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009021 }
9022#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9023
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009024 /*
9025 * Misc extension-related info
9026 */
9027#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9028 used += 1;
9029
9030 if( used <= buf_len )
9031 *p++ = session->mfl_code;
9032#endif
9033
9034#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9035 used += 1;
9036
9037 if( used <= buf_len )
9038 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9039#endif
9040
9041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9042 used += 1;
9043
9044 if( used <= buf_len )
9045 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9046#endif
9047
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009048 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009049 *olen = used;
9050
9051 if( used > buf_len )
9052 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009053
9054 return( 0 );
9055}
9056
9057/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009058 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009059 *
9060 * This internal version is wrapped by a public function that cleans up in
9061 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009062 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009063static int ssl_session_load( mbedtls_ssl_session *session,
9064 const unsigned char *buf,
9065 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009066{
9067 const unsigned char *p = buf;
9068 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009069#if defined(MBEDTLS_HAVE_TIME)
9070 uint64_t start;
9071#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009072#if defined(MBEDTLS_X509_CRT_PARSE_C)
9073 size_t cert_len;
9074#endif /* MBEDTLS_X509_CRT_PARSE_C */
9075
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009076 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009077 * Check version identifier
9078 */
9079
9080 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009082
9083 if( memcmp( p, ssl_serialized_session_header,
9084 sizeof( ssl_serialized_session_header ) ) != 0 )
9085 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009086 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009087 }
9088 p += sizeof( ssl_serialized_session_header );
9089
9090 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009091 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009092 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009093#if defined(MBEDTLS_HAVE_TIME)
9094 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9096
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009097 start = ( (uint64_t) p[0] << 56 ) |
9098 ( (uint64_t) p[1] << 48 ) |
9099 ( (uint64_t) p[2] << 40 ) |
9100 ( (uint64_t) p[3] << 32 ) |
9101 ( (uint64_t) p[4] << 24 ) |
9102 ( (uint64_t) p[5] << 16 ) |
9103 ( (uint64_t) p[6] << 8 ) |
9104 ( (uint64_t) p[7] );
9105 p += 8;
9106
9107 session->start = (time_t) start;
9108#endif /* MBEDTLS_HAVE_TIME */
9109
9110 /*
9111 * Basic mandatory fields
9112 */
9113 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9115
9116 session->ciphersuite = ( p[0] << 8 ) | p[1];
9117 p += 2;
9118
9119 session->compression = *p++;
9120
9121 session->id_len = *p++;
9122 memcpy( session->id, p, 32 );
9123 p += 32;
9124
9125 memcpy( session->master, p, 48 );
9126 p += 48;
9127
9128 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9129 ( (uint32_t) p[1] << 16 ) |
9130 ( (uint32_t) p[2] << 8 ) |
9131 ( (uint32_t) p[3] );
9132 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009133
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009134 /* Immediately clear invalid pointer values that have been read, in case
9135 * we exit early before we replaced them with valid ones. */
9136#if defined(MBEDTLS_X509_CRT_PARSE_C)
9137 session->peer_cert = NULL;
9138#endif
9139#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9140 session->ticket = NULL;
9141#endif
9142
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009143 /*
9144 * Peer certificate
9145 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009146#if defined(MBEDTLS_X509_CRT_PARSE_C)
9147 if( 3 > (size_t)( end - p ) )
9148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9149
9150 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9151 p += 3;
9152
9153 if( cert_len == 0 )
9154 {
9155 session->peer_cert = NULL;
9156 }
9157 else
9158 {
9159 int ret;
9160
9161 if( cert_len > (size_t)( end - p ) )
9162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9163
9164 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9165
9166 if( session->peer_cert == NULL )
9167 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9168
9169 mbedtls_x509_crt_init( session->peer_cert );
9170
9171 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9172 p, cert_len ) ) != 0 )
9173 {
9174 mbedtls_x509_crt_free( session->peer_cert );
9175 mbedtls_free( session->peer_cert );
9176 session->peer_cert = NULL;
9177 return( ret );
9178 }
9179
9180 p += cert_len;
9181 }
9182#endif /* MBEDTLS_X509_CRT_PARSE_C */
9183
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009184 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009185 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009186 */
9187#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9188 if( 3 > (size_t)( end - p ) )
9189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9190
9191 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9192 p += 3;
9193
9194 if( session->ticket_len != 0 )
9195 {
9196 if( session->ticket_len > (size_t)( end - p ) )
9197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9198
9199 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9200 if( session->ticket == NULL )
9201 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9202
9203 memcpy( session->ticket, p, session->ticket_len );
9204 p += session->ticket_len;
9205 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009206
9207 if( 4 > (size_t)( end - p ) )
9208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9209
9210 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9211 ( (uint32_t) p[1] << 16 ) |
9212 ( (uint32_t) p[2] << 8 ) |
9213 ( (uint32_t) p[3] );
9214 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009215#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9216
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009217 /*
9218 * Misc extension-related info
9219 */
9220#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9221 if( 1 > (size_t)( end - p ) )
9222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9223
9224 session->mfl_code = *p++;
9225#endif
9226
9227#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9228 if( 1 > (size_t)( end - p ) )
9229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9230
9231 session->trunc_hmac = *p++;
9232#endif
9233
9234#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9235 if( 1 > (size_t)( end - p ) )
9236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9237
9238 session->encrypt_then_mac = *p++;
9239#endif
9240
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009241 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009242 if( p != end )
9243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9244
9245 return( 0 );
9246}
9247
9248/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009249 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009250 */
9251int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9252 const unsigned char *buf,
9253 size_t len )
9254{
9255 int ret = ssl_session_load( session, buf, len );
9256
9257 if( ret != 0 )
9258 mbedtls_ssl_session_free( session );
9259
9260 return( ret );
9261}
9262
9263/*
Paul Bakker1961b702013-01-25 14:49:24 +01009264 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009267{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009269
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009270 if( ssl == NULL || ssl->conf == NULL )
9271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009274 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009278 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009280#endif
9281
Paul Bakker1961b702013-01-25 14:49:24 +01009282 return( ret );
9283}
9284
9285/*
9286 * Perform the SSL handshake
9287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009289{
9290 int ret = 0;
9291
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009292 if( ssl == NULL || ssl->conf == NULL )
9293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009300
9301 if( ret != 0 )
9302 break;
9303 }
9304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009306
9307 return( ret );
9308}
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310#if defined(MBEDTLS_SSL_RENEGOTIATION)
9311#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009312/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009313 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009316{
9317 int ret;
9318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009320
9321 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9323 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009324
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009325 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009326 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009328 return( ret );
9329 }
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009332
9333 return( 0 );
9334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009336
9337/*
9338 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 * - any side: calling mbedtls_ssl_renegotiate(),
9340 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9341 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009342 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009343 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009347{
9348 int ret;
9349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009351
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009352 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9353 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009354
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009355 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9356 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009360 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009361 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009362 ssl->handshake->out_msg_seq = 1;
9363 else
9364 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009365 }
9366#endif
9367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9369 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009374 return( ret );
9375 }
9376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009378
9379 return( 0 );
9380}
9381
9382/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009383 * Renegotiate current connection on client,
9384 * or request renegotiation on server
9385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009389
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009390 if( ssl == NULL || ssl->conf == NULL )
9391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009394 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009395 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009401
9402 /* Did we already try/start sending HelloRequest? */
9403 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009405
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009406 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009407 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009411 /*
9412 * On client, either start the renegotiation process or,
9413 * if already in progress, continue the handshake
9414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009419
9420 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009423 return( ret );
9424 }
9425 }
9426 else
9427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009431 return( ret );
9432 }
9433 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009435
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009436 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009437}
9438
9439/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009440 * Check record counters and renegotiate if they're above the limit.
9441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009443{
Andres AG2196c7f2016-12-15 17:01:16 +00009444 size_t ep_len = ssl_ep_len( ssl );
9445 int in_ctr_cmp;
9446 int out_ctr_cmp;
9447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9449 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009450 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009451 {
9452 return( 0 );
9453 }
9454
Andres AG2196c7f2016-12-15 17:01:16 +00009455 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9456 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009457 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009458 ssl->conf->renego_period + ep_len, 8 - ep_len );
9459
9460 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009461 {
9462 return( 0 );
9463 }
9464
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009469
9470/*
9471 * Receive application data decrypted from the SSL layer
9472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009474{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009475 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009476 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009477
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009478 if( ssl == NULL || ssl->conf == NULL )
9479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009484 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009487 return( ret );
9488
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009489 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009491 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009492 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009493 return( ret );
9494 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009495 }
9496#endif
9497
Hanno Becker4a810fb2017-05-24 16:27:30 +01009498 /*
9499 * Check if renegotiation is necessary and/or handshake is
9500 * in process. If yes, perform/continue, and fall through
9501 * if an unexpected packet is received while the client
9502 * is waiting for the ServerHello.
9503 *
9504 * (There is no equivalent to the last condition on
9505 * the server-side as it is not treated as within
9506 * a handshake while waiting for the ClientHello
9507 * after a renegotiation request.)
9508 */
9509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009511 ret = ssl_check_ctr_renegotiate( ssl );
9512 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9513 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009516 return( ret );
9517 }
9518#endif
9519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009523 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9524 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009527 return( ret );
9528 }
9529 }
9530
Hanno Beckere41158b2017-10-23 13:30:32 +01009531 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009532 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009533 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009534 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009535 if( ssl->f_get_timer != NULL &&
9536 ssl->f_get_timer( ssl->p_timer ) == -1 )
9537 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009538 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009539 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009540
Hanno Becker327c93b2018-08-15 13:56:18 +01009541 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009542 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009543 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9544 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009545
Hanno Becker4a810fb2017-05-24 16:27:30 +01009546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9547 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009548 }
9549
9550 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009552 {
9553 /*
9554 * OpenSSL sends empty messages to randomize the IV
9555 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009556 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009559 return( 0 );
9560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009562 return( ret );
9563 }
9564 }
9565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009569
Hanno Becker4a810fb2017-05-24 16:27:30 +01009570 /*
9571 * - For client-side, expect SERVER_HELLO_REQUEST.
9572 * - For server-side, expect CLIENT_HELLO.
9573 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9574 */
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009577 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009579 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009582
9583 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009585 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009586 {
9587 continue;
9588 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009591 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009592#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009593
Hanno Becker4a810fb2017-05-24 16:27:30 +01009594#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009599
9600 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009602 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009603 {
9604 continue;
9605 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009608 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009609#endif /* MBEDTLS_SSL_SRV_C */
9610
Hanno Becker21df7f92017-10-17 11:03:26 +01009611#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009612 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009613 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9614 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9615 ssl->conf->allow_legacy_renegotiation ==
9616 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9617 {
9618 /*
9619 * Accept renegotiation request
9620 */
Paul Bakker48916f92012-09-16 19:57:18 +00009621
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009622 /* DTLS clients need to know renego is server-initiated */
9623#if defined(MBEDTLS_SSL_PROTO_DTLS)
9624 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9625 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9626 {
9627 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9628 }
9629#endif
9630 ret = ssl_start_renegotiation( ssl );
9631 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9632 ret != 0 )
9633 {
9634 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9635 return( ret );
9636 }
9637 }
9638 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009639#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009640 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009641 /*
9642 * Refuse renegotiation
9643 */
9644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647#if defined(MBEDTLS_SSL_PROTO_SSL3)
9648 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009649 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009650 /* SSLv3 does not have a "no_renegotiation" warning, so
9651 we send a fatal alert and abort the connection. */
9652 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9653 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9654 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009655 }
9656 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9658#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9659 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9660 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9663 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9664 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009665 {
9666 return( ret );
9667 }
Paul Bakker48916f92012-09-16 19:57:18 +00009668 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009669 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9671 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009675 }
Paul Bakker48916f92012-09-16 19:57:18 +00009676 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009677
Hanno Becker90333da2017-10-10 11:27:13 +01009678 /* At this point, we don't know whether the renegotiation has been
9679 * completed or not. The cases to consider are the following:
9680 * 1) The renegotiation is complete. In this case, no new record
9681 * has been read yet.
9682 * 2) The renegotiation is incomplete because the client received
9683 * an application data record while awaiting the ServerHello.
9684 * 3) The renegotiation is incomplete because the client received
9685 * a non-handshake, non-application data message while awaiting
9686 * the ServerHello.
9687 * In each of these case, looping will be the proper action:
9688 * - For 1), the next iteration will read a new record and check
9689 * if it's application data.
9690 * - For 2), the loop condition isn't satisfied as application data
9691 * is present, hence continue is the same as break
9692 * - For 3), the loop condition is satisfied and read_record
9693 * will re-deliver the message that was held back by the client
9694 * when expecting the ServerHello.
9695 */
9696 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009697 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009698#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009700 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009701 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009702 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009703 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009706 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009708 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009709 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9714 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009717 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009718 }
9719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9723 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009724 }
9725
9726 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009727
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009728 /* We're going to return something now, cancel timer,
9729 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009731 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009732
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009734 /* If we requested renego but received AppData, resend HelloRequest.
9735 * Do it now, after setting in_offt, to avoid taking this branch
9736 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009740 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009741 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009744 return( ret );
9745 }
9746 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009748#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009749 }
9750
9751 n = ( len < ssl->in_msglen )
9752 ? len : ssl->in_msglen;
9753
9754 memcpy( buf, ssl->in_offt, n );
9755 ssl->in_msglen -= n;
9756
9757 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009758 {
9759 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009760 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009761 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009763 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009764 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009765 /* more data available */
9766 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009770
Paul Bakker23986e52011-04-24 08:57:21 +00009771 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009772}
9773
9774/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009775 * Send application data to be encrypted by the SSL layer, taking care of max
9776 * fragment length and buffer size.
9777 *
9778 * According to RFC 5246 Section 6.2.1:
9779 *
9780 * Zero-length fragments of Application data MAY be sent as they are
9781 * potentially useful as a traffic analysis countermeasure.
9782 *
9783 * Therefore, it is possible that the input message length is 0 and the
9784 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009785 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009786static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009787 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009788{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009789 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9790 const size_t max_len = (size_t) ret;
9791
9792 if( ret < 0 )
9793 {
9794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9795 return( ret );
9796 }
9797
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009798 if( len > max_len )
9799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009801 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009804 "maximum fragment length: %d > %d",
9805 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009807 }
9808 else
9809#endif
9810 len = max_len;
9811 }
Paul Bakker887bd502011-06-08 13:10:54 +00009812
Paul Bakker5121ce52009-01-03 21:22:43 +00009813 if( ssl->out_left != 0 )
9814 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009815 /*
9816 * The user has previously tried to send the data and
9817 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9818 * written. In this case, we expect the high-level write function
9819 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009824 return( ret );
9825 }
9826 }
Paul Bakker887bd502011-06-08 13:10:54 +00009827 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009828 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009829 /*
9830 * The user is trying to send a message the first time, so we need to
9831 * copy the data into the internal buffers and setup the data structure
9832 * to keep track of partial writes
9833 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009834 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009836 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009837
Hanno Becker67bc7c32018-08-06 11:33:50 +01009838 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009841 return( ret );
9842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009843 }
9844
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009845 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009846}
9847
9848/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009849 * Write application data, doing 1/n-1 splitting if necessary.
9850 *
9851 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009852 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009853 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009856static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009857 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009858{
9859 int ret;
9860
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009861 if( ssl->conf->cbc_record_splitting ==
9862 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009863 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9865 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9866 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009867 {
9868 return( ssl_write_real( ssl, buf, len ) );
9869 }
9870
9871 if( ssl->split_done == 0 )
9872 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009873 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009874 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009875 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009876 }
9877
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009878 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9879 return( ret );
9880 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009881
9882 return( ret + 1 );
9883}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009885
9886/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009887 * Write application data (public-facing wrapper)
9888 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009889int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009890{
9891 int ret;
9892
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009894
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009895 if( ssl == NULL || ssl->conf == NULL )
9896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9897
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009898#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009899 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9900 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009901 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009902 return( ret );
9903 }
9904#endif
9905
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009906 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009907 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009908 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009909 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009911 return( ret );
9912 }
9913 }
9914
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009915#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009916 ret = ssl_write_split( ssl, buf, len );
9917#else
9918 ret = ssl_write_real( ssl, buf, len );
9919#endif
9920
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009922
9923 return( ret );
9924}
9925
9926/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009927 * Notify the peer that the connection is being closed
9928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009930{
9931 int ret;
9932
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009933 if( ssl == NULL || ssl->conf == NULL )
9934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009937
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009938 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9944 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9945 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009948 return( ret );
9949 }
9950 }
9951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009953
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009954 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009955}
9956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009958{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009959 if( transform == NULL )
9960 return;
9961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009963 deflateEnd( &transform->ctx_deflate );
9964 inflateEnd( &transform->ctx_inflate );
9965#endif
9966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9968 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009969
Hanno Becker92231322018-01-03 15:32:51 +00009970#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 mbedtls_md_free( &transform->md_ctx_enc );
9972 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009973#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009974
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009975 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009976}
9977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978#if defined(MBEDTLS_X509_CRT_PARSE_C)
9979static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009980{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009982
9983 while( cur != NULL )
9984 {
9985 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009987 cur = next;
9988 }
9989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009991
Hanno Becker0271f962018-08-16 13:23:47 +01009992#if defined(MBEDTLS_SSL_PROTO_DTLS)
9993
9994static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9995{
9996 unsigned offset;
9997 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9998
9999 if( hs == NULL )
10000 return;
10001
Hanno Becker283f5ef2018-08-24 09:34:47 +010010002 ssl_free_buffered_record( ssl );
10003
Hanno Becker0271f962018-08-16 13:23:47 +010010004 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010005 ssl_buffering_free_slot( ssl, offset );
10006}
10007
10008static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10009 uint8_t slot )
10010{
10011 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10012 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010013
10014 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10015 return;
10016
Hanno Beckere605b192018-08-21 15:59:07 +010010017 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010018 {
Hanno Beckere605b192018-08-21 15:59:07 +010010019 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010020 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010021 mbedtls_free( hs_buf->data );
10022 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010023 }
10024}
10025
10026#endif /* MBEDTLS_SSL_PROTO_DTLS */
10027
Gilles Peskine9b562d52018-04-25 20:32:43 +020010028void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010029{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010030 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10031
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010032 if( handshake == NULL )
10033 return;
10034
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010035#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10036 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10037 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010038 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010039 handshake->async_in_progress = 0;
10040 }
10041#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10042
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010043#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10044 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10045 mbedtls_md5_free( &handshake->fin_md5 );
10046 mbedtls_sha1_free( &handshake->fin_sha1 );
10047#endif
10048#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10049#if defined(MBEDTLS_SHA256_C)
10050 mbedtls_sha256_free( &handshake->fin_sha256 );
10051#endif
10052#if defined(MBEDTLS_SHA512_C)
10053 mbedtls_sha512_free( &handshake->fin_sha512 );
10054#endif
10055#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057#if defined(MBEDTLS_DHM_C)
10058 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060#if defined(MBEDTLS_ECDH_C)
10061 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010062#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010063#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010064 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010065#if defined(MBEDTLS_SSL_CLI_C)
10066 mbedtls_free( handshake->ecjpake_cache );
10067 handshake->ecjpake_cache = NULL;
10068 handshake->ecjpake_cache_len = 0;
10069#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010070#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010071
Janos Follath4ae5c292016-02-10 11:27:43 +000010072#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10073 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010074 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010076#endif
10077
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010078#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10079 if( handshake->psk != NULL )
10080 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010081 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010082 mbedtls_free( handshake->psk );
10083 }
10084#endif
10085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10087 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010088 /*
10089 * Free only the linked list wrapper, not the keys themselves
10090 * since the belong to the SNI callback
10091 */
10092 if( handshake->sni_key_cert != NULL )
10093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010095
10096 while( cur != NULL )
10097 {
10098 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010100 cur = next;
10101 }
10102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010104
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010105#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010106 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010107#endif
10108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109#if defined(MBEDTLS_SSL_PROTO_DTLS)
10110 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010111 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010112 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010113#endif
10114
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010115 mbedtls_platform_zeroize( handshake,
10116 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010117}
10118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010120{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010121 if( session == NULL )
10122 return;
10123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000010125 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +000010126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010127 mbedtls_x509_crt_free( session->peer_cert );
10128 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +000010129 }
Paul Bakkered27a042013-04-18 22:46:23 +020010130#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010131
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010132#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010134#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010135
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010136 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010137}
10138
Paul Bakker5121ce52009-01-03 21:22:43 +000010139/*
10140 * Free an SSL context
10141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010143{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010144 if( ssl == NULL )
10145 return;
10146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010148
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010149 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010150 {
Angus Grattond8213d02016-05-25 20:56:48 +100010151 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010153 }
10154
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010155 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010156 {
Angus Grattond8213d02016-05-25 20:56:48 +100010157 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010158 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010159 }
10160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010162 if( ssl->compress_buf != NULL )
10163 {
Angus Grattond8213d02016-05-25 20:56:48 +100010164 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010166 }
10167#endif
10168
Paul Bakker48916f92012-09-16 19:57:18 +000010169 if( ssl->transform )
10170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 mbedtls_ssl_transform_free( ssl->transform );
10172 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010173 }
10174
10175 if( ssl->handshake )
10176 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010177 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10179 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181 mbedtls_free( ssl->handshake );
10182 mbedtls_free( ssl->transform_negotiate );
10183 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010184 }
10185
Paul Bakkerc0463502013-02-14 11:19:38 +010010186 if( ssl->session )
10187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 mbedtls_ssl_session_free( ssl->session );
10189 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010190 }
10191
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010192#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010193 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010194 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010195 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010197 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010198#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10201 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10204 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010205 }
10206#endif
10207
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010208#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010210#endif
10211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010213
Paul Bakker86f04f42013-02-14 11:20:09 +010010214 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010215 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010216}
10217
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010218/*
10219 * Initialze mbedtls_ssl_config
10220 */
10221void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10222{
10223 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10224}
10225
Simon Butcherc97b6972015-12-27 23:48:17 +000010226#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010227static int ssl_preset_default_hashes[] = {
10228#if defined(MBEDTLS_SHA512_C)
10229 MBEDTLS_MD_SHA512,
10230 MBEDTLS_MD_SHA384,
10231#endif
10232#if defined(MBEDTLS_SHA256_C)
10233 MBEDTLS_MD_SHA256,
10234 MBEDTLS_MD_SHA224,
10235#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010236#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010237 MBEDTLS_MD_SHA1,
10238#endif
10239 MBEDTLS_MD_NONE
10240};
Simon Butcherc97b6972015-12-27 23:48:17 +000010241#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010242
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010243static int ssl_preset_suiteb_ciphersuites[] = {
10244 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10245 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10246 0
10247};
10248
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010249#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010250static int ssl_preset_suiteb_hashes[] = {
10251 MBEDTLS_MD_SHA256,
10252 MBEDTLS_MD_SHA384,
10253 MBEDTLS_MD_NONE
10254};
10255#endif
10256
10257#if defined(MBEDTLS_ECP_C)
10258static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10259 MBEDTLS_ECP_DP_SECP256R1,
10260 MBEDTLS_ECP_DP_SECP384R1,
10261 MBEDTLS_ECP_DP_NONE
10262};
10263#endif
10264
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010265/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010266 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010267 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010268int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010269 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010270{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010271#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010272 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010273#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010274
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010275 /* Use the functions here so that they are covered in tests,
10276 * but otherwise access member directly for efficiency */
10277 mbedtls_ssl_conf_endpoint( conf, endpoint );
10278 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010279
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010280 /*
10281 * Things that are common to all presets
10282 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010283#if defined(MBEDTLS_SSL_CLI_C)
10284 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10285 {
10286 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10287#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10288 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10289#endif
10290 }
10291#endif
10292
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010293#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010294 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010295#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010296
10297#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10298 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10299#endif
10300
10301#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10302 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10303#endif
10304
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010305#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10306 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10307#endif
10308
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010309#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010310 conf->f_cookie_write = ssl_cookie_write_dummy;
10311 conf->f_cookie_check = ssl_cookie_check_dummy;
10312#endif
10313
10314#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10315 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10316#endif
10317
Janos Follath088ce432017-04-10 12:42:31 +010010318#if defined(MBEDTLS_SSL_SRV_C)
10319 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10320#endif
10321
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010322#if defined(MBEDTLS_SSL_PROTO_DTLS)
10323 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10324 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10325#endif
10326
10327#if defined(MBEDTLS_SSL_RENEGOTIATION)
10328 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010329 memset( conf->renego_period, 0x00, 2 );
10330 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010331#endif
10332
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010333#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10334 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10335 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010336 const unsigned char dhm_p[] =
10337 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10338 const unsigned char dhm_g[] =
10339 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10340
Hanno Beckera90658f2017-10-04 15:29:08 +010010341 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10342 dhm_p, sizeof( dhm_p ),
10343 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010344 {
10345 return( ret );
10346 }
10347 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010348#endif
10349
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010350 /*
10351 * Preset-specific defaults
10352 */
10353 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010354 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010355 /*
10356 * NSA Suite B
10357 */
10358 case MBEDTLS_SSL_PRESET_SUITEB:
10359 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10360 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10361 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10362 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10363
10364 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10365 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10366 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10367 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10368 ssl_preset_suiteb_ciphersuites;
10369
10370#if defined(MBEDTLS_X509_CRT_PARSE_C)
10371 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010372#endif
10373
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010374#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010375 conf->sig_hashes = ssl_preset_suiteb_hashes;
10376#endif
10377
10378#if defined(MBEDTLS_ECP_C)
10379 conf->curve_list = ssl_preset_suiteb_curves;
10380#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010381 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010382
10383 /*
10384 * Default
10385 */
10386 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010387 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10388 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10389 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10390 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10391 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10392 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10393 MBEDTLS_SSL_MIN_MINOR_VERSION :
10394 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010395 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10396 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10397
10398#if defined(MBEDTLS_SSL_PROTO_DTLS)
10399 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10400 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10401#endif
10402
10403 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10404 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10405 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10406 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10407 mbedtls_ssl_list_ciphersuites();
10408
10409#if defined(MBEDTLS_X509_CRT_PARSE_C)
10410 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10411#endif
10412
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010413#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010414 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010415#endif
10416
10417#if defined(MBEDTLS_ECP_C)
10418 conf->curve_list = mbedtls_ecp_grp_id_list();
10419#endif
10420
10421#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10422 conf->dhm_min_bitlen = 1024;
10423#endif
10424 }
10425
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010426 return( 0 );
10427}
10428
10429/*
10430 * Free mbedtls_ssl_config
10431 */
10432void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10433{
10434#if defined(MBEDTLS_DHM_C)
10435 mbedtls_mpi_free( &conf->dhm_P );
10436 mbedtls_mpi_free( &conf->dhm_G );
10437#endif
10438
10439#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10440 if( conf->psk != NULL )
10441 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010442 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010443 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010444 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010445 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010446 }
10447
10448 if( conf->psk_identity != NULL )
10449 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010450 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010451 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010452 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010453 conf->psk_identity_len = 0;
10454 }
10455#endif
10456
10457#if defined(MBEDTLS_X509_CRT_PARSE_C)
10458 ssl_key_cert_free( conf->key_cert );
10459#endif
10460
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010461 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010462}
10463
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010464#if defined(MBEDTLS_PK_C) && \
10465 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010466/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471#if defined(MBEDTLS_RSA_C)
10472 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10473 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475#if defined(MBEDTLS_ECDSA_C)
10476 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10477 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010480}
10481
Hanno Becker7e5437a2017-04-28 17:15:26 +010010482unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10483{
10484 switch( type ) {
10485 case MBEDTLS_PK_RSA:
10486 return( MBEDTLS_SSL_SIG_RSA );
10487 case MBEDTLS_PK_ECDSA:
10488 case MBEDTLS_PK_ECKEY:
10489 return( MBEDTLS_SSL_SIG_ECDSA );
10490 default:
10491 return( MBEDTLS_SSL_SIG_ANON );
10492 }
10493}
10494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010496{
10497 switch( sig )
10498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010499#if defined(MBEDTLS_RSA_C)
10500 case MBEDTLS_SSL_SIG_RSA:
10501 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010502#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503#if defined(MBEDTLS_ECDSA_C)
10504 case MBEDTLS_SSL_SIG_ECDSA:
10505 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010506#endif
10507 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010508 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010509 }
10510}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010511#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010512
Hanno Becker7e5437a2017-04-28 17:15:26 +010010513#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10514 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10515
10516/* Find an entry in a signature-hash set matching a given hash algorithm. */
10517mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10518 mbedtls_pk_type_t sig_alg )
10519{
10520 switch( sig_alg )
10521 {
10522 case MBEDTLS_PK_RSA:
10523 return( set->rsa );
10524 case MBEDTLS_PK_ECDSA:
10525 return( set->ecdsa );
10526 default:
10527 return( MBEDTLS_MD_NONE );
10528 }
10529}
10530
10531/* Add a signature-hash-pair to a signature-hash set */
10532void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10533 mbedtls_pk_type_t sig_alg,
10534 mbedtls_md_type_t md_alg )
10535{
10536 switch( sig_alg )
10537 {
10538 case MBEDTLS_PK_RSA:
10539 if( set->rsa == MBEDTLS_MD_NONE )
10540 set->rsa = md_alg;
10541 break;
10542
10543 case MBEDTLS_PK_ECDSA:
10544 if( set->ecdsa == MBEDTLS_MD_NONE )
10545 set->ecdsa = md_alg;
10546 break;
10547
10548 default:
10549 break;
10550 }
10551}
10552
10553/* Allow exactly one hash algorithm for each signature. */
10554void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10555 mbedtls_md_type_t md_alg )
10556{
10557 set->rsa = md_alg;
10558 set->ecdsa = md_alg;
10559}
10560
10561#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10562 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10563
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010564/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010565 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010567mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010568{
10569 switch( hash )
10570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571#if defined(MBEDTLS_MD5_C)
10572 case MBEDTLS_SSL_HASH_MD5:
10573 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575#if defined(MBEDTLS_SHA1_C)
10576 case MBEDTLS_SSL_HASH_SHA1:
10577 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579#if defined(MBEDTLS_SHA256_C)
10580 case MBEDTLS_SSL_HASH_SHA224:
10581 return( MBEDTLS_MD_SHA224 );
10582 case MBEDTLS_SSL_HASH_SHA256:
10583 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585#if defined(MBEDTLS_SHA512_C)
10586 case MBEDTLS_SSL_HASH_SHA384:
10587 return( MBEDTLS_MD_SHA384 );
10588 case MBEDTLS_SSL_HASH_SHA512:
10589 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010590#endif
10591 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010593 }
10594}
10595
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010596/*
10597 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10598 */
10599unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10600{
10601 switch( md )
10602 {
10603#if defined(MBEDTLS_MD5_C)
10604 case MBEDTLS_MD_MD5:
10605 return( MBEDTLS_SSL_HASH_MD5 );
10606#endif
10607#if defined(MBEDTLS_SHA1_C)
10608 case MBEDTLS_MD_SHA1:
10609 return( MBEDTLS_SSL_HASH_SHA1 );
10610#endif
10611#if defined(MBEDTLS_SHA256_C)
10612 case MBEDTLS_MD_SHA224:
10613 return( MBEDTLS_SSL_HASH_SHA224 );
10614 case MBEDTLS_MD_SHA256:
10615 return( MBEDTLS_SSL_HASH_SHA256 );
10616#endif
10617#if defined(MBEDTLS_SHA512_C)
10618 case MBEDTLS_MD_SHA384:
10619 return( MBEDTLS_SSL_HASH_SHA384 );
10620 case MBEDTLS_MD_SHA512:
10621 return( MBEDTLS_SSL_HASH_SHA512 );
10622#endif
10623 default:
10624 return( MBEDTLS_SSL_HASH_NONE );
10625 }
10626}
10627
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010628#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010629/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010630 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010631 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010632 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010633int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010636
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010637 if( ssl->conf->curve_list == NULL )
10638 return( -1 );
10639
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010640 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010641 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010642 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010643
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010644 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010645}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010646#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010647
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010648#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010649/*
10650 * Check if a hash proposed by the peer is in our list.
10651 * Return 0 if we're willing to use it, -1 otherwise.
10652 */
10653int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10654 mbedtls_md_type_t md )
10655{
10656 const int *cur;
10657
10658 if( ssl->conf->sig_hashes == NULL )
10659 return( -1 );
10660
10661 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10662 if( *cur == (int) md )
10663 return( 0 );
10664
10665 return( -1 );
10666}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010667#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669#if defined(MBEDTLS_X509_CRT_PARSE_C)
10670int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10671 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010672 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010673 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010674{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010675 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010676#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010677 int usage = 0;
10678#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010680 const char *ext_oid;
10681 size_t ext_len;
10682#endif
10683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10685 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010686 ((void) cert);
10687 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010688 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010689#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10692 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010693 {
10694 /* Server part of the key exchange */
10695 switch( ciphersuite->key_exchange )
10696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697 case MBEDTLS_KEY_EXCHANGE_RSA:
10698 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010699 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010700 break;
10701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10703 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10704 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10705 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
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_ECDH_RSA:
10709 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010710 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010711 break;
10712
10713 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 case MBEDTLS_KEY_EXCHANGE_NONE:
10715 case MBEDTLS_KEY_EXCHANGE_PSK:
10716 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10717 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010718 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010719 usage = 0;
10720 }
10721 }
10722 else
10723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10725 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010726 }
10727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010729 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010730 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010731 ret = -1;
10732 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010733#else
10734 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10738 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10741 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010742 }
10743 else
10744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10746 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010747 }
10748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010750 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010751 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010752 ret = -1;
10753 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010755
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010756 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010759
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010760/*
10761 * Convert version numbers to/from wire format
10762 * and, for DTLS, to/from TLS equivalent.
10763 *
10764 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010765 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010766 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10767 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010770 unsigned char ver[2] )
10771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772#if defined(MBEDTLS_SSL_PROTO_DTLS)
10773 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010776 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10777
10778 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10779 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10780 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010781 else
10782#else
10783 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010784#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010785 {
10786 ver[0] = (unsigned char) major;
10787 ver[1] = (unsigned char) minor;
10788 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010789}
10790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010792 const unsigned char ver[2] )
10793{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794#if defined(MBEDTLS_SSL_PROTO_DTLS)
10795 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010796 {
10797 *major = 255 - ver[0] + 2;
10798 *minor = 255 - ver[1] + 1;
10799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010800 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010801 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10802 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010803 else
10804#else
10805 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010806#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010807 {
10808 *major = ver[0];
10809 *minor = ver[1];
10810 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010811}
10812
Simon Butcher99000142016-10-13 17:21:01 +010010813int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10814{
10815#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10816 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10817 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10818
10819 switch( md )
10820 {
10821#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10822#if defined(MBEDTLS_MD5_C)
10823 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010824 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010825#endif
10826#if defined(MBEDTLS_SHA1_C)
10827 case MBEDTLS_SSL_HASH_SHA1:
10828 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10829 break;
10830#endif
10831#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10832#if defined(MBEDTLS_SHA512_C)
10833 case MBEDTLS_SSL_HASH_SHA384:
10834 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10835 break;
10836#endif
10837#if defined(MBEDTLS_SHA256_C)
10838 case MBEDTLS_SSL_HASH_SHA256:
10839 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10840 break;
10841#endif
10842 default:
10843 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10844 }
10845
10846 return 0;
10847#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10848 (void) ssl;
10849 (void) md;
10850
10851 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10852#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10853}
10854
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010855#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10856 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10857int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10858 unsigned char *output,
10859 unsigned char *data, size_t data_len )
10860{
10861 int ret = 0;
10862 mbedtls_md5_context mbedtls_md5;
10863 mbedtls_sha1_context mbedtls_sha1;
10864
10865 mbedtls_md5_init( &mbedtls_md5 );
10866 mbedtls_sha1_init( &mbedtls_sha1 );
10867
10868 /*
10869 * digitally-signed struct {
10870 * opaque md5_hash[16];
10871 * opaque sha_hash[20];
10872 * };
10873 *
10874 * md5_hash
10875 * MD5(ClientHello.random + ServerHello.random
10876 * + ServerParams);
10877 * sha_hash
10878 * SHA(ClientHello.random + ServerHello.random
10879 * + ServerParams);
10880 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010881 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010882 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010884 goto exit;
10885 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010886 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010887 ssl->handshake->randbytes, 64 ) ) != 0 )
10888 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_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, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010893 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010895 goto exit;
10896 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010897 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010898 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010900 goto exit;
10901 }
10902
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010903 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010904 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010906 goto exit;
10907 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010908 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010909 ssl->handshake->randbytes, 64 ) ) != 0 )
10910 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_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, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010915 data_len ) ) != 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_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010921 output + 16 ) ) != 0 )
10922 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010924 goto exit;
10925 }
10926
10927exit:
10928 mbedtls_md5_free( &mbedtls_md5 );
10929 mbedtls_sha1_free( &mbedtls_sha1 );
10930
10931 if( ret != 0 )
10932 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10933 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10934
10935 return( ret );
10936
10937}
10938#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10939 MBEDTLS_SSL_PROTO_TLS1_1 */
10940
10941#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10942 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10943int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010944 unsigned char *hash, size_t *hashlen,
10945 unsigned char *data, size_t data_len,
10946 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010947{
10948 int ret = 0;
10949 mbedtls_md_context_t ctx;
10950 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010951 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010952
10953 mbedtls_md_init( &ctx );
10954
10955 /*
10956 * digitally-signed struct {
10957 * opaque client_random[32];
10958 * opaque server_random[32];
10959 * ServerDHParams params;
10960 * };
10961 */
10962 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10963 {
10964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10965 goto exit;
10966 }
10967 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10968 {
10969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10970 goto exit;
10971 }
10972 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10973 {
10974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10975 goto exit;
10976 }
10977 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10978 {
10979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10980 goto exit;
10981 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010982 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010983 {
10984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10985 goto exit;
10986 }
10987
10988exit:
10989 mbedtls_md_free( &ctx );
10990
10991 if( ret != 0 )
10992 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10993 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10994
10995 return( ret );
10996}
10997#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10998 MBEDTLS_SSL_PROTO_TLS1_2 */
10999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011000#endif /* MBEDTLS_SSL_TLS_C */