blob: 005362337f431678c6641cef34398e33accfab4b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010067#else
68 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069#endif
70 return( 0 );
71}
72
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020073/*
74 * Start a timer.
75 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020079 if( ssl->f_set_timer == NULL )
80 return;
81
82 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
83 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084}
85
86/*
87 * Return -1 is timer is expired, 0 if it isn't.
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020090{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020091 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020092 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020093
94 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020095 {
96 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020098 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020099
100 return( 0 );
101}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100103static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
104 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100105static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100106
107#define SSL_DONT_FORCE_FLUSH 0
108#define SSL_FORCE_FLUSH 1
109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100111
Hanno Beckera5a2b082019-05-15 14:03:01 +0100112#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100113/* Top-level Connection ID API */
114
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100115int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
116 size_t len,
117 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100118{
119 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
121
Hanno Becker791ec6b2019-05-14 11:45:26 +0100122 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
123 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
124 {
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126 }
127
128 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100129 conf->cid_len = len;
130 return( 0 );
131}
132
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100133int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
134 int enable,
135 unsigned char const *own_cid,
136 size_t own_cid_len )
137{
Hanno Becker78c43022019-05-03 14:38:32 +0100138 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
140
Hanno Becker07489862019-04-25 16:01:49 +0100141 ssl->negotiate_cid = enable;
142 if( enable == MBEDTLS_SSL_CID_DISABLED )
143 {
144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
145 return( 0 );
146 }
147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100148 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100149
Hanno Beckereec2be92019-05-03 13:06:44 +0100150 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100151 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
153 (unsigned) own_cid_len,
154 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
156 }
157
158 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100159 /* Truncation is not an issue here because
160 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
161 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100162
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100163 return( 0 );
164}
165
166int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
167 int *enabled,
168 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
169 size_t *peer_cid_len )
170{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100171 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100172
Hanno Becker78c43022019-05-03 14:38:32 +0100173 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
174 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
175 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100177 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100178
Hanno Beckercb063f52019-05-03 12:54:52 +0100179 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
180 * were used, but client and server requested the empty CID.
181 * This is indistinguishable from not using the CID extension
182 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183 if( ssl->transform_in->in_cid_len == 0 &&
184 ssl->transform_in->out_cid_len == 0 )
185 {
186 return( 0 );
187 }
188
Hanno Becker633d6042019-05-22 16:50:35 +0100189 if( peer_cid_len != NULL )
190 {
191 *peer_cid_len = ssl->transform_in->out_cid_len;
192 if( peer_cid != NULL )
193 {
194 memcpy( peer_cid, ssl->transform_in->out_cid,
195 ssl->transform_in->out_cid_len );
196 }
197 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100198
199 *enabled = MBEDTLS_SSL_CID_ENABLED;
200
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100201 return( 0 );
202}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100203#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100204
Hanno Beckerd5847772018-08-28 10:09:23 +0100205/* Forward declarations for functions related to message buffering. */
206static void ssl_buffering_free( mbedtls_ssl_context *ssl );
207static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
208 uint8_t slot );
209static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
210static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
211static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
212static int ssl_buffer_message( mbedtls_ssl_context *ssl );
213static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100214static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100215
Hanno Beckera67dee22018-08-22 10:05:20 +0100216static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100217static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100218{
Hanno Becker11682cc2018-08-22 14:41:02 +0100219 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100220
221 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100222 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223
224 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
225}
226
Hanno Becker67bc7c32018-08-06 11:33:50 +0100227static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
228{
Hanno Becker11682cc2018-08-22 14:41:02 +0100229 size_t const bytes_written = ssl->out_left;
230 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231
232 /* Double-check that the write-index hasn't gone
233 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235 {
236 /* Should never happen... */
237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
238 }
239
240 return( (int) ( mtu - bytes_written ) );
241}
242
243static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
244{
245 int ret;
246 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400247 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100248
249#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
250 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
251
252 if( max_len > mfl )
253 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100254
255 /* By the standard (RFC 6066 Sect. 4), the MFL extension
256 * only limits the maximum record payload size, so in theory
257 * we would be allowed to pack multiple records of payload size
258 * MFL into a single datagram. However, this would mean that there's
259 * no way to explicitly communicate MTU restrictions to the peer.
260 *
261 * The following reduction of max_len makes sure that we never
262 * write datagrams larger than MFL + Record Expansion Overhead.
263 */
264 if( max_len <= ssl->out_left )
265 return( 0 );
266
267 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100268#endif
269
270 ret = ssl_get_remaining_space_in_datagram( ssl );
271 if( ret < 0 )
272 return( ret );
273 remaining = (size_t) ret;
274
275 ret = mbedtls_ssl_get_record_expansion( ssl );
276 if( ret < 0 )
277 return( ret );
278 expansion = (size_t) ret;
279
280 if( remaining <= expansion )
281 return( 0 );
282
283 remaining -= expansion;
284 if( remaining >= max_len )
285 remaining = max_len;
286
287 return( (int) remaining );
288}
289
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290/*
291 * Double the retransmit timeout value, within the allowed range,
292 * returning -1 if the maximum value has already been reached.
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295{
296 uint32_t new_timeout;
297
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200298 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299 return( -1 );
300
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200301 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
302 * in the following way: after the initial transmission and a first
303 * retransmission, back off to a temporary estimated MTU of 508 bytes.
304 * This value is guaranteed to be deliverable (if not guaranteed to be
305 * delivered) of any compliant IPv4 (and IPv6) network, and should work
306 * on most non-IP stacks too. */
307 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400308 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200309 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
311 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200312
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313 new_timeout = 2 * ssl->handshake->retransmit_timeout;
314
315 /* Avoid arithmetic overflow and range overflow */
316 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200317 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200319 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200320 }
321
322 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200324 ssl->handshake->retransmit_timeout ) );
325
326 return( 0 );
327}
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200330{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200331 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200333 ssl->handshake->retransmit_timeout ) );
334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200338/*
339 * Convert max_fragment_length codes to length.
340 * RFC 6066 says:
341 * enum{
342 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
343 * } MaxFragmentLength;
344 * and we add 0 -> extension unused
345 */
Angus Grattond8213d02016-05-25 20:56:48 +1000346static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200347{
Angus Grattond8213d02016-05-25 20:56:48 +1000348 switch( mfl )
349 {
350 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
351 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
352 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
353 return 512;
354 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
355 return 1024;
356 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
357 return 2048;
358 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
359 return 4096;
360 default:
361 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
362 }
363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200365
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200366#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_ssl_session_free( dst );
370 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373 if( src->peer_cert != NULL )
374 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200375 int ret;
376
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200377 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200378 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200384 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387 dst->peer_cert = NULL;
388 return( ret );
389 }
390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200393#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 if( src->ticket != NULL )
395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200396 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399
400 memcpy( dst->ticket, src->ticket, src->ticket_len );
401 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403
404 return( 0 );
405}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200406#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
409int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200410 const unsigned char *key_enc, const unsigned char *key_dec,
411 size_t keylen,
412 const unsigned char *iv_enc, const unsigned char *iv_dec,
413 size_t ivlen,
414 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200415 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
417int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
420int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
421#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423/*
424 * Key material generation
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427static int ssl3_prf( const unsigned char *secret, size_t slen,
428 const char *label,
429 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000430 unsigned char *dstbuf, size_t dlen )
431{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100432 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000433 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200434 mbedtls_md5_context md5;
435 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000436 unsigned char padding[16];
437 unsigned char sha1sum[20];
438 ((void)label);
439
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200440 mbedtls_md5_init( &md5 );
441 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200442
Paul Bakker5f70b252012-09-13 14:23:06 +0000443 /*
444 * SSLv3:
445 * block =
446 * MD5( secret + SHA1( 'A' + secret + random ) ) +
447 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
448 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
449 * ...
450 */
451 for( i = 0; i < dlen / 16; i++ )
452 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200453 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100455 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100456 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000465
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000474 }
475
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200477 mbedtls_md5_free( &md5 );
478 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000479
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500480 mbedtls_platform_zeroize( padding, sizeof( padding ) );
481 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000482
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200488static int tls1_prf( const unsigned char *secret, size_t slen,
489 const char *label,
490 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000491 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000492{
Paul Bakker23986e52011-04-24 08:57:21 +0000493 size_t nb, hs;
494 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200495 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 unsigned char tmp[128];
497 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 const mbedtls_md_info_t *md_info;
499 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100500 int ret;
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507 hs = ( slen + 1 ) / 2;
508 S1 = secret;
509 S2 = secret + slen - hs;
510
511 nb = strlen( label );
512 memcpy( tmp + 20, label, nb );
513 memcpy( tmp + 20 + nb, random, rlen );
514 nb += rlen;
515
516 /*
517 * First compute P_md5(secret,label+random)[0..dlen]
518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100523 return( ret );
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
526 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
527 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 for( i = 0; i < dlen; i += 16 )
530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_md_hmac_reset ( &md_ctx );
532 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
533 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_reset ( &md_ctx );
536 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
537 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
540
541 for( j = 0; j < k; j++ )
542 dstbuf[i + j] = h_i[j];
543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100546
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 /*
548 * XOR out with P_sha1(secret,label+random)[0..dlen]
549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554 return( ret );
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
557 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
558 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 for( i = 0; i < dlen; i += 20 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_md_hmac_reset ( &md_ctx );
563 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
564 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_hmac_reset ( &md_ctx );
567 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
568 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
571
572 for( j = 0; j < k; j++ )
573 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
574 }
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100577
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500578 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
579 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 return( 0 );
582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
586static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100587 const unsigned char *secret, size_t slen,
588 const char *label,
589 const unsigned char *random, size_t rlen,
590 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000591{
592 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100593 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000594 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
596 const mbedtls_md_info_t *md_info;
597 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598 int ret;
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
607 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000609
610 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100611 memcpy( tmp + md_len, label, nb );
612 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000613 nb += rlen;
614
615 /*
616 * Compute P_<hash>(secret, label + random)[0..dlen]
617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 return( ret );
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
622 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
623 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100624
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_md_hmac_reset ( &md_ctx );
628 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
629 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_md_hmac_reset ( &md_ctx );
632 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
633 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100635 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
637 for( j = 0; j < k; j++ )
638 dstbuf[i + j] = h_i[j];
639 }
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100642
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500643 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
644 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000645
646 return( 0 );
647}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100650static int tls_prf_sha256( const unsigned char *secret, size_t slen,
651 const char *label,
652 const unsigned char *random, size_t rlen,
653 unsigned char *dstbuf, size_t dlen )
654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 label, random, rlen, dstbuf, dlen ) );
657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200661static int tls_prf_sha384( const unsigned char *secret, size_t slen,
662 const char *label,
663 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 unsigned char *dstbuf, size_t dlen )
665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100667 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#endif /* MBEDTLS_SHA512_C */
670#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
675 defined(MBEDTLS_SSL_PROTO_TLS1_1)
676static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677#endif
Paul Bakker380da532012-04-18 16:10:25 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200680static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682#endif
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200685static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
690#if defined(MBEDTLS_SHA256_C)
691static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200692static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_SHA512_C)
697static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200698static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200703/* Type for the TLS PRF */
704typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
705 const unsigned char *, size_t,
706 unsigned char *, size_t);
707
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200708/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200709 * Populate a transform structure with session keys and all the other
710 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200711 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200712 * Parameters:
713 * - [in/out]: transform: structure to populate
714 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200715 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200716 * - [in] ciphersuite
717 * - [in] master
718 * - [in] encrypt_then_mac
719 * - [in] trunc_hmac
720 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200721 * - [in] tls_prf: pointer to PRF to use for key derivation
722 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200723 * - [in] minor_ver: SSL/TLS minor version
724 * - [in] endpoint: client or server
725 * - [in] ssl: optionally used for:
726 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
727 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
728 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200731 int ciphersuite,
732 const unsigned char master[48],
733#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
734 int encrypt_then_mac,
735#endif
736#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
737 int trunc_hmac,
738#endif
739#if defined(MBEDTLS_ZLIB_SUPPORT)
740 int compression,
741#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 ssl_tls_prf_t tls_prf,
743 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 int minor_ver,
745 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200746 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000747{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200748 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 unsigned char keyblk[256];
750 unsigned char *key1;
751 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100752 unsigned char *mac_enc;
753 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000754 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200755 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000756 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000757 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 const mbedtls_cipher_info_t *cipher_info;
759 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100760
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200761#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
762 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
763 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200764 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200765 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000766#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000767
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200768 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200770 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000771#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200772 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200774 /*
775 * Get various info structures
776 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200777 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200778 if( ciphersuite_info == NULL )
779 {
780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200781 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
783 }
784
Hanno Becker8759e162017-12-27 21:34:08 +0000785 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100786 if( cipher_info == NULL )
787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000789 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100791 }
792
Hanno Becker8759e162017-12-27 21:34:08 +0000793 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100794 if( md_info == NULL )
795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000797 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 }
800
Hanno Beckera5a2b082019-05-15 14:03:01 +0100801#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100802 /* Copy own and peer's CID if the use of the CID
803 * extension has been negotiated. */
804 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
805 {
806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100807
Hanno Becker4932f9f2019-05-03 15:23:51 +0100808 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100809 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100810 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100811 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100812
813 transform->out_cid_len = ssl->handshake->peer_cid_len;
814 memcpy( transform->out_cid, ssl->handshake->peer_cid,
815 ssl->handshake->peer_cid_len );
816 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
817 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100818 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100819#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200822 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000823 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200824 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100825 if( ret != 0 )
826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100828 return( ret );
829 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200832 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200833 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200834 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 /*
838 * Determine the appropriate key, IV and MAC length.
839 */
Paul Bakker68884e32013-01-07 18:20:04 +0100840
Hanno Beckere7f2df02017-12-27 08:17:40 +0000841 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200842
Hanno Beckerf1229442018-01-03 15:32:31 +0000843#if defined(MBEDTLS_GCM_C) || \
844 defined(MBEDTLS_CCM_C) || \
845 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200847 cipher_info->mode == MBEDTLS_MODE_CCM ||
848 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 {
Hanno Becker8759e162017-12-27 21:34:08 +0000850 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200851
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200852 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000853 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000854 transform->taglen =
855 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200856
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200857 /* All modes haves 96-bit IVs;
858 * GCM and CCM has 4 implicit and 8 explicit bytes
859 * ChachaPoly has all 12 bytes implicit
860 */
Paul Bakker68884e32013-01-07 18:20:04 +0100861 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200862 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
863 transform->fixed_ivlen = 12;
864 else
865 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 /* Minimum length of encrypted record */
868 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000869 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100870 }
871 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000872#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
873#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
874 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
875 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100876 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
879 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200882 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100883 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200885 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000886 mac_key_len = mbedtls_md_get_size( md_info );
887 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890 /*
891 * If HMAC is to be truncated, we shall keep the leftmost bytes,
892 * (rfc 6066 page 13 or rfc 2104 section 4),
893 * so we only need to adjust the length here.
894 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200895 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000898
899#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
900 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000901 * HMAC implementation which also truncates the key
902 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000903 mac_key_len = transform->maclen;
904#endif
905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200907
908 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100909 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200911 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200913 transform->minlen = transform->maclen;
914 else
Paul Bakker68884e32013-01-07 18:20:04 +0100915 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200916 /*
917 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100918 * 1. if EtM is in use: one block plus MAC
919 * otherwise: * first multiple of blocklen greater than maclen
920 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200923 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100924 {
925 transform->minlen = transform->maclen
926 + cipher_info->block_size;
927 }
928 else
929#endif
930 {
931 transform->minlen = transform->maclen
932 + cipher_info->block_size
933 - transform->maclen % cipher_info->block_size;
934 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200937 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
938 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200939 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100940 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200943 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
944 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 {
946 transform->minlen += transform->ivlen;
947 }
948 else
949#endif
950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200953 }
Paul Bakker68884e32013-01-07 18:20:04 +0100954 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000956 else
957#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
958 {
959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
961 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Hanno Beckere7f2df02017-12-27 08:17:40 +0000963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
964 (unsigned) keylen,
965 (unsigned) transform->minlen,
966 (unsigned) transform->ivlen,
967 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
969 /*
970 * Finally setup the cipher contexts, IVs and MAC secrets.
971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200973 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000975 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000976 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Paul Bakker68884e32013-01-07 18:20:04 +0100978 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000979 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000981 /*
982 * This is not used in TLS v1.1.
983 */
Paul Bakker48916f92012-09-16 19:57:18 +0000984 iv_copy_len = ( transform->fixed_ivlen ) ?
985 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000986 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
987 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000988 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 }
990 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_SSL_CLI_C */
992#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200993 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000995 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000996 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100999 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001001 /*
1002 * This is not used in TLS v1.1.
1003 */
Paul Bakker48916f92012-09-16 19:57:18 +00001004 iv_copy_len = ( transform->fixed_ivlen ) ?
1005 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001006 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1007 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1014 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Hanno Becker92231322018-01-03 15:32:51 +00001017#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001019 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001020 {
Hanno Becker92231322018-01-03 15:32:51 +00001021 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001025 }
1026
Hanno Becker81c7b182017-11-09 18:39:33 +00001027 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1028 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
1030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1032#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1033 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001034 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001035 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001036 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1037 For AEAD-based ciphersuites, there is nothing to do here. */
1038 if( mac_key_len != 0 )
1039 {
1040 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1041 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1042 }
Paul Bakker68884e32013-01-07 18:20:04 +01001043 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001044 else
1045#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001049 }
Hanno Becker92231322018-01-03 15:32:51 +00001050#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1053 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001054 {
1055 int ret = 0;
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001058
Hanno Beckere7f2df02017-12-27 08:17:40 +00001059 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001060 transform->iv_enc, transform->iv_dec,
1061 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001062 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001063 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1066 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001067 }
1068 }
Hanno Becker92231322018-01-03 15:32:51 +00001069#else
1070 ((void) mac_dec);
1071 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001073
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001074#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1075 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001076 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001077 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001078 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001079 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001080 iv_copy_len );
1081 }
1082#endif
1083
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001084 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001085 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001088 return( ret );
1089 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001090
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001091 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001092 cipher_info ) ) != 0 )
1093 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095 return( ret );
1096 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001099 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001103 return( ret );
1104 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001107 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001111 return( ret );
1112 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_CIPHER_MODE_CBC)
1115 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1118 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001122 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1125 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128 return( ret );
1129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001133 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001135 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001137 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001140
Paul Bakker48916f92012-09-16 19:57:18 +00001141 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1142 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001143
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001144 if( deflateInit( &transform->ctx_deflate,
1145 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001146 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1149 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001150 }
1151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001153
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( 0 );
1155}
1156
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001157/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001158 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001159 *
1160 * Inputs:
1161 * - SSL/TLS minor version
1162 * - hash associated with the ciphersuite (only used by TLS 1.2)
1163 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001164 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001165 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1166 */
1167static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1168 int minor_ver,
1169 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001170{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001171#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1172 (void) hash;
1173#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001174
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001175#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001176 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001177 {
1178 handshake->tls_prf = ssl3_prf;
1179 handshake->calc_verify = ssl_calc_verify_ssl;
1180 handshake->calc_finished = ssl_calc_finished_ssl;
1181 }
1182 else
1183#endif
1184#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001185 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001186 {
1187 handshake->tls_prf = tls1_prf;
1188 handshake->calc_verify = ssl_calc_verify_tls;
1189 handshake->calc_finished = ssl_calc_finished_tls;
1190 }
1191 else
1192#endif
1193#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1194#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1196 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001197 {
1198 handshake->tls_prf = tls_prf_sha384;
1199 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1200 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1201 }
1202 else
1203#endif
1204#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001206 {
1207 handshake->tls_prf = tls_prf_sha256;
1208 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1209 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1210 }
1211 else
1212#endif
1213#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1214 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1216 }
1217
1218 return( 0 );
1219}
1220
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001221/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001222 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001223 *
1224 * Parameters:
1225 * [in/out] handshake
1226 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1227 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001228 * [out] master
1229 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001230 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001231static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001232 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001233 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001234{
1235 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001236
1237#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001238 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001239 (void) ssl;
1240#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001241
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001242 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001243 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1245 return( 0 );
1246 }
1247
1248 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1249 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001250
1251#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001253 {
1254 unsigned char session_hash[48];
1255 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001256
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001257 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001258
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001259 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1260 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001261
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001262 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263 "extended master secret",
1264 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001265 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001266 }
1267 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001269 {
1270 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1271 "master secret",
1272 handshake->randbytes, 64,
1273 master, 48 );
1274 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001275 if( ret != 0 )
1276 {
1277 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1278 return( ret );
1279 }
1280
1281 mbedtls_platform_zeroize( handshake->premaster,
1282 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283
1284 return( 0 );
1285}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001286
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001287int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1288{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001289 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001290 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1291 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001292
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1294
1295 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001296 ret = ssl_set_handshake_prfs( ssl->handshake,
1297 ssl->minor_ver,
1298 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001299 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001300 {
1301 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001302 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001303 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001304
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001305 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001306 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001307 ssl->session_negotiate->master,
1308 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001309 if( ret != 0 )
1310 {
1311 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1312 return( ret );
1313 }
1314
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001315 /* Swap the client and server random values:
1316 * - MS derivation wanted client+server (RFC 5246 8.1)
1317 * - key derivation wants server+client (RFC 5246 6.3) */
1318 {
1319 unsigned char tmp[64];
1320 memcpy( tmp, ssl->handshake->randbytes, 64 );
1321 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1322 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1323 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1324 }
1325
1326 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001327 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001328 ssl->session_negotiate->ciphersuite,
1329 ssl->session_negotiate->master,
1330#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1331 ssl->session_negotiate->encrypt_then_mac,
1332#endif
1333#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1334 ssl->session_negotiate->trunc_hmac,
1335#endif
1336#if defined(MBEDTLS_ZLIB_SUPPORT)
1337 ssl->session_negotiate->compression,
1338#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 ssl->handshake->tls_prf,
1340 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001341 ssl->minor_ver,
1342 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001343 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001344 if( ret != 0 )
1345 {
1346 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1347 return( ret );
1348 }
1349
1350 /* We no longer need Server/ClientHello.random values */
1351 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1352 sizeof( ssl->handshake->randbytes ) );
1353
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001354 /* Allocate compression buffer */
1355#if defined(MBEDTLS_ZLIB_SUPPORT)
1356 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1357 ssl->compress_buf == NULL )
1358 {
1359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1360 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1361 if( ssl->compress_buf == NULL )
1362 {
1363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001364 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001365 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1366 }
1367 }
1368#endif
1369
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1371
1372 return( 0 );
1373}
1374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001376void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1377 unsigned char hash[36],
1378 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001380 mbedtls_md5_context md5;
1381 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 unsigned char pad_1[48];
1383 unsigned char pad_2[48];
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001387 mbedtls_md5_init( &md5 );
1388 mbedtls_sha1_init( &sha1 );
1389
1390 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1391 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Paul Bakker380da532012-04-18 16:10:25 +00001393 memset( pad_1, 0x36, 48 );
1394 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001396 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1397 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1398 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_md5_starts_ret( &md5 );
1401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1403 mbedtls_md5_update_ret( &md5, hash, 16 );
1404 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001406 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1407 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1408 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001410 mbedtls_sha1_starts_ret( &sha1 );
1411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1413 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1414 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001415
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001416 *hlen = 36;
1417
1418 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001421 mbedtls_md5_free( &md5 );
1422 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001423
Paul Bakker380da532012-04-18 16:10:25 +00001424 return;
1425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001429void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1430 unsigned char hash[36],
1431 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001432{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001433 mbedtls_md5_context md5;
1434 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001438 mbedtls_md5_init( &md5 );
1439 mbedtls_sha1_init( &sha1 );
1440
1441 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1442 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001444 mbedtls_md5_finish_ret( &md5, hash );
1445 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001447 *hlen = 36;
1448
1449 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001452 mbedtls_md5_free( &md5 );
1453 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001454
Paul Bakker380da532012-04-18 16:10:25 +00001455 return;
1456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1460#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001461void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1462 unsigned char hash[32],
1463 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001464{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001465 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001467 mbedtls_sha256_init( &sha256 );
1468
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001470
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001471 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001472 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001474 *hlen = 32;
1475
1476 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001479 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001480
Paul Bakker380da532012-04-18 16:10:25 +00001481 return;
1482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001486void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1487 unsigned char hash[48],
1488 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001489{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001490 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_sha512_init( &sha512 );
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001496 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001497 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001499 *hlen = 48;
1500
1501 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001504 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001505
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 return;
1507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SHA512_C */
1509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1512int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001513{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001514 unsigned char *p = ssl->handshake->premaster;
1515 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001516 const unsigned char *psk = ssl->conf->psk;
1517 size_t psk_len = ssl->conf->psk_len;
1518
1519 /* If the psk callback was called, use its result */
1520 if( ssl->handshake->psk != NULL )
1521 {
1522 psk = ssl->handshake->psk;
1523 psk_len = ssl->handshake->psk_len;
1524 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001525
1526 /*
1527 * PMS = struct {
1528 * opaque other_secret<0..2^16-1>;
1529 * opaque psk<0..2^16-1>;
1530 * };
1531 * with "other_secret" depending on the particular key exchange
1532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1534 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001535 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001536 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001538
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001539 *(p++) = (unsigned char)( psk_len >> 8 );
1540 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001541
1542 if( end < p || (size_t)( end - p ) < psk_len )
1543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1544
1545 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001546 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547 }
1548 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1550#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1551 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001552 {
1553 /*
1554 * other_secret already set by the ClientKeyExchange message,
1555 * and is 48 bytes long
1556 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001557 if( end - p < 2 )
1558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1559
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001560 *p++ = 0;
1561 *p++ = 48;
1562 p += 48;
1563 }
1564 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1566#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1567 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001569 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001570 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001571
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001572 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001574 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001575 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001578 return( ret );
1579 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001580 *(p++) = (unsigned char)( len >> 8 );
1581 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001582 p += len;
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001585 }
1586 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1588#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1589 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001591 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592 size_t zlen;
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001595 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599 return( ret );
1600 }
1601
1602 *(p++) = (unsigned char)( zlen >> 8 );
1603 *(p++) = (unsigned char)( zlen );
1604 p += zlen;
1605
Janos Follath3fbdada2018-08-15 10:26:53 +01001606 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1607 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608 }
1609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001614 }
1615
1616 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001617 if( end - p < 2 )
1618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001619
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001620 *(p++) = (unsigned char)( psk_len >> 8 );
1621 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001622
1623 if( end < p || (size_t)( end - p ) < psk_len )
1624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1625
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001626 memcpy( p, psk, psk_len );
1627 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628
1629 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1630
1631 return( 0 );
1632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001636/*
1637 * SSLv3.0 MAC functions
1638 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001639#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001640static void ssl_mac( mbedtls_md_context_t *md_ctx,
1641 const unsigned char *secret,
1642 const unsigned char *buf, size_t len,
1643 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001644 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001645{
1646 unsigned char header[11];
1647 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001648 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1650 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001651
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001652 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001654 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001655 else
Paul Bakker68884e32013-01-07 18:20:04 +01001656 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
1658 memcpy( header, ctr, 8 );
1659 header[ 8] = (unsigned char) type;
1660 header[ 9] = (unsigned char)( len >> 8 );
1661 header[10] = (unsigned char)( len );
1662
Paul Bakker68884e32013-01-07 18:20:04 +01001663 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_md_starts( md_ctx );
1665 mbedtls_md_update( md_ctx, secret, md_size );
1666 mbedtls_md_update( md_ctx, padding, padlen );
1667 mbedtls_md_update( md_ctx, header, 11 );
1668 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001669 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Paul Bakker68884e32013-01-07 18:20:04 +01001671 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 mbedtls_md_starts( md_ctx );
1673 mbedtls_md_update( md_ctx, secret, md_size );
1674 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001675 mbedtls_md_update( md_ctx, out, md_size );
1676 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001679
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001680/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001681 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001682#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001683 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1684 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1685 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1686/* This function makes sure every byte in the memory region is accessed
1687 * (in ascending addresses order) */
1688static void ssl_read_memory( unsigned char *p, size_t len )
1689{
1690 unsigned char acc = 0;
1691 volatile unsigned char force;
1692
1693 for( ; len != 0; p++, len-- )
1694 acc ^= *p;
1695
1696 force = acc;
1697 (void) force;
1698}
1699#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1700
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001701/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001703 */
Hanno Becker3307b532017-12-27 21:37:21 +00001704
Hanno Beckera5a2b082019-05-15 14:03:01 +01001705#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001706/* This functions transforms a DTLS plaintext fragment and a record content
1707 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001708 *
1709 * struct {
1710 * opaque content[DTLSPlaintext.length];
1711 * ContentType real_type;
1712 * uint8 zeros[length_of_padding];
1713 * } DTLSInnerPlaintext;
1714 *
1715 * Input:
1716 * - `content`: The beginning of the buffer holding the
1717 * plaintext to be wrapped.
1718 * - `*content_size`: The length of the plaintext in Bytes.
1719 * - `max_len`: The number of Bytes available starting from
1720 * `content`. This must be `>= *content_size`.
1721 * - `rec_type`: The desired record content type.
1722 *
1723 * Output:
1724 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1725 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1726 *
1727 * Returns:
1728 * - `0` on success.
1729 * - A negative error code if `max_len` didn't offer enough space
1730 * for the expansion.
1731 */
1732static int ssl_cid_build_inner_plaintext( unsigned char *content,
1733 size_t *content_size,
1734 size_t remaining,
1735 uint8_t rec_type )
1736{
1737 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001738 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1739 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1740 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001741
1742 /* Write real content type */
1743 if( remaining == 0 )
1744 return( -1 );
1745 content[ len ] = rec_type;
1746 len++;
1747 remaining--;
1748
1749 if( remaining < pad )
1750 return( -1 );
1751 memset( content + len, 0, pad );
1752 len += pad;
1753 remaining -= pad;
1754
1755 *content_size = len;
1756 return( 0 );
1757}
1758
Hanno Becker7dc25772019-05-20 15:08:01 +01001759/* This function parses a DTLSInnerPlaintext structure.
1760 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001761static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1762 size_t *content_size,
1763 uint8_t *rec_type )
1764{
1765 size_t remaining = *content_size;
1766
1767 /* Determine length of padding by skipping zeroes from the back. */
1768 do
1769 {
1770 if( remaining == 0 )
1771 return( -1 );
1772 remaining--;
1773 } while( content[ remaining ] == 0 );
1774
1775 *content_size = remaining;
1776 *rec_type = content[ remaining ];
1777
1778 return( 0 );
1779}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001780#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001781
Hanno Becker99abf512019-05-20 14:50:53 +01001782/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001783 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001784static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001785 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001786 mbedtls_record *rec )
1787{
Hanno Becker99abf512019-05-20 14:50:53 +01001788 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001789 *
1790 * additional_data = seq_num + TLSCompressed.type +
1791 * TLSCompressed.version + TLSCompressed.length;
1792 *
Hanno Becker99abf512019-05-20 14:50:53 +01001793 * For the CID extension, this is extended as follows
1794 * (quoting draft-ietf-tls-dtls-connection-id-05,
1795 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001796 *
1797 * additional_data = seq_num + DTLSPlaintext.type +
1798 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001799 * cid +
1800 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001801 * length_of_DTLSInnerPlaintext;
1802 */
1803
Hanno Becker3307b532017-12-27 21:37:21 +00001804 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1805 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001806 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001807
Hanno Beckera5a2b082019-05-15 14:03:01 +01001808#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001809 if( rec->cid_len != 0 )
1810 {
1811 memcpy( add_data + 11, rec->cid, rec->cid_len );
1812 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1813 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1814 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1815 *add_data_len = 13 + 1 + rec->cid_len;
1816 }
1817 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001818#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001819 {
1820 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1821 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1822 *add_data_len = 13;
1823 }
Hanno Becker3307b532017-12-27 21:37:21 +00001824}
1825
Hanno Becker611a83b2018-01-03 14:27:32 +00001826int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1827 mbedtls_ssl_transform *transform,
1828 mbedtls_record *rec,
1829 int (*f_rng)(void *, unsigned char *, size_t),
1830 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001833 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001834 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001835 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001836 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001837 size_t post_avail;
1838
1839 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001840#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001841 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001842 ((void) ssl);
1843#endif
1844
1845 /* The PRNG is used for dynamic IV generation that's used
1846 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1847#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1848 ( defined(MBEDTLS_AES_C) || \
1849 defined(MBEDTLS_ARIA_C) || \
1850 defined(MBEDTLS_CAMELLIA_C) ) && \
1851 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1852 ((void) f_rng);
1853 ((void) p_rng);
1854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Hanno Becker3307b532017-12-27 21:37:21 +00001858 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859 {
Hanno Becker3307b532017-12-27 21:37:21 +00001860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1861 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1862 }
Hanno Becker505089d2019-05-01 09:45:57 +01001863 if( rec == NULL
1864 || rec->buf == NULL
1865 || rec->buf_len < rec->data_offset
1866 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001867#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001868 || rec->cid_len != 0
1869#endif
1870 )
Hanno Becker3307b532017-12-27 21:37:21 +00001871 {
1872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001874 }
1875
Hanno Becker3307b532017-12-27 21:37:21 +00001876 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001877 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001879 data, rec->data_len );
1880
1881 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1882
1883 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1884 {
1885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1886 (unsigned) rec->data_len,
1887 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1889 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001890
Hanno Beckera5a2b082019-05-15 14:03:01 +01001891#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001892 /*
1893 * Add CID information
1894 */
1895 rec->cid_len = transform->out_cid_len;
1896 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1897 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001898
1899 if( rec->cid_len != 0 )
1900 {
1901 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001902 * Wrap plaintext into DTLSInnerPlaintext structure.
1903 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001904 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001905 * Note that this changes `rec->data_len`, and hence
1906 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001907 */
1908 if( ssl_cid_build_inner_plaintext( data,
1909 &rec->data_len,
1910 post_avail,
1911 rec->type ) != 0 )
1912 {
1913 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1914 }
1915
1916 rec->type = MBEDTLS_SSL_MSG_CID;
1917 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001918#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001919
Hanno Becker92c930f2019-04-29 17:31:37 +01001920 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1921
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001923 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001925#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 if( mode == MBEDTLS_MODE_STREAM ||
1927 ( mode == MBEDTLS_MODE_CBC
1928#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001929 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001930#endif
1931 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 {
Hanno Becker3307b532017-12-27 21:37:21 +00001933 if( post_avail < transform->maclen )
1934 {
1935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1936 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1937 }
1938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001940 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001941 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001942 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001943 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1944 data, rec->data_len, rec->ctr, rec->type, mac );
1945 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001946 }
1947 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1950 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001951 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001952 {
Hanno Becker992b6872017-11-09 18:57:39 +00001953 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1954
Hanno Beckere83efe62019-04-29 13:52:53 +01001955 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001956
Hanno Becker3307b532017-12-27 21:37:21 +00001957 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001958 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001959 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1960 data, rec->data_len );
1961 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1962 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1963
1964 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001965 }
1966 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001967#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001971 }
1972
Hanno Becker3307b532017-12-27 21:37:21 +00001973 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1974 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001975
Hanno Becker3307b532017-12-27 21:37:21 +00001976 rec->data_len += transform->maclen;
1977 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001978 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001979 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001980#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001982 /*
1983 * Encrypt
1984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1986 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001988 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001989 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001991 "including %d bytes of padding",
1992 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
Hanno Becker3307b532017-12-27 21:37:21 +00001994 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1995 transform->iv_enc, transform->ivlen,
1996 data, rec->data_len,
1997 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002000 return( ret );
2001 }
2002
Hanno Becker3307b532017-12-27 21:37:21 +00002003 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
Paul Bakker68884e32013-01-07 18:20:04 +01002009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002011
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002012#if defined(MBEDTLS_GCM_C) || \
2013 defined(MBEDTLS_CCM_C) || \
2014 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002016 mode == MBEDTLS_MODE_CCM ||
2017 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002018 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002019 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002021 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 /* Check that there's space for both the authentication tag
2024 * and the explicit IV before and after the record content. */
2025 if( post_avail < transform->taglen ||
2026 rec->data_offset < explicit_iv_len )
2027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2029 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2030 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002031
Paul Bakker68884e32013-01-07 18:20:04 +01002032 /*
2033 * Generate IV
2034 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002035 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2036 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002037 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002038 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002039 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2040 explicit_iv_len );
2041 /* Prefix record content with explicit IV. */
2042 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 }
2044 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2045 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002046 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002047 unsigned char i;
2048
2049 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2050
2051 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002052 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002053 }
2054 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002055 {
2056 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002059 }
2060
Hanno Beckere83efe62019-04-29 13:52:53 +01002061 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002062
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2064 iv, transform->ivlen );
2065 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002066 data - explicit_iv_len, explicit_iv_len );
2067 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002068 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002070 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002071 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002072
Paul Bakker68884e32013-01-07 18:20:04 +01002073 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002074 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002075 */
Hanno Becker3307b532017-12-27 21:37:21 +00002076
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002077 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002078 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002079 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002080 data, rec->data_len, /* source */
2081 data, &rec->data_len, /* destination */
2082 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002085 return( ret );
2086 }
2087
Hanno Becker3307b532017-12-27 21:37:21 +00002088 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2089 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002090
Hanno Becker3307b532017-12-27 21:37:21 +00002091 rec->data_len += transform->taglen + explicit_iv_len;
2092 rec->data_offset -= explicit_iv_len;
2093 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002094 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2098#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002099 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002102 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002103 size_t padlen, i;
2104 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002105
Hanno Becker3307b532017-12-27 21:37:21 +00002106 /* Currently we're always using minimal padding
2107 * (up to 255 bytes would be allowed). */
2108 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2109 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 padlen = 0;
2111
Hanno Becker3307b532017-12-27 21:37:21 +00002112 /* Check there's enough space in the buffer for the padding. */
2113 if( post_avail < padlen + 1 )
2114 {
2115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2116 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2117 }
2118
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002120 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Hanno Becker3307b532017-12-27 21:37:21 +00002122 rec->data_len += padlen + 1;
2123 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002127 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2128 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002129 */
Hanno Becker3307b532017-12-27 21:37:21 +00002130 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 {
Hanno Becker3307b532017-12-27 21:37:21 +00002132 if( f_rng == NULL )
2133 {
2134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2136 }
2137
2138 if( rec->data_offset < transform->ivlen )
2139 {
2140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2142 }
2143
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144 /*
2145 * Generate IV
2146 */
Hanno Becker3307b532017-12-27 21:37:21 +00002147 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002148 if( ret != 0 )
2149 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150
Hanno Becker3307b532017-12-27 21:37:21 +00002151 memcpy( data - transform->ivlen, transform->iv_enc,
2152 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002159 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002160 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Hanno Becker3307b532017-12-27 21:37:21 +00002162 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2163 transform->iv_enc,
2164 transform->ivlen,
2165 data, rec->data_len,
2166 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002169 return( ret );
2170 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002171
Hanno Becker3307b532017-12-27 21:37:21 +00002172 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002176 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002179 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002180 {
2181 /*
2182 * Save IV in SSL3 and TLS1
2183 */
Hanno Becker3307b532017-12-27 21:37:21 +00002184 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2185 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 }
Hanno Becker3307b532017-12-27 21:37:21 +00002187 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002188#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002189 {
2190 data -= transform->ivlen;
2191 rec->data_offset -= transform->ivlen;
2192 rec->data_len += transform->ivlen;
2193 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002196 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002197 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002198 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2199
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002200 /*
2201 * MAC(MAC_write_key, seq_num +
2202 * TLSCipherText.type +
2203 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002204 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002205 * IV + // except for TLS 1.0
2206 * ENC(content + padding + padding_length));
2207 */
Hanno Becker3307b532017-12-27 21:37:21 +00002208
2209 if( post_avail < transform->maclen)
2210 {
2211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2212 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2213 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002214
Hanno Beckere83efe62019-04-29 13:52:53 +01002215 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002218 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002219 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002220
Hanno Becker3307b532017-12-27 21:37:21 +00002221 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002222 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002223 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2224 data, rec->data_len );
2225 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2226 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002227
Hanno Becker3307b532017-12-27 21:37:21 +00002228 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002229
Hanno Becker3307b532017-12-27 21:37:21 +00002230 rec->data_len += transform->maclen;
2231 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002232 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002238 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002244 /* Make extra sure authentication was performed, exactly once */
2245 if( auth_done != 1 )
2246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249 }
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
2253 return( 0 );
2254}
2255
Hanno Becker611a83b2018-01-03 14:27:32 +00002256int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2257 mbedtls_ssl_transform *transform,
2258 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002260 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002262 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002263#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002264 size_t padlen = 0, correct = 1;
2265#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002266 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002267 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002268 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002269
Hanno Becker611a83b2018-01-03 14:27:32 +00002270#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002271 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002272 ((void) ssl);
2273#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002276 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002277 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2280 }
2281 if( rec == NULL ||
2282 rec->buf == NULL ||
2283 rec->buf_len < rec->data_offset ||
2284 rec->buf_len - rec->data_offset < rec->data_len )
2285 {
2286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002288 }
2289
Hanno Becker4c6876b2017-12-27 21:28:58 +00002290 data = rec->buf + rec->data_offset;
2291 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Hanno Beckera5a2b082019-05-15 14:03:01 +01002293#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002294 /*
2295 * Match record's CID with incoming CID.
2296 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002297 if( rec->cid_len != transform->in_cid_len ||
2298 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2299 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002300 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002301 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002302#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2305 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002306 {
2307 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002308 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2309 transform->iv_dec,
2310 transform->ivlen,
2311 data, rec->data_len,
2312 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002315 return( ret );
2316 }
2317
Hanno Becker4c6876b2017-12-27 21:28:58 +00002318 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 }
Paul Bakker68884e32013-01-07 18:20:04 +01002324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002326#if defined(MBEDTLS_GCM_C) || \
2327 defined(MBEDTLS_CCM_C) || \
2328 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330 mode == MBEDTLS_MODE_CCM ||
2331 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002332 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002333 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002335
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002336 /*
2337 * Compute and update sizes
2338 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002342 "+ taglen (%d)", rec->data_len,
2343 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002345 }
Paul Bakker68884e32013-01-07 18:20:04 +01002346
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 /*
2348 * Prepare IV
2349 */
2350 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2351 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002352 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002354 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 }
2357 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2358 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002359 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 unsigned char i;
2361
2362 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2363
2364 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002365 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002366 }
2367 else
2368 {
2369 /* Reminder if we ever add an AEAD mode with a different size */
2370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2372 }
2373
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 data += explicit_iv_len;
2375 rec->data_offset += explicit_iv_len;
2376 rec->data_len -= explicit_iv_len + transform->taglen;
2377
Hanno Beckere83efe62019-04-29 13:52:53 +01002378 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002380 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002381
2382 memcpy( transform->iv_dec + transform->fixed_ivlen,
2383 data - explicit_iv_len, explicit_iv_len );
2384
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002385 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002387 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002388
Paul Bakker68884e32013-01-07 18:20:04 +01002389
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002390 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002391 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002392 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002393 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2394 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002395 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002396 data, rec->data_len,
2397 data, &olen,
2398 data + rec->data_len,
2399 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2404 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002405
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002406 return( ret );
2407 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002408 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002409
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002414 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2418#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002419 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002422 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 /*
Paul Bakker45829992013-01-03 14:52:21 +01002425 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002428 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2429 {
2430 /* The ciphertext is prefixed with the CBC IV. */
2431 minlen += transform->ivlen;
2432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#endif
Paul Bakker45829992013-01-03 14:52:21 +01002434
Hanno Becker4c6876b2017-12-27 21:28:58 +00002435 /* Size considerations:
2436 *
2437 * - The CBC cipher text must not be empty and hence
2438 * at least of size transform->ivlen.
2439 *
2440 * Together with the potential IV-prefix, this explains
2441 * the first of the two checks below.
2442 *
2443 * - The record must contain a MAC, either in plain or
2444 * encrypted, depending on whether Encrypt-then-MAC
2445 * is used or not.
2446 * - If it is, the message contains the IV-prefix,
2447 * the CBC ciphertext, and the MAC.
2448 * - If it is not, the padded plaintext, and hence
2449 * the CBC ciphertext, has at least length maclen + 1
2450 * because there is at least the padding length byte.
2451 *
2452 * As the CBC ciphertext is not empty, both cases give the
2453 * lower bound minlen + maclen + 1 on the record size, which
2454 * we test for in the second check below.
2455 */
2456 if( rec->data_len < minlen + transform->ivlen ||
2457 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002460 "+ 1 ) ( + expl IV )", rec->data_len,
2461 transform->ivlen,
2462 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002464 }
2465
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002466 /*
2467 * Authenticate before decrypt if enabled
2468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002470 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 {
Hanno Becker992b6872017-11-09 18:57:39 +00002472 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475
Hanno Becker4c6876b2017-12-27 21:28:58 +00002476 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2477 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Hanno Beckere83efe62019-04-29 13:52:53 +01002479 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002480
Hanno Beckere83efe62019-04-29 13:52:53 +01002481 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2482 add_data_len );
2483 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2484 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002485 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2486 data, rec->data_len );
2487 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2488 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002489
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2491 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002492 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002493 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2496 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002501 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
2505 /*
2506 * Check length sanity
2507 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002508 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002513 }
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002516 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002517 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002518 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002520 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002521 /* This is safe because data_len >= minlen + maclen + 1 initially,
2522 * and at this point we have at most subtracted maclen (note that
2523 * minlen == transform->ivlen here). */
2524 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525
Hanno Becker4c6876b2017-12-27 21:28:58 +00002526 data += transform->ivlen;
2527 rec->data_offset += transform->ivlen;
2528 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002531
Hanno Becker4c6876b2017-12-27 21:28:58 +00002532 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2533 transform->iv_dec, transform->ivlen,
2534 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002537 return( ret );
2538 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002539
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002544 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002548 {
2549 /*
2550 * Save IV in SSL3 and TLS1
2551 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2553 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002555#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 /* Safe since data_len >= minlen + maclen + 1, so after having
2558 * subtracted at most minlen and maclen up to this point,
2559 * data_len > 0. */
2560 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002561
Hanno Becker4c6876b2017-12-27 21:28:58 +00002562 if( auth_done == 1 )
2563 {
2564 correct *= ( rec->data_len >= padlen + 1 );
2565 padlen *= ( rec->data_len >= padlen + 1 );
2566 }
2567 else
Paul Bakker45829992013-01-03 14:52:21 +01002568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002570 if( rec->data_len < transform->maclen + padlen + 1 )
2571 {
2572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2573 rec->data_len,
2574 transform->maclen,
2575 padlen + 1 ) );
2576 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002577#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578
2579 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2580 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 padlen++;
2584
2585 /* Regardless of the validity of the padding,
2586 * we have data_len >= padlen here. */
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_DEBUG_ALL)
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002595 "should be no more than %d",
2596 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002597#endif
Paul Bakker45829992013-01-03 14:52:21 +01002598 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600 }
2601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2603#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2604 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002605 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002607 /* The padding check involves a series of up to 256
2608 * consecutive memory reads at the end of the record
2609 * plaintext buffer. In order to hide the length and
2610 * validity of the padding, always perform exactly
2611 * `min(256,plaintext_len)` reads (but take into account
2612 * only the last `padlen` bytes for the padding check). */
2613 size_t pad_count = 0;
2614 size_t real_count = 0;
2615 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002616
Hanno Becker4c6876b2017-12-27 21:28:58 +00002617 /* Index of first padding byte; it has been ensured above
2618 * that the subtraction is safe. */
2619 size_t const padding_idx = rec->data_len - padlen;
2620 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2621 size_t const start_idx = rec->data_len - num_checks;
2622 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002623
Hanno Becker4c6876b2017-12-27 21:28:58 +00002624 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002625 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002626 real_count |= ( idx >= padding_idx );
2627 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002628 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002629 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002632 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002634#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2639 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002643 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002644
Hanno Becker4c6876b2017-12-27 21:28:58 +00002645 /* If the padding was found to be invalid, padlen == 0
2646 * and the subtraction is safe. If the padding was found valid,
2647 * padlen hasn't been changed and the previous assertion
2648 * data_len >= padlen still holds. */
2649 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002651 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002653 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002659#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002661 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002662#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002665 * Authenticate if not done yet.
2666 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002668#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002669 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002670 {
Hanno Becker992b6872017-11-09 18:57:39 +00002671 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002672
Hanno Becker4c6876b2017-12-27 21:28:58 +00002673 /* If the initial value of padlen was such that
2674 * data_len < maclen + padlen + 1, then padlen
2675 * got reset to 1, and the initial check
2676 * data_len >= minlen + maclen + 1
2677 * guarantees that at this point we still
2678 * have at least data_len >= maclen.
2679 *
2680 * If the initial value of padlen was such that
2681 * data_len >= maclen + padlen + 1, then we have
2682 * subtracted either padlen + 1 (if the padding was correct)
2683 * or 0 (if the padding was incorrect) since then,
2684 * hence data_len >= maclen in any case.
2685 */
2686 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Hanno Beckere83efe62019-04-29 13:52:53 +01002688 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002691 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002692 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 ssl_mac( &transform->md_ctx_dec,
2694 transform->mac_dec,
2695 data, rec->data_len,
2696 rec->ctr, rec->type,
2697 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002698 }
2699 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2701#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2702 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002703 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002704 {
2705 /*
2706 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002707 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002708 *
2709 * Known timing attacks:
2710 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2711 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002712 * To compensate for different timings for the MAC calculation
2713 * depending on how much padding was removed (which is determined
2714 * by padlen), process extra_run more blocks through the hash
2715 * function.
2716 *
2717 * The formula in the paper is
2718 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2719 * where L1 is the size of the header plus the decrypted message
2720 * plus CBC padding and L2 is the size of the header plus the
2721 * decrypted message. This is for an underlying hash function
2722 * with 64-byte blocks.
2723 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2724 * correctly. We round down instead of up, so -56 is the correct
2725 * value for our calculations instead of -55.
2726 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002727 * Repeat the formula rather than defining a block_size variable.
2728 * This avoids requiring division by a variable at runtime
2729 * (which would be marginally less efficient and would require
2730 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002731 */
2732 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002733 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002734
2735 /*
2736 * The next two sizes are the minimum and maximum values of
2737 * in_msglen over all padlen values.
2738 *
2739 * They're independent of padlen, since we previously did
2740 * in_msglen -= padlen.
2741 *
2742 * Note that max_len + maclen is never more than the buffer
2743 * length, as we previously did in_msglen -= maclen too.
2744 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002745 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002746 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2747
Hanno Becker4c6876b2017-12-27 21:28:58 +00002748 memset( tmp, 0, sizeof( tmp ) );
2749
2750 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002751 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002752#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2753 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002754 case MBEDTLS_MD_MD5:
2755 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002757 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002758 extra_run =
2759 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2760 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 break;
2762#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002763#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002764 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002765 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002766 extra_run =
2767 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2768 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 break;
2770#endif
2771 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2774 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002775
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002776 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002777
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2779 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002780 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2781 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002782 /* Make sure we access everything even when padlen > 0. This
2783 * makes the synchronisation requirements for just-in-time
2784 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002785 ssl_read_memory( data + rec->data_len, padlen );
2786 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002787
2788 /* Call mbedtls_md_process at least once due to cache attacks
2789 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002790 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002791 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002792
Hanno Becker4c6876b2017-12-27 21:28:58 +00002793 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002794
2795 /* Make sure we access all the memory that could contain the MAC,
2796 * before we check it in the next code block. This makes the
2797 * synchronisation requirements for just-in-time Prime+Probe
2798 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002799 ssl_read_memory( data + min_len,
2800 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002801 }
2802 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2804 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002810#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2812 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002813#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Hanno Becker4c6876b2017-12-27 21:28:58 +00002815 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2816 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_DEBUG_ALL)
2819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002820#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 correct = 0;
2822 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002823 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002824 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002825
2826 /*
2827 * Finally check the correct flag
2828 */
2829 if( correct == 0 )
2830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002831#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002832
2833 /* Make extra sure authentication was performed, exactly once */
2834 if( auth_done != 1 )
2835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Hanno Beckera5a2b082019-05-15 14:03:01 +01002840#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002841 if( rec->cid_len != 0 )
2842 {
2843 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2844 &rec->type );
2845 if( ret != 0 )
2846 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2847 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002848#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
2852 return( 0 );
2853}
2854
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002855#undef MAC_NONE
2856#undef MAC_PLAINTEXT
2857#undef MAC_CIPHERTEXT
2858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002860/*
2861 * Compression/decompression functions
2862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002864{
2865 int ret;
2866 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002867 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002868 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002869 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002872
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002873 if( len_pre == 0 )
2874 return( 0 );
2875
Paul Bakker2770fbd2012-07-03 13:30:23 +00002876 memcpy( msg_pre, ssl->out_msg, len_pre );
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002879 ssl->out_msglen ) );
2880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002882 ssl->out_msg, ssl->out_msglen );
2883
Paul Bakker48916f92012-09-16 19:57:18 +00002884 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2885 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2886 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002887 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890 if( ret != Z_OK )
2891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2893 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002894 }
2895
Angus Grattond8213d02016-05-25 20:56:48 +10002896 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002897 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900 ssl->out_msglen ) );
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 ssl->out_msg, ssl->out_msglen );
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002906
2907 return( 0 );
2908}
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911{
2912 int ret;
2913 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002914 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002916 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002920 if( len_pre == 0 )
2921 return( 0 );
2922
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 memcpy( msg_pre, ssl->in_msg, len_pre );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926 ssl->in_msglen ) );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->in_msg, ssl->in_msglen );
2930
Paul Bakker48916f92012-09-16 19:57:18 +00002931 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2932 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2933 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002934 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002935 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936
Paul Bakker48916f92012-09-16 19:57:18 +00002937 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938 if( ret != Z_OK )
2939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2941 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002942 }
2943
Angus Grattond8213d02016-05-25 20:56:48 +10002944 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002945 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948 ssl->in_msglen ) );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951 ssl->in_msg, ssl->in_msglen );
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002954
2955 return( 0 );
2956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2960static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_SSL_PROTO_DTLS)
2963static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002964{
2965 /* If renegotiation is not enforced, retransmit until we would reach max
2966 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002967 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002968 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002969 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002970 unsigned char doublings = 1;
2971
2972 while( ratio != 0 )
2973 {
2974 ++doublings;
2975 ratio >>= 1;
2976 }
2977
2978 if( ++ssl->renego_records_seen > doublings )
2979 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981 return( 0 );
2982 }
2983 }
2984
2985 return( ssl_write_hello_request( ssl ) );
2986}
2987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002989
Paul Bakker5121ce52009-01-03 21:22:43 +00002990/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002991 * Fill the input message buffer by appending data to it.
2992 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002993 *
2994 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2995 * available (from this read and/or a previous one). Otherwise, an error code
2996 * is returned (possibly EOF or WANT_READ).
2997 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002998 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2999 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3000 * since we always read a whole datagram at once.
3001 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003002 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003003 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003006{
Paul Bakker23986e52011-04-24 08:57:21 +00003007 int ret;
3008 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003012 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003015 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 }
3018
Angus Grattond8213d02016-05-25 20:56:48 +10003019 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003023 }
3024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003028 uint32_t timeout;
3029
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003030 /* Just to be sure */
3031 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3032 {
3033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3034 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3036 }
3037
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003038 /*
3039 * The point is, we need to always read a full datagram at once, so we
3040 * sometimes read more then requested, and handle the additional data.
3041 * It could be the rest of the current record (while fetching the
3042 * header) and/or some other records in the same datagram.
3043 */
3044
3045 /*
3046 * Move to the next record in the already read datagram if applicable
3047 */
3048 if( ssl->next_record_offset != 0 )
3049 {
3050 if( ssl->in_left < ssl->next_record_offset )
3051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003054 }
3055
3056 ssl->in_left -= ssl->next_record_offset;
3057
3058 if( ssl->in_left != 0 )
3059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003061 ssl->next_record_offset ) );
3062 memmove( ssl->in_hdr,
3063 ssl->in_hdr + ssl->next_record_offset,
3064 ssl->in_left );
3065 }
3066
3067 ssl->next_record_offset = 0;
3068 }
3069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003071 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003072
3073 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003075 */
3076 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003079 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003080 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003081
3082 /*
3083 * A record can't be split accross datagrams. If we need to read but
3084 * are not at the beginning of a new record, the caller did something
3085 * wrong.
3086 */
3087 if( ssl->in_left != 0 )
3088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003091 }
3092
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003093 /*
3094 * Don't even try to read if time's out already.
3095 * This avoids by-passing the timer when repeatedly receiving messages
3096 * that will end up being dropped.
3097 */
3098 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003099 {
3100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003101 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003102 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003103 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003104 {
Angus Grattond8213d02016-05-25 20:56:48 +10003105 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003108 timeout = ssl->handshake->retransmit_timeout;
3109 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003110 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003114 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003115 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3116 timeout );
3117 else
3118 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003121
3122 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003124 }
3125
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003126 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003132 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003133 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003136 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003137 }
3138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 return( ret );
3143 }
3144
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003145 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003148 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003150 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003151 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003154 return( ret );
3155 }
3156
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003157 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003160 }
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 if( ret < 0 )
3163 return( ret );
3164
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003165 ssl->in_left = ret;
3166 }
3167 else
3168#endif
3169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003171 ssl->in_left, nb_want ) );
3172
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003173 while( ssl->in_left < nb_want )
3174 {
3175 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003176
3177 if( ssl_check_timer( ssl ) != 0 )
3178 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3179 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003180 {
3181 if( ssl->f_recv_timeout != NULL )
3182 {
3183 ret = ssl->f_recv_timeout( ssl->p_bio,
3184 ssl->in_hdr + ssl->in_left, len,
3185 ssl->conf->read_timeout );
3186 }
3187 else
3188 {
3189 ret = ssl->f_recv( ssl->p_bio,
3190 ssl->in_hdr + ssl->in_left, len );
3191 }
3192 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003195 ssl->in_left, nb_want ) );
3196 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003197
3198 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003200
3201 if( ret < 0 )
3202 return( ret );
3203
mohammad160352aecb92018-03-28 23:41:40 -07003204 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003205 {
Darryl Green11999bb2018-03-13 15:22:58 +00003206 MBEDTLS_SSL_DEBUG_MSG( 1,
3207 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003208 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3210 }
3211
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003212 ssl->in_left += ret;
3213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 }
3215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
3218 return( 0 );
3219}
3220
3221/*
3222 * Flush any data not yet written
3223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003225{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003226 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003227 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003231 if( ssl->f_send == NULL )
3232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003234 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003236 }
3237
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003238 /* Avoid incrementing counter if data is flushed */
3239 if( ssl->out_left == 0 )
3240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003242 return( 0 );
3243 }
3244
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 while( ssl->out_left > 0 )
3246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003248 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
Hanno Becker2b1e3542018-08-06 11:19:13 +01003250 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003251 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
3255 if( ret <= 0 )
3256 return( ret );
3257
mohammad160352aecb92018-03-28 23:41:40 -07003258 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003259 {
Darryl Green11999bb2018-03-13 15:22:58 +00003260 MBEDTLS_SSL_DEBUG_MSG( 1,
3261 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003262 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3264 }
3265
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 ssl->out_left -= ret;
3267 }
3268
Hanno Becker2b1e3542018-08-06 11:19:13 +01003269#if defined(MBEDTLS_SSL_PROTO_DTLS)
3270 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003271 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003272 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003273 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003274 else
3275#endif
3276 {
3277 ssl->out_hdr = ssl->out_buf + 8;
3278 }
3279 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
3283 return( 0 );
3284}
3285
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003286/*
3287 * Functions to handle the DTLS retransmission state machine
3288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003290/*
3291 * Append current handshake message to current outgoing flight
3292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3297 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3298 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003299
3300 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003301 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003302 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003306 }
3307
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003308 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003312 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003313 }
3314
3315 /* Copy current handshake message with headers */
3316 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3317 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003318 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319 msg->next = NULL;
3320
3321 /* Append to the current flight */
3322 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003323 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324 else
3325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327 while( cur->next != NULL )
3328 cur = cur->next;
3329 cur->next = msg;
3330 }
3331
Hanno Becker3b235902018-08-06 09:54:53 +01003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 return( 0 );
3334}
3335
3336/*
3337 * Free the current flight of handshake messages
3338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_ssl_flight_item *cur = flight;
3342 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343
3344 while( cur != NULL )
3345 {
3346 next = cur->next;
3347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 mbedtls_free( cur->p );
3349 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350
3351 cur = next;
3352 }
3353}
3354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3356static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003357#endif
3358
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003360 * Swap transform_out and out_ctr with the alternative ones
3361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003365 unsigned char tmp_out_ctr[8];
3366
3367 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003370 return;
3371 }
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003374
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003375 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003376 tmp_transform = ssl->transform_out;
3377 ssl->transform_out = ssl->handshake->alt_transform_out;
3378 ssl->handshake->alt_transform_out = tmp_transform;
3379
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003380 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003381 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3382 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003383 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003384
3385 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003386 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3389 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3394 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003395 }
3396 }
3397#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398}
3399
3400/*
3401 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003402 */
3403int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3404{
3405 int ret = 0;
3406
3407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3408
3409 ret = mbedtls_ssl_flight_transmit( ssl );
3410
3411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3412
3413 return( ret );
3414}
3415
3416/*
3417 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003418 *
3419 * Need to remember the current message in case flush_output returns
3420 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003421 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003422 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003423int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003424{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003425 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431
3432 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003433 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003434 ssl_swap_epochs( ssl );
3435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003437 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003438
3439 while( ssl->handshake->cur_msg != NULL )
3440 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003441 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003442 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003443
Hanno Beckere1dcb032018-08-17 16:47:58 +01003444 int const is_finished =
3445 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3446 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3447
Hanno Becker04da1892018-08-14 13:22:10 +01003448 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3449 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3450
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003451 /* Swap epochs before sending Finished: we can't do it after
3452 * sending ChangeCipherSpec, in case write returns WANT_READ.
3453 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003454 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003455 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003457 ssl_swap_epochs( ssl );
3458 }
3459
Hanno Becker67bc7c32018-08-06 11:33:50 +01003460 ret = ssl_get_remaining_payload_in_datagram( ssl );
3461 if( ret < 0 )
3462 return( ret );
3463 max_frag_len = (size_t) ret;
3464
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003465 /* CCS is copied as is, while HS messages may need fragmentation */
3466 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3467 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003468 if( max_frag_len == 0 )
3469 {
3470 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3471 return( ret );
3472
3473 continue;
3474 }
3475
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003476 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003477 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003478 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003479
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003480 /* Update position inside current message */
3481 ssl->handshake->cur_msg_p += cur->len;
3482 }
3483 else
3484 {
3485 const unsigned char * const p = ssl->handshake->cur_msg_p;
3486 const size_t hs_len = cur->len - 12;
3487 const size_t frag_off = p - ( cur->p + 12 );
3488 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003489 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003490
Hanno Beckere1dcb032018-08-17 16:47:58 +01003491 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003492 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003493 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003494 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003495
Hanno Becker67bc7c32018-08-06 11:33:50 +01003496 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3497 return( ret );
3498
3499 continue;
3500 }
3501 max_hs_frag_len = max_frag_len - 12;
3502
3503 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3504 max_hs_frag_len : rem_len;
3505
3506 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003507 {
3508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003509 (unsigned) cur_hs_frag_len,
3510 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003511 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003512
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003513 /* Messages are stored with handshake headers as if not fragmented,
3514 * copy beginning of headers then fill fragmentation fields.
3515 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3516 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003517
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003518 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3519 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3520 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3521
Hanno Becker67bc7c32018-08-06 11:33:50 +01003522 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3523 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3524 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003525
3526 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3527
Hanno Becker3f7b9732018-08-28 09:53:25 +01003528 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003529 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3530 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003531 ssl->out_msgtype = cur->type;
3532
3533 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003534 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003535 }
3536
3537 /* If done with the current message move to the next one if any */
3538 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3539 {
3540 if( cur->next != NULL )
3541 {
3542 ssl->handshake->cur_msg = cur->next;
3543 ssl->handshake->cur_msg_p = cur->next->p + 12;
3544 }
3545 else
3546 {
3547 ssl->handshake->cur_msg = NULL;
3548 ssl->handshake->cur_msg_p = NULL;
3549 }
3550 }
3551
3552 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003553 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003556 return( ret );
3557 }
3558 }
3559
Hanno Becker67bc7c32018-08-06 11:33:50 +01003560 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3561 return( ret );
3562
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003563 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3565 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003566 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003569 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3570 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003571
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573
3574 return( 0 );
3575}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003576
3577/*
3578 * To be called when the last message of an incoming flight is received.
3579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003581{
3582 /* We won't need to resend that one any more */
3583 ssl_flight_free( ssl->handshake->flight );
3584 ssl->handshake->flight = NULL;
3585 ssl->handshake->cur_msg = NULL;
3586
3587 /* The next incoming flight will start with this msg_seq */
3588 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3589
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003590 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003591 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003592
Hanno Becker0271f962018-08-16 13:23:47 +01003593 /* Clear future message buffering structure. */
3594 ssl_buffering_free( ssl );
3595
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003596 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003597 ssl_set_timer( ssl, 0 );
3598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3600 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003603 }
3604 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003607
3608/*
3609 * To be called when the last message of an outgoing flight is send.
3610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003612{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003613 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003614 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3617 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003620 }
3621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625
Paul Bakker5121ce52009-01-03 21:22:43 +00003626/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003627 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003629
3630/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003631 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003632 *
3633 * - fill in handshake headers
3634 * - update handshake checksum
3635 * - DTLS: save message for resending
3636 * - then pass to the record layer
3637 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003638 * DTLS: except for HelloRequest, messages are only queued, and will only be
3639 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003640 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003641 * Inputs:
3642 * - ssl->out_msglen: 4 + actual handshake message len
3643 * (4 is the size of handshake headers for TLS)
3644 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3645 * - ssl->out_msg + 4: the handshake message body
3646 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003647 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003648 * - ssl->out_msglen: the length of the record contents
3649 * (including handshake headers but excluding record headers)
3650 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003651 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003652int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003653{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003654 int ret;
3655 const size_t hs_len = ssl->out_msglen - 4;
3656 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3659
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003660 /*
3661 * Sanity checks
3662 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003663 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003664 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3665 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003666 /* In SSLv3, the client might send a NoCertificate alert. */
3667#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3668 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3669 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3670 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3671#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3672 {
3673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3675 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003677
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003678 /* Whenever we send anything different from a
3679 * HelloRequest we should be in a handshake - double check. */
3680 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3681 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003682 ssl->handshake == NULL )
3683 {
3684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003689 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003690 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003695 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003697
Hanno Beckerb50a2532018-08-06 11:52:54 +01003698 /* Double-check that we did not exceed the bounds
3699 * of the outgoing record buffer.
3700 * This should never fail as the various message
3701 * writing functions must obey the bounds of the
3702 * outgoing record buffer, but better be safe.
3703 *
3704 * Note: We deliberately do not check for the MTU or MFL here.
3705 */
3706 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3707 {
3708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3709 "size %u, maximum %u",
3710 (unsigned) ssl->out_msglen,
3711 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3713 }
3714
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003715 /*
3716 * Fill handshake headers
3717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003719 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003720 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3721 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3722 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003724 /*
3725 * DTLS has additional fields in the Handshake layer,
3726 * between the length field and the actual payload:
3727 * uint16 message_seq;
3728 * uint24 fragment_offset;
3729 * uint24 fragment_length;
3730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003733 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003734 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003735 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003736 {
3737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3738 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003739 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003740 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3742 }
3743
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003745 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003746
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003747 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003749 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003750 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3751 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3752 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003753 }
3754 else
3755 {
3756 ssl->out_msg[4] = 0;
3757 ssl->out_msg[5] = 0;
3758 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003759
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003760 /* Handshake hashes are computed without fragmentation,
3761 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003762 memset( ssl->out_msg + 6, 0x00, 3 );
3763 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003766
Hanno Becker0207e532018-08-28 10:28:28 +01003767 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3769 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 }
3771
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003772 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003775 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3776 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777 {
3778 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781 return( ret );
3782 }
3783 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003784 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003785#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003786 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003787 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003788 {
3789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3790 return( ret );
3791 }
3792 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003793
3794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3795
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003796 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003797}
3798
3799/*
3800 * Record layer functions
3801 */
3802
3803/*
3804 * Write current record.
3805 *
3806 * Uses:
3807 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3808 * - ssl->out_msglen: length of the record content (excl headers)
3809 * - ssl->out_msg: record content
3810 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003812{
3813 int ret, done = 0;
3814 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003815 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003816
3817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003820 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003822 {
3823 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003826 return( ret );
3827 }
3828
3829 len = ssl->out_msglen;
3830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3834 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 ret = mbedtls_ssl_hw_record_write( ssl );
3839 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3842 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003843 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003844
3845 if( ret == 0 )
3846 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003847 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003849 if( !done )
3850 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003851 unsigned i;
3852 size_t protected_record_size;
3853
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003854 /* Skip writing the record content type to after the encryption,
3855 * as it may change when using the CID extension. */
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003858 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003859
Hanno Becker19859472018-08-06 09:40:20 +01003860 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003861 ssl->out_len[0] = (unsigned char)( len >> 8 );
3862 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003863
Paul Bakker48916f92012-09-16 19:57:18 +00003864 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003865 {
Hanno Becker3307b532017-12-27 21:37:21 +00003866 mbedtls_record rec;
3867
3868 rec.buf = ssl->out_iv;
3869 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3870 ( ssl->out_iv - ssl->out_buf );
3871 rec.data_len = ssl->out_msglen;
3872 rec.data_offset = ssl->out_msg - rec.buf;
3873
3874 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3875 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3876 ssl->conf->transport, rec.ver );
3877 rec.type = ssl->out_msgtype;
3878
Hanno Beckera5a2b082019-05-15 14:03:01 +01003879#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003880 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003881 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003882#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003883
Hanno Becker611a83b2018-01-03 14:27:32 +00003884 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003885 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003888 return( ret );
3889 }
3890
Hanno Becker3307b532017-12-27 21:37:21 +00003891 if( rec.data_offset != 0 )
3892 {
3893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3895 }
3896
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003897 /* Update the record content type and CID. */
3898 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003899#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003900 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003901#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003902 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003903 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3904 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003905 }
3906
Hanno Becker43395762019-05-03 14:46:38 +01003907 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003908
3909#if defined(MBEDTLS_SSL_PROTO_DTLS)
3910 /* In case of DTLS, double-check that we don't exceed
3911 * the remaining space in the datagram. */
3912 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3913 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003914 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003915 if( ret < 0 )
3916 return( ret );
3917
3918 if( protected_record_size > (size_t) ret )
3919 {
3920 /* Should never happen */
3921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3922 }
3923 }
3924#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003925
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003926 /* Now write the potentially updated record content type. */
3927 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003930 "version = [%d:%d], msglen = %d",
3931 ssl->out_hdr[0], ssl->out_hdr[1],
3932 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003935 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003936
3937 ssl->out_left += protected_record_size;
3938 ssl->out_hdr += protected_record_size;
3939 ssl_update_out_pointers( ssl, ssl->transform_out );
3940
Hanno Becker04484622018-08-06 09:49:38 +01003941 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3942 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3943 break;
3944
3945 /* The loop goes to its end iff the counter is wrapping */
3946 if( i == ssl_ep_len( ssl ) )
3947 {
3948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3949 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003951 }
3952
Hanno Becker67bc7c32018-08-06 11:33:50 +01003953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003954 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3955 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003956 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003957 size_t remaining;
3958 ret = ssl_get_remaining_payload_in_datagram( ssl );
3959 if( ret < 0 )
3960 {
3961 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3962 ret );
3963 return( ret );
3964 }
3965
3966 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003967 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003968 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003969 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003970 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003971 else
3972 {
Hanno Becker513815a2018-08-20 11:56:09 +01003973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003974 }
3975 }
3976#endif /* MBEDTLS_SSL_PROTO_DTLS */
3977
3978 if( ( flush == SSL_FORCE_FLUSH ) &&
3979 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 return( ret );
3983 }
3984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
3987 return( 0 );
3988}
3989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003991
3992static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3993{
3994 if( ssl->in_msglen < ssl->in_hslen ||
3995 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3996 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3997 {
3998 return( 1 );
3999 }
4000 return( 0 );
4001}
Hanno Becker44650b72018-08-16 12:51:11 +01004002
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004003static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004004{
4005 return( ( ssl->in_msg[9] << 16 ) |
4006 ( ssl->in_msg[10] << 8 ) |
4007 ssl->in_msg[11] );
4008}
4009
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004010static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004011{
4012 return( ( ssl->in_msg[6] << 16 ) |
4013 ( ssl->in_msg[7] << 8 ) |
4014 ssl->in_msg[8] );
4015}
4016
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004017static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004018{
4019 uint32_t msg_len, frag_off, frag_len;
4020
4021 msg_len = ssl_get_hs_total_len( ssl );
4022 frag_off = ssl_get_hs_frag_off( ssl );
4023 frag_len = ssl_get_hs_frag_len( ssl );
4024
4025 if( frag_off > msg_len )
4026 return( -1 );
4027
4028 if( frag_len > msg_len - frag_off )
4029 return( -1 );
4030
4031 if( frag_len + 12 > ssl->in_msglen )
4032 return( -1 );
4033
4034 return( 0 );
4035}
4036
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004037/*
4038 * Mark bits in bitmask (used for DTLS HS reassembly)
4039 */
4040static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4041{
4042 unsigned int start_bits, end_bits;
4043
4044 start_bits = 8 - ( offset % 8 );
4045 if( start_bits != 8 )
4046 {
4047 size_t first_byte_idx = offset / 8;
4048
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004049 /* Special case */
4050 if( len <= start_bits )
4051 {
4052 for( ; len != 0; len-- )
4053 mask[first_byte_idx] |= 1 << ( start_bits - len );
4054
4055 /* Avoid potential issues with offset or len becoming invalid */
4056 return;
4057 }
4058
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004059 offset += start_bits; /* Now offset % 8 == 0 */
4060 len -= start_bits;
4061
4062 for( ; start_bits != 0; start_bits-- )
4063 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4064 }
4065
4066 end_bits = len % 8;
4067 if( end_bits != 0 )
4068 {
4069 size_t last_byte_idx = ( offset + len ) / 8;
4070
4071 len -= end_bits; /* Now len % 8 == 0 */
4072
4073 for( ; end_bits != 0; end_bits-- )
4074 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4075 }
4076
4077 memset( mask + offset / 8, 0xFF, len / 8 );
4078}
4079
4080/*
4081 * Check that bitmask is full
4082 */
4083static int ssl_bitmask_check( unsigned char *mask, size_t len )
4084{
4085 size_t i;
4086
4087 for( i = 0; i < len / 8; i++ )
4088 if( mask[i] != 0xFF )
4089 return( -1 );
4090
4091 for( i = 0; i < len % 8; i++ )
4092 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4093 return( -1 );
4094
4095 return( 0 );
4096}
4097
Hanno Becker56e205e2018-08-16 09:06:12 +01004098/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004099static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004100 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004101{
Hanno Becker56e205e2018-08-16 09:06:12 +01004102 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004103
Hanno Becker56e205e2018-08-16 09:06:12 +01004104 alloc_len = 12; /* Handshake header */
4105 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004106
Hanno Beckerd07df862018-08-16 09:14:58 +01004107 if( add_bitmap )
4108 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004109
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004110 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004111}
Hanno Becker56e205e2018-08-16 09:06:12 +01004112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004114
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004115static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004116{
4117 return( ( ssl->in_msg[1] << 16 ) |
4118 ( ssl->in_msg[2] << 8 ) |
4119 ssl->in_msg[3] );
4120}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004121
Simon Butcher99000142016-10-13 17:21:01 +01004122int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004127 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004129 }
4130
Hanno Becker12555c62018-08-16 12:47:53 +01004131 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004134 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004135 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004139 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004140 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004141 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004142
Hanno Becker44650b72018-08-16 12:51:11 +01004143 if( ssl_check_hs_header( ssl ) != 0 )
4144 {
4145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4146 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4147 }
4148
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004149 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004150 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4151 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4152 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4153 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004154 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004155 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4156 {
4157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4158 recv_msg_seq,
4159 ssl->handshake->in_msg_seq ) );
4160 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4161 }
4162
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004163 /* Retransmit only on last message from previous flight, to avoid
4164 * too many retransmissions.
4165 * Besides, No sane server ever retransmits HelloVerifyRequest */
4166 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004170 "message_seq = %d, start_of_flight = %d",
4171 recv_msg_seq,
4172 ssl->handshake->in_flight_start_seq ) );
4173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004177 return( ret );
4178 }
4179 }
4180 else
4181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004183 "message_seq = %d, expected = %d",
4184 recv_msg_seq,
4185 ssl->handshake->in_msg_seq ) );
4186 }
4187
Hanno Becker90333da2017-10-10 11:27:13 +01004188 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004189 }
4190 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004191
Hanno Becker6d97ef52018-08-16 13:09:04 +01004192 /* Message reassembly is handled alongside buffering of future
4193 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004194 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004195 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004196 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004199 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004200 }
4201 }
4202 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004204 /* With TLS we don't handle fragmentation (for now) */
4205 if( ssl->in_msglen < ssl->in_hslen )
4206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4208 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004209 }
4210
Simon Butcher99000142016-10-13 17:21:01 +01004211 return( 0 );
4212}
4213
4214void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4215{
Hanno Becker0271f962018-08-16 13:23:47 +01004216 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004217
Hanno Becker0271f962018-08-16 13:23:47 +01004218 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004219 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004220 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004221 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004222
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004223 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004225 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004226 ssl->handshake != NULL )
4227 {
Hanno Becker0271f962018-08-16 13:23:47 +01004228 unsigned offset;
4229 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004230
Hanno Becker0271f962018-08-16 13:23:47 +01004231 /* Increment handshake sequence number */
4232 hs->in_msg_seq++;
4233
4234 /*
4235 * Clear up handshake buffering and reassembly structure.
4236 */
4237
4238 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004239 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004240
4241 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004242 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4243 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004244 offset++, hs_buf++ )
4245 {
4246 *hs_buf = *(hs_buf + 1);
4247 }
4248
4249 /* Create a fresh last entry */
4250 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004251 }
4252#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004253}
4254
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004255/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004256 * DTLS anti-replay: RFC 6347 4.1.2.6
4257 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004258 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4259 * Bit n is set iff record number in_window_top - n has been seen.
4260 *
4261 * Usually, in_window_top is the last record number seen and the lsb of
4262 * in_window is set. The only exception is the initial state (record number 0
4263 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4266static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004267{
4268 ssl->in_window_top = 0;
4269 ssl->in_window = 0;
4270}
4271
4272static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4273{
4274 return( ( (uint64_t) buf[0] << 40 ) |
4275 ( (uint64_t) buf[1] << 32 ) |
4276 ( (uint64_t) buf[2] << 24 ) |
4277 ( (uint64_t) buf[3] << 16 ) |
4278 ( (uint64_t) buf[4] << 8 ) |
4279 ( (uint64_t) buf[5] ) );
4280}
4281
4282/*
4283 * Return 0 if sequence number is acceptable, -1 otherwise
4284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004286{
4287 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4288 uint64_t bit;
4289
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004290 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004291 return( 0 );
4292
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 if( rec_seqnum > ssl->in_window_top )
4294 return( 0 );
4295
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004296 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004297
4298 if( bit >= 64 )
4299 return( -1 );
4300
4301 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4302 return( -1 );
4303
4304 return( 0 );
4305}
4306
4307/*
4308 * Update replay window on new validated record
4309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004311{
4312 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4313
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004314 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004315 return;
4316
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004317 if( rec_seqnum > ssl->in_window_top )
4318 {
4319 /* Update window_top and the contents of the window */
4320 uint64_t shift = rec_seqnum - ssl->in_window_top;
4321
4322 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004323 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004324 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004325 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004327 ssl->in_window |= 1;
4328 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004329
4330 ssl->in_window_top = rec_seqnum;
4331 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004332 else
4333 {
4334 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004335 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004336
4337 if( bit < 64 ) /* Always true, but be extra sure */
4338 ssl->in_window |= (uint64_t) 1 << bit;
4339 }
4340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004342
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004343#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004344/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004345static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4346
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004347/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004348 * Without any SSL context, check if a datagram looks like a ClientHello with
4349 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004350 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004351 *
4352 * - if cookie is valid, return 0
4353 * - if ClientHello looks superficially valid but cookie is not,
4354 * fill obuf and set olen, then
4355 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4356 * - otherwise return a specific error code
4357 */
4358static int ssl_check_dtls_clihlo_cookie(
4359 mbedtls_ssl_cookie_write_t *f_cookie_write,
4360 mbedtls_ssl_cookie_check_t *f_cookie_check,
4361 void *p_cookie,
4362 const unsigned char *cli_id, size_t cli_id_len,
4363 const unsigned char *in, size_t in_len,
4364 unsigned char *obuf, size_t buf_len, size_t *olen )
4365{
4366 size_t sid_len, cookie_len;
4367 unsigned char *p;
4368
4369 if( f_cookie_write == NULL || f_cookie_check == NULL )
4370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4371
4372 /*
4373 * Structure of ClientHello with record and handshake headers,
4374 * and expected values. We don't need to check a lot, more checks will be
4375 * done when actually parsing the ClientHello - skipping those checks
4376 * avoids code duplication and does not make cookie forging any easier.
4377 *
4378 * 0-0 ContentType type; copied, must be handshake
4379 * 1-2 ProtocolVersion version; copied
4380 * 3-4 uint16 epoch; copied, must be 0
4381 * 5-10 uint48 sequence_number; copied
4382 * 11-12 uint16 length; (ignored)
4383 *
4384 * 13-13 HandshakeType msg_type; (ignored)
4385 * 14-16 uint24 length; (ignored)
4386 * 17-18 uint16 message_seq; copied
4387 * 19-21 uint24 fragment_offset; copied, must be 0
4388 * 22-24 uint24 fragment_length; (ignored)
4389 *
4390 * 25-26 ProtocolVersion client_version; (ignored)
4391 * 27-58 Random random; (ignored)
4392 * 59-xx SessionID session_id; 1 byte len + sid_len content
4393 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4394 * ...
4395 *
4396 * Minimum length is 61 bytes.
4397 */
4398 if( in_len < 61 ||
4399 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4400 in[3] != 0 || in[4] != 0 ||
4401 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4402 {
4403 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4404 }
4405
4406 sid_len = in[59];
4407 if( sid_len > in_len - 61 )
4408 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4409
4410 cookie_len = in[60 + sid_len];
4411 if( cookie_len > in_len - 60 )
4412 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4413
4414 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4415 cli_id, cli_id_len ) == 0 )
4416 {
4417 /* Valid cookie */
4418 return( 0 );
4419 }
4420
4421 /*
4422 * If we get here, we've got an invalid cookie, let's prepare HVR.
4423 *
4424 * 0-0 ContentType type; copied
4425 * 1-2 ProtocolVersion version; copied
4426 * 3-4 uint16 epoch; copied
4427 * 5-10 uint48 sequence_number; copied
4428 * 11-12 uint16 length; olen - 13
4429 *
4430 * 13-13 HandshakeType msg_type; hello_verify_request
4431 * 14-16 uint24 length; olen - 25
4432 * 17-18 uint16 message_seq; copied
4433 * 19-21 uint24 fragment_offset; copied
4434 * 22-24 uint24 fragment_length; olen - 25
4435 *
4436 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4437 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4438 *
4439 * Minimum length is 28.
4440 */
4441 if( buf_len < 28 )
4442 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4443
4444 /* Copy most fields and adapt others */
4445 memcpy( obuf, in, 25 );
4446 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4447 obuf[25] = 0xfe;
4448 obuf[26] = 0xff;
4449
4450 /* Generate and write actual cookie */
4451 p = obuf + 28;
4452 if( f_cookie_write( p_cookie,
4453 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4454 {
4455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4456 }
4457
4458 *olen = p - obuf;
4459
4460 /* Go back and fill length fields */
4461 obuf[27] = (unsigned char)( *olen - 28 );
4462
4463 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4464 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4465 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4466
4467 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4468 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4469
4470 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4471}
4472
4473/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004474 * Handle possible client reconnect with the same UDP quadruplet
4475 * (RFC 6347 Section 4.2.8).
4476 *
4477 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4478 * that looks like a ClientHello.
4479 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004480 * - if the input looks like a ClientHello without cookies,
4481 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004482 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004483 * - if the input looks like a ClientHello with a valid cookie,
4484 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004485 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004486 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004487 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004488 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004489 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4490 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004491 */
4492static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4493{
4494 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004495 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004496
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004497 ret = ssl_check_dtls_clihlo_cookie(
4498 ssl->conf->f_cookie_write,
4499 ssl->conf->f_cookie_check,
4500 ssl->conf->p_cookie,
4501 ssl->cli_id, ssl->cli_id_len,
4502 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004503 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004504
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004505 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4506
4507 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004509 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 * If the error is permanent we'll catch it later,
4511 * if it's not, then hopefully it'll work next time. */
4512 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4513
4514 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 }
4516
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004518 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004519 /* Got a valid cookie, partially reset context */
4520 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4521 {
4522 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4523 return( ret );
4524 }
4525
4526 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004527 }
4528
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004529 return( ret );
4530}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004531#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532
Hanno Becker46483f12019-05-03 13:25:54 +01004533static int ssl_check_record_type( uint8_t record_type )
4534{
4535 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4536 record_type != MBEDTLS_SSL_MSG_ALERT &&
4537 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4538 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4539 {
4540 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4541 }
4542
4543 return( 0 );
4544}
4545
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004546/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004547 * ContentType type;
4548 * ProtocolVersion version;
4549 * uint16 epoch; // DTLS only
4550 * uint48 sequence_number; // DTLS only
4551 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004552 *
4553 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004554 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004555 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4556 *
4557 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004558 * 1. proceed with the record if this function returns 0
4559 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4560 * 3. return CLIENT_RECONNECT if this function return that value
4561 * 4. drop the whole datagram if this function returns anything else.
4562 * Point 2 is needed when the peer is resending, and we have already received
4563 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004567 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004568 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004569
Hanno Becker8b09b732019-05-08 12:03:28 +01004570 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004571
Paul Bakker5121ce52009-01-03 21:22:43 +00004572 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004573 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004574
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004575 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004576#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004577 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4578 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4579 ssl->conf->cid_len != 0 )
4580 {
4581 /* Shift pointers to account for record header including CID
4582 * struct {
4583 * ContentType special_type = tls12_cid;
4584 * ProtocolVersion version;
4585 * uint16 epoch;
4586 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004587 * opaque cid[cid_length]; // Additional field compared to
4588 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004589 * uint16 length;
4590 * opaque enc_content[DTLSCiphertext.length];
4591 * } DTLSCiphertext;
4592 */
4593
4594 /* So far, we only support static CID lengths
4595 * fixed in the configuration. */
4596 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4597 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4598 }
4599 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004600#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004601 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004604
4605#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004606 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4607 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004608 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4609#endif /* MBEDTLS_SSL_PROTO_DTLS */
4610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4611 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004614 }
4615
4616 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004617 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4620 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 }
4622
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004623 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 }
4628
Hanno Becker8b09b732019-05-08 12:03:28 +01004629 /* Now that the total length of the record header is known, ensure
4630 * that the current datagram is large enough to hold it.
4631 * This would fail, for example, if we received a datagram of
4632 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4633 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4634 if( ret != 0 )
4635 {
4636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4637 return( ret );
4638 }
4639 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4640
4641 /* Parse and validate record length
4642 * This must happen after the CID parsing because
4643 * its position in the record header depends on
4644 * the presence of a CID. */
4645
4646 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004647 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004648 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4651 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004652 }
4653
Hanno Becker8b09b732019-05-08 12:03:28 +01004654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004655 "version = [%d:%d], msglen = %d",
4656 ssl->in_msgtype,
4657 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004658
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004659 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004660 * DTLS-related tests.
4661 * Check epoch before checking length constraint because
4662 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4663 * message gets duplicated before the corresponding Finished message,
4664 * the second ChangeCipherSpec should be discarded because it belongs
4665 * to an old epoch, but not because its length is shorter than
4666 * the minimum record length for packets using the new record transform.
4667 * Note that these two kinds of failures are handled differently,
4668 * as an unexpected record is silently skipped but an invalid
4669 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004670 */
4671#if defined(MBEDTLS_SSL_PROTO_DTLS)
4672 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4673 {
4674 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4675
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004676 /* Check epoch (and sequence number) with DTLS */
4677 if( rec_epoch != ssl->in_epoch )
4678 {
4679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4680 "expected %d, received %d",
4681 ssl->in_epoch, rec_epoch ) );
4682
4683#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4684 /*
4685 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4686 * access the first byte of record content (handshake type), as we
4687 * have an active transform (possibly iv_len != 0), so use the
4688 * fact that the record header len is 13 instead.
4689 */
4690 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4691 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4692 rec_epoch == 0 &&
4693 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4694 ssl->in_left > 13 &&
4695 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4696 {
4697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4698 "from the same port" ) );
4699 return( ssl_handle_possible_reconnect( ssl ) );
4700 }
4701 else
4702#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004703 {
4704 /* Consider buffering the record. */
4705 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4706 {
4707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4708 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4709 }
4710
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004711 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004712 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004713 }
4714
4715#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4716 /* Replay detection only works for the current epoch */
4717 if( rec_epoch == ssl->in_epoch &&
4718 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4719 {
4720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4721 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4722 }
4723#endif
4724 }
4725#endif /* MBEDTLS_SSL_PROTO_DTLS */
4726
Hanno Becker52c6dc62017-05-26 16:07:36 +01004727
4728 /* Check length against bounds of the current transform and version */
4729 if( ssl->transform_in == NULL )
4730 {
4731 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004732 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004733 {
4734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4735 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4736 }
4737 }
4738 else
4739 {
4740 if( ssl->in_msglen < ssl->transform_in->minlen )
4741 {
4742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4743 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4744 }
4745
4746#if defined(MBEDTLS_SSL_PROTO_SSL3)
4747 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004748 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4751 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4752 }
4753#endif
4754#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4755 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4756 /*
4757 * TLS encrypted messages can have up to 256 bytes of padding
4758 */
4759 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4760 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004761 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004762 {
4763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4764 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4765 }
4766#endif
4767 }
4768
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004769 return( 0 );
4770}
Paul Bakker5121ce52009-01-03 21:22:43 +00004771
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004772/*
4773 * If applicable, decrypt (and decompress) record content
4774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004776{
4777 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004780 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4783 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 ret = mbedtls_ssl_hw_record_read( ssl );
4788 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4791 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004792 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004793
4794 if( ret == 0 )
4795 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004798 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004799 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004800 mbedtls_record rec;
4801
4802 rec.buf = ssl->in_iv;
4803 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4804 - ( ssl->in_iv - ssl->in_buf );
4805 rec.data_len = ssl->in_msglen;
4806 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004807#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004808 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004809 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004810#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004811
4812 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4813 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4814 ssl->conf->transport, rec.ver );
4815 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004816 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4817 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004820
Hanno Beckera5a2b082019-05-15 14:03:01 +01004821#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004822 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4823 ssl->conf->ignore_unexpected_cid
4824 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4825 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004827 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004828 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004829#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004830
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 return( ret );
4832 }
4833
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004834 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004835 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004836 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4837 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004839
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004840 /* The record content type may change during decryption,
4841 * so re-read it. */
4842 ssl->in_msgtype = rec.type;
4843 /* Also update the input buffer, because unfortunately
4844 * the server-side ssl_parse_client_hello() reparses the
4845 * record header when receiving a ClientHello initiating
4846 * a renegotiation. */
4847 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004848 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004849 ssl->in_msglen = rec.data_len;
4850 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4851 ssl->in_len[1] = (unsigned char)( rec.data_len );
4852
Paul Bakker5121ce52009-01-03 21:22:43 +00004853 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4854 ssl->in_msg, ssl->in_msglen );
4855
Hanno Beckera5a2b082019-05-15 14:03:01 +01004856#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004857 /* We have already checked the record content type
4858 * in ssl_parse_record_header(), failing or silently
4859 * dropping the record in the case of an unknown type.
4860 *
4861 * Since with the use of CIDs, the record content type
4862 * might change during decryption, re-check the record
4863 * content type, but treat a failure as fatal this time. */
4864 if( ssl_check_record_type( ssl->in_msgtype ) )
4865 {
4866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4867 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4868 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004869#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870
Angus Grattond8213d02016-05-25 20:56:48 +10004871 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4874 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004875 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004876 else if( ssl->in_msglen == 0 )
4877 {
4878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4879 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4880 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4881 {
4882 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4884 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4885 }
4886#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4887
4888 ssl->nb_zero++;
4889
4890 /*
4891 * Three or more empty messages may be a DoS attack
4892 * (excessive CPU consumption).
4893 */
4894 if( ssl->nb_zero > 3 )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004897 "messages, possible DoS attack" ) );
4898 /* Treat the records as if they were not properly authenticated,
4899 * thereby failing the connection if we see more than allowed
4900 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004901 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4902 }
4903 }
4904 else
4905 ssl->nb_zero = 0;
4906
4907#if defined(MBEDTLS_SSL_PROTO_DTLS)
4908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4909 {
4910 ; /* in_ctr read from peer, not maintained internally */
4911 }
4912 else
4913#endif
4914 {
4915 unsigned i;
4916 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4917 if( ++ssl->in_ctr[i - 1] != 0 )
4918 break;
4919
4920 /* The loop goes to its end iff the counter is wrapping */
4921 if( i == ssl_ep_len( ssl ) )
4922 {
4923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4924 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4925 }
4926 }
4927
Paul Bakker5121ce52009-01-03 21:22:43 +00004928 }
4929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004931 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004933 {
4934 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004937 return( ret );
4938 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004939 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004946 }
4947#endif
4948
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004949 return( 0 );
4950}
4951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004953
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004954/*
4955 * Read a record.
4956 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004957 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4958 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4959 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004960 */
Hanno Becker1097b342018-08-15 14:09:41 +01004961
4962/* Helper functions for mbedtls_ssl_read_record(). */
4963static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004964static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4965static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004966
Hanno Becker327c93b2018-08-15 13:56:18 +01004967int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004968 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004969{
4970 int ret;
4971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004974 if( ssl->keep_current_message == 0 )
4975 {
4976 do {
Simon Butcher99000142016-10-13 17:21:01 +01004977
Hanno Becker26994592018-08-15 14:14:59 +01004978 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004979 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004980 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004981
Hanno Beckere74d5562018-08-15 14:26:08 +01004982 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004983 {
Hanno Becker40f50842018-08-15 14:48:01 +01004984#if defined(MBEDTLS_SSL_PROTO_DTLS)
4985 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004986
Hanno Becker40f50842018-08-15 14:48:01 +01004987 /* We only check for buffered messages if the
4988 * current datagram is fully consumed. */
4989 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004990 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004991 {
Hanno Becker40f50842018-08-15 14:48:01 +01004992 if( ssl_load_buffered_message( ssl ) == 0 )
4993 have_buffered = 1;
4994 }
4995
4996 if( have_buffered == 0 )
4997#endif /* MBEDTLS_SSL_PROTO_DTLS */
4998 {
4999 ret = ssl_get_next_record( ssl );
5000 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5001 continue;
5002
5003 if( ret != 0 )
5004 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005005 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005006 return( ret );
5007 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005008 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005009 }
5010
5011 ret = mbedtls_ssl_handle_message_type( ssl );
5012
Hanno Becker40f50842018-08-15 14:48:01 +01005013#if defined(MBEDTLS_SSL_PROTO_DTLS)
5014 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5015 {
5016 /* Buffer future message */
5017 ret = ssl_buffer_message( ssl );
5018 if( ret != 0 )
5019 return( ret );
5020
5021 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5022 }
5023#endif /* MBEDTLS_SSL_PROTO_DTLS */
5024
Hanno Becker90333da2017-10-10 11:27:13 +01005025 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5026 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005027
5028 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005029 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005030 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005031 return( ret );
5032 }
5033
Hanno Becker327c93b2018-08-15 13:56:18 +01005034 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005035 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036 {
5037 mbedtls_ssl_update_handshake_status( ssl );
5038 }
Simon Butcher99000142016-10-13 17:21:01 +01005039 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005040 else
Simon Butcher99000142016-10-13 17:21:01 +01005041 {
Hanno Becker02f59072018-08-15 14:00:24 +01005042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005043 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005044 }
5045
5046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5047
5048 return( 0 );
5049}
5050
Hanno Becker40f50842018-08-15 14:48:01 +01005051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005052static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005053{
Hanno Becker40f50842018-08-15 14:48:01 +01005054 if( ssl->in_left > ssl->next_record_offset )
5055 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005056
Hanno Becker40f50842018-08-15 14:48:01 +01005057 return( 0 );
5058}
5059
5060static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5061{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005062 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005063 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005064 int ret = 0;
5065
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005066 if( hs == NULL )
5067 return( -1 );
5068
Hanno Beckere00ae372018-08-20 09:39:42 +01005069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5070
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005071 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5072 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5073 {
5074 /* Check if we have seen a ChangeCipherSpec before.
5075 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005076 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005077 {
5078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5079 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005080 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005081 }
5082
Hanno Becker39b8bc92018-08-28 17:17:13 +01005083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005084 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5085 ssl->in_msglen = 1;
5086 ssl->in_msg[0] = 1;
5087
5088 /* As long as they are equal, the exact value doesn't matter. */
5089 ssl->in_left = 0;
5090 ssl->next_record_offset = 0;
5091
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005092 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 goto exit;
5094 }
Hanno Becker37f95322018-08-16 13:55:32 +01005095
Hanno Beckerb8f50142018-08-28 10:01:34 +01005096#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005097 /* Debug only */
5098 {
5099 unsigned offset;
5100 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5101 {
5102 hs_buf = &hs->buffering.hs[offset];
5103 if( hs_buf->is_valid == 1 )
5104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5106 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005107 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005108 }
5109 }
5110 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005111#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005112
5113 /* Check if we have buffered and/or fully reassembled the
5114 * next handshake message. */
5115 hs_buf = &hs->buffering.hs[0];
5116 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5117 {
5118 /* Synthesize a record containing the buffered HS message. */
5119 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5120 ( hs_buf->data[2] << 8 ) |
5121 hs_buf->data[3];
5122
5123 /* Double-check that we haven't accidentally buffered
5124 * a message that doesn't fit into the input buffer. */
5125 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5126 {
5127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5129 }
5130
5131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5132 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5133 hs_buf->data, msg_len + 12 );
5134
5135 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5136 ssl->in_hslen = msg_len + 12;
5137 ssl->in_msglen = msg_len + 12;
5138 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5139
5140 ret = 0;
5141 goto exit;
5142 }
5143 else
5144 {
5145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5146 hs->in_msg_seq ) );
5147 }
5148
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005149 ret = -1;
5150
5151exit:
5152
5153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5154 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005155}
5156
Hanno Beckera02b0b42018-08-21 17:20:27 +01005157static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5158 size_t desired )
5159{
5160 int offset;
5161 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5163 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005164
Hanno Becker01315ea2018-08-21 17:22:17 +01005165 /* Get rid of future records epoch first, if such exist. */
5166 ssl_free_buffered_record( ssl );
5167
5168 /* Check if we have enough space available now. */
5169 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5170 hs->buffering.total_bytes_buffered ) )
5171 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005173 return( 0 );
5174 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005175
Hanno Becker4f432ad2018-08-28 10:02:32 +01005176 /* We don't have enough space to buffer the next expected handshake
5177 * message. Remove buffers used for future messages to gain space,
5178 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005179 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5180 offset >= 0; offset-- )
5181 {
5182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5183 offset ) );
5184
Hanno Beckerb309b922018-08-23 13:18:05 +01005185 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005186
5187 /* Check if we have enough space available now. */
5188 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5189 hs->buffering.total_bytes_buffered ) )
5190 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005192 return( 0 );
5193 }
5194 }
5195
5196 return( -1 );
5197}
5198
Hanno Becker40f50842018-08-15 14:48:01 +01005199static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5200{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005201 int ret = 0;
5202 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5203
5204 if( hs == NULL )
5205 return( 0 );
5206
5207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5208
5209 switch( ssl->in_msgtype )
5210 {
5211 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005213
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005214 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005215 break;
5216
5217 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005218 {
5219 unsigned recv_msg_seq_offset;
5220 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5221 mbedtls_ssl_hs_buffer *hs_buf;
5222 size_t msg_len = ssl->in_hslen - 12;
5223
5224 /* We should never receive an old handshake
5225 * message - double-check nonetheless. */
5226 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5227 {
5228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5230 }
5231
5232 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5233 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5234 {
5235 /* Silently ignore -- message too far in the future */
5236 MBEDTLS_SSL_DEBUG_MSG( 2,
5237 ( "Ignore future HS message with sequence number %u, "
5238 "buffering window %u - %u",
5239 recv_msg_seq, ssl->handshake->in_msg_seq,
5240 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5241
5242 goto exit;
5243 }
5244
5245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5246 recv_msg_seq, recv_msg_seq_offset ) );
5247
5248 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5249
5250 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005251 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005252 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005253 size_t reassembly_buf_sz;
5254
Hanno Becker37f95322018-08-16 13:55:32 +01005255 hs_buf->is_fragmented =
5256 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5257
5258 /* We copy the message back into the input buffer
5259 * after reassembly, so check that it's not too large.
5260 * This is an implementation-specific limitation
5261 * and not one from the standard, hence it is not
5262 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005263 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005264 {
5265 /* Ignore message */
5266 goto exit;
5267 }
5268
Hanno Beckere0b150f2018-08-21 15:51:03 +01005269 /* Check if we have enough space to buffer the message. */
5270 if( hs->buffering.total_bytes_buffered >
5271 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5272 {
5273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5275 }
5276
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005277 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5278 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005279
5280 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5281 hs->buffering.total_bytes_buffered ) )
5282 {
5283 if( recv_msg_seq_offset > 0 )
5284 {
5285 /* If we can't buffer a future message because
5286 * of space limitations -- ignore. */
5287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5288 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5289 (unsigned) hs->buffering.total_bytes_buffered ) );
5290 goto exit;
5291 }
Hanno Beckere1801392018-08-21 16:51:05 +01005292 else
5293 {
5294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5295 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5296 (unsigned) hs->buffering.total_bytes_buffered ) );
5297 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005298
Hanno Beckera02b0b42018-08-21 17:20:27 +01005299 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005300 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5302 (unsigned) msg_len,
5303 (unsigned) reassembly_buf_sz,
5304 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005305 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005306 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5307 goto exit;
5308 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005309 }
5310
5311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5312 msg_len ) );
5313
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005314 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5315 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005316 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005317 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005318 goto exit;
5319 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005320 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005321
5322 /* Prepare final header: copy msg_type, length and message_seq,
5323 * then add standardised fragment_offset and fragment_length */
5324 memcpy( hs_buf->data, ssl->in_msg, 6 );
5325 memset( hs_buf->data + 6, 0, 3 );
5326 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5327
5328 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005329
5330 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005331 }
5332 else
5333 {
5334 /* Make sure msg_type and length are consistent */
5335 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5336 {
5337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5338 /* Ignore */
5339 goto exit;
5340 }
5341 }
5342
Hanno Becker4422bbb2018-08-20 09:40:19 +01005343 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005344 {
5345 size_t frag_len, frag_off;
5346 unsigned char * const msg = hs_buf->data + 12;
5347
5348 /*
5349 * Check and copy current fragment
5350 */
5351
5352 /* Validation of header fields already done in
5353 * mbedtls_ssl_prepare_handshake_record(). */
5354 frag_off = ssl_get_hs_frag_off( ssl );
5355 frag_len = ssl_get_hs_frag_len( ssl );
5356
5357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5358 frag_off, frag_len ) );
5359 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5360
5361 if( hs_buf->is_fragmented )
5362 {
5363 unsigned char * const bitmask = msg + msg_len;
5364 ssl_bitmask_set( bitmask, frag_off, frag_len );
5365 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5366 msg_len ) == 0 );
5367 }
5368 else
5369 {
5370 hs_buf->is_complete = 1;
5371 }
5372
5373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5374 hs_buf->is_complete ? "" : "not yet " ) );
5375 }
5376
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005377 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005378 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005379
5380 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005381 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005382 break;
5383 }
5384
5385exit:
5386
5387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5388 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005389}
5390#endif /* MBEDTLS_SSL_PROTO_DTLS */
5391
Hanno Becker1097b342018-08-15 14:09:41 +01005392static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005393{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005394 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005395 * Consume last content-layer message and potentially
5396 * update in_msglen which keeps track of the contents'
5397 * consumption state.
5398 *
5399 * (1) Handshake messages:
5400 * Remove last handshake message, move content
5401 * and adapt in_msglen.
5402 *
5403 * (2) Alert messages:
5404 * Consume whole record content, in_msglen = 0.
5405 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005406 * (3) Change cipher spec:
5407 * Consume whole record content, in_msglen = 0.
5408 *
5409 * (4) Application data:
5410 * Don't do anything - the record layer provides
5411 * the application data as a stream transport
5412 * and consumes through mbedtls_ssl_read only.
5413 *
5414 */
5415
5416 /* Case (1): Handshake messages */
5417 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005418 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005419 /* Hard assertion to be sure that no application data
5420 * is in flight, as corrupting ssl->in_msglen during
5421 * ssl->in_offt != NULL is fatal. */
5422 if( ssl->in_offt != NULL )
5423 {
5424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5426 }
5427
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005428 /*
5429 * Get next Handshake message in the current record
5430 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005431
Hanno Becker4a810fb2017-05-24 16:27:30 +01005432 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005433 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434 * current handshake content: If DTLS handshake
5435 * fragmentation is used, that's the fragment
5436 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005437 * size here is faulty and should be changed at
5438 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005439 * (2) While it doesn't seem to cause problems, one
5440 * has to be very careful not to assume that in_hslen
5441 * is always <= in_msglen in a sensible communication.
5442 * Again, it's wrong for DTLS handshake fragmentation.
5443 * The following check is therefore mandatory, and
5444 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005445 * Additionally, ssl->in_hslen might be arbitrarily out of
5446 * bounds after handling a DTLS message with an unexpected
5447 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005448 */
5449 if( ssl->in_hslen < ssl->in_msglen )
5450 {
5451 ssl->in_msglen -= ssl->in_hslen;
5452 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5453 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005454
Hanno Becker4a810fb2017-05-24 16:27:30 +01005455 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5456 ssl->in_msg, ssl->in_msglen );
5457 }
5458 else
5459 {
5460 ssl->in_msglen = 0;
5461 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005462
Hanno Becker4a810fb2017-05-24 16:27:30 +01005463 ssl->in_hslen = 0;
5464 }
5465 /* Case (4): Application data */
5466 else if( ssl->in_offt != NULL )
5467 {
5468 return( 0 );
5469 }
5470 /* Everything else (CCS & Alerts) */
5471 else
5472 {
5473 ssl->in_msglen = 0;
5474 }
5475
Hanno Becker1097b342018-08-15 14:09:41 +01005476 return( 0 );
5477}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005478
Hanno Beckere74d5562018-08-15 14:26:08 +01005479static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5480{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005481 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005482 return( 1 );
5483
5484 return( 0 );
5485}
5486
Hanno Becker5f066e72018-08-16 14:56:31 +01005487#if defined(MBEDTLS_SSL_PROTO_DTLS)
5488
5489static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5490{
5491 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5492 if( hs == NULL )
5493 return;
5494
Hanno Becker01315ea2018-08-21 17:22:17 +01005495 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005496 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005497 hs->buffering.total_bytes_buffered -=
5498 hs->buffering.future_record.len;
5499
5500 mbedtls_free( hs->buffering.future_record.data );
5501 hs->buffering.future_record.data = NULL;
5502 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005503}
5504
5505static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5506{
5507 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5508 unsigned char * rec;
5509 size_t rec_len;
5510 unsigned rec_epoch;
5511
5512 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5513 return( 0 );
5514
5515 if( hs == NULL )
5516 return( 0 );
5517
Hanno Becker5f066e72018-08-16 14:56:31 +01005518 rec = hs->buffering.future_record.data;
5519 rec_len = hs->buffering.future_record.len;
5520 rec_epoch = hs->buffering.future_record.epoch;
5521
5522 if( rec == NULL )
5523 return( 0 );
5524
Hanno Becker4cb782d2018-08-20 11:19:05 +01005525 /* Only consider loading future records if the
5526 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005527 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005528 return( 0 );
5529
Hanno Becker5f066e72018-08-16 14:56:31 +01005530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5531
5532 if( rec_epoch != ssl->in_epoch )
5533 {
5534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5535 goto exit;
5536 }
5537
5538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5539
5540 /* Double-check that the record is not too large */
5541 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5542 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5543 {
5544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5546 }
5547
5548 memcpy( ssl->in_hdr, rec, rec_len );
5549 ssl->in_left = rec_len;
5550 ssl->next_record_offset = 0;
5551
5552 ssl_free_buffered_record( ssl );
5553
5554exit:
5555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5556 return( 0 );
5557}
5558
5559static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5560{
5561 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5562 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005563 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005564
5565 /* Don't buffer future records outside handshakes. */
5566 if( hs == NULL )
5567 return( 0 );
5568
5569 /* Only buffer handshake records (we are only interested
5570 * in Finished messages). */
5571 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5572 return( 0 );
5573
5574 /* Don't buffer more than one future epoch record. */
5575 if( hs->buffering.future_record.data != NULL )
5576 return( 0 );
5577
Hanno Becker01315ea2018-08-21 17:22:17 +01005578 /* Don't buffer record if there's not enough buffering space remaining. */
5579 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5580 hs->buffering.total_bytes_buffered ) )
5581 {
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5583 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5584 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005585 return( 0 );
5586 }
5587
Hanno Becker5f066e72018-08-16 14:56:31 +01005588 /* Buffer record */
5589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5590 ssl->in_epoch + 1 ) );
5591 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5592 rec_hdr_len + ssl->in_msglen );
5593
5594 /* ssl_parse_record_header() only considers records
5595 * of the next epoch as candidates for buffering. */
5596 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005597 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005598
5599 hs->buffering.future_record.data =
5600 mbedtls_calloc( 1, hs->buffering.future_record.len );
5601 if( hs->buffering.future_record.data == NULL )
5602 {
5603 /* If we run out of RAM trying to buffer a
5604 * record from the next epoch, just ignore. */
5605 return( 0 );
5606 }
5607
Hanno Becker01315ea2018-08-21 17:22:17 +01005608 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005609
Hanno Becker01315ea2018-08-21 17:22:17 +01005610 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005611 return( 0 );
5612}
5613
5614#endif /* MBEDTLS_SSL_PROTO_DTLS */
5615
Hanno Beckere74d5562018-08-15 14:26:08 +01005616static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005617{
5618 int ret;
5619
Hanno Becker5f066e72018-08-16 14:56:31 +01005620#if defined(MBEDTLS_SSL_PROTO_DTLS)
5621 /* We might have buffered a future record; if so,
5622 * and if the epoch matches now, load it.
5623 * On success, this call will set ssl->in_left to
5624 * the length of the buffered record, so that
5625 * the calls to ssl_fetch_input() below will
5626 * essentially be no-ops. */
5627 ret = ssl_load_buffered_record( ssl );
5628 if( ret != 0 )
5629 return( ret );
5630#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005631
Hanno Becker8b09b732019-05-08 12:03:28 +01005632 /* Reset in pointers to default state for TLS/DTLS records,
5633 * assuming no CID and no offset between record content and
5634 * record plaintext. */
5635 ssl_update_in_pointers( ssl );
5636
5637 /* Ensure that we have enough space available for the default form
5638 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5639 * with no space for CIDs counted in). */
5640 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5641 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005644 return( ret );
5645 }
5646
5647 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5651 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005652 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005653 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5654 {
5655 ret = ssl_buffer_future_record( ssl );
5656 if( ret != 0 )
5657 return( ret );
5658
5659 /* Fall through to handling of unexpected records */
5660 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5661 }
5662
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005663 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5664 {
5665 /* Skip unexpected record (but not whole datagram) */
5666 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005667 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005668
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5670 "(header)" ) );
5671 }
5672 else
5673 {
5674 /* Skip invalid record and the rest of the datagram */
5675 ssl->next_record_offset = 0;
5676 ssl->in_left = 0;
5677
5678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5679 "(header)" ) );
5680 }
5681
5682 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005683 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005684 }
5685#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005686 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005687 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005688
5689 /*
5690 * Read and optionally decrypt the message contents
5691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005693 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005696 return( ret );
5697 }
5698
5699 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005701 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005702 {
Hanno Becker43395762019-05-03 14:46:38 +01005703 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005704 if( ssl->next_record_offset < ssl->in_left )
5705 {
5706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5707 }
5708 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005709 else
5710#endif
5711 ssl->in_left = 0;
5712
5713 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005716 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005717 {
5718 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005719 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005720 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005721 /* Except when waiting for Finished as a bad mac here
5722 * probably means something went wrong in the handshake
5723 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5724 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5725 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5726 {
5727#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5728 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5729 {
5730 mbedtls_ssl_send_alert_message( ssl,
5731 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5732 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5733 }
5734#endif
5735 return( ret );
5736 }
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005739 if( ssl->conf->badmac_limit != 0 &&
5740 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5743 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005744 }
5745#endif
5746
Hanno Becker4a810fb2017-05-24 16:27:30 +01005747 /* As above, invalid records cause
5748 * dismissal of the whole datagram. */
5749
5750 ssl->next_record_offset = 0;
5751 ssl->in_left = 0;
5752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005754 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005755 }
5756
5757 return( ret );
5758 }
5759 else
5760#endif
5761 {
5762 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5764 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 mbedtls_ssl_send_alert_message( ssl,
5767 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5768 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005769 }
5770#endif
5771 return( ret );
5772 }
5773 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005774
Simon Butcher99000142016-10-13 17:21:01 +01005775 return( 0 );
5776}
5777
5778int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5779{
5780 int ret;
5781
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005782 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005783 * Handle particular types of records
5784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005786 {
Simon Butcher99000142016-10-13 17:21:01 +01005787 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5788 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005789 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 }
5792
Hanno Beckere678eaa2018-08-21 14:57:46 +01005793 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005794 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005795 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005796 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5798 ssl->in_msglen ) );
5799 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005800 }
5801
Hanno Beckere678eaa2018-08-21 14:57:46 +01005802 if( ssl->in_msg[0] != 1 )
5803 {
5804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5805 ssl->in_msg[0] ) );
5806 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5807 }
5808
5809#if defined(MBEDTLS_SSL_PROTO_DTLS)
5810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5811 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5812 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5813 {
5814 if( ssl->handshake == NULL )
5815 {
5816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5817 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5818 }
5819
5820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5821 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5822 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005823#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005824 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005827 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005828 if( ssl->in_msglen != 2 )
5829 {
5830 /* Note: Standard allows for more than one 2 byte alert
5831 to be packed in a single message, but Mbed TLS doesn't
5832 currently support this. */
5833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5834 ssl->in_msglen ) );
5835 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5836 }
5837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 ssl->in_msg[0], ssl->in_msg[1] ) );
5840
5841 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005842 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005847 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005849 }
5850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5852 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5855 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005856 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005857
5858#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5859 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5860 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5861 {
Hanno Becker90333da2017-10-10 11:27:13 +01005862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005863 /* Will be handled when trying to parse ServerHello */
5864 return( 0 );
5865 }
5866#endif
5867
5868#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5869 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5870 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5871 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5872 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5875 /* Will be handled in mbedtls_ssl_parse_certificate() */
5876 return( 0 );
5877 }
5878#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5879
5880 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005881 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 }
5883
Hanno Beckerc76c6192017-06-06 10:03:17 +01005884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005885 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005886 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005887 /* Drop unexpected ApplicationData records,
5888 * except at the beginning of renegotiations */
5889 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5890 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5891#if defined(MBEDTLS_SSL_RENEGOTIATION)
5892 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5893 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005894#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005895 )
5896 {
5897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5898 return( MBEDTLS_ERR_SSL_NON_FATAL );
5899 }
5900
5901 if( ssl->handshake != NULL &&
5902 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5903 {
5904 ssl_handshake_wrapup_free_hs_transform( ssl );
5905 }
5906 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005907#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005908
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 return( 0 );
5910}
5911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005913{
5914 int ret;
5915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5917 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5918 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005919 {
5920 return( ret );
5921 }
5922
5923 return( 0 );
5924}
5925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005927 unsigned char level,
5928 unsigned char message )
5929{
5930 int ret;
5931
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005932 if( ssl == NULL || ssl->conf == NULL )
5933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005939 ssl->out_msglen = 2;
5940 ssl->out_msg[0] = level;
5941 ssl->out_msg[1] = message;
5942
Hanno Becker67bc7c32018-08-06 11:33:50 +01005943 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005946 return( ret );
5947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005949
5950 return( 0 );
5951}
5952
Paul Bakker5121ce52009-01-03 21:22:43 +00005953/*
5954 * Handshake functions
5955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5957 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5958 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5959 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5960 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5961 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5962 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005963/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005965{
Hanno Becker8759e162017-12-27 21:34:08 +00005966 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5971 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005972 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5973 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005976 ssl->state++;
5977 return( 0 );
5978 }
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005982}
5983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005985{
Hanno Becker8759e162017-12-27 21:34:08 +00005986 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5991 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005992 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5993 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005996 ssl->state++;
5997 return( 0 );
5998 }
5999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006002}
Gilles Peskinef9828522017-05-03 12:28:43 +02006003
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006004#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006005/* Some certificate support -> implement write and parse */
6006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006010 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006012 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6017 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006018 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6019 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006022 ssl->state++;
6023 return( 0 );
6024 }
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006027 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006028 {
6029 if( ssl->client_auth == 0 )
6030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006032 ssl->state++;
6033 return( 0 );
6034 }
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 /*
6038 * If using SSLv3 and got no cert, send an Alert message
6039 * (otherwise an empty Certificate message will be sent).
6040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6042 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006043 {
6044 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6046 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6047 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 goto write_msg;
6051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#endif /* MBEDTLS_SSL_CLI_C */
6055#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006056 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6061 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 }
6063 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
6068 /*
6069 * 0 . 0 handshake type
6070 * 1 . 3 handshake length
6071 * 4 . 6 length of all certs
6072 * 7 . 9 length of cert. 1
6073 * 10 . n-1 peer certificate
6074 * n . n+2 length of cert. 2
6075 * n+3 . ... upper level cert, etc.
6076 */
6077 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079
Paul Bakker29087132010-03-21 21:03:34 +00006080 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 {
6082 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006083 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006086 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006088 }
6089
6090 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6091 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6092 ssl->out_msg[i + 2] = (unsigned char)( n );
6093
6094 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6095 i += n; crt = crt->next;
6096 }
6097
6098 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6099 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6100 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6101
6102 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6104 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006105
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006106#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006107write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006108#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
6110 ssl->state++;
6111
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006112 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006113 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006115 return( ret );
6116 }
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006119
Paul Bakkered27a042013-04-18 22:46:23 +02006120 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006121}
6122
Hanno Becker33c3dc82019-01-30 14:46:46 +00006123static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6124 unsigned char *crt_buf,
6125 size_t crt_buf_len )
6126{
6127 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6128
6129 if( peer_crt == NULL )
6130 return( -1 );
6131
6132 if( peer_crt->raw.len != crt_buf_len )
6133 return( -1 );
6134
6135 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len) );
6136}
6137
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006138/*
6139 * Once the certificate message is read, parse it into a cert chain and
6140 * perform basic checks, but leave actual verification to the caller
6141 */
6142static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006144 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006145 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006146 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_SSL_SRV_C)
6149#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006150 /*
6151 * Check if the client sent an empty certificate
6152 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006153 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006156 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6158 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6159 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006162
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006163 /* The client was asked for a certificate but didn't send
6164 one. The client should know what's going on, so we
6165 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006166 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006167 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 }
6169 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6173 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006174 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6178 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6179 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6180 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006183
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006184 /* The client was asked for a certificate but didn't send
6185 one. The client should know what's going on, so we
6186 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006187 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006188 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 }
6190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6192 MBEDTLS_SSL_PROTO_TLS1_2 */
6193#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006198 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6199 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 }
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6204 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006207 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6208 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210 }
6211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006213
Paul Bakker5121ce52009-01-03 21:22:43 +00006214 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006216 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006217 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006218
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006219 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006223 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6224 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 }
6227
Hanno Becker33c3dc82019-01-30 14:46:46 +00006228 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6229 i += 3;
6230
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006231 /* In case we tried to reuse a session but it failed */
6232 if( ssl->session_negotiate->peer_cert != NULL )
6233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6235 mbedtls_free( ssl->session_negotiate->peer_cert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006236 ssl->session_negotiate->peer_cert = NULL;
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006237 }
6238
Hanno Becker33c3dc82019-01-30 14:46:46 +00006239 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 while( i < ssl->in_hslen )
6241 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006242 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006243 if ( i + 3 > ssl->in_hslen ) {
6244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6245 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker33c3dc82019-01-30 14:46:46 +00006246 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006247 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6248 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006249 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6250 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006251 if( ssl->in_msg[i] != 0 )
6252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6255 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006257 }
6258
Hanno Becker33c3dc82019-01-30 14:46:46 +00006259 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6261 | (unsigned int) ssl->in_msg[i + 2];
6262 i += 3;
6263
6264 if( n < 128 || i + n > ssl->in_hslen )
6265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006267 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6268 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006270 }
6271
Hanno Becker33c3dc82019-01-30 14:46:46 +00006272 /* Check if we're handling the first CRT in the chain. */
6273 if( ssl->session_negotiate->peer_cert == NULL )
6274 {
6275 /* During client-side renegotiation, check the server's end-CRTs
6276 * hasn't changed compared to the initial handshake, mitigating
6277 * the triple handshake attack. On success, reuse the original
6278 * end-CRT instead of parsing it again. */
6279#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6281 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6282 {
6283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6284 if( ssl_check_peer_crt_unchanged( ssl, &ssl->in_msg[i], n ) != 0 )
6285 {
6286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6287 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6288 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6289 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6290 }
6291
6292 /* Move CRT chain structure to new session instance. */
6293 ssl->session_negotiate->peer_cert = ssl->session->peer_cert;
6294 ssl->session->peer_cert = NULL;
6295
6296 /* Delete all remaining CRTs from the original CRT chain. */
6297 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert->next );
6298 ssl->session_negotiate->peer_cert->next = NULL;
6299
6300 i += n;
6301 continue;
6302 }
6303#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6304
6305 /* Outside of client-side renegotiation, create a fresh X.509 CRT
6306 * instance to parse the end-CRT into. */
6307
6308 ssl->session_negotiate->peer_cert =
6309 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6310 if( ssl->session_negotiate->peer_cert == NULL )
6311 {
6312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6313 sizeof( mbedtls_x509_crt ) ) );
6314 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6315 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6316 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6317 }
6318
6319 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
6320
6321 /* Intentional fall through */
6322 }
6323
6324 /* Parse the next certificate in the chain. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Hanno Becker33c3dc82019-01-30 14:46:46 +00006326 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006327 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006329 case 0: /*ok*/
6330 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6331 /* Ignore certificate with an unknown algorithm: maybe a
6332 prior certificate was already trusted. */
6333 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006334
Hanno Becker33c3dc82019-01-30 14:46:46 +00006335 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6336 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6337 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006338
Hanno Becker33c3dc82019-01-30 14:46:46 +00006339 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6340 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6341 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006342
Hanno Becker33c3dc82019-01-30 14:46:46 +00006343 default:
6344 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6345 crt_parse_der_failed:
6346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6347 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6348 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 }
6350
6351 i += n;
6352 }
6353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006355 return( 0 );
6356}
6357
6358int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6359{
6360 int ret;
6361 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006362 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006363#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6364 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6365 ? ssl->handshake->sni_authmode
6366 : ssl->conf->authmode;
6367#else
6368 const int authmode = ssl->conf->authmode;
6369#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006370 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006371
6372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6373
6374 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6375 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6376 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6377 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6378 {
6379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6380 ssl->state++;
6381 return( 0 );
6382 }
6383
6384#if defined(MBEDTLS_SSL_SRV_C)
6385 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6386 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6387 {
6388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6389 ssl->state++;
6390 return( 0 );
6391 }
6392
6393 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6394 authmode == MBEDTLS_SSL_VERIFY_NONE )
6395 {
6396 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006398
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006399 ssl->state++;
6400 return( 0 );
6401 }
6402#endif
6403
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006404#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6405 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006406 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006407 {
6408 goto crt_verify;
6409 }
6410#endif
6411
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006412 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006413 {
6414 /* mbedtls_ssl_read_record may have sent an alert already. We
6415 let it decide whether to alert. */
6416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6417 return( ret );
6418 }
6419
6420 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6421 {
6422#if defined(MBEDTLS_SSL_SRV_C)
6423 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6424 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6425 {
6426 ret = 0;
6427 }
6428#endif
6429
6430 ssl->state++;
6431 return( ret );
6432 }
6433
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006434#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6435 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006436 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006437
6438crt_verify:
6439 if( ssl->handshake->ecrs_enabled)
6440 rs_ctx = &ssl->handshake->ecrs_ctx;
6441#endif
6442
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006443 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006444 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006445 mbedtls_x509_crt *ca_chain;
6446 mbedtls_x509_crl *ca_crl;
6447
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006448#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006449 if( ssl->handshake->sni_ca_chain != NULL )
6450 {
6451 ca_chain = ssl->handshake->sni_ca_chain;
6452 ca_crl = ssl->handshake->sni_ca_crl;
6453 }
6454 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006455#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006456 {
6457 ca_chain = ssl->conf->ca_chain;
6458 ca_crl = ssl->conf->ca_crl;
6459 }
6460
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006461 /*
6462 * Main check: verify certificate
6463 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006464 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006465 ssl->session_negotiate->peer_cert,
6466 ca_chain, ca_crl,
6467 ssl->conf->cert_profile,
6468 ssl->hostname,
6469 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006470 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006471
6472 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006475 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006476
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006477#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6478 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006479 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006480#endif
6481
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006482 /*
6483 * Secondary checks: always done, but change 'ret' only if it was 0
6484 */
6485
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006486#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006489
6490 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006492 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006493 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006494 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006497 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006499 }
6500 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006501#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006504 ciphersuite_info,
6505 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006506 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006509 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006511 }
6512
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006513 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6514 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6515 * with details encoded in the verification flags. All other kinds
6516 * of error codes, including those from the user provided f_vrfy
6517 * functions, are treated as fatal and lead to a failure of
6518 * ssl_parse_certificate even if verification was optional. */
6519 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6520 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6521 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6522 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006524 }
6525
6526 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6527 {
6528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6529 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6530 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006531
6532 if( ret != 0 )
6533 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006534 uint8_t alert;
6535
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006536 /* The certificate may have been rejected for several reasons.
6537 Pick one and send the corresponding alert. Which alert to send
6538 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006539 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6540 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6541 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6542 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6543 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6544 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6545 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6546 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6547 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6548 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6549 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6550 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6551 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6552 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6553 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6554 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6555 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6556 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6557 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6558 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006559 else
6560 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6562 alert );
6563 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006564
Hanno Beckere6706e62017-05-15 16:05:15 +01006565#if defined(MBEDTLS_DEBUG_C)
6566 if( ssl->session_negotiate->verify_result != 0 )
6567 {
6568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6569 ssl->session_negotiate->verify_result ) );
6570 }
6571 else
6572 {
6573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6574 }
6575#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006576 }
6577
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006578 ssl->state++;
6579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581
6582 return( ret );
6583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6585 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6586 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6587 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6588 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6589 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6590 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006593{
6594 int ret;
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006599 ssl->out_msglen = 1;
6600 ssl->out_msg[0] = 1;
6601
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 ssl->state++;
6603
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006604 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 return( ret );
6608 }
6609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006611
6612 return( 0 );
6613}
6614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006616{
6617 int ret;
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006620
Hanno Becker327c93b2018-08-15 13:56:18 +01006621 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006624 return( ret );
6625 }
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006630 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6631 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006633 }
6634
Hanno Beckere678eaa2018-08-21 14:57:46 +01006635 /* CCS records are only accepted if they have length 1 and content '1',
6636 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006637
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006638 /*
6639 * Switch to our negotiated transform and session parameters for inbound
6640 * data.
6641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006643 ssl->transform_in = ssl->transform_negotiate;
6644 ssl->session_in = ssl->session_negotiate;
6645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006647 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006650 ssl_dtls_replay_reset( ssl );
6651#endif
6652
6653 /* Increment epoch */
6654 if( ++ssl->in_epoch == 0 )
6655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006657 /* This is highly unlikely to happen for legitimate reasons, so
6658 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006660 }
6661 }
6662 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006664 memset( ssl->in_ctr, 0, 8 );
6665
Hanno Beckerf5970a02019-05-08 09:38:41 +01006666 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6669 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006674 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6675 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006677 }
6678 }
6679#endif
6680
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 ssl->state++;
6682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684
6685 return( 0 );
6686}
6687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6689 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006690{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006691 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6694 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6695 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006696 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006697 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6700#if defined(MBEDTLS_SHA512_C)
6701 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006702 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6703 else
6704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if defined(MBEDTLS_SHA256_C)
6706 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006707 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006708 else
6709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006713 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006714 }
Paul Bakker380da532012-04-18 16:10:25 +00006715}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6720 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006721 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6722 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6725#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006726 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006729 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006732}
6733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006735 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6738 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006739 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6740 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6743#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006744 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006747 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006750}
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6753 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6754static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006755 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006756{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006757 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6758 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006759}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006760#endif
Paul Bakker380da532012-04-18 16:10:25 +00006761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6763#if defined(MBEDTLS_SHA256_C)
6764static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006765 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006766{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006767 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006768}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006769#endif
Paul Bakker380da532012-04-18 16:10:25 +00006770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771#if defined(MBEDTLS_SHA512_C)
6772static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006773 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006774{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006775 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006776}
Paul Bakker769075d2012-11-24 11:26:46 +01006777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006781static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006783{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006784 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006785 mbedtls_md5_context md5;
6786 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006787
Paul Bakker5121ce52009-01-03 21:22:43 +00006788 unsigned char padbuf[48];
6789 unsigned char md5sum[16];
6790 unsigned char sha1sum[20];
6791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006793 if( !session )
6794 session = ssl->session;
6795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006797
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006798 mbedtls_md5_init( &md5 );
6799 mbedtls_sha1_init( &sha1 );
6800
6801 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6802 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803
6804 /*
6805 * SSLv3:
6806 * hash =
6807 * MD5( master + pad2 +
6808 * MD5( handshake + sender + master + pad1 ) )
6809 * + SHA1( master + pad2 +
6810 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 */
6812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006814 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6815 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006819 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6820 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006824 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006825
Paul Bakker1ef83d62012-04-11 12:09:53 +00006826 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006828 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6829 mbedtls_md5_update_ret( &md5, session->master, 48 );
6830 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6831 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006833 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6834 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6835 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6836 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Paul Bakker1ef83d62012-04-11 12:09:53 +00006838 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006839
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006840 mbedtls_md5_starts_ret( &md5 );
6841 mbedtls_md5_update_ret( &md5, session->master, 48 );
6842 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6843 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6844 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006845
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006846 mbedtls_sha1_starts_ret( &sha1 );
6847 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6848 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6849 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6850 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006854 mbedtls_md5_free( &md5 );
6855 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006857 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6858 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6859 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006866static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006868{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006869 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006870 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006871 mbedtls_md5_context md5;
6872 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006873 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006876 if( !session )
6877 session = ssl->session;
6878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006880
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006881 mbedtls_md5_init( &md5 );
6882 mbedtls_sha1_init( &sha1 );
6883
6884 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6885 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006886
Paul Bakker1ef83d62012-04-11 12:09:53 +00006887 /*
6888 * TLSv1:
6889 * hash = PRF( master, finished_label,
6890 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6891 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006894 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6895 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006896#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006899 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6900 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006901#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006904 ? "client finished"
6905 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006906
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006907 mbedtls_md5_finish_ret( &md5, padbuf );
6908 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006909
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006910 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006911 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006914
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006915 mbedtls_md5_free( &md5 );
6916 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006917
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006918 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6925#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006926static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928{
6929 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006930 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006931 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006932 unsigned char padbuf[32];
6933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006935 if( !session )
6936 session = ssl->session;
6937
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006938 mbedtls_sha256_init( &sha256 );
6939
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006941
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006942 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006943
6944 /*
6945 * TLSv1.2:
6946 * hash = PRF( master, finished_label,
6947 * Hash( handshake ) )[0.11]
6948 */
6949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if !defined(MBEDTLS_SHA256_ALT)
6951 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006952 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006953#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006956 ? "client finished"
6957 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006958
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006959 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006960
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006961 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006962 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006965
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006966 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006967
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006968 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006975static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006977{
6978 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006979 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006980 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006981 unsigned char padbuf[48];
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006984 if( !session )
6985 session = ssl->session;
6986
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006987 mbedtls_sha512_init( &sha512 );
6988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006990
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006991 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006992
6993 /*
6994 * TLSv1.2:
6995 * hash = PRF( master, finished_label,
6996 * Hash( handshake ) )[0.11]
6997 */
6998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007000 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7001 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007002#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007005 ? "client finished"
7006 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007007
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007008 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007009
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007010 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007011 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007014
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007015 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007016
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007017 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007020}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021#endif /* MBEDTLS_SHA512_C */
7022#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007027
7028 /*
7029 * Free our handshake params
7030 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007031 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007033 ssl->handshake = NULL;
7034
7035 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007036 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007037 */
7038 if( ssl->transform )
7039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 mbedtls_ssl_transform_free( ssl->transform );
7041 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007042 }
7043 ssl->transform = ssl->transform_negotiate;
7044 ssl->transform_negotiate = NULL;
7045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007047}
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007050{
7051 int resume = ssl->handshake->resume;
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055#if defined(MBEDTLS_SSL_RENEGOTIATION)
7056 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007059 ssl->renego_records_seen = 0;
7060 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007061#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007062
7063 /*
7064 * Free the previous session and switch in the current one
7065 */
Paul Bakker0a597072012-09-25 21:55:46 +00007066 if( ssl->session )
7067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007069 /* RFC 7366 3.1: keep the EtM state */
7070 ssl->session_negotiate->encrypt_then_mac =
7071 ssl->session->encrypt_then_mac;
7072#endif
7073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 mbedtls_ssl_session_free( ssl->session );
7075 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007076 }
7077 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007078 ssl->session_negotiate = NULL;
7079
Paul Bakker0a597072012-09-25 21:55:46 +00007080 /*
7081 * Add cache entry
7082 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007083 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007084 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007085 resume == 0 )
7086 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007087 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007089 }
Paul Bakker0a597072012-09-25 21:55:46 +00007090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007093 ssl->handshake->flight != NULL )
7094 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007095 /* Cancel handshake timer */
7096 ssl_set_timer( ssl, 0 );
7097
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007098 /* Keep last flight around in case we need to resend it:
7099 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007101 }
7102 else
7103#endif
7104 ssl_handshake_wrapup_free_hs_transform( ssl );
7105
Paul Bakker48916f92012-09-16 19:57:18 +00007106 ssl->state++;
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007109}
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007112{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007113 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007116
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007117 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007118
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007119 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007120
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007121 /*
7122 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7123 * may define some other value. Currently (early 2016), no defined
7124 * ciphersuite does this (and this is unlikely to change as activity has
7125 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007130 ssl->verify_data_len = hash_len;
7131 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007132#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007133
Paul Bakker5121ce52009-01-03 21:22:43 +00007134 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7136 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007137
7138 /*
7139 * In case of session resuming, invert the client and server
7140 * ChangeCipherSpec messages order.
7141 */
Paul Bakker0a597072012-09-25 21:55:46 +00007142 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007145 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007149 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007152 }
7153 else
7154 ssl->state++;
7155
Paul Bakker48916f92012-09-16 19:57:18 +00007156 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007157 * Switch to our negotiated transform and session parameters for outbound
7158 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007164 {
7165 unsigned char i;
7166
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007167 /* Remember current epoch settings for resending */
7168 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007169 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007170
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007171 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007172 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007173
7174 /* Increment epoch */
7175 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007176 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007177 break;
7178
7179 /* The loop goes to its end iff the counter is wrapping */
7180 if( i == 0 )
7181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7183 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007184 }
7185 }
7186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007188 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007189
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007190 ssl->transform_out = ssl->transform_negotiate;
7191 ssl->session_out = ssl->session_negotiate;
7192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7194 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7199 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007200 }
7201 }
7202#endif
7203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007205 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007207#endif
7208
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007209 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007210 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007212 return( ret );
7213 }
7214
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007215#if defined(MBEDTLS_SSL_PROTO_DTLS)
7216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7217 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7218 {
7219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7220 return( ret );
7221 }
7222#endif
7223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007225
7226 return( 0 );
7227}
7228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007230#define SSL_MAX_HASH_LEN 36
7231#else
7232#define SSL_MAX_HASH_LEN 12
7233#endif
7234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007236{
Paul Bakker23986e52011-04-24 08:57:21 +00007237 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007238 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007239 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007242
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007243 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007244
Hanno Becker327c93b2018-08-15 13:56:18 +01007245 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007248 return( ret );
7249 }
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7255 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007257 }
7258
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007259 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260#if defined(MBEDTLS_SSL_PROTO_SSL3)
7261 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007262 hash_len = 36;
7263 else
7264#endif
7265 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7268 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007271 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7272 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007274 }
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007277 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007280 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007283 }
7284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007286 ssl->verify_data_len = hash_len;
7287 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007288#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007289
Paul Bakker0a597072012-09-25 21:55:46 +00007290 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007293 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007295#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007297 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007299#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007300 }
7301 else
7302 ssl->state++;
7303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007307#endif
7308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007310
7311 return( 0 );
7312}
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7319 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7320 mbedtls_md5_init( &handshake->fin_md5 );
7321 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007322 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7323 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7326#if defined(MBEDTLS_SHA256_C)
7327 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007328 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007329#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#if defined(MBEDTLS_SHA512_C)
7331 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007332 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007335
7336 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007337
7338#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7339 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7340 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7341#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#if defined(MBEDTLS_DHM_C)
7344 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007345#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346#if defined(MBEDTLS_ECDH_C)
7347 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007348#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007349#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007350 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007351#if defined(MBEDTLS_SSL_CLI_C)
7352 handshake->ecjpake_cache = NULL;
7353 handshake->ecjpake_cache_len = 0;
7354#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007355#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007356
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007357#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007358 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007359#endif
7360
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007361#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7362 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7363#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007364}
7365
Hanno Becker611a83b2018-01-03 14:27:32 +00007366void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7371 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007372
Hanno Becker92231322018-01-03 15:32:51 +00007373#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 mbedtls_md_init( &transform->md_ctx_enc );
7375 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007376#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007377}
7378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007382}
7383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007385{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007386 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007387 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007389 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007391 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007392 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007393
7394 /*
7395 * Either the pointers are now NULL or cleared properly and can be freed.
7396 * Now allocate missing structures.
7397 */
7398 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007399 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007400 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007401 }
Paul Bakker48916f92012-09-16 19:57:18 +00007402
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007403 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007404 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007405 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007406 }
Paul Bakker48916f92012-09-16 19:57:18 +00007407
Paul Bakker82788fb2014-10-20 13:59:19 +02007408 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007409 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007410 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007411 }
Paul Bakker48916f92012-09-16 19:57:18 +00007412
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007413 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007414 if( ssl->handshake == NULL ||
7415 ssl->transform_negotiate == NULL ||
7416 ssl->session_negotiate == NULL )
7417 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 mbedtls_free( ssl->handshake );
7421 mbedtls_free( ssl->transform_negotiate );
7422 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007423
7424 ssl->handshake = NULL;
7425 ssl->transform_negotiate = NULL;
7426 ssl->session_negotiate = NULL;
7427
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007428 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007429 }
7430
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007431 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007433 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007434 ssl_handshake_params_init( ssl->handshake );
7435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7438 {
7439 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007440
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007441 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7442 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7443 else
7444 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007445
7446 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007447 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007448#endif
7449
Paul Bakker48916f92012-09-16 19:57:18 +00007450 return( 0 );
7451}
7452
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007453#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007454/* Dummy cookie callbacks for defaults */
7455static int ssl_cookie_write_dummy( void *ctx,
7456 unsigned char **p, unsigned char *end,
7457 const unsigned char *cli_id, size_t cli_id_len )
7458{
7459 ((void) ctx);
7460 ((void) p);
7461 ((void) end);
7462 ((void) cli_id);
7463 ((void) cli_id_len);
7464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007466}
7467
7468static int ssl_cookie_check_dummy( void *ctx,
7469 const unsigned char *cookie, size_t cookie_len,
7470 const unsigned char *cli_id, size_t cli_id_len )
7471{
7472 ((void) ctx);
7473 ((void) cookie);
7474 ((void) cookie_len);
7475 ((void) cli_id);
7476 ((void) cli_id_len);
7477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007479}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007480#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007481
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007482/* Once ssl->out_hdr as the address of the beginning of the
7483 * next outgoing record is set, deduce the other pointers.
7484 *
7485 * Note: For TLS, we save the implicit record sequence number
7486 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7487 * and the caller has to make sure there's space for this.
7488 */
7489
7490static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7491 mbedtls_ssl_transform *transform )
7492{
7493#if defined(MBEDTLS_SSL_PROTO_DTLS)
7494 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7495 {
7496 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007497#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007498 ssl->out_cid = ssl->out_ctr + 8;
7499 ssl->out_len = ssl->out_cid;
7500 if( transform != NULL )
7501 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007502#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007503 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007504#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007505 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007506 }
7507 else
7508#endif
7509 {
7510 ssl->out_ctr = ssl->out_hdr - 8;
7511 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007512#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007513 ssl->out_cid = ssl->out_len;
7514#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007515 ssl->out_iv = ssl->out_hdr + 5;
7516 }
7517
7518 /* Adjust out_msg to make space for explicit IV, if used. */
7519 if( transform != NULL &&
7520 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7521 {
7522 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7523 }
7524 else
7525 ssl->out_msg = ssl->out_iv;
7526}
7527
7528/* Once ssl->in_hdr as the address of the beginning of the
7529 * next incoming record is set, deduce the other pointers.
7530 *
7531 * Note: For TLS, we save the implicit record sequence number
7532 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7533 * and the caller has to make sure there's space for this.
7534 */
7535
Hanno Beckerf5970a02019-05-08 09:38:41 +01007536static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007537{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007538 /* This function sets the pointers to match the case
7539 * of unprotected TLS/DTLS records, with both ssl->in_iv
7540 * and ssl->in_msg pointing to the beginning of the record
7541 * content.
7542 *
7543 * When decrypting a protected record, ssl->in_msg
7544 * will be shifted to point to the beginning of the
7545 * record plaintext.
7546 */
7547
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007548#if defined(MBEDTLS_SSL_PROTO_DTLS)
7549 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7550 {
Hanno Becker70e79282019-05-03 14:34:53 +01007551 /* This sets the header pointers to match records
7552 * without CID. When we receive a record containing
7553 * a CID, the fields are shifted accordingly in
7554 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007555 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007556#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007557 ssl->in_cid = ssl->in_ctr + 8;
7558 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007559#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007560 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007561#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007562 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007563 }
7564 else
7565#endif
7566 {
7567 ssl->in_ctr = ssl->in_hdr - 8;
7568 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007569#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007570 ssl->in_cid = ssl->in_len;
7571#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007572 ssl->in_iv = ssl->in_hdr + 5;
7573 }
7574
Hanno Beckerf5970a02019-05-08 09:38:41 +01007575 /* This will be adjusted at record decryption time. */
7576 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007577}
7578
Paul Bakker5121ce52009-01-03 21:22:43 +00007579/*
7580 * Initialize an SSL context
7581 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007582void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7583{
7584 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7585}
7586
7587/*
7588 * Setup an SSL context
7589 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007590
7591static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7592{
7593 /* Set the incoming and outgoing record pointers. */
7594#if defined(MBEDTLS_SSL_PROTO_DTLS)
7595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7596 {
7597 ssl->out_hdr = ssl->out_buf;
7598 ssl->in_hdr = ssl->in_buf;
7599 }
7600 else
7601#endif /* MBEDTLS_SSL_PROTO_DTLS */
7602 {
7603 ssl->out_hdr = ssl->out_buf + 8;
7604 ssl->in_hdr = ssl->in_buf + 8;
7605 }
7606
7607 /* Derive other internal pointers. */
7608 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007609 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007610}
7611
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007612int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007613 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007614{
Paul Bakker48916f92012-09-16 19:57:18 +00007615 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007616
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007617 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007618
7619 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007620 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007621 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007622
7623 /* Set to NULL in case of an error condition */
7624 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007625
Angus Grattond8213d02016-05-25 20:56:48 +10007626 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7627 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007628 {
Angus Grattond8213d02016-05-25 20:56:48 +10007629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007630 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007631 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007632 }
7633
7634 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7635 if( ssl->out_buf == NULL )
7636 {
7637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007638 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007639 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007640 }
7641
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007642 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007643
Paul Bakker48916f92012-09-16 19:57:18 +00007644 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007645 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007646
7647 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007648
7649error:
7650 mbedtls_free( ssl->in_buf );
7651 mbedtls_free( ssl->out_buf );
7652
7653 ssl->conf = NULL;
7654
7655 ssl->in_buf = NULL;
7656 ssl->out_buf = NULL;
7657
7658 ssl->in_hdr = NULL;
7659 ssl->in_ctr = NULL;
7660 ssl->in_len = NULL;
7661 ssl->in_iv = NULL;
7662 ssl->in_msg = NULL;
7663
7664 ssl->out_hdr = NULL;
7665 ssl->out_ctr = NULL;
7666 ssl->out_len = NULL;
7667 ssl->out_iv = NULL;
7668 ssl->out_msg = NULL;
7669
k-stachowiak9f7798e2018-07-31 16:52:32 +02007670 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007671}
7672
7673/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007674 * Reset an initialized and used SSL context for re-use while retaining
7675 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007676 *
7677 * If partial is non-zero, keep data in the input buffer and client ID.
7678 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007679 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007680static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007681{
Paul Bakker48916f92012-09-16 19:57:18 +00007682 int ret;
7683
Hanno Becker7e772132018-08-10 12:38:21 +01007684#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7685 !defined(MBEDTLS_SSL_SRV_C)
7686 ((void) partial);
7687#endif
7688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007690
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007691 /* Cancel any possibly running timer */
7692 ssl_set_timer( ssl, 0 );
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if defined(MBEDTLS_SSL_RENEGOTIATION)
7695 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007696 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007697
7698 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7700 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007703
Paul Bakker7eb013f2011-10-06 12:37:39 +00007704 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007705 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007706
7707 ssl->in_msgtype = 0;
7708 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007710 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007711 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007714 ssl_dtls_replay_reset( ssl );
7715#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007716
7717 ssl->in_hslen = 0;
7718 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007719
7720 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007721
7722 ssl->out_msgtype = 0;
7723 ssl->out_msglen = 0;
7724 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7726 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007727 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007728#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007729
Hanno Becker19859472018-08-06 09:40:20 +01007730 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7731
Paul Bakker48916f92012-09-16 19:57:18 +00007732 ssl->transform_in = NULL;
7733 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007734
Hanno Becker78640902018-08-13 16:35:15 +01007735 ssl->session_in = NULL;
7736 ssl->session_out = NULL;
7737
Angus Grattond8213d02016-05-25 20:56:48 +10007738 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007739
7740#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007741 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007742#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7743 {
7744 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007745 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007746 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7749 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7752 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7755 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007756 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007757 }
7758#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007759
Paul Bakker48916f92012-09-16 19:57:18 +00007760 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 mbedtls_ssl_transform_free( ssl->transform );
7763 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007764 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007765 }
Paul Bakker48916f92012-09-16 19:57:18 +00007766
Paul Bakkerc0463502013-02-14 11:19:38 +01007767 if( ssl->session )
7768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 mbedtls_ssl_session_free( ssl->session );
7770 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007771 ssl->session = NULL;
7772 }
7773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007775 ssl->alpn_chosen = NULL;
7776#endif
7777
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007778#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007779#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007780 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007781#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007782 {
7783 mbedtls_free( ssl->cli_id );
7784 ssl->cli_id = NULL;
7785 ssl->cli_id_len = 0;
7786 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007787#endif
7788
Paul Bakker48916f92012-09-16 19:57:18 +00007789 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7790 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007791
7792 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007793}
7794
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007795/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007796 * Reset an initialized and used SSL context for re-use while retaining
7797 * all application-set variables, function pointers and data.
7798 */
7799int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7800{
7801 return( ssl_session_reset_int( ssl, 0 ) );
7802}
7803
7804/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007805 * SSL set accessors
7806 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007807void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007808{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007809 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007810}
7811
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007812void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007813{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007814 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007815}
7816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007818void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007820 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007821}
7822#endif
7823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007825void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007826{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007827 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007828}
7829#endif
7830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007832
Hanno Becker1841b0a2018-08-24 11:13:57 +01007833void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7834 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007835{
7836 ssl->disable_datagram_packing = !allow_packing;
7837}
7838
7839void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7840 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007841{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007842 conf->hs_timeout_min = min;
7843 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007844}
7845#endif
7846
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007847void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007848{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007849 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007850}
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007853void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007854 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007855 void *p_vrfy )
7856{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007857 conf->f_vrfy = f_vrfy;
7858 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007859}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007861
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007862void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007863 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007864 void *p_rng )
7865{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007866 conf->f_rng = f_rng;
7867 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007868}
7869
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007870void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007871 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007872 void *p_dbg )
7873{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007874 conf->f_dbg = f_dbg;
7875 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007876}
7877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007879 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007880 mbedtls_ssl_send_t *f_send,
7881 mbedtls_ssl_recv_t *f_recv,
7882 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007883{
7884 ssl->p_bio = p_bio;
7885 ssl->f_send = f_send;
7886 ssl->f_recv = f_recv;
7887 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007888}
7889
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007890#if defined(MBEDTLS_SSL_PROTO_DTLS)
7891void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7892{
7893 ssl->mtu = mtu;
7894}
7895#endif
7896
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007897void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007898{
7899 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007900}
7901
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007902void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7903 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007904 mbedtls_ssl_set_timer_t *f_set_timer,
7905 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007906{
7907 ssl->p_timer = p_timer;
7908 ssl->f_set_timer = f_set_timer;
7909 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007910
7911 /* Make sure we start with no timer running */
7912 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007913}
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007916void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007917 void *p_cache,
7918 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7919 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007920{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007921 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007923 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007924}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#if defined(MBEDTLS_SSL_CLI_C)
7928int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007929{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007930 int ret;
7931
7932 if( ssl == NULL ||
7933 session == NULL ||
7934 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007935 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007938 }
7939
7940 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7941 return( ret );
7942
Paul Bakker0a597072012-09-25 21:55:46 +00007943 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007944
7945 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007946}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007948
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007949void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007950 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007951{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007952 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7953 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7954 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7955 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007956}
7957
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007958void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007959 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007960 int major, int minor )
7961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007963 return;
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007966 return;
7967
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007968 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007969}
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007972void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007973 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007974{
7975 conf->cert_profile = profile;
7976}
7977
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007978/* Append a new keycert entry to a (possibly empty) list */
7979static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7980 mbedtls_x509_crt *cert,
7981 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007982{
niisato8ee24222018-06-25 19:05:48 +09007983 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007984
niisato8ee24222018-06-25 19:05:48 +09007985 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7986 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007987 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007988
niisato8ee24222018-06-25 19:05:48 +09007989 new_cert->cert = cert;
7990 new_cert->key = key;
7991 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007992
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007993 /* Update head is the list was null, else add to the end */
7994 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007995 {
niisato8ee24222018-06-25 19:05:48 +09007996 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007997 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007998 else
7999 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008000 mbedtls_ssl_key_cert *cur = *head;
8001 while( cur->next != NULL )
8002 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008003 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008004 }
8005
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008006 return( 0 );
8007}
8008
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008009int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008010 mbedtls_x509_crt *own_cert,
8011 mbedtls_pk_context *pk_key )
8012{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008013 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008014}
8015
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008016void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008017 mbedtls_x509_crt *ca_chain,
8018 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008019{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008020 conf->ca_chain = ca_chain;
8021 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008024
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008025#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8026int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8027 mbedtls_x509_crt *own_cert,
8028 mbedtls_pk_context *pk_key )
8029{
8030 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8031 own_cert, pk_key ) );
8032}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008033
8034void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8035 mbedtls_x509_crt *ca_chain,
8036 mbedtls_x509_crl *ca_crl )
8037{
8038 ssl->handshake->sni_ca_chain = ca_chain;
8039 ssl->handshake->sni_ca_crl = ca_crl;
8040}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008041
8042void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8043 int authmode )
8044{
8045 ssl->handshake->sni_authmode = authmode;
8046}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008047#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8048
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008049#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008050/*
8051 * Set EC J-PAKE password for current handshake
8052 */
8053int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8054 const unsigned char *pw,
8055 size_t pw_len )
8056{
8057 mbedtls_ecjpake_role role;
8058
Janos Follath8eb64132016-06-03 15:40:57 +01008059 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8061
8062 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8063 role = MBEDTLS_ECJPAKE_SERVER;
8064 else
8065 role = MBEDTLS_ECJPAKE_CLIENT;
8066
8067 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8068 role,
8069 MBEDTLS_MD_SHA256,
8070 MBEDTLS_ECP_DP_SECP256R1,
8071 pw, pw_len ) );
8072}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008073#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008076int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008077 const unsigned char *psk, size_t psk_len,
8078 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008079{
Paul Bakker6db455e2013-09-18 17:29:31 +02008080 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008085
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008086 /* Identity len will be encoded on two bytes */
8087 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008088 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008089 {
8090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8091 }
8092
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008093 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008094 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008095 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008096
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008097 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008098 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008099 conf->psk_len = 0;
8100 }
8101 if( conf->psk_identity != NULL )
8102 {
8103 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008104 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008105 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008106 }
8107
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008108 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8109 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008110 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008111 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008112 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008113 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008114 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008115 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008116 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008117
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008118 conf->psk_len = psk_len;
8119 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008120
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008121 memcpy( conf->psk, psk, conf->psk_len );
8122 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008123
8124 return( 0 );
8125}
8126
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008127int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8128 const unsigned char *psk, size_t psk_len )
8129{
8130 if( psk == NULL || ssl->handshake == NULL )
8131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8132
8133 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8135
8136 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008137 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008138 mbedtls_platform_zeroize( ssl->handshake->psk,
8139 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008140 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008141 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008142 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008143
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008144 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008145 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008146
8147 ssl->handshake->psk_len = psk_len;
8148 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8149
8150 return( 0 );
8151}
8152
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008153void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008155 size_t),
8156 void *p_psk )
8157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008158 conf->f_psk = f_psk;
8159 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008162
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008163#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008164
8165#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008166int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008167{
8168 int ret;
8169
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008170 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8171 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8172 {
8173 mbedtls_mpi_free( &conf->dhm_P );
8174 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008175 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008177
8178 return( 0 );
8179}
Hanno Becker470a8c42017-10-04 15:28:46 +01008180#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008181
Hanno Beckera90658f2017-10-04 15:29:08 +01008182int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8183 const unsigned char *dhm_P, size_t P_len,
8184 const unsigned char *dhm_G, size_t G_len )
8185{
8186 int ret;
8187
8188 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8189 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8190 {
8191 mbedtls_mpi_free( &conf->dhm_P );
8192 mbedtls_mpi_free( &conf->dhm_G );
8193 return( ret );
8194 }
8195
8196 return( 0 );
8197}
Paul Bakker5121ce52009-01-03 21:22:43 +00008198
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008199int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008200{
8201 int ret;
8202
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008203 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8204 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8205 {
8206 mbedtls_mpi_free( &conf->dhm_P );
8207 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008208 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008209 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008210
8211 return( 0 );
8212}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008213#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008214
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008215#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8216/*
8217 * Set the minimum length for Diffie-Hellman parameters
8218 */
8219void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8220 unsigned int bitlen )
8221{
8222 conf->dhm_min_bitlen = bitlen;
8223}
8224#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8225
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008226#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008227/*
8228 * Set allowed/preferred hashes for handshake signatures
8229 */
8230void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8231 const int *hashes )
8232{
8233 conf->sig_hashes = hashes;
8234}
Hanno Becker947194e2017-04-07 13:25:49 +01008235#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008236
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008237#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008238/*
8239 * Set the allowed elliptic curves
8240 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008241void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008242 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008243{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008244 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008245}
Hanno Becker947194e2017-04-07 13:25:49 +01008246#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008247
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008248#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008250{
Hanno Becker947194e2017-04-07 13:25:49 +01008251 /* Initialize to suppress unnecessary compiler warning */
8252 size_t hostname_len = 0;
8253
8254 /* Check if new hostname is valid before
8255 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008256 if( hostname != NULL )
8257 {
8258 hostname_len = strlen( hostname );
8259
8260 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8262 }
8263
8264 /* Now it's clear that we will overwrite the old hostname,
8265 * so we can free it safely */
8266
8267 if( ssl->hostname != NULL )
8268 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008269 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008270 mbedtls_free( ssl->hostname );
8271 }
8272
8273 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008274
Paul Bakker5121ce52009-01-03 21:22:43 +00008275 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008276 {
8277 ssl->hostname = NULL;
8278 }
8279 else
8280 {
8281 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008282 if( ssl->hostname == NULL )
8283 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008284
Hanno Becker947194e2017-04-07 13:25:49 +01008285 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008286
Hanno Becker947194e2017-04-07 13:25:49 +01008287 ssl->hostname[hostname_len] = '\0';
8288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008289
8290 return( 0 );
8291}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008292#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008293
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008294#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008295void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008297 const unsigned char *, size_t),
8298 void *p_sni )
8299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008300 conf->f_sni = f_sni;
8301 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008306int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008307{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008308 size_t cur_len, tot_len;
8309 const char **p;
8310
8311 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008312 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8313 * MUST NOT be truncated."
8314 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008315 */
8316 tot_len = 0;
8317 for( p = protos; *p != NULL; p++ )
8318 {
8319 cur_len = strlen( *p );
8320 tot_len += cur_len;
8321
8322 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008324 }
8325
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008326 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008327
8328 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008329}
8330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008332{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008333 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008336
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008337void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008338{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008339 conf->max_major_ver = major;
8340 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008341}
8342
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008343void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008344{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008345 conf->min_major_ver = major;
8346 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008347}
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008350void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008351{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008352 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008353}
8354#endif
8355
Janos Follath088ce432017-04-10 12:42:31 +01008356#if defined(MBEDTLS_SSL_SRV_C)
8357void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8358 char cert_req_ca_list )
8359{
8360 conf->cert_req_ca_list = cert_req_ca_list;
8361}
8362#endif
8363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008365void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008366{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008367 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008368}
8369#endif
8370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008372void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008373{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008374 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008375}
8376#endif
8377
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008378#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008379void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008380{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008381 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008382}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008383#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008386int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008389 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008392 }
8393
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008394 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008395
8396 return( 0 );
8397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008401void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008403 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008408void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008409{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008410 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008411}
8412#endif
8413
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008414void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008415{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008416 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008417}
8418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008420void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008422 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008423}
8424
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008425void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008426{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008427 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008428}
8429
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008430void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008431 const unsigned char period[8] )
8432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008438#if defined(MBEDTLS_SSL_CLI_C)
8439void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008440{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008441 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008442}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008443#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008444
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008446void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8447 mbedtls_ssl_ticket_write_t *f_ticket_write,
8448 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8449 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008450{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008451 conf->f_ticket_write = f_ticket_write;
8452 conf->f_ticket_parse = f_ticket_parse;
8453 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008454}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008455#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008457
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008458#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8459void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8460 mbedtls_ssl_export_keys_t *f_export_keys,
8461 void *p_export_keys )
8462{
8463 conf->f_export_keys = f_export_keys;
8464 conf->p_export_keys = p_export_keys;
8465}
8466#endif
8467
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008468#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008469void mbedtls_ssl_conf_async_private_cb(
8470 mbedtls_ssl_config *conf,
8471 mbedtls_ssl_async_sign_t *f_async_sign,
8472 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8473 mbedtls_ssl_async_resume_t *f_async_resume,
8474 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008475 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008476{
8477 conf->f_async_sign_start = f_async_sign;
8478 conf->f_async_decrypt_start = f_async_decrypt;
8479 conf->f_async_resume = f_async_resume;
8480 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008481 conf->p_async_config_data = async_config_data;
8482}
8483
Gilles Peskine8f97af72018-04-26 11:46:10 +02008484void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8485{
8486 return( conf->p_async_config_data );
8487}
8488
Gilles Peskine1febfef2018-04-30 11:54:39 +02008489void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008490{
8491 if( ssl->handshake == NULL )
8492 return( NULL );
8493 else
8494 return( ssl->handshake->user_async_ctx );
8495}
8496
Gilles Peskine1febfef2018-04-30 11:54:39 +02008497void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008498 void *ctx )
8499{
8500 if( ssl->handshake != NULL )
8501 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008502}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008503#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008504
Paul Bakker5121ce52009-01-03 21:22:43 +00008505/*
8506 * SSL get accessors
8507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008509{
8510 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8511}
8512
Hanno Becker8b170a02017-10-10 11:51:19 +01008513int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8514{
8515 /*
8516 * Case A: We're currently holding back
8517 * a message for further processing.
8518 */
8519
8520 if( ssl->keep_current_message == 1 )
8521 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008523 return( 1 );
8524 }
8525
8526 /*
8527 * Case B: Further records are pending in the current datagram.
8528 */
8529
8530#if defined(MBEDTLS_SSL_PROTO_DTLS)
8531 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8532 ssl->in_left > ssl->next_record_offset )
8533 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008535 return( 1 );
8536 }
8537#endif /* MBEDTLS_SSL_PROTO_DTLS */
8538
8539 /*
8540 * Case C: A handshake message is being processed.
8541 */
8542
Hanno Becker8b170a02017-10-10 11:51:19 +01008543 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8544 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008546 return( 1 );
8547 }
8548
8549 /*
8550 * Case D: An application data message is being processed
8551 */
8552 if( ssl->in_offt != NULL )
8553 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008555 return( 1 );
8556 }
8557
8558 /*
8559 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008560 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008561 * we implement support for multiple alerts in single records.
8562 */
8563
8564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8565 return( 0 );
8566}
8567
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008568uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008569{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008570 if( ssl->session != NULL )
8571 return( ssl->session->verify_result );
8572
8573 if( ssl->session_negotiate != NULL )
8574 return( ssl->session_negotiate->verify_result );
8575
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008576 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008577}
8578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008580{
Paul Bakker926c8e42013-03-06 10:23:34 +01008581 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008582 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008585}
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008590 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008591 {
8592 switch( ssl->minor_ver )
8593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008595 return( "DTLSv1.0" );
8596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008598 return( "DTLSv1.2" );
8599
8600 default:
8601 return( "unknown (DTLS)" );
8602 }
8603 }
8604#endif
8605
Paul Bakker43ca69c2011-01-15 17:35:19 +00008606 switch( ssl->minor_ver )
8607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008609 return( "SSLv3.0" );
8610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008612 return( "TLSv1.0" );
8613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008615 return( "TLSv1.1" );
8616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008618 return( "TLSv1.2" );
8619
Paul Bakker43ca69c2011-01-15 17:35:19 +00008620 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008621 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008622 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008623}
8624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008626{
Hanno Becker3136ede2018-08-17 15:28:19 +01008627 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008629 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008630
Hanno Becker43395762019-05-03 14:46:38 +01008631 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8632
Hanno Becker78640902018-08-13 16:35:15 +01008633 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008634 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636#if defined(MBEDTLS_ZLIB_SUPPORT)
8637 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8638 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008639#endif
8640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 case MBEDTLS_MODE_GCM:
8644 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008645 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008647 transform_expansion = transform->minlen;
8648 break;
8649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008651
8652 block_size = mbedtls_cipher_get_block_size(
8653 &transform->cipher_ctx_enc );
8654
Hanno Becker3136ede2018-08-17 15:28:19 +01008655 /* Expansion due to the addition of the MAC. */
8656 transform_expansion += transform->maclen;
8657
8658 /* Expansion due to the addition of CBC padding;
8659 * Theoretically up to 256 bytes, but we never use
8660 * more than the block size of the underlying cipher. */
8661 transform_expansion += block_size;
8662
8663 /* For TLS 1.1 or higher, an explicit IV is added
8664 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008665#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8666 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008667 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008668#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008669
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008670 break;
8671
8672 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008675 }
8676
Hanno Beckera5a2b082019-05-15 14:03:01 +01008677#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008678 if( transform->out_cid_len != 0 )
8679 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008680#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008681
Hanno Becker43395762019-05-03 14:46:38 +01008682 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008683}
8684
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008685#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8686size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8687{
8688 size_t max_len;
8689
8690 /*
8691 * Assume mfl_code is correct since it was checked when set
8692 */
Angus Grattond8213d02016-05-25 20:56:48 +10008693 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008694
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008695 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008696 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008697 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008698 {
Angus Grattond8213d02016-05-25 20:56:48 +10008699 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008700 }
8701
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008702 /* During a handshake, use the value being negotiated */
8703 if( ssl->session_negotiate != NULL &&
8704 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8705 {
8706 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8707 }
8708
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008709 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008710}
8711#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8712
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008713#if defined(MBEDTLS_SSL_PROTO_DTLS)
8714static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8715{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008716 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8717 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8718 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8719 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8720 return ( 0 );
8721
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008722 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8723 return( ssl->mtu );
8724
8725 if( ssl->mtu == 0 )
8726 return( ssl->handshake->mtu );
8727
8728 return( ssl->mtu < ssl->handshake->mtu ?
8729 ssl->mtu : ssl->handshake->mtu );
8730}
8731#endif /* MBEDTLS_SSL_PROTO_DTLS */
8732
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008733int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8734{
8735 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8736
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008737#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8738 !defined(MBEDTLS_SSL_PROTO_DTLS)
8739 (void) ssl;
8740#endif
8741
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008742#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8743 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8744
8745 if( max_len > mfl )
8746 max_len = mfl;
8747#endif
8748
8749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008750 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008751 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008752 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008753 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8754 const size_t overhead = (size_t) ret;
8755
8756 if( ret < 0 )
8757 return( ret );
8758
8759 if( mtu <= overhead )
8760 {
8761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8762 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8763 }
8764
8765 if( max_len > mtu - overhead )
8766 max_len = mtu - overhead;
8767 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008768#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008769
Hanno Becker0defedb2018-08-10 12:35:02 +01008770#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8771 !defined(MBEDTLS_SSL_PROTO_DTLS)
8772 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008773#endif
8774
8775 return( (int) max_len );
8776}
8777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#if defined(MBEDTLS_X509_CRT_PARSE_C)
8779const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008780{
8781 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008782 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008783
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008784 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788#if defined(MBEDTLS_SSL_CLI_C)
8789int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008790{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008791 if( ssl == NULL ||
8792 dst == NULL ||
8793 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008794 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008797 }
8798
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008799 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008802
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008803const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8804{
8805 if( ssl == NULL )
8806 return( NULL );
8807
8808 return( ssl->session );
8809}
8810
Paul Bakker5121ce52009-01-03 21:22:43 +00008811/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008812 * Define ticket header determining Mbed TLS version
8813 * and structure of the ticket.
8814 */
8815
Hanno Becker41527622019-05-16 12:50:45 +01008816/*
Hanno Becker26829e92019-05-28 14:30:45 +01008817 * Define bitflag determining compile-time settings influencing
8818 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008819 */
8820
Hanno Becker26829e92019-05-28 14:30:45 +01008821#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008822#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008823#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008824#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008825#endif /* MBEDTLS_HAVE_TIME */
8826
8827#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008828#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008829#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008830#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008831#endif /* MBEDTLS_X509_CRT_PARSE_C */
8832
8833#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008834#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008835#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008836#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008837#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8838
8839#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008840#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008841#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008842#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008843#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8844
8845#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008846#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008847#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008848#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008849#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8850
8851#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008852#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008853#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008854#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008855#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8856
Hanno Becker41527622019-05-16 12:50:45 +01008857#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8858#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8859#else
8860#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8861#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8862
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008863#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8864#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8865#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8866#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8867#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8868#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8869#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008870
Hanno Becker26829e92019-05-28 14:30:45 +01008871#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008872 ( (uint16_t) ( \
8873 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8874 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8875 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8876 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8877 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8878 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008879 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008880
Hanno Becker557fe9f2019-05-16 12:41:07 +01008881static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008882 MBEDTLS_VERSION_MAJOR,
8883 MBEDTLS_VERSION_MINOR,
8884 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008885 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8886 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008887};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008888
8889/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008890 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008891 * (in the presentation language of TLS, RFC 8446 section 3)
8892 *
Hanno Becker26829e92019-05-28 14:30:45 +01008893 * opaque mbedtls_version[3]; // major, minor, patch
8894 * opaque session_format[2]; // version-specific 16-bit field determining
8895 * // the format of the remaining
8896 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008897 *
8898 * Note: When updating the format, remember to keep
8899 * these version+format bytes.
8900 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008901 * // In this version, `session_format` determines
8902 * // the setting of those compile-time
8903 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008904 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008905 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008906 * uint8 ciphersuite[2]; // defined by the standard
8907 * uint8 compression; // 0 or 1
8908 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008909 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008910 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008911 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008912 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8913 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008914 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008915 * uint8 mfl_code; // up to 255 according to standard
8916 * uint8 trunc_hmac; // 0 or 1
8917 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008918 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008919 * The order is the same as in the definition of the structure, except
8920 * verify_result is put before peer_cert so that all mandatory fields come
8921 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008922 */
8923int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8924 unsigned char *buf,
8925 size_t buf_len,
8926 size_t *olen )
8927{
8928 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008929 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008930#if defined(MBEDTLS_HAVE_TIME)
8931 uint64_t start;
8932#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008933#if defined(MBEDTLS_X509_CRT_PARSE_C)
8934 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008935#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008936
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008937 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008938 * Add version identifier
8939 */
8940
8941 used += sizeof( ssl_serialized_session_header );
8942
8943 if( used <= buf_len )
8944 {
8945 memcpy( p, ssl_serialized_session_header,
8946 sizeof( ssl_serialized_session_header ) );
8947 p += sizeof( ssl_serialized_session_header );
8948 }
8949
8950 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008951 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008952 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008953#if defined(MBEDTLS_HAVE_TIME)
8954 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008955
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008956 if( used <= buf_len )
8957 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008958 start = (uint64_t) session->start;
8959
8960 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8961 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8962 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8963 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8964 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
8965 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
8966 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
8967 *p++ = (unsigned char)( ( start ) & 0xFF );
8968 }
8969#endif /* MBEDTLS_HAVE_TIME */
8970
8971 /*
8972 * Basic mandatory fields
8973 */
8974 used += 2 /* ciphersuite */
8975 + 1 /* compression */
8976 + 1 /* id_len */
8977 + sizeof( session->id )
8978 + sizeof( session->master )
8979 + 4; /* verify_result */
8980
8981 if( used <= buf_len )
8982 {
8983 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
8984 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
8985
8986 *p++ = (unsigned char)( session->compression & 0xFF );
8987
8988 *p++ = (unsigned char)( session->id_len & 0xFF );
8989 memcpy( p, session->id, 32 );
8990 p += 32;
8991
8992 memcpy( p, session->master, 48 );
8993 p += 48;
8994
8995 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
8996 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
8997 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
8998 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008999 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009000
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009001 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009002 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009003 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009004#if defined(MBEDTLS_X509_CRT_PARSE_C)
9005 if( session->peer_cert == NULL )
9006 cert_len = 0;
9007 else
9008 cert_len = session->peer_cert->raw.len;
9009
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009010 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009011
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009012 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009013 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009014 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9015 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9016 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9017
9018 if( session->peer_cert != NULL )
9019 {
9020 memcpy( p, session->peer_cert->raw.p, cert_len );
9021 p += cert_len;
9022 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009023 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009024#endif /* MBEDTLS_X509_CRT_PARSE_C */
9025
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009026 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009027 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009028 */
9029#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009030 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009031
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009032 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009033 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009034 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9035 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9036 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9037
9038 if( session->ticket != NULL )
9039 {
9040 memcpy( p, session->ticket, session->ticket_len );
9041 p += session->ticket_len;
9042 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009043
9044 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9045 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9046 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9047 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009048 }
9049#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9050
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009051 /*
9052 * Misc extension-related info
9053 */
9054#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9055 used += 1;
9056
9057 if( used <= buf_len )
9058 *p++ = session->mfl_code;
9059#endif
9060
9061#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9062 used += 1;
9063
9064 if( used <= buf_len )
9065 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9066#endif
9067
9068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9069 used += 1;
9070
9071 if( used <= buf_len )
9072 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9073#endif
9074
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009075 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009076 *olen = used;
9077
9078 if( used > buf_len )
9079 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009080
9081 return( 0 );
9082}
9083
9084/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009085 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009086 *
9087 * This internal version is wrapped by a public function that cleans up in
9088 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009089 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009090static int ssl_session_load( mbedtls_ssl_session *session,
9091 const unsigned char *buf,
9092 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009093{
9094 const unsigned char *p = buf;
9095 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009096#if defined(MBEDTLS_HAVE_TIME)
9097 uint64_t start;
9098#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009099#if defined(MBEDTLS_X509_CRT_PARSE_C)
9100 size_t cert_len;
9101#endif /* MBEDTLS_X509_CRT_PARSE_C */
9102
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009103 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009104 * Check version identifier
9105 */
9106
9107 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009109
9110 if( memcmp( p, ssl_serialized_session_header,
9111 sizeof( ssl_serialized_session_header ) ) != 0 )
9112 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009113 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009114 }
9115 p += sizeof( ssl_serialized_session_header );
9116
9117 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009118 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009119 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009120#if defined(MBEDTLS_HAVE_TIME)
9121 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9123
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009124 start = ( (uint64_t) p[0] << 56 ) |
9125 ( (uint64_t) p[1] << 48 ) |
9126 ( (uint64_t) p[2] << 40 ) |
9127 ( (uint64_t) p[3] << 32 ) |
9128 ( (uint64_t) p[4] << 24 ) |
9129 ( (uint64_t) p[5] << 16 ) |
9130 ( (uint64_t) p[6] << 8 ) |
9131 ( (uint64_t) p[7] );
9132 p += 8;
9133
9134 session->start = (time_t) start;
9135#endif /* MBEDTLS_HAVE_TIME */
9136
9137 /*
9138 * Basic mandatory fields
9139 */
9140 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9142
9143 session->ciphersuite = ( p[0] << 8 ) | p[1];
9144 p += 2;
9145
9146 session->compression = *p++;
9147
9148 session->id_len = *p++;
9149 memcpy( session->id, p, 32 );
9150 p += 32;
9151
9152 memcpy( session->master, p, 48 );
9153 p += 48;
9154
9155 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9156 ( (uint32_t) p[1] << 16 ) |
9157 ( (uint32_t) p[2] << 8 ) |
9158 ( (uint32_t) p[3] );
9159 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009160
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009161 /* Immediately clear invalid pointer values that have been read, in case
9162 * we exit early before we replaced them with valid ones. */
9163#if defined(MBEDTLS_X509_CRT_PARSE_C)
9164 session->peer_cert = NULL;
9165#endif
9166#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9167 session->ticket = NULL;
9168#endif
9169
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009170 /*
9171 * Peer certificate
9172 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009173#if defined(MBEDTLS_X509_CRT_PARSE_C)
9174 if( 3 > (size_t)( end - p ) )
9175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9176
9177 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9178 p += 3;
9179
9180 if( cert_len == 0 )
9181 {
9182 session->peer_cert = NULL;
9183 }
9184 else
9185 {
9186 int ret;
9187
9188 if( cert_len > (size_t)( end - p ) )
9189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9190
9191 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9192
9193 if( session->peer_cert == NULL )
9194 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9195
9196 mbedtls_x509_crt_init( session->peer_cert );
9197
9198 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9199 p, cert_len ) ) != 0 )
9200 {
9201 mbedtls_x509_crt_free( session->peer_cert );
9202 mbedtls_free( session->peer_cert );
9203 session->peer_cert = NULL;
9204 return( ret );
9205 }
9206
9207 p += cert_len;
9208 }
9209#endif /* MBEDTLS_X509_CRT_PARSE_C */
9210
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009211 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009212 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009213 */
9214#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9215 if( 3 > (size_t)( end - p ) )
9216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9217
9218 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9219 p += 3;
9220
9221 if( session->ticket_len != 0 )
9222 {
9223 if( session->ticket_len > (size_t)( end - p ) )
9224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9225
9226 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9227 if( session->ticket == NULL )
9228 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9229
9230 memcpy( session->ticket, p, session->ticket_len );
9231 p += session->ticket_len;
9232 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009233
9234 if( 4 > (size_t)( end - p ) )
9235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9236
9237 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9238 ( (uint32_t) p[1] << 16 ) |
9239 ( (uint32_t) p[2] << 8 ) |
9240 ( (uint32_t) p[3] );
9241 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009242#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9243
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009244 /*
9245 * Misc extension-related info
9246 */
9247#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9248 if( 1 > (size_t)( end - p ) )
9249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9250
9251 session->mfl_code = *p++;
9252#endif
9253
9254#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9255 if( 1 > (size_t)( end - p ) )
9256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9257
9258 session->trunc_hmac = *p++;
9259#endif
9260
9261#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9262 if( 1 > (size_t)( end - p ) )
9263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9264
9265 session->encrypt_then_mac = *p++;
9266#endif
9267
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009268 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009269 if( p != end )
9270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9271
9272 return( 0 );
9273}
9274
9275/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009276 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009277 */
9278int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9279 const unsigned char *buf,
9280 size_t len )
9281{
9282 int ret = ssl_session_load( session, buf, len );
9283
9284 if( ret != 0 )
9285 mbedtls_ssl_session_free( session );
9286
9287 return( ret );
9288}
9289
9290/*
Paul Bakker1961b702013-01-25 14:49:24 +01009291 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009296
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009297 if( ssl == NULL || ssl->conf == NULL )
9298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009301 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009307#endif
9308
Paul Bakker1961b702013-01-25 14:49:24 +01009309 return( ret );
9310}
9311
9312/*
9313 * Perform the SSL handshake
9314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009316{
9317 int ret = 0;
9318
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009319 if( ssl == NULL || ssl->conf == NULL )
9320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009327
9328 if( ret != 0 )
9329 break;
9330 }
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009333
9334 return( ret );
9335}
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337#if defined(MBEDTLS_SSL_RENEGOTIATION)
9338#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009339/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009340 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009343{
9344 int ret;
9345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009347
9348 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9350 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009351
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009352 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009353 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009355 return( ret );
9356 }
9357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009359
9360 return( 0 );
9361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009363
9364/*
9365 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366 * - any side: calling mbedtls_ssl_renegotiate(),
9367 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9368 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009369 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009370 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009374{
9375 int ret;
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
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009379 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9380 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009381
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009382 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9383 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009385 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009387 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009388 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009389 ssl->handshake->out_msg_seq = 1;
9390 else
9391 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009392 }
9393#endif
9394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9396 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009401 return( ret );
9402 }
9403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009405
9406 return( 0 );
9407}
9408
9409/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009410 * Renegotiate current connection on client,
9411 * or request renegotiation on server
9412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009416
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009417 if( ssl == NULL || ssl->conf == NULL )
9418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009420#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009421 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009422 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009428
9429 /* Did we already try/start sending HelloRequest? */
9430 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009432
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009433 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009437#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009438 /*
9439 * On client, either start the renegotiation process or,
9440 * if already in progress, continue the handshake
9441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009446
9447 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009450 return( ret );
9451 }
9452 }
9453 else
9454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009458 return( ret );
9459 }
9460 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009462
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009463 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009464}
9465
9466/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009467 * Check record counters and renegotiate if they're above the limit.
9468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009470{
Andres AG2196c7f2016-12-15 17:01:16 +00009471 size_t ep_len = ssl_ep_len( ssl );
9472 int in_ctr_cmp;
9473 int out_ctr_cmp;
9474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9476 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009477 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009478 {
9479 return( 0 );
9480 }
9481
Andres AG2196c7f2016-12-15 17:01:16 +00009482 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9483 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009484 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009485 ssl->conf->renego_period + ep_len, 8 - ep_len );
9486
9487 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009488 {
9489 return( 0 );
9490 }
9491
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009496
9497/*
9498 * Receive application data decrypted from the SSL layer
9499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009501{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009502 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009503 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009504
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009505 if( ssl == NULL || ssl->conf == NULL )
9506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009514 return( ret );
9515
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009516 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009518 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009519 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009520 return( ret );
9521 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009522 }
9523#endif
9524
Hanno Becker4a810fb2017-05-24 16:27:30 +01009525 /*
9526 * Check if renegotiation is necessary and/or handshake is
9527 * in process. If yes, perform/continue, and fall through
9528 * if an unexpected packet is received while the client
9529 * is waiting for the ServerHello.
9530 *
9531 * (There is no equivalent to the last condition on
9532 * the server-side as it is not treated as within
9533 * a handshake while waiting for the ClientHello
9534 * after a renegotiation request.)
9535 */
9536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009538 ret = ssl_check_ctr_renegotiate( ssl );
9539 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9540 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009543 return( ret );
9544 }
9545#endif
9546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009550 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9551 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009554 return( ret );
9555 }
9556 }
9557
Hanno Beckere41158b2017-10-23 13:30:32 +01009558 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009559 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009560 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009561 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009562 if( ssl->f_get_timer != NULL &&
9563 ssl->f_get_timer( ssl->p_timer ) == -1 )
9564 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009565 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009566 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009567
Hanno Becker327c93b2018-08-15 13:56:18 +01009568 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009569 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009570 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9571 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009572
Hanno Becker4a810fb2017-05-24 16:27:30 +01009573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9574 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009575 }
9576
9577 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009579 {
9580 /*
9581 * OpenSSL sends empty messages to randomize the IV
9582 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009583 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009586 return( 0 );
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009589 return( ret );
9590 }
9591 }
9592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009596
Hanno Becker4a810fb2017-05-24 16:27:30 +01009597 /*
9598 * - For client-side, expect SERVER_HELLO_REQUEST.
9599 * - For server-side, expect CLIENT_HELLO.
9600 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9601 */
9602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009604 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009606 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009609
9610 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009612 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009613 {
9614 continue;
9615 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009618 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009619#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009620
Hanno Becker4a810fb2017-05-24 16:27:30 +01009621#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009622 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009626
9627 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009629 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009630 {
9631 continue;
9632 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009635 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009636#endif /* MBEDTLS_SSL_SRV_C */
9637
Hanno Becker21df7f92017-10-17 11:03:26 +01009638#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009639 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009640 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9641 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9642 ssl->conf->allow_legacy_renegotiation ==
9643 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9644 {
9645 /*
9646 * Accept renegotiation request
9647 */
Paul Bakker48916f92012-09-16 19:57:18 +00009648
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009649 /* DTLS clients need to know renego is server-initiated */
9650#if defined(MBEDTLS_SSL_PROTO_DTLS)
9651 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9652 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9653 {
9654 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9655 }
9656#endif
9657 ret = ssl_start_renegotiation( ssl );
9658 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9659 ret != 0 )
9660 {
9661 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9662 return( ret );
9663 }
9664 }
9665 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009666#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009667 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009668 /*
9669 * Refuse renegotiation
9670 */
9671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_SSL_PROTO_SSL3)
9675 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009676 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009677 /* SSLv3 does not have a "no_renegotiation" warning, so
9678 we send a fatal alert and abort the connection. */
9679 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9680 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9681 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009682 }
9683 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9686 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9687 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9690 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9691 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009692 {
9693 return( ret );
9694 }
Paul Bakker48916f92012-09-16 19:57:18 +00009695 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009696 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9698 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009702 }
Paul Bakker48916f92012-09-16 19:57:18 +00009703 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009704
Hanno Becker90333da2017-10-10 11:27:13 +01009705 /* At this point, we don't know whether the renegotiation has been
9706 * completed or not. The cases to consider are the following:
9707 * 1) The renegotiation is complete. In this case, no new record
9708 * has been read yet.
9709 * 2) The renegotiation is incomplete because the client received
9710 * an application data record while awaiting the ServerHello.
9711 * 3) The renegotiation is incomplete because the client received
9712 * a non-handshake, non-application data message while awaiting
9713 * the ServerHello.
9714 * In each of these case, looping will be the proper action:
9715 * - For 1), the next iteration will read a new record and check
9716 * if it's application data.
9717 * - For 2), the loop condition isn't satisfied as application data
9718 * is present, hence continue is the same as break
9719 * - For 3), the loop condition is satisfied and read_record
9720 * will re-deliver the message that was held back by the client
9721 * when expecting the ServerHello.
9722 */
9723 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009724 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009725#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009727 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009728 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009729 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009730 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009733 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009735 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009736 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009737 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9741 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009744 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009745 }
9746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9750 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009751 }
9752
9753 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009754
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009755 /* We're going to return something now, cancel timer,
9756 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009758 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009759
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009761 /* If we requested renego but received AppData, resend HelloRequest.
9762 * Do it now, after setting in_offt, to avoid taking this branch
9763 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009765 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009767 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009768 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009771 return( ret );
9772 }
9773 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009775#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009776 }
9777
9778 n = ( len < ssl->in_msglen )
9779 ? len : ssl->in_msglen;
9780
9781 memcpy( buf, ssl->in_offt, n );
9782 ssl->in_msglen -= n;
9783
9784 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009785 {
9786 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009788 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009790 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009791 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009792 /* more data available */
9793 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009797
Paul Bakker23986e52011-04-24 08:57:21 +00009798 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009799}
9800
9801/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009802 * Send application data to be encrypted by the SSL layer, taking care of max
9803 * fragment length and buffer size.
9804 *
9805 * According to RFC 5246 Section 6.2.1:
9806 *
9807 * Zero-length fragments of Application data MAY be sent as they are
9808 * potentially useful as a traffic analysis countermeasure.
9809 *
9810 * Therefore, it is possible that the input message length is 0 and the
9811 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009812 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009813static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009814 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009815{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009816 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9817 const size_t max_len = (size_t) ret;
9818
9819 if( ret < 0 )
9820 {
9821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9822 return( ret );
9823 }
9824
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009825 if( len > max_len )
9826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009828 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009831 "maximum fragment length: %d > %d",
9832 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009834 }
9835 else
9836#endif
9837 len = max_len;
9838 }
Paul Bakker887bd502011-06-08 13:10:54 +00009839
Paul Bakker5121ce52009-01-03 21:22:43 +00009840 if( ssl->out_left != 0 )
9841 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009842 /*
9843 * The user has previously tried to send the data and
9844 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9845 * written. In this case, we expect the high-level write function
9846 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009851 return( ret );
9852 }
9853 }
Paul Bakker887bd502011-06-08 13:10:54 +00009854 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009855 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009856 /*
9857 * The user is trying to send a message the first time, so we need to
9858 * copy the data into the internal buffers and setup the data structure
9859 * to keep track of partial writes
9860 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009861 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009863 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009864
Hanno Becker67bc7c32018-08-06 11:33:50 +01009865 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009868 return( ret );
9869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009870 }
9871
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009872 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009873}
9874
9875/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009876 * Write application data, doing 1/n-1 splitting if necessary.
9877 *
9878 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009879 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009880 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009883static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009884 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009885{
9886 int ret;
9887
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009888 if( ssl->conf->cbc_record_splitting ==
9889 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009890 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9892 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9893 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009894 {
9895 return( ssl_write_real( ssl, buf, len ) );
9896 }
9897
9898 if( ssl->split_done == 0 )
9899 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009900 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009901 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009902 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009903 }
9904
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009905 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9906 return( ret );
9907 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009908
9909 return( ret + 1 );
9910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009912
9913/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009914 * Write application data (public-facing wrapper)
9915 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009916int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009917{
9918 int ret;
9919
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009921
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009922 if( ssl == NULL || ssl->conf == NULL )
9923 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9924
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009925#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009926 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9927 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009928 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009929 return( ret );
9930 }
9931#endif
9932
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009933 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009934 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009935 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009936 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009938 return( ret );
9939 }
9940 }
9941
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009942#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009943 ret = ssl_write_split( ssl, buf, len );
9944#else
9945 ret = ssl_write_real( ssl, buf, len );
9946#endif
9947
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009949
9950 return( ret );
9951}
9952
9953/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 * Notify the peer that the connection is being closed
9955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009957{
9958 int ret;
9959
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009960 if( ssl == NULL || ssl->conf == NULL )
9961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009964
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009965 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9971 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9972 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009975 return( ret );
9976 }
9977 }
9978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009980
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009981 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009982}
9983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009985{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009986 if( transform == NULL )
9987 return;
9988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009990 deflateEnd( &transform->ctx_deflate );
9991 inflateEnd( &transform->ctx_inflate );
9992#endif
9993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9995 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009996
Hanno Becker92231322018-01-03 15:32:51 +00009997#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 mbedtls_md_free( &transform->md_ctx_enc );
9999 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010000#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010001
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010002 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010003}
10004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005#if defined(MBEDTLS_X509_CRT_PARSE_C)
10006static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010008 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010009
10010 while( cur != NULL )
10011 {
10012 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010014 cur = next;
10015 }
10016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010018
Hanno Becker0271f962018-08-16 13:23:47 +010010019#if defined(MBEDTLS_SSL_PROTO_DTLS)
10020
10021static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10022{
10023 unsigned offset;
10024 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10025
10026 if( hs == NULL )
10027 return;
10028
Hanno Becker283f5ef2018-08-24 09:34:47 +010010029 ssl_free_buffered_record( ssl );
10030
Hanno Becker0271f962018-08-16 13:23:47 +010010031 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010032 ssl_buffering_free_slot( ssl, offset );
10033}
10034
10035static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10036 uint8_t slot )
10037{
10038 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10039 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010040
10041 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10042 return;
10043
Hanno Beckere605b192018-08-21 15:59:07 +010010044 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010045 {
Hanno Beckere605b192018-08-21 15:59:07 +010010046 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010047 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010048 mbedtls_free( hs_buf->data );
10049 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010050 }
10051}
10052
10053#endif /* MBEDTLS_SSL_PROTO_DTLS */
10054
Gilles Peskine9b562d52018-04-25 20:32:43 +020010055void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010056{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010057 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10058
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010059 if( handshake == NULL )
10060 return;
10061
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010062#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10063 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10064 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010065 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010066 handshake->async_in_progress = 0;
10067 }
10068#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10069
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010070#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10071 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10072 mbedtls_md5_free( &handshake->fin_md5 );
10073 mbedtls_sha1_free( &handshake->fin_sha1 );
10074#endif
10075#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10076#if defined(MBEDTLS_SHA256_C)
10077 mbedtls_sha256_free( &handshake->fin_sha256 );
10078#endif
10079#if defined(MBEDTLS_SHA512_C)
10080 mbedtls_sha512_free( &handshake->fin_sha512 );
10081#endif
10082#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084#if defined(MBEDTLS_DHM_C)
10085 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087#if defined(MBEDTLS_ECDH_C)
10088 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010089#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010090#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010091 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010092#if defined(MBEDTLS_SSL_CLI_C)
10093 mbedtls_free( handshake->ecjpake_cache );
10094 handshake->ecjpake_cache = NULL;
10095 handshake->ecjpake_cache_len = 0;
10096#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010097#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010098
Janos Follath4ae5c292016-02-10 11:27:43 +000010099#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10100 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010101 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010103#endif
10104
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010105#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10106 if( handshake->psk != NULL )
10107 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010108 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010109 mbedtls_free( handshake->psk );
10110 }
10111#endif
10112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10114 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010115 /*
10116 * Free only the linked list wrapper, not the keys themselves
10117 * since the belong to the SNI callback
10118 */
10119 if( handshake->sni_key_cert != NULL )
10120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010122
10123 while( cur != NULL )
10124 {
10125 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010127 cur = next;
10128 }
10129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010131
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010132#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010133 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010134#endif
10135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136#if defined(MBEDTLS_SSL_PROTO_DTLS)
10137 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010138 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010139 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010140#endif
10141
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010142 mbedtls_platform_zeroize( handshake,
10143 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010144}
10145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010147{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010148 if( session == NULL )
10149 return;
10150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000010152 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +000010153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154 mbedtls_x509_crt_free( session->peer_cert );
10155 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +000010156 }
Paul Bakkered27a042013-04-18 22:46:23 +020010157#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010158
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010159#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010160 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010161#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010162
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010163 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010164}
10165
Paul Bakker5121ce52009-01-03 21:22:43 +000010166/*
10167 * Free an SSL context
10168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010170{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010171 if( ssl == NULL )
10172 return;
10173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010175
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010176 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010177 {
Angus Grattond8213d02016-05-25 20:56:48 +100010178 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010180 }
10181
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010182 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010183 {
Angus Grattond8213d02016-05-25 20:56:48 +100010184 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010186 }
10187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010189 if( ssl->compress_buf != NULL )
10190 {
Angus Grattond8213d02016-05-25 20:56:48 +100010191 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010193 }
10194#endif
10195
Paul Bakker48916f92012-09-16 19:57:18 +000010196 if( ssl->transform )
10197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 mbedtls_ssl_transform_free( ssl->transform );
10199 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010200 }
10201
10202 if( ssl->handshake )
10203 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010204 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10206 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 mbedtls_free( ssl->handshake );
10209 mbedtls_free( ssl->transform_negotiate );
10210 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010211 }
10212
Paul Bakkerc0463502013-02-14 11:19:38 +010010213 if( ssl->session )
10214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215 mbedtls_ssl_session_free( ssl->session );
10216 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010217 }
10218
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010219#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010220 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010221 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010222 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010224 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010225#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10228 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10231 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010232 }
10233#endif
10234
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010235#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010237#endif
10238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010240
Paul Bakker86f04f42013-02-14 11:20:09 +010010241 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010242 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010243}
10244
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010245/*
10246 * Initialze mbedtls_ssl_config
10247 */
10248void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10249{
10250 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10251}
10252
Simon Butcherc97b6972015-12-27 23:48:17 +000010253#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010254static int ssl_preset_default_hashes[] = {
10255#if defined(MBEDTLS_SHA512_C)
10256 MBEDTLS_MD_SHA512,
10257 MBEDTLS_MD_SHA384,
10258#endif
10259#if defined(MBEDTLS_SHA256_C)
10260 MBEDTLS_MD_SHA256,
10261 MBEDTLS_MD_SHA224,
10262#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010263#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010264 MBEDTLS_MD_SHA1,
10265#endif
10266 MBEDTLS_MD_NONE
10267};
Simon Butcherc97b6972015-12-27 23:48:17 +000010268#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010269
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010270static int ssl_preset_suiteb_ciphersuites[] = {
10271 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10272 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10273 0
10274};
10275
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010276#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010277static int ssl_preset_suiteb_hashes[] = {
10278 MBEDTLS_MD_SHA256,
10279 MBEDTLS_MD_SHA384,
10280 MBEDTLS_MD_NONE
10281};
10282#endif
10283
10284#if defined(MBEDTLS_ECP_C)
10285static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10286 MBEDTLS_ECP_DP_SECP256R1,
10287 MBEDTLS_ECP_DP_SECP384R1,
10288 MBEDTLS_ECP_DP_NONE
10289};
10290#endif
10291
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010292/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010293 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010294 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010295int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010296 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010297{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010298#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010299 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010300#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010301
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010302 /* Use the functions here so that they are covered in tests,
10303 * but otherwise access member directly for efficiency */
10304 mbedtls_ssl_conf_endpoint( conf, endpoint );
10305 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010306
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010307 /*
10308 * Things that are common to all presets
10309 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010310#if defined(MBEDTLS_SSL_CLI_C)
10311 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10312 {
10313 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10314#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10315 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10316#endif
10317 }
10318#endif
10319
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010320#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010321 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010322#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010323
10324#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10325 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10326#endif
10327
10328#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10329 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10330#endif
10331
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010332#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10333 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10334#endif
10335
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010336#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010337 conf->f_cookie_write = ssl_cookie_write_dummy;
10338 conf->f_cookie_check = ssl_cookie_check_dummy;
10339#endif
10340
10341#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10342 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10343#endif
10344
Janos Follath088ce432017-04-10 12:42:31 +010010345#if defined(MBEDTLS_SSL_SRV_C)
10346 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10347#endif
10348
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010349#if defined(MBEDTLS_SSL_PROTO_DTLS)
10350 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10351 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10352#endif
10353
10354#if defined(MBEDTLS_SSL_RENEGOTIATION)
10355 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010356 memset( conf->renego_period, 0x00, 2 );
10357 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010358#endif
10359
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010360#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10361 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10362 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010363 const unsigned char dhm_p[] =
10364 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10365 const unsigned char dhm_g[] =
10366 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10367
Hanno Beckera90658f2017-10-04 15:29:08 +010010368 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10369 dhm_p, sizeof( dhm_p ),
10370 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010371 {
10372 return( ret );
10373 }
10374 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010375#endif
10376
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010377 /*
10378 * Preset-specific defaults
10379 */
10380 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010381 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010382 /*
10383 * NSA Suite B
10384 */
10385 case MBEDTLS_SSL_PRESET_SUITEB:
10386 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10387 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10388 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10389 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10390
10391 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10392 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10393 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10394 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10395 ssl_preset_suiteb_ciphersuites;
10396
10397#if defined(MBEDTLS_X509_CRT_PARSE_C)
10398 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010399#endif
10400
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010401#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010402 conf->sig_hashes = ssl_preset_suiteb_hashes;
10403#endif
10404
10405#if defined(MBEDTLS_ECP_C)
10406 conf->curve_list = ssl_preset_suiteb_curves;
10407#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010408 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010409
10410 /*
10411 * Default
10412 */
10413 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010414 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10415 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10416 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10417 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10418 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10419 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10420 MBEDTLS_SSL_MIN_MINOR_VERSION :
10421 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010422 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10423 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10424
10425#if defined(MBEDTLS_SSL_PROTO_DTLS)
10426 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10427 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10428#endif
10429
10430 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10431 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10432 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10433 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10434 mbedtls_ssl_list_ciphersuites();
10435
10436#if defined(MBEDTLS_X509_CRT_PARSE_C)
10437 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10438#endif
10439
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010440#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010441 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010442#endif
10443
10444#if defined(MBEDTLS_ECP_C)
10445 conf->curve_list = mbedtls_ecp_grp_id_list();
10446#endif
10447
10448#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10449 conf->dhm_min_bitlen = 1024;
10450#endif
10451 }
10452
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010453 return( 0 );
10454}
10455
10456/*
10457 * Free mbedtls_ssl_config
10458 */
10459void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10460{
10461#if defined(MBEDTLS_DHM_C)
10462 mbedtls_mpi_free( &conf->dhm_P );
10463 mbedtls_mpi_free( &conf->dhm_G );
10464#endif
10465
10466#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10467 if( conf->psk != NULL )
10468 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010469 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010470 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010471 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010472 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010473 }
10474
10475 if( conf->psk_identity != NULL )
10476 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010477 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010478 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010479 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010480 conf->psk_identity_len = 0;
10481 }
10482#endif
10483
10484#if defined(MBEDTLS_X509_CRT_PARSE_C)
10485 ssl_key_cert_free( conf->key_cert );
10486#endif
10487
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010488 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010489}
10490
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010491#if defined(MBEDTLS_PK_C) && \
10492 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010493/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010498#if defined(MBEDTLS_RSA_C)
10499 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10500 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502#if defined(MBEDTLS_ECDSA_C)
10503 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10504 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010507}
10508
Hanno Becker7e5437a2017-04-28 17:15:26 +010010509unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10510{
10511 switch( type ) {
10512 case MBEDTLS_PK_RSA:
10513 return( MBEDTLS_SSL_SIG_RSA );
10514 case MBEDTLS_PK_ECDSA:
10515 case MBEDTLS_PK_ECKEY:
10516 return( MBEDTLS_SSL_SIG_ECDSA );
10517 default:
10518 return( MBEDTLS_SSL_SIG_ANON );
10519 }
10520}
10521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010523{
10524 switch( sig )
10525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010526#if defined(MBEDTLS_RSA_C)
10527 case MBEDTLS_SSL_SIG_RSA:
10528 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010530#if defined(MBEDTLS_ECDSA_C)
10531 case MBEDTLS_SSL_SIG_ECDSA:
10532 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010533#endif
10534 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010536 }
10537}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010538#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010539
Hanno Becker7e5437a2017-04-28 17:15:26 +010010540#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10541 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10542
10543/* Find an entry in a signature-hash set matching a given hash algorithm. */
10544mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10545 mbedtls_pk_type_t sig_alg )
10546{
10547 switch( sig_alg )
10548 {
10549 case MBEDTLS_PK_RSA:
10550 return( set->rsa );
10551 case MBEDTLS_PK_ECDSA:
10552 return( set->ecdsa );
10553 default:
10554 return( MBEDTLS_MD_NONE );
10555 }
10556}
10557
10558/* Add a signature-hash-pair to a signature-hash set */
10559void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10560 mbedtls_pk_type_t sig_alg,
10561 mbedtls_md_type_t md_alg )
10562{
10563 switch( sig_alg )
10564 {
10565 case MBEDTLS_PK_RSA:
10566 if( set->rsa == MBEDTLS_MD_NONE )
10567 set->rsa = md_alg;
10568 break;
10569
10570 case MBEDTLS_PK_ECDSA:
10571 if( set->ecdsa == MBEDTLS_MD_NONE )
10572 set->ecdsa = md_alg;
10573 break;
10574
10575 default:
10576 break;
10577 }
10578}
10579
10580/* Allow exactly one hash algorithm for each signature. */
10581void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10582 mbedtls_md_type_t md_alg )
10583{
10584 set->rsa = md_alg;
10585 set->ecdsa = md_alg;
10586}
10587
10588#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10589 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10590
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010591/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010592 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010595{
10596 switch( hash )
10597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598#if defined(MBEDTLS_MD5_C)
10599 case MBEDTLS_SSL_HASH_MD5:
10600 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602#if defined(MBEDTLS_SHA1_C)
10603 case MBEDTLS_SSL_HASH_SHA1:
10604 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606#if defined(MBEDTLS_SHA256_C)
10607 case MBEDTLS_SSL_HASH_SHA224:
10608 return( MBEDTLS_MD_SHA224 );
10609 case MBEDTLS_SSL_HASH_SHA256:
10610 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010611#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#if defined(MBEDTLS_SHA512_C)
10613 case MBEDTLS_SSL_HASH_SHA384:
10614 return( MBEDTLS_MD_SHA384 );
10615 case MBEDTLS_SSL_HASH_SHA512:
10616 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010617#endif
10618 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010620 }
10621}
10622
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010623/*
10624 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10625 */
10626unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10627{
10628 switch( md )
10629 {
10630#if defined(MBEDTLS_MD5_C)
10631 case MBEDTLS_MD_MD5:
10632 return( MBEDTLS_SSL_HASH_MD5 );
10633#endif
10634#if defined(MBEDTLS_SHA1_C)
10635 case MBEDTLS_MD_SHA1:
10636 return( MBEDTLS_SSL_HASH_SHA1 );
10637#endif
10638#if defined(MBEDTLS_SHA256_C)
10639 case MBEDTLS_MD_SHA224:
10640 return( MBEDTLS_SSL_HASH_SHA224 );
10641 case MBEDTLS_MD_SHA256:
10642 return( MBEDTLS_SSL_HASH_SHA256 );
10643#endif
10644#if defined(MBEDTLS_SHA512_C)
10645 case MBEDTLS_MD_SHA384:
10646 return( MBEDTLS_SSL_HASH_SHA384 );
10647 case MBEDTLS_MD_SHA512:
10648 return( MBEDTLS_SSL_HASH_SHA512 );
10649#endif
10650 default:
10651 return( MBEDTLS_SSL_HASH_NONE );
10652 }
10653}
10654
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010655#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010656/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010657 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010658 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010659 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010660int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010661{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010663
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010664 if( ssl->conf->curve_list == NULL )
10665 return( -1 );
10666
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010667 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010668 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010669 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010670
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010671 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010672}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010673#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010674
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010675#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010676/*
10677 * Check if a hash proposed by the peer is in our list.
10678 * Return 0 if we're willing to use it, -1 otherwise.
10679 */
10680int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10681 mbedtls_md_type_t md )
10682{
10683 const int *cur;
10684
10685 if( ssl->conf->sig_hashes == NULL )
10686 return( -1 );
10687
10688 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10689 if( *cur == (int) md )
10690 return( 0 );
10691
10692 return( -1 );
10693}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010694#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696#if defined(MBEDTLS_X509_CRT_PARSE_C)
10697int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10698 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010699 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010700 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010701{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010702 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010704 int usage = 0;
10705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010706#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010707 const char *ext_oid;
10708 size_t ext_len;
10709#endif
10710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10712 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010713 ((void) cert);
10714 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010715 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010716#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10719 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010720 {
10721 /* Server part of the key exchange */
10722 switch( ciphersuite->key_exchange )
10723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 case MBEDTLS_KEY_EXCHANGE_RSA:
10725 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010726 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010727 break;
10728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10730 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10731 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10732 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010733 break;
10734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10736 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010737 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010738 break;
10739
10740 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741 case MBEDTLS_KEY_EXCHANGE_NONE:
10742 case MBEDTLS_KEY_EXCHANGE_PSK:
10743 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10744 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010745 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010746 usage = 0;
10747 }
10748 }
10749 else
10750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10752 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010753 }
10754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010756 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010757 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010758 ret = -1;
10759 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010760#else
10761 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10765 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10768 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010769 }
10770 else
10771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10773 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010774 }
10775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010777 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010778 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010779 ret = -1;
10780 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010782
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010783 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010786
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010787/*
10788 * Convert version numbers to/from wire format
10789 * and, for DTLS, to/from TLS equivalent.
10790 *
10791 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010792 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010793 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10794 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010797 unsigned char ver[2] )
10798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#if defined(MBEDTLS_SSL_PROTO_DTLS)
10800 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010803 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10804
10805 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10806 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10807 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010808 else
10809#else
10810 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010811#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010812 {
10813 ver[0] = (unsigned char) major;
10814 ver[1] = (unsigned char) minor;
10815 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010816}
10817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010818void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010819 const unsigned char ver[2] )
10820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821#if defined(MBEDTLS_SSL_PROTO_DTLS)
10822 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010823 {
10824 *major = 255 - ver[0] + 2;
10825 *minor = 255 - ver[1] + 1;
10826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010828 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10829 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010830 else
10831#else
10832 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010833#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010834 {
10835 *major = ver[0];
10836 *minor = ver[1];
10837 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010838}
10839
Simon Butcher99000142016-10-13 17:21:01 +010010840int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10841{
10842#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10843 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10844 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10845
10846 switch( md )
10847 {
10848#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10849#if defined(MBEDTLS_MD5_C)
10850 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010851 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010852#endif
10853#if defined(MBEDTLS_SHA1_C)
10854 case MBEDTLS_SSL_HASH_SHA1:
10855 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10856 break;
10857#endif
10858#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10859#if defined(MBEDTLS_SHA512_C)
10860 case MBEDTLS_SSL_HASH_SHA384:
10861 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10862 break;
10863#endif
10864#if defined(MBEDTLS_SHA256_C)
10865 case MBEDTLS_SSL_HASH_SHA256:
10866 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10867 break;
10868#endif
10869 default:
10870 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10871 }
10872
10873 return 0;
10874#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10875 (void) ssl;
10876 (void) md;
10877
10878 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10880}
10881
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010882#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10883 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10884int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10885 unsigned char *output,
10886 unsigned char *data, size_t data_len )
10887{
10888 int ret = 0;
10889 mbedtls_md5_context mbedtls_md5;
10890 mbedtls_sha1_context mbedtls_sha1;
10891
10892 mbedtls_md5_init( &mbedtls_md5 );
10893 mbedtls_sha1_init( &mbedtls_sha1 );
10894
10895 /*
10896 * digitally-signed struct {
10897 * opaque md5_hash[16];
10898 * opaque sha_hash[20];
10899 * };
10900 *
10901 * md5_hash
10902 * MD5(ClientHello.random + ServerHello.random
10903 * + ServerParams);
10904 * sha_hash
10905 * SHA(ClientHello.random + ServerHello.random
10906 * + ServerParams);
10907 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010908 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010909 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010911 goto exit;
10912 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010913 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010914 ssl->handshake->randbytes, 64 ) ) != 0 )
10915 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010917 goto exit;
10918 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010919 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010920 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010922 goto exit;
10923 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010924 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010925 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010927 goto exit;
10928 }
10929
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010930 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010931 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010933 goto exit;
10934 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010935 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010936 ssl->handshake->randbytes, 64 ) ) != 0 )
10937 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010939 goto exit;
10940 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010941 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010942 data_len ) ) != 0 )
10943 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010945 goto exit;
10946 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010947 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010948 output + 16 ) ) != 0 )
10949 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010951 goto exit;
10952 }
10953
10954exit:
10955 mbedtls_md5_free( &mbedtls_md5 );
10956 mbedtls_sha1_free( &mbedtls_sha1 );
10957
10958 if( ret != 0 )
10959 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10960 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10961
10962 return( ret );
10963
10964}
10965#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10966 MBEDTLS_SSL_PROTO_TLS1_1 */
10967
10968#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10969 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10970int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010971 unsigned char *hash, size_t *hashlen,
10972 unsigned char *data, size_t data_len,
10973 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010974{
10975 int ret = 0;
10976 mbedtls_md_context_t ctx;
10977 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010978 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010979
10980 mbedtls_md_init( &ctx );
10981
10982 /*
10983 * digitally-signed struct {
10984 * opaque client_random[32];
10985 * opaque server_random[32];
10986 * ServerDHParams params;
10987 * };
10988 */
10989 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10990 {
10991 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10992 goto exit;
10993 }
10994 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10995 {
10996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10997 goto exit;
10998 }
10999 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11000 {
11001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11002 goto exit;
11003 }
11004 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11005 {
11006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11007 goto exit;
11008 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011009 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011010 {
11011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11012 goto exit;
11013 }
11014
11015exit:
11016 mbedtls_md_free( &ctx );
11017
11018 if( ret != 0 )
11019 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11020 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11021
11022 return( ret );
11023}
11024#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11025 MBEDTLS_SSL_PROTO_TLS1_2 */
11026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027#endif /* MBEDTLS_SSL_TLS_C */