blob: 61157432f488294aaa6674f61c4aa6e7ac767f9e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
115#define SSL_DONT_FORCE_FLUSH 0
116#define SSL_FORCE_FLUSH 1
117
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100119
Hanno Becker35c36a62019-04-23 12:31:42 +0100120#if defined(MBEDTLS_SSL_CID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100121/* Top-level Connection ID API */
122
Hanno Beckerca092242019-04-25 16:01:49 +0100123/* WARNING: The CID feature isn't fully implemented yet
124 * and will not be used. */
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125int mbedtls_ssl_conf_cid_len( mbedtls_ssl_config *conf,
126 size_t len )
127{
128 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
130
131 conf->cid_len = len;
132 return( 0 );
133}
134
135/* WARNING: The CID feature isn't fully implemented yet
136 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100137int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
138 int enable,
139 unsigned char const *own_cid,
140 size_t own_cid_len )
141{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
Hanno Beckerca092242019-04-25 16:01:49 +0100145 ssl->negotiate_cid = enable;
146 if( enable == MBEDTLS_SSL_CID_DISABLED )
147 {
148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
149 return( 0 );
150 }
151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100152 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100153
Hanno Beckerad4a1372019-05-03 13:06:44 +0100154 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100155 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
157 (unsigned) own_cid_len,
158 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
160 }
161
162 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100163 /* Truncation is not an issue here because
164 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
165 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100166
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100167 return( 0 );
168}
169
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170/* WARNING: The CID feature isn't fully implemented yet
171 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100172int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
173 int *enabled,
174 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
175 size_t *peer_cid_len )
176{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100177 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100178
Hanno Becker76a79ab2019-05-03 14:38:32 +0100179 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
180 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
181 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100183 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100184
Hanno Beckerc5f24222019-05-03 12:54:52 +0100185 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
186 * were used, but client and server requested the empty CID.
187 * This is indistinguishable from not using the CID extension
188 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100189 if( ssl->transform_in->in_cid_len == 0 &&
190 ssl->transform_in->out_cid_len == 0 )
191 {
192 return( 0 );
193 }
194
195 *peer_cid_len = ssl->transform_in->out_cid_len;
196 memcpy( peer_cid, ssl->transform_in->out_cid,
197 ssl->transform_in->out_cid_len );
198
199 *enabled = MBEDTLS_SSL_CID_ENABLED;
200
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201 return( 0 );
202}
Hanno Becker35c36a62019-04-23 12:31:42 +0100203#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-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
Hanno Becker52055ae2019-02-06 14:30:46 +0000366int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
367 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)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000373
374#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200375 if( src->peer_cert != NULL )
376 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200377 int ret;
378
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200379 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200380 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200381 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200386 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389 dst->peer_cert = NULL;
390 return( ret );
391 }
392 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000393#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000394 if( src->peer_cert_digest != NULL )
395 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000396 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000397 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000398 if( dst->peer_cert_digest == NULL )
399 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
400
401 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
402 src->peer_cert_digest_len );
403 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000404 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000405 }
406#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200410#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200411 if( src->ticket != NULL )
412 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200413 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200414 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200415 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416
417 memcpy( dst->ticket, src->ticket, src->ticket_len );
418 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200419#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200420
421 return( 0 );
422}
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
425int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200426 const unsigned char *key_enc, const unsigned char *key_dec,
427 size_t keylen,
428 const unsigned char *iv_enc, const unsigned char *iv_dec,
429 size_t ivlen,
430 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200431 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
433int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
434int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
435int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
436int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
437#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000438
Paul Bakker5121ce52009-01-03 21:22:43 +0000439/*
440 * Key material generation
441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200443static int ssl3_prf( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000446 unsigned char *dstbuf, size_t dlen )
447{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100448 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000449 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200450 mbedtls_md5_context md5;
451 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000452 unsigned char padding[16];
453 unsigned char sha1sum[20];
454 ((void)label);
455
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200456 mbedtls_md5_init( &md5 );
457 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200458
Paul Bakker5f70b252012-09-13 14:23:06 +0000459 /*
460 * SSLv3:
461 * block =
462 * MD5( secret + SHA1( 'A' + secret + random ) ) +
463 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
464 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
465 * ...
466 */
467 for( i = 0; i < dlen / 16; i++ )
468 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200469 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000470
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100473 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100474 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000481
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100486 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100489 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000490 }
491
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200493 mbedtls_md5_free( &md5 );
494 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000495
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500496 mbedtls_platform_zeroize( padding, sizeof( padding ) );
497 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000498
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100499 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200504static int tls1_prf( const unsigned char *secret, size_t slen,
505 const char *label,
506 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000507 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000508{
Paul Bakker23986e52011-04-24 08:57:21 +0000509 size_t nb, hs;
510 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200511 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300512 unsigned char *tmp;
513 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 const mbedtls_md_info_t *md_info;
516 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100517 int ret;
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
Ron Eldor3b350852019-05-07 18:31:49 +0300521 tmp_len = 20 + strlen( label ) + rlen;
522 tmp = mbedtls_calloc( 1, tmp_len );
523 if( tmp == NULL )
524 {
525 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
526 goto exit;
527 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 hs = ( slen + 1 ) / 2;
530 S1 = secret;
531 S2 = secret + slen - hs;
532
533 nb = strlen( label );
534 memcpy( tmp + 20, label, nb );
535 memcpy( tmp + 20 + nb, random, rlen );
536 nb += rlen;
537
538 /*
539 * First compute P_md5(secret,label+random)[0..dlen]
540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300542 {
543 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
544 goto exit;
545 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300548 {
549 goto exit;
550 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
553 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
554 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 for( i = 0; i < dlen; i += 16 )
557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_md_hmac_reset ( &md_ctx );
559 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
560 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_md_hmac_reset ( &md_ctx );
563 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
564 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
567
568 for( j = 0; j < k; j++ )
569 dstbuf[i + j] = h_i[j];
570 }
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100573
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 /*
575 * XOR out with P_sha1(secret,label+random)[0..dlen]
576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300578 {
579 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
580 goto exit;
581 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300584 {
585 goto exit;
586 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
589 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
590 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
592 for( i = 0; i < dlen; i += 20 )
593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_md_hmac_reset ( &md_ctx );
595 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
596 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_md_hmac_reset ( &md_ctx );
599 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
600 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
603
604 for( j = 0; j < k; j++ )
605 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
606 }
607
Ron Eldor3b350852019-05-07 18:31:49 +0300608exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100610
Ron Eldor3b350852019-05-07 18:31:49 +0300611 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500612 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Ron Eldor3b350852019-05-07 18:31:49 +0300614 mbedtls_free( tmp );
615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500620#if defined(MBEDTLS_USE_PSA_CRYPTO)
621static int tls_prf_generic( mbedtls_md_type_t md_type,
622 const unsigned char *secret, size_t slen,
623 const char *label,
624 const unsigned char *random, size_t rlen,
625 unsigned char *dstbuf, size_t dlen )
626{
627 psa_status_t status;
628 psa_algorithm_t alg;
629 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500630 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500631 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
632
Andrzej Kurek2f760752019-01-28 08:08:15 -0500633 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500634 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500635
Andrzej Kurekc929a822019-01-14 03:51:11 -0500636 if( md_type == MBEDTLS_MD_SHA384 )
637 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
638 else
639 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
640
Andrzej Kurek2f760752019-01-28 08:08:15 -0500641 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500642 psa_key_policy_set_usage( &policy,
643 PSA_KEY_USAGE_DERIVE,
644 alg );
645 status = psa_set_key_policy( master_slot, &policy );
646 if( status != PSA_SUCCESS )
647 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
648
649 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
650 if( status != PSA_SUCCESS )
651 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
652
653 status = psa_key_derivation( &generator,
654 master_slot, alg,
655 random, rlen,
656 (unsigned char const *) label,
657 (size_t) strlen( label ),
658 dlen );
659 if( status != PSA_SUCCESS )
660 {
661 psa_generator_abort( &generator );
662 psa_destroy_key( master_slot );
663 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
664 }
665
666 status = psa_generator_read( &generator, dstbuf, dlen );
667 if( status != PSA_SUCCESS )
668 {
669 psa_generator_abort( &generator );
670 psa_destroy_key( master_slot );
671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
672 }
673
674 status = psa_generator_abort( &generator );
675 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500676 {
677 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500678 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500679 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500680
681 status = psa_destroy_key( master_slot );
682 if( status != PSA_SUCCESS )
683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
684
Andrzej Kurek33171262019-01-15 03:25:18 -0500685 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500686}
687
688#else /* MBEDTLS_USE_PSA_CRYPTO */
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100691 const unsigned char *secret, size_t slen,
692 const char *label,
693 const unsigned char *random, size_t rlen,
694 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000695{
696 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100697 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300698 unsigned char *tmp;
699 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
701 const mbedtls_md_info_t *md_info;
702 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100703 int ret;
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100711
Ron Eldor3b350852019-05-07 18:31:49 +0300712 tmp_len = md_len + strlen( label ) + rlen;
713 tmp = mbedtls_calloc( 1, tmp_len );
714 if( tmp == NULL )
715 {
716 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
717 goto exit;
718 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000719
720 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100721 memcpy( tmp + md_len, label, nb );
722 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723 nb += rlen;
724
725 /*
726 * Compute P_<hash>(secret, label + random)[0..dlen]
727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300729 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
732 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100734
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100735 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_hmac_reset ( &md_ctx );
738 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
739 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_hmac_reset ( &md_ctx );
742 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
743 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100745 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000746
747 for( j = 0; j < k; j++ )
748 dstbuf[i + j] = h_i[j];
749 }
750
Ron Eldor3b350852019-05-07 18:31:49 +0300751exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100753
Ron Eldor3b350852019-05-07 18:31:49 +0300754 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500755 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756
Ron Eldor3b350852019-05-07 18:31:49 +0300757 mbedtls_free( tmp );
758
759 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500761#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763static int tls_prf_sha256( const unsigned char *secret, size_t slen,
764 const char *label,
765 const unsigned char *random, size_t rlen,
766 unsigned char *dstbuf, size_t dlen )
767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100769 label, random, rlen, dstbuf, dlen ) );
770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200774static int tls_prf_sha384( const unsigned char *secret, size_t slen,
775 const char *label,
776 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000777 unsigned char *dstbuf, size_t dlen )
778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100780 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_SHA512_C */
783#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
788 defined(MBEDTLS_SSL_PROTO_TLS1_1)
789static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790#endif
Paul Bakker380da532012-04-18 16:10:25 +0000791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SSL_PROTO_SSL3)
793static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
794static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200795#endif
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
798static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
799static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200800#endif
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
803#if defined(MBEDTLS_SHA256_C)
804static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
805static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
806static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200807#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#if defined(MBEDTLS_SHA512_C)
810static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
811static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
812static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100816#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100817 defined(MBEDTLS_USE_PSA_CRYPTO)
818static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
819{
820 if( ssl->conf->f_psk != NULL )
821 {
822 /* If we've used a callback to select the PSK,
823 * the static configuration is irrelevant. */
824 if( ssl->handshake->psk_opaque != 0 )
825 return( 1 );
826
827 return( 0 );
828 }
829
830 if( ssl->conf->psk_opaque != 0 )
831 return( 1 );
832
833 return( 0 );
834}
835#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100836 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100837
Ron Eldorcf280092019-05-14 20:19:13 +0300838#if defined(MBEDTLS_SSL_EXPORT_KEYS)
839static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
840{
841#if defined(MBEDTLS_SSL_PROTO_SSL3)
842 if( tls_prf == ssl3_prf )
843 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300844 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300845 }
846 else
847#endif
848#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
849 if( tls_prf == tls1_prf )
850 {
851 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
852 }
853 else
854#endif
855#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
856#if defined(MBEDTLS_SHA512_C)
857 if( tls_prf == tls_prf_sha384 )
858 {
859 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
860 }
861 else
862#endif
863#if defined(MBEDTLS_SHA256_C)
864 if( tls_prf == tls_prf_sha256 )
865 {
866 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
867 }
868 else
869#endif
870#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
871 return( MBEDTLS_SSL_TLS_PRF_NONE );
872}
873#endif /* MBEDTLS_SSL_EXPORT_KEYS */
874
Ron Eldor51d3ab52019-05-12 14:54:30 +0300875int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
876 const unsigned char *secret, size_t slen,
877 const char *label,
878 const unsigned char *random, size_t rlen,
879 unsigned char *dstbuf, size_t dlen )
880{
881 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
882
883 switch( prf )
884 {
885#if defined(MBEDTLS_SSL_PROTO_SSL3)
886 case MBEDTLS_SSL_TLS_PRF_SSL3:
887 tls_prf = ssl3_prf;
888 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300889#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300890#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
891 case MBEDTLS_SSL_TLS_PRF_TLS1:
892 tls_prf = tls1_prf;
893 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300894#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
895
896#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300897#if defined(MBEDTLS_SHA512_C)
898 case MBEDTLS_SSL_TLS_PRF_SHA384:
899 tls_prf = tls_prf_sha384;
900 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300901#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300902#if defined(MBEDTLS_SHA256_C)
903 case MBEDTLS_SSL_TLS_PRF_SHA256:
904 tls_prf = tls_prf_sha256;
905 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300906#endif /* MBEDTLS_SHA256_C */
907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300908 default:
909 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
910 }
911
912 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
913}
914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000916{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200917 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000918#if defined(MBEDTLS_USE_PSA_CRYPTO)
919 int psa_fallthrough;
920#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 unsigned char keyblk[256];
923 unsigned char *key1;
924 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100925 unsigned char *mac_enc;
926 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000927 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200928 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000929 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000930 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 const mbedtls_cipher_info_t *cipher_info;
932 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100933
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000934 /* cf. RFC 5246, Section 8.1:
935 * "The master secret is always exactly 48 bytes in length." */
936 size_t const master_secret_len = 48;
937
Hanno Becker35b23c72018-10-23 12:10:41 +0100938#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
939 unsigned char session_hash[48];
940#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_ssl_session *session = ssl->session_negotiate;
943 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
944 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
949 transform->encrypt_then_mac = session->encrypt_then_mac;
950#endif
951 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000952
953 ciphersuite_info = handshake->ciphersuite_info;
954 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100955 if( cipher_info == NULL )
956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000958 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100960 }
961
Hanno Beckere694c3e2017-12-27 21:34:08 +0000962 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100963 if( md_info == NULL )
964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000966 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100968 }
969
Hanno Becker4bf74652019-04-26 16:22:27 +0100970#if defined(MBEDTLS_SSL_CID)
971 /* Copy own and peer's CID if the use of the CID
972 * extension has been negotiated. */
973 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
974 {
975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100976
977 /* Uncomment this once CID-parsing and support for a change
978 * record content type during record decryption are added. */
979 /* transform->in_cid_len = ssl->own_cid_len; */
980 /* transform->out_cid_len = ssl->handshake->peer_cid_len; */
981 /* memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); */
982 /* memcpy( transform->out_cid, ssl->handshake->peer_cid, */
983 /* ssl->handshake->peer_cid_len ); */
Hanno Becker4bf74652019-04-26 16:22:27 +0100984
985 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
986 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100987 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100988 transform->in_cid_len );
989 }
990#endif /* MBEDTLS_SSL_CID */
991
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_SSL_PROTO_SSL3)
996 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000997 {
Paul Bakker48916f92012-09-16 19:57:18 +0000998 handshake->tls_prf = ssl3_prf;
999 handshake->calc_verify = ssl_calc_verify_ssl;
1000 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001001 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001002 else
1003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1005 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001006 {
Paul Bakker48916f92012-09-16 19:57:18 +00001007 handshake->tls_prf = tls1_prf;
1008 handshake->calc_verify = ssl_calc_verify_tls;
1009 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001010 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001011 else
1012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1014#if defined(MBEDTLS_SHA512_C)
1015 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001016 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001017 {
Paul Bakker48916f92012-09-16 19:57:18 +00001018 handshake->tls_prf = tls_prf_sha384;
1019 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1020 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001021 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001022 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_SHA256_C)
1025 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001026 {
Paul Bakker48916f92012-09-16 19:57:18 +00001027 handshake->tls_prf = tls_prf_sha256;
1028 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1029 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001030 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001031 else
1032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001037 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001038
1039 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 * SSLv3:
1041 * master =
1042 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1043 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1044 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001045 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001046 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 * master = PRF( premaster, "master secret", randbytes )[0..47]
1048 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001049 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1052 }
1053 else
1054 {
1055 /* The label for the KDF used for key expansion.
1056 * This is either "master secret" or "extended master secret"
1057 * depending on whether the Extended Master Secret extension
1058 * is used. */
1059 char const *lbl = "master secret";
1060
1061 /* The salt for the KDF used for key expansion.
1062 * - If the Extended Master Secret extension is not used,
1063 * this is ClientHello.Random + ServerHello.Random
1064 * (see Sect. 8.1 in RFC 5246).
1065 * - If the Extended Master Secret extension is used,
1066 * this is the transcript of the handshake so far.
1067 * (see Sect. 4 in RFC 7627). */
1068 unsigned char const *salt = handshake->randbytes;
1069 size_t salt_len = 64;
1070
1071#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001075
Hanno Becker35b23c72018-10-23 12:10:41 +01001076 lbl = "extended master secret";
1077 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001078 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1080 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001083 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1084 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001085 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001086 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001087 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001088#endif /* MBEDTLS_SHA512_C */
1089 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001090 }
1091 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001093 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001094
Hanno Becker35b23c72018-10-23 12:10:41 +01001095 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001096 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001097#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1098
Hanno Becker7d0a5692018-10-23 15:26:22 +01001099#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1100 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1101 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1102 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1103 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001104 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001105 /* Perform PSK-to-MS expansion in a single step. */
1106 psa_status_t status;
1107 psa_algorithm_t alg;
1108 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001109 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001110
1111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1112
1113 psk = ssl->conf->psk_opaque;
1114 if( ssl->handshake->psk_opaque != 0 )
1115 psk = ssl->handshake->psk_opaque;
1116
Hanno Becker22bf1452019-04-05 11:21:08 +01001117 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001118 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1119 else
1120 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1121
1122 status = psa_key_derivation( &generator, psk, alg,
1123 salt, salt_len,
1124 (unsigned char const *) lbl,
1125 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001126 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001127 if( status != PSA_SUCCESS )
1128 {
1129 psa_generator_abort( &generator );
1130 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1131 }
1132
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001133 status = psa_generator_read( &generator, session->master,
1134 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001135 if( status != PSA_SUCCESS )
1136 {
1137 psa_generator_abort( &generator );
1138 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1139 }
1140
1141 status = psa_generator_abort( &generator );
1142 if( status != PSA_SUCCESS )
1143 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001144 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001145 else
1146#endif
1147 {
1148 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1149 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001150 session->master,
1151 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001152 if( ret != 0 )
1153 {
1154 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1155 return( ret );
1156 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001157
Hanno Becker7d0a5692018-10-23 15:26:22 +01001158 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1159 handshake->premaster,
1160 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001161
Hanno Becker7d0a5692018-10-23 15:26:22 +01001162 mbedtls_platform_zeroize( handshake->premaster,
1163 sizeof(handshake->premaster) );
1164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
1167 /*
1168 * Swap the client and server random values.
1169 */
Paul Bakker48916f92012-09-16 19:57:18 +00001170 memcpy( tmp, handshake->randbytes, 64 );
1171 memcpy( handshake->randbytes, tmp + 32, 32 );
1172 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001173 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
1175 /*
1176 * SSLv3:
1177 * key block =
1178 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1179 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1180 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1181 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1182 * ...
1183 *
1184 * TLSv1:
1185 * key block = PRF( master, "key expansion", randbytes )
1186 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001187 ret = handshake->tls_prf( session->master, 48, "key expansion",
1188 handshake->randbytes, 64, keyblk, 256 );
1189 if( ret != 0 )
1190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001192 return( ret );
1193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1196 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1197 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1198 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1199 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 /*
1202 * Determine the appropriate key, IV and MAC length.
1203 */
Paul Bakker68884e32013-01-07 18:20:04 +01001204
Hanno Becker88aaf652017-12-27 08:17:40 +00001205 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001206
Hanno Becker8031d062018-01-03 15:32:31 +00001207#if defined(MBEDTLS_GCM_C) || \
1208 defined(MBEDTLS_CCM_C) || \
1209 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001211 cipher_info->mode == MBEDTLS_MODE_CCM ||
1212 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001214 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001215
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001216 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001217 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001218 transform->taglen =
1219 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001220
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001221 /* All modes haves 96-bit IVs;
1222 * GCM and CCM has 4 implicit and 8 explicit bytes
1223 * ChachaPoly has all 12 bytes implicit
1224 */
Paul Bakker68884e32013-01-07 18:20:04 +01001225 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001226 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1227 transform->fixed_ivlen = 12;
1228 else
1229 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001230
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001231 /* Minimum length of encrypted record */
1232 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001233 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001234 }
1235 else
Hanno Becker8031d062018-01-03 15:32:31 +00001236#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1237#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1238 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1239 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001240 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001241 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1243 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001246 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001249 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001250 mac_key_len = mbedtls_md_get_size( md_info );
1251 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001254 /*
1255 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1256 * (rfc 6066 page 13 or rfc 2104 section 4),
1257 * so we only need to adjust the length here.
1258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001262
1263#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1264 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001265 * HMAC implementation which also truncates the key
1266 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001267 mac_key_len = transform->maclen;
1268#endif
1269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001271
1272 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001273 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001275 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001277 transform->minlen = transform->maclen;
1278 else
Paul Bakker68884e32013-01-07 18:20:04 +01001279 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001280 /*
1281 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001282 * 1. if EtM is in use: one block plus MAC
1283 * otherwise: * first multiple of blocklen greater than maclen
1284 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1287 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001288 {
1289 transform->minlen = transform->maclen
1290 + cipher_info->block_size;
1291 }
1292 else
1293#endif
1294 {
1295 transform->minlen = transform->maclen
1296 + cipher_info->block_size
1297 - transform->maclen % cipher_info->block_size;
1298 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1301 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1302 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001303 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001304 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1307 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1308 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001309 {
1310 transform->minlen += transform->ivlen;
1311 }
1312 else
1313#endif
1314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001316 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1317 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001318 }
Paul Bakker68884e32013-01-07 18:20:04 +01001319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 }
Hanno Becker8031d062018-01-03 15:32:31 +00001321 else
1322#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1323 {
1324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Hanno Becker88aaf652017-12-27 08:17:40 +00001328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1329 (unsigned) keylen,
1330 (unsigned) transform->minlen,
1331 (unsigned) transform->ivlen,
1332 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
1334 /*
1335 * Finally setup the cipher contexts, IVs and MAC secrets.
1336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001340 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001341 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Paul Bakker68884e32013-01-07 18:20:04 +01001343 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001344 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001346 /*
1347 * This is not used in TLS v1.1.
1348 */
Paul Bakker48916f92012-09-16 19:57:18 +00001349 iv_copy_len = ( transform->fixed_ivlen ) ?
1350 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001351 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1352 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
1355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_SSL_CLI_C */
1357#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001360 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001361 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Hanno Becker81c7b182017-11-09 18:39:33 +00001363 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001364 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001366 /*
1367 * This is not used in TLS v1.1.
1368 */
Paul Bakker48916f92012-09-16 19:57:18 +00001369 iv_copy_len = ( transform->fixed_ivlen ) ?
1370 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001371 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1372 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001373 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001375 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001379 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1380 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Hanno Beckerd56ed242018-01-03 15:32:51 +00001383#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_SSL_PROTO_SSL3)
1385 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001386 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001387 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001390 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1391 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001392 }
1393
Hanno Becker81c7b182017-11-09 18:39:33 +00001394 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1395 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001396 }
1397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1399#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1400 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1401 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001402 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001403 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1404 For AEAD-based ciphersuites, there is nothing to do here. */
1405 if( mac_key_len != 0 )
1406 {
1407 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1408 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1409 }
Paul Bakker68884e32013-01-07 18:20:04 +01001410 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001411 else
1412#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001415 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1416 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001417 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001418#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1421 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001422 {
1423 int ret = 0;
1424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001426
Hanno Becker88aaf652017-12-27 08:17:40 +00001427 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001428 transform->iv_enc, transform->iv_dec,
1429 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001430 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001431 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001434 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1435 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001436 }
1437 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001438#else
1439 ((void) mac_dec);
1440 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001442
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001443#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1444 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001445 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001446 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1447 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001448 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001449 iv_copy_len );
1450 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001451
1452 if( ssl->conf->f_export_keys_ext != NULL )
1453 {
1454 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1455 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001456 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001457 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001458 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001459 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001460 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001461 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001462#endif
1463
Hanno Beckerf704bef2018-11-16 15:21:18 +00001464#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001465
1466 /* Only use PSA-based ciphers for TLS-1.2.
1467 * That's relevant at least for TLS-1.0, where
1468 * we assume that mbedtls_cipher_crypt() updates
1469 * the structure field for the IV, which the PSA-based
1470 * implementation currently doesn't. */
1471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1472 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001473 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001474 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001475 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001476 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1477 {
1478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001479 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 }
1481
1482 if( ret == 0 )
1483 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001485 psa_fallthrough = 0;
1486 }
1487 else
1488 {
1489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1490 psa_fallthrough = 1;
1491 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001492 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001493 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001494 psa_fallthrough = 1;
1495#else
1496 psa_fallthrough = 1;
1497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001500#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001501 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001502 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001505 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001506 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001507
Hanno Beckerf704bef2018-11-16 15:21:18 +00001508#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001509 /* Only use PSA-based ciphers for TLS-1.2.
1510 * That's relevant at least for TLS-1.0, where
1511 * we assume that mbedtls_cipher_crypt() updates
1512 * the structure field for the IV, which the PSA-based
1513 * implementation currently doesn't. */
1514#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1515 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001516 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001517 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001518 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001519 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1520 {
1521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001522 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001523 }
1524
1525 if( ret == 0 )
1526 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001528 psa_fallthrough = 0;
1529 }
1530 else
1531 {
1532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1533 psa_fallthrough = 1;
1534 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001535 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001536 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001537 psa_fallthrough = 1;
1538#else
1539 psa_fallthrough = 1;
1540#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001541
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001543#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001548 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001556 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001564 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001574 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001581 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001588 // Initialize compression
1589 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Paul Bakker16770332013-10-11 09:59:44 +02001592 if( ssl->compress_buf == NULL )
1593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001595 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001596 if( ssl->compress_buf == NULL )
1597 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001599 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001600 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1601 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001602 }
1603 }
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker48916f92012-09-16 19:57:18 +00001607 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1608 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001610 if( deflateInit( &transform->ctx_deflate,
1611 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001612 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001615 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1616 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001617 }
1618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001622end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001623 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1624 mbedtls_platform_zeroize( handshake->randbytes,
1625 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001626 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627}
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_SSL_PROTO_SSL3)
1630void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001631{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001632 mbedtls_md5_context md5;
1633 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 unsigned char pad_1[48];
1635 unsigned char pad_2[48];
1636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001639 mbedtls_md5_init( &md5 );
1640 mbedtls_sha1_init( &sha1 );
1641
1642 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1643 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Paul Bakker380da532012-04-18 16:10:25 +00001645 memset( pad_1, 0x36, 48 );
1646 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001648 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1649 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1650 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001652 mbedtls_md5_starts_ret( &md5 );
1653 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1654 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1655 mbedtls_md5_update_ret( &md5, hash, 16 );
1656 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001658 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1659 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1660 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001661
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001662 mbedtls_sha1_starts_ret( &sha1 );
1663 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1664 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1665 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1666 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001670
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001671 mbedtls_md5_free( &md5 );
1672 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001673
Paul Bakker380da532012-04-18 16:10:25 +00001674 return;
1675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1679void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001680{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001681 mbedtls_md5_context md5;
1682 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001685
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001686 mbedtls_md5_init( &md5 );
1687 mbedtls_sha1_init( &sha1 );
1688
1689 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1690 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001691
Andrzej Kurekeb342242019-01-29 09:14:33 -05001692 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001693 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001697
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001698 mbedtls_md5_free( &md5 );
1699 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001700
Paul Bakker380da532012-04-18 16:10:25 +00001701 return;
1702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1706#if defined(MBEDTLS_SHA256_C)
1707void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001708{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001709#if defined(MBEDTLS_USE_PSA_CRYPTO)
1710 size_t hash_size;
1711 psa_status_t status;
1712 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1713
1714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1715 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1716 if( status != PSA_SUCCESS )
1717 {
1718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1719 return;
1720 }
1721
1722 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1723 if( status != PSA_SUCCESS )
1724 {
1725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1726 return;
1727 }
1728 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1730#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001731 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001732
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001733 mbedtls_sha256_init( &sha256 );
1734
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001736
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001737 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001738 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001742
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001743 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001745 return;
1746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#if defined(MBEDTLS_SHA512_C)
1750void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001751{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001752#if defined(MBEDTLS_USE_PSA_CRYPTO)
1753 size_t hash_size;
1754 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001755 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001756
1757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001758 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001759 if( status != PSA_SUCCESS )
1760 {
1761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1762 return;
1763 }
1764
Andrzej Kurek972fba52019-01-30 03:29:12 -05001765 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001766 if( status != PSA_SUCCESS )
1767 {
1768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1769 return;
1770 }
1771 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1773#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001774 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001775
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001776 mbedtls_sha512_init( &sha512 );
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001779
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001780 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001781 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001786 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001787#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 return;
1789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#endif /* MBEDTLS_SHA512_C */
1791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1794int 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 +02001795{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001796 unsigned char *p = ssl->handshake->premaster;
1797 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001798 const unsigned char *psk = ssl->conf->psk;
1799 size_t psk_len = ssl->conf->psk_len;
1800
1801 /* If the psk callback was called, use its result */
1802 if( ssl->handshake->psk != NULL )
1803 {
1804 psk = ssl->handshake->psk;
1805 psk_len = ssl->handshake->psk_len;
1806 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001807
1808 /*
1809 * PMS = struct {
1810 * opaque other_secret<0..2^16-1>;
1811 * opaque psk<0..2^16-1>;
1812 * };
1813 * with "other_secret" depending on the particular key exchange
1814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1816 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001817 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001818 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001820
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001821 *(p++) = (unsigned char)( psk_len >> 8 );
1822 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001823
1824 if( end < p || (size_t)( end - p ) < psk_len )
1825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1826
1827 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001828 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001829 }
1830 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1832#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1833 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001834 {
1835 /*
1836 * other_secret already set by the ClientKeyExchange message,
1837 * and is 48 bytes long
1838 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001839 if( end - p < 2 )
1840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1841
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001842 *p++ = 0;
1843 *p++ = 48;
1844 p += 48;
1845 }
1846 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1848#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1849 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001850 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001851 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001852 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001854 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001856 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001857 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860 return( ret );
1861 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001862 *(p++) = (unsigned char)( len >> 8 );
1863 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001864 p += len;
1865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001867 }
1868 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1870#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1871 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001873 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001874 size_t zlen;
1875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001877 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001878 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001881 return( ret );
1882 }
1883
1884 *(p++) = (unsigned char)( zlen >> 8 );
1885 *(p++) = (unsigned char)( zlen );
1886 p += zlen;
1887
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001888 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1889 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001890 }
1891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001896 }
1897
1898 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001899 if( end - p < 2 )
1900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001901
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001902 *(p++) = (unsigned char)( psk_len >> 8 );
1903 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001904
1905 if( end < p || (size_t)( end - p ) < psk_len )
1906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1907
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001908 memcpy( p, psk, psk_len );
1909 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001910
1911 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1912
1913 return( 0 );
1914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001918/*
1919 * SSLv3.0 MAC functions
1920 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001921#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001922static void ssl_mac( mbedtls_md_context_t *md_ctx,
1923 const unsigned char *secret,
1924 const unsigned char *buf, size_t len,
1925 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001926 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001927{
1928 unsigned char header[11];
1929 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001930 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1932 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001933
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001934 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001936 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001937 else
Paul Bakker68884e32013-01-07 18:20:04 +01001938 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
1940 memcpy( header, ctr, 8 );
1941 header[ 8] = (unsigned char) type;
1942 header[ 9] = (unsigned char)( len >> 8 );
1943 header[10] = (unsigned char)( len );
1944
Paul Bakker68884e32013-01-07 18:20:04 +01001945 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 mbedtls_md_starts( md_ctx );
1947 mbedtls_md_update( md_ctx, secret, md_size );
1948 mbedtls_md_update( md_ctx, padding, padlen );
1949 mbedtls_md_update( md_ctx, header, 11 );
1950 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001951 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
Paul Bakker68884e32013-01-07 18:20:04 +01001953 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 mbedtls_md_starts( md_ctx );
1955 mbedtls_md_update( md_ctx, secret, md_size );
1956 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001957 mbedtls_md_update( md_ctx, out, md_size );
1958 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001959}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001961
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001962/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001963 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001964#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001965 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1966 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1967 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1968/* This function makes sure every byte in the memory region is accessed
1969 * (in ascending addresses order) */
1970static void ssl_read_memory( unsigned char *p, size_t len )
1971{
1972 unsigned char acc = 0;
1973 volatile unsigned char force;
1974
1975 for( ; len != 0; p++, len-- )
1976 acc ^= *p;
1977
1978 force = acc;
1979 (void) force;
1980}
1981#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1982
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001983/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001985 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001986
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001987#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001988/* This functions transforms a DTLS plaintext fragment and a record content
1989 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001990 *
1991 * struct {
1992 * opaque content[DTLSPlaintext.length];
1993 * ContentType real_type;
1994 * uint8 zeros[length_of_padding];
1995 * } DTLSInnerPlaintext;
1996 *
1997 * Input:
1998 * - `content`: The beginning of the buffer holding the
1999 * plaintext to be wrapped.
2000 * - `*content_size`: The length of the plaintext in Bytes.
2001 * - `max_len`: The number of Bytes available starting from
2002 * `content`. This must be `>= *content_size`.
2003 * - `rec_type`: The desired record content type.
2004 *
2005 * Output:
2006 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2007 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2008 *
2009 * Returns:
2010 * - `0` on success.
2011 * - A negative error code if `max_len` didn't offer enough space
2012 * for the expansion.
2013 */
2014static int ssl_cid_build_inner_plaintext( unsigned char *content,
2015 size_t *content_size,
2016 size_t remaining,
2017 uint8_t rec_type )
2018{
2019 size_t len = *content_size;
2020 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2021
2022 /* Write real content type */
2023 if( remaining == 0 )
2024 return( -1 );
2025 content[ len ] = rec_type;
2026 len++;
2027 remaining--;
2028
2029 if( remaining < pad )
2030 return( -1 );
2031 memset( content + len, 0, pad );
2032 len += pad;
2033 remaining -= pad;
2034
2035 *content_size = len;
2036 return( 0 );
2037}
2038
Hanno Becker07dc97d2019-05-20 15:08:01 +01002039/* This function parses a DTLSInnerPlaintext structure.
2040 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002041static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2042 size_t *content_size,
2043 uint8_t *rec_type )
2044{
2045 size_t remaining = *content_size;
2046
2047 /* Determine length of padding by skipping zeroes from the back. */
2048 do
2049 {
2050 if( remaining == 0 )
2051 return( -1 );
2052 remaining--;
2053 } while( content[ remaining ] == 0 );
2054
2055 *content_size = remaining;
2056 *rec_type = content[ remaining ];
2057
2058 return( 0 );
2059}
2060#endif /* MBEDTLS_SSL_CID */
2061
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002062/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002063 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002064static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002065 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002066 mbedtls_record *rec )
2067{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002068 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002069 *
2070 * additional_data = seq_num + TLSCompressed.type +
2071 * TLSCompressed.version + TLSCompressed.length;
2072 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002073 * For the CID extension, this is extended as follows
2074 * (quoting draft-ietf-tls-dtls-connection-id-05,
2075 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002076 *
2077 * additional_data = seq_num + DTLSPlaintext.type +
2078 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002079 * cid +
2080 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002081 * length_of_DTLSInnerPlaintext;
2082 */
2083
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002084 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2085 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002086 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002087
2088#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002089 if( rec->cid_len != 0 )
2090 {
2091 memcpy( add_data + 11, rec->cid, rec->cid_len );
2092 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2093 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2094 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2095 *add_data_len = 13 + 1 + rec->cid_len;
2096 }
2097 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002098#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002099 {
2100 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2101 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2102 *add_data_len = 13;
2103 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002104}
2105
Hanno Beckera18d1322018-01-03 14:27:32 +00002106int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2107 mbedtls_ssl_transform *transform,
2108 mbedtls_record *rec,
2109 int (*f_rng)(void *, unsigned char *, size_t),
2110 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002113 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002114 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002115 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002116 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002117 size_t post_avail;
2118
2119 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002120#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002121 ((void) ssl);
2122#endif
2123
2124 /* The PRNG is used for dynamic IV generation that's used
2125 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2126#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2127 ( defined(MBEDTLS_AES_C) || \
2128 defined(MBEDTLS_ARIA_C) || \
2129 defined(MBEDTLS_CAMELLIA_C) ) && \
2130 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2131 ((void) f_rng);
2132 ((void) p_rng);
2133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002137 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002138 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2141 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002142 if( rec == NULL
2143 || rec->buf == NULL
2144 || rec->buf_len < rec->data_offset
2145 || rec->buf_len - rec->data_offset < rec->data_len
2146#if defined(MBEDTLS_SSL_CID)
2147 || rec->cid_len != 0
2148#endif
2149 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002150 {
2151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002153 }
2154
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002155 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002156 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158 data, rec->data_len );
2159
2160 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2161
2162 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2163 {
2164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2165 (unsigned) rec->data_len,
2166 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2168 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002169
Hanno Beckercab87e62019-04-29 13:52:53 +01002170#if defined(MBEDTLS_SSL_CID)
2171 /*
2172 * Add CID information
2173 */
2174 rec->cid_len = transform->out_cid_len;
2175 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2176 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002177
2178 if( rec->cid_len != 0 )
2179 {
2180 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002181 * Wrap plaintext into DTLSInnerPlaintext structure.
2182 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002183 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002184 * Note that this changes `rec->data_len`, and hence
2185 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002186 */
2187 if( ssl_cid_build_inner_plaintext( data,
2188 &rec->data_len,
2189 post_avail,
2190 rec->type ) != 0 )
2191 {
2192 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2193 }
2194
2195 rec->type = MBEDTLS_SSL_MSG_CID;
2196 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002197#endif /* MBEDTLS_SSL_CID */
2198
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002199 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2200
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002202 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 */
Hanno Becker52344c22018-01-03 15:24:20 +00002204#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 if( mode == MBEDTLS_MODE_STREAM ||
2206 ( mode == MBEDTLS_MODE_CBC
2207#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002208 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002209#endif
2210 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002212 if( post_avail < transform->maclen )
2213 {
2214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2215 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2216 }
2217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002220 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002221 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002222 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2223 data, rec->data_len, rec->ctr, rec->type, mac );
2224 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002225 }
2226 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2229 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002231 {
Hanno Becker992b6872017-11-09 18:57:39 +00002232 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2233
Hanno Beckercab87e62019-04-29 13:52:53 +01002234 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002235
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002236 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002237 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002238 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2239 data, rec->data_len );
2240 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2241 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2242
2243 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002244 }
2245 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002246#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002250 }
2251
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002252 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2253 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002254
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002255 rec->data_len += transform->maclen;
2256 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002257 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002258 }
Hanno Becker52344c22018-01-03 15:24:20 +00002259#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002261 /*
2262 * Encrypt
2263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2265 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002267 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002268 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002270 "including %d bytes of padding",
2271 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2274 transform->iv_enc, transform->ivlen,
2275 data, rec->data_len,
2276 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002279 return( ret );
2280 }
2281
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002282 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
Paul Bakker68884e32013-01-07 18:20:04 +01002288 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002290
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002291#if defined(MBEDTLS_GCM_C) || \
2292 defined(MBEDTLS_CCM_C) || \
2293 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002295 mode == MBEDTLS_MODE_CCM ||
2296 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002297 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002298 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002299 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002300 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002301
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002302 /* Check that there's space for both the authentication tag
2303 * and the explicit IV before and after the record content. */
2304 if( post_avail < transform->taglen ||
2305 rec->data_offset < explicit_iv_len )
2306 {
2307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2308 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2309 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002310
Paul Bakker68884e32013-01-07 18:20:04 +01002311 /*
2312 * Generate IV
2313 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002314 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2315 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002316 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002317 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002318 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2319 explicit_iv_len );
2320 /* Prefix record content with explicit IV. */
2321 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002322 }
2323 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2324 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002325 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002326 unsigned char i;
2327
2328 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2329
2330 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002331 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002332 }
2333 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002334 {
2335 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002338 }
2339
Hanno Beckercab87e62019-04-29 13:52:53 +01002340 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002341
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002342 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2343 iv, transform->ivlen );
2344 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 data - explicit_iv_len, explicit_iv_len );
2346 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002347 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002349 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002350 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002351
Paul Bakker68884e32013-01-07 18:20:04 +01002352 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002353 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002354 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002358 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 data, rec->data_len, /* source */
2360 data, &rec->data_len, /* destination */
2361 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002364 return( ret );
2365 }
2366
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002367 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2368 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002369
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002370 rec->data_len += transform->taglen + explicit_iv_len;
2371 rec->data_offset -= explicit_iv_len;
2372 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002373 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002374 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2377#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002378 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002381 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002382 size_t padlen, i;
2383 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002384
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002385 /* Currently we're always using minimal padding
2386 * (up to 255 bytes would be allowed). */
2387 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2388 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 padlen = 0;
2390
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 /* Check there's enough space in the buffer for the padding. */
2392 if( post_avail < padlen + 1 )
2393 {
2394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2395 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2396 }
2397
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002401 rec->data_len += padlen + 1;
2402 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002405 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002406 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2407 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002408 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002409 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002410 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002411 if( f_rng == NULL )
2412 {
2413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2415 }
2416
2417 if( rec->data_offset < transform->ivlen )
2418 {
2419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2420 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2421 }
2422
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423 /*
2424 * Generate IV
2425 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002426 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002427 if( ret != 0 )
2428 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 memcpy( data - transform->ivlen, transform->iv_enc,
2431 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002432
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002433 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002437 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002439 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002441 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2442 transform->iv_enc,
2443 transform->ivlen,
2444 data, rec->data_len,
2445 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002448 return( ret );
2449 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002450
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002455 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002458 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002459 {
2460 /*
2461 * Save IV in SSL3 and TLS1
2462 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002463 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2464 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002466 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002467#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002468 {
2469 data -= transform->ivlen;
2470 rec->data_offset -= transform->ivlen;
2471 rec->data_len += transform->ivlen;
2472 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002476 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002477 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2478
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002479 /*
2480 * MAC(MAC_write_key, seq_num +
2481 * TLSCipherText.type +
2482 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002483 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002484 * IV + // except for TLS 1.0
2485 * ENC(content + padding + padding_length));
2486 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002487
2488 if( post_avail < transform->maclen)
2489 {
2490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2491 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2492 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493
Hanno Beckercab87e62019-04-29 13:52:53 +01002494 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002497 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002498 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002499
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002500 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002501 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002502 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2503 data, rec->data_len );
2504 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2505 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002506
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002507 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002508
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 rec->data_len += transform->maclen;
2510 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002511 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002515 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002517 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002523 /* Make extra sure authentication was performed, exactly once */
2524 if( auth_done != 1 )
2525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002528 }
2529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
2532 return( 0 );
2533}
2534
Hanno Beckera18d1322018-01-03 14:27:32 +00002535int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2536 mbedtls_ssl_transform *transform,
2537 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002539 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002541 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002542#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002543 size_t padlen = 0, correct = 1;
2544#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002545 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002546 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002547 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002548
Hanno Beckera18d1322018-01-03 14:27:32 +00002549#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550 ((void) ssl);
2551#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002554 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002555 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2558 }
2559 if( rec == NULL ||
2560 rec->buf == NULL ||
2561 rec->buf_len < rec->data_offset ||
2562 rec->buf_len - rec->data_offset < rec->data_len )
2563 {
2564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002566 }
2567
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002568 data = rec->buf + rec->data_offset;
2569 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Hanno Beckercab87e62019-04-29 13:52:53 +01002571#if defined(MBEDTLS_SSL_CID)
2572 /*
2573 * Match record's CID with incoming CID.
2574 */
2575
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002576 /* Uncomment this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002577 /* if( rec->cid_len != transform->in_cid_len || */
2578 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2579 /* { */
2580 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2581 /* } */
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002582
2583 /* Remove this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002584 rec->cid_len = transform->in_cid_len;
2585 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2586 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2587#endif /* MBEDTLS_SSL_CID */
2588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2590 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002591 {
2592 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2594 transform->iv_dec,
2595 transform->ivlen,
2596 data, rec->data_len,
2597 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002600 return( ret );
2601 }
2602
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002603 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
Paul Bakker68884e32013-01-07 18:20:04 +01002609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002611#if defined(MBEDTLS_GCM_C) || \
2612 defined(MBEDTLS_CCM_C) || \
2613 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002615 mode == MBEDTLS_MODE_CCM ||
2616 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002617 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002618 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002620
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 /*
2622 * Compute and update sizes
2623 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002624 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002627 "+ taglen (%d)", rec->data_len,
2628 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002630 }
Paul Bakker68884e32013-01-07 18:20:04 +01002631
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 /*
2633 * Prepare IV
2634 */
2635 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2636 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002637 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002638 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002639 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002640
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002641 }
2642 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2643 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002644 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002645 unsigned char i;
2646
2647 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2648
2649 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002650 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002651 }
2652 else
2653 {
2654 /* Reminder if we ever add an AEAD mode with a different size */
2655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2657 }
2658
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 data += explicit_iv_len;
2660 rec->data_offset += explicit_iv_len;
2661 rec->data_len -= explicit_iv_len + transform->taglen;
2662
Hanno Beckercab87e62019-04-29 13:52:53 +01002663 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002665 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666
2667 memcpy( transform->iv_dec + transform->fixed_ivlen,
2668 data - explicit_iv_len, explicit_iv_len );
2669
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002670 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002672 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002673
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002674
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002675 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002676 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002677 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2679 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002680 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002681 data, rec->data_len,
2682 data, &olen,
2683 data + rec->data_len,
2684 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2689 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002690
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002691 return( ret );
2692 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002693 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002694
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002695 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002699 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2703#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002704 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002707 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002708
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 /*
Paul Bakker45829992013-01-03 14:52:21 +01002710 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002713 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2714 {
2715 /* The ciphertext is prefixed with the CBC IV. */
2716 minlen += transform->ivlen;
2717 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002718#endif
Paul Bakker45829992013-01-03 14:52:21 +01002719
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002720 /* Size considerations:
2721 *
2722 * - The CBC cipher text must not be empty and hence
2723 * at least of size transform->ivlen.
2724 *
2725 * Together with the potential IV-prefix, this explains
2726 * the first of the two checks below.
2727 *
2728 * - The record must contain a MAC, either in plain or
2729 * encrypted, depending on whether Encrypt-then-MAC
2730 * is used or not.
2731 * - If it is, the message contains the IV-prefix,
2732 * the CBC ciphertext, and the MAC.
2733 * - If it is not, the padded plaintext, and hence
2734 * the CBC ciphertext, has at least length maclen + 1
2735 * because there is at least the padding length byte.
2736 *
2737 * As the CBC ciphertext is not empty, both cases give the
2738 * lower bound minlen + maclen + 1 on the record size, which
2739 * we test for in the second check below.
2740 */
2741 if( rec->data_len < minlen + transform->ivlen ||
2742 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002745 "+ 1 ) ( + expl IV )", rec->data_len,
2746 transform->ivlen,
2747 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002749 }
2750
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002751 /*
2752 * Authenticate before decrypt if enabled
2753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002755 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002756 {
Hanno Becker992b6872017-11-09 18:57:39 +00002757 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002760
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2762 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002763
Hanno Beckercab87e62019-04-29 13:52:53 +01002764 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002765
Hanno Beckercab87e62019-04-29 13:52:53 +01002766 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2767 add_data_len );
2768 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2769 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002770 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2771 data, rec->data_len );
2772 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2773 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002774
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2776 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002777 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002778 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002779
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002780 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2781 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002786 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002789
2790 /*
2791 * Check length sanity
2792 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002798 }
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002801 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002802 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002804 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 /* This is safe because data_len >= minlen + maclen + 1 initially,
2807 * and at this point we have at most subtracted maclen (note that
2808 * minlen == transform->ivlen here). */
2809 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002810
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 data += transform->ivlen;
2812 rec->data_offset += transform->ivlen;
2813 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002816
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002817 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2818 transform->iv_dec, transform->ivlen,
2819 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002822 return( ret );
2823 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002824
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002829 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002833 {
2834 /*
2835 * Save IV in SSL3 and TLS1
2836 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002837 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2838 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002840#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002842 /* Safe since data_len >= minlen + maclen + 1, so after having
2843 * subtracted at most minlen and maclen up to this point,
2844 * data_len > 0. */
2845 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002846
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 if( auth_done == 1 )
2848 {
2849 correct *= ( rec->data_len >= padlen + 1 );
2850 padlen *= ( rec->data_len >= padlen + 1 );
2851 }
2852 else
Paul Bakker45829992013-01-03 14:52:21 +01002853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002855 if( rec->data_len < transform->maclen + padlen + 1 )
2856 {
2857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2858 rec->data_len,
2859 transform->maclen,
2860 padlen + 1 ) );
2861 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002862#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863
2864 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2865 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002866 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002868 padlen++;
2869
2870 /* Regardless of the validity of the padding,
2871 * we have data_len >= padlen here. */
2872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002876 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_DEBUG_ALL)
2879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002880 "should be no more than %d",
2881 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002882#endif
Paul Bakker45829992013-01-03 14:52:21 +01002883 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 }
2885 }
2886 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2888#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2889 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002892 /* The padding check involves a series of up to 256
2893 * consecutive memory reads at the end of the record
2894 * plaintext buffer. In order to hide the length and
2895 * validity of the padding, always perform exactly
2896 * `min(256,plaintext_len)` reads (but take into account
2897 * only the last `padlen` bytes for the padding check). */
2898 size_t pad_count = 0;
2899 size_t real_count = 0;
2900 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002901
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002902 /* Index of first padding byte; it has been ensured above
2903 * that the subtraction is safe. */
2904 size_t const padding_idx = rec->data_len - padlen;
2905 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2906 size_t const start_idx = rec->data_len - num_checks;
2907 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002908
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002910 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002911 real_count |= ( idx >= padding_idx );
2912 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002913 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002914 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002917 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002919#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002920 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002921 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002922 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2924 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002928 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002929
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002930 /* If the padding was found to be invalid, padlen == 0
2931 * and the subtraction is safe. If the padding was found valid,
2932 * padlen hasn't been changed and the previous assertion
2933 * data_len >= padlen still holds. */
2934 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002938 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002942 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002944#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002946 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
2949 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002950 * Authenticate if not done yet.
2951 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 */
Hanno Becker52344c22018-01-03 15:24:20 +00002953#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002954 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002955 {
Hanno Becker992b6872017-11-09 18:57:39 +00002956 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002957
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002958 /* If the initial value of padlen was such that
2959 * data_len < maclen + padlen + 1, then padlen
2960 * got reset to 1, and the initial check
2961 * data_len >= minlen + maclen + 1
2962 * guarantees that at this point we still
2963 * have at least data_len >= maclen.
2964 *
2965 * If the initial value of padlen was such that
2966 * data_len >= maclen + padlen + 1, then we have
2967 * subtracted either padlen + 1 (if the padding was correct)
2968 * or 0 (if the padding was incorrect) since then,
2969 * hence data_len >= maclen in any case.
2970 */
2971 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Hanno Beckercab87e62019-04-29 13:52:53 +01002973 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002976 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002977 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002978 ssl_mac( &transform->md_ctx_dec,
2979 transform->mac_dec,
2980 data, rec->data_len,
2981 rec->ctr, rec->type,
2982 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002983 }
2984 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2986#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2987 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002988 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002989 {
2990 /*
2991 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002992 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002993 *
2994 * Known timing attacks:
2995 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2996 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002997 * To compensate for different timings for the MAC calculation
2998 * depending on how much padding was removed (which is determined
2999 * by padlen), process extra_run more blocks through the hash
3000 * function.
3001 *
3002 * The formula in the paper is
3003 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3004 * where L1 is the size of the header plus the decrypted message
3005 * plus CBC padding and L2 is the size of the header plus the
3006 * decrypted message. This is for an underlying hash function
3007 * with 64-byte blocks.
3008 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3009 * correctly. We round down instead of up, so -56 is the correct
3010 * value for our calculations instead of -55.
3011 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003012 * Repeat the formula rather than defining a block_size variable.
3013 * This avoids requiring division by a variable at runtime
3014 * (which would be marginally less efficient and would require
3015 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003016 */
3017 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003019
3020 /*
3021 * The next two sizes are the minimum and maximum values of
3022 * in_msglen over all padlen values.
3023 *
3024 * They're independent of padlen, since we previously did
3025 * in_msglen -= padlen.
3026 *
3027 * Note that max_len + maclen is never more than the buffer
3028 * length, as we previously did in_msglen -= maclen too.
3029 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003030 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003031 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3032
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003033 memset( tmp, 0, sizeof( tmp ) );
3034
3035 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003036 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003037#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3038 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003039 case MBEDTLS_MD_MD5:
3040 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003041 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003042 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003043 extra_run =
3044 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3045 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003046 break;
3047#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003048#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003049 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003050 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003051 extra_run =
3052 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3053 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003054 break;
3055#endif
3056 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3059 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003060
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003061 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003062
Hanno Beckercab87e62019-04-29 13:52:53 +01003063 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3064 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3066 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003067 /* Make sure we access everything even when padlen > 0. This
3068 * makes the synchronisation requirements for just-in-time
3069 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 ssl_read_memory( data + rec->data_len, padlen );
3071 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003072
3073 /* Call mbedtls_md_process at least once due to cache attacks
3074 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003075 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003076 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003077
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003078 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003079
3080 /* Make sure we access all the memory that could contain the MAC,
3081 * before we check it in the next code block. This makes the
3082 * synchronisation requirements for just-in-time Prime+Probe
3083 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 ssl_read_memory( data + min_len,
3085 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003086 }
3087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3089 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003093 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003095#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003096 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3097 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003099
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003100 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3101 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#if defined(MBEDTLS_SSL_DEBUG_ALL)
3104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003105#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003106 correct = 0;
3107 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003108 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003109 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003110
3111 /*
3112 * Finally check the correct flag
3113 */
3114 if( correct == 0 )
3115 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003116#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003117
3118 /* Make extra sure authentication was performed, exactly once */
3119 if( auth_done != 1 )
3120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003125#if defined(MBEDTLS_SSL_CID)
3126 if( rec->cid_len != 0 )
3127 {
3128 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3129 &rec->type );
3130 if( ret != 0 )
3131 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3132 }
3133#endif /* MBEDTLS_SSL_CID */
3134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
3137 return( 0 );
3138}
3139
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003140#undef MAC_NONE
3141#undef MAC_PLAINTEXT
3142#undef MAC_CIPHERTEXT
3143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003145/*
3146 * Compression/decompression functions
3147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003149{
3150 int ret;
3151 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003152 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003153 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003154 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003157
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003158 if( len_pre == 0 )
3159 return( 0 );
3160
Paul Bakker2770fbd2012-07-03 13:30:23 +00003161 memcpy( msg_pre, ssl->out_msg, len_pre );
3162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003164 ssl->out_msglen ) );
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167 ssl->out_msg, ssl->out_msglen );
3168
Paul Bakker48916f92012-09-16 19:57:18 +00003169 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3170 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3171 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003172 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173
Paul Bakker48916f92012-09-16 19:57:18 +00003174 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175 if( ret != Z_OK )
3176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3178 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179 }
3180
Angus Grattond8213d02016-05-25 20:56:48 +10003181 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003182 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185 ssl->out_msglen ) );
3186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003188 ssl->out_msg, ssl->out_msglen );
3189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191
3192 return( 0 );
3193}
3194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003196{
3197 int ret;
3198 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003199 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003200 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003201 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003204
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003205 if( len_pre == 0 )
3206 return( 0 );
3207
Paul Bakker2770fbd2012-07-03 13:30:23 +00003208 memcpy( msg_pre, ssl->in_msg, len_pre );
3209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003211 ssl->in_msglen ) );
3212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214 ssl->in_msg, ssl->in_msglen );
3215
Paul Bakker48916f92012-09-16 19:57:18 +00003216 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3217 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3218 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003219 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003220 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221
Paul Bakker48916f92012-09-16 19:57:18 +00003222 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003223 if( ret != Z_OK )
3224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3226 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003227 }
3228
Angus Grattond8213d02016-05-25 20:56:48 +10003229 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003230 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003233 ssl->in_msglen ) );
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003236 ssl->in_msg, ssl->in_msglen );
3237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003239
3240 return( 0 );
3241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3245static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247#if defined(MBEDTLS_SSL_PROTO_DTLS)
3248static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003249{
3250 /* If renegotiation is not enforced, retransmit until we would reach max
3251 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003252 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003253 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003254 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003255 unsigned char doublings = 1;
3256
3257 while( ratio != 0 )
3258 {
3259 ++doublings;
3260 ratio >>= 1;
3261 }
3262
3263 if( ++ssl->renego_records_seen > doublings )
3264 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003266 return( 0 );
3267 }
3268 }
3269
3270 return( ssl_write_hello_request( ssl ) );
3271}
3272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003274
Paul Bakker5121ce52009-01-03 21:22:43 +00003275/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003276 * Fill the input message buffer by appending data to it.
3277 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003278 *
3279 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3280 * available (from this read and/or a previous one). Otherwise, an error code
3281 * is returned (possibly EOF or WANT_READ).
3282 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003283 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3284 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3285 * since we always read a whole datagram at once.
3286 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003287 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003288 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003291{
Paul Bakker23986e52011-04-24 08:57:21 +00003292 int ret;
3293 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003297 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003300 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003302 }
3303
Angus Grattond8213d02016-05-25 20:56:48 +10003304 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003308 }
3309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003313 uint32_t timeout;
3314
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003315 /* Just to be sure */
3316 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3317 {
3318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3319 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3321 }
3322
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003323 /*
3324 * The point is, we need to always read a full datagram at once, so we
3325 * sometimes read more then requested, and handle the additional data.
3326 * It could be the rest of the current record (while fetching the
3327 * header) and/or some other records in the same datagram.
3328 */
3329
3330 /*
3331 * Move to the next record in the already read datagram if applicable
3332 */
3333 if( ssl->next_record_offset != 0 )
3334 {
3335 if( ssl->in_left < ssl->next_record_offset )
3336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003339 }
3340
3341 ssl->in_left -= ssl->next_record_offset;
3342
3343 if( ssl->in_left != 0 )
3344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003346 ssl->next_record_offset ) );
3347 memmove( ssl->in_hdr,
3348 ssl->in_hdr + ssl->next_record_offset,
3349 ssl->in_left );
3350 }
3351
3352 ssl->next_record_offset = 0;
3353 }
3354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003357
3358 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003359 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003360 */
3361 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003364 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003365 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003366
3367 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003368 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003369 * are not at the beginning of a new record, the caller did something
3370 * wrong.
3371 */
3372 if( ssl->in_left != 0 )
3373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3375 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003376 }
3377
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003378 /*
3379 * Don't even try to read if time's out already.
3380 * This avoids by-passing the timer when repeatedly receiving messages
3381 * that will end up being dropped.
3382 */
3383 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003384 {
3385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003386 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003387 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003388 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003389 {
Angus Grattond8213d02016-05-25 20:56:48 +10003390 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003393 timeout = ssl->handshake->retransmit_timeout;
3394 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003395 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003399 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3401 timeout );
3402 else
3403 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003406
3407 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003409 }
3410
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003411 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003414 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003417 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003418 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003421 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003422 }
3423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003427 return( ret );
3428 }
3429
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003430 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003433 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003435 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003436 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003439 return( ret );
3440 }
3441
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003442 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003445 }
3446
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 if( ret < 0 )
3448 return( ret );
3449
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003450 ssl->in_left = ret;
3451 }
3452 else
3453#endif
3454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003456 ssl->in_left, nb_want ) );
3457
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003458 while( ssl->in_left < nb_want )
3459 {
3460 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003461
3462 if( ssl_check_timer( ssl ) != 0 )
3463 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3464 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003465 {
3466 if( ssl->f_recv_timeout != NULL )
3467 {
3468 ret = ssl->f_recv_timeout( ssl->p_bio,
3469 ssl->in_hdr + ssl->in_left, len,
3470 ssl->conf->read_timeout );
3471 }
3472 else
3473 {
3474 ret = ssl->f_recv( ssl->p_bio,
3475 ssl->in_hdr + ssl->in_left, len );
3476 }
3477 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003480 ssl->in_left, nb_want ) );
3481 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003482
3483 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003485
3486 if( ret < 0 )
3487 return( ret );
3488
mohammad160352aecb92018-03-28 23:41:40 -07003489 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003490 {
Darryl Green11999bb2018-03-13 15:22:58 +00003491 MBEDTLS_SSL_DEBUG_MSG( 1,
3492 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003493 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3495 }
3496
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003497 ssl->in_left += ret;
3498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 }
3500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
3503 return( 0 );
3504}
3505
3506/*
3507 * Flush any data not yet written
3508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003510{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003511 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003512 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003516 if( ssl->f_send == NULL )
3517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003519 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003521 }
3522
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003523 /* Avoid incrementing counter if data is flushed */
3524 if( ssl->out_left == 0 )
3525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003527 return( 0 );
3528 }
3529
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 while( ssl->out_left > 0 )
3531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003533 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Hanno Becker2b1e3542018-08-06 11:19:13 +01003535 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003536 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
3540 if( ret <= 0 )
3541 return( ret );
3542
mohammad160352aecb92018-03-28 23:41:40 -07003543 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003544 {
Darryl Green11999bb2018-03-13 15:22:58 +00003545 MBEDTLS_SSL_DEBUG_MSG( 1,
3546 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003547 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3549 }
3550
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 ssl->out_left -= ret;
3552 }
3553
Hanno Becker2b1e3542018-08-06 11:19:13 +01003554#if defined(MBEDTLS_SSL_PROTO_DTLS)
3555 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003556 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003557 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003558 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003559 else
3560#endif
3561 {
3562 ssl->out_hdr = ssl->out_buf + 8;
3563 }
3564 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003567
3568 return( 0 );
3569}
3570
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003571/*
3572 * Functions to handle the DTLS retransmission state machine
3573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575/*
3576 * Append current handshake message to current outgoing flight
3577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3582 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3583 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003584
3585 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003586 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003590 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003591 }
3592
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003593 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003594 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003597 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598 }
3599
3600 /* Copy current handshake message with headers */
3601 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3602 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003603 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604 msg->next = NULL;
3605
3606 /* Append to the current flight */
3607 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003608 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003609 else
3610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 while( cur->next != NULL )
3613 cur = cur->next;
3614 cur->next = msg;
3615 }
3616
Hanno Becker3b235902018-08-06 09:54:53 +01003617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618 return( 0 );
3619}
3620
3621/*
3622 * Free the current flight of handshake messages
3623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 mbedtls_ssl_flight_item *cur = flight;
3627 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003628
3629 while( cur != NULL )
3630 {
3631 next = cur->next;
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 mbedtls_free( cur->p );
3634 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003635
3636 cur = next;
3637 }
3638}
3639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3641static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003642#endif
3643
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003644/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003645 * Swap transform_out and out_ctr with the alternative ones
3646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650 unsigned char tmp_out_ctr[8];
3651
3652 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003655 return;
3656 }
3657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003659
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003660 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003661 tmp_transform = ssl->transform_out;
3662 ssl->transform_out = ssl->handshake->alt_transform_out;
3663 ssl->handshake->alt_transform_out = tmp_transform;
3664
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003665 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003666 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3667 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003668 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003669
3670 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003671 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3674 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3679 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003680 }
3681 }
3682#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003683}
3684
3685/*
3686 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003687 */
3688int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3689{
3690 int ret = 0;
3691
3692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3693
3694 ret = mbedtls_ssl_flight_transmit( ssl );
3695
3696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3697
3698 return( ret );
3699}
3700
3701/*
3702 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703 *
3704 * Need to remember the current message in case flush_output returns
3705 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003706 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003707 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003708int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003709{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003710 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003714 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003716
3717 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003718 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003719 ssl_swap_epochs( ssl );
3720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003722 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723
3724 while( ssl->handshake->cur_msg != NULL )
3725 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003726 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003727 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003728
Hanno Beckere1dcb032018-08-17 16:47:58 +01003729 int const is_finished =
3730 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3731 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3732
Hanno Becker04da1892018-08-14 13:22:10 +01003733 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3734 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3735
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003736 /* Swap epochs before sending Finished: we can't do it after
3737 * sending ChangeCipherSpec, in case write returns WANT_READ.
3738 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003739 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003740 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003742 ssl_swap_epochs( ssl );
3743 }
3744
Hanno Becker67bc7c32018-08-06 11:33:50 +01003745 ret = ssl_get_remaining_payload_in_datagram( ssl );
3746 if( ret < 0 )
3747 return( ret );
3748 max_frag_len = (size_t) ret;
3749
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003750 /* CCS is copied as is, while HS messages may need fragmentation */
3751 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3752 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003753 if( max_frag_len == 0 )
3754 {
3755 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3756 return( ret );
3757
3758 continue;
3759 }
3760
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003761 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003763 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003765 /* Update position inside current message */
3766 ssl->handshake->cur_msg_p += cur->len;
3767 }
3768 else
3769 {
3770 const unsigned char * const p = ssl->handshake->cur_msg_p;
3771 const size_t hs_len = cur->len - 12;
3772 const size_t frag_off = p - ( cur->p + 12 );
3773 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003774 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003775
Hanno Beckere1dcb032018-08-17 16:47:58 +01003776 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003777 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003778 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003779 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003780
Hanno Becker67bc7c32018-08-06 11:33:50 +01003781 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3782 return( ret );
3783
3784 continue;
3785 }
3786 max_hs_frag_len = max_frag_len - 12;
3787
3788 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3789 max_hs_frag_len : rem_len;
3790
3791 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003792 {
3793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003794 (unsigned) cur_hs_frag_len,
3795 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003796 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003797
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003798 /* Messages are stored with handshake headers as if not fragmented,
3799 * copy beginning of headers then fill fragmentation fields.
3800 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3801 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003802
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003803 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3804 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3805 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3806
Hanno Becker67bc7c32018-08-06 11:33:50 +01003807 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3808 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3809 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003810
3811 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3812
Hanno Becker3f7b9732018-08-28 09:53:25 +01003813 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3815 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003816 ssl->out_msgtype = cur->type;
3817
3818 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003819 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003820 }
3821
3822 /* If done with the current message move to the next one if any */
3823 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3824 {
3825 if( cur->next != NULL )
3826 {
3827 ssl->handshake->cur_msg = cur->next;
3828 ssl->handshake->cur_msg_p = cur->next->p + 12;
3829 }
3830 else
3831 {
3832 ssl->handshake->cur_msg = NULL;
3833 ssl->handshake->cur_msg_p = NULL;
3834 }
3835 }
3836
3837 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003838 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003841 return( ret );
3842 }
3843 }
3844
Hanno Becker67bc7c32018-08-06 11:33:50 +01003845 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3846 return( ret );
3847
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003848 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3850 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003851 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003854 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3855 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003856
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003858
3859 return( 0 );
3860}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003861
3862/*
3863 * To be called when the last message of an incoming flight is received.
3864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003866{
3867 /* We won't need to resend that one any more */
3868 ssl_flight_free( ssl->handshake->flight );
3869 ssl->handshake->flight = NULL;
3870 ssl->handshake->cur_msg = NULL;
3871
3872 /* The next incoming flight will start with this msg_seq */
3873 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3874
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003875 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003876 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003877
Hanno Becker0271f962018-08-16 13:23:47 +01003878 /* Clear future message buffering structure. */
3879 ssl_buffering_free( ssl );
3880
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003881 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003882 ssl_set_timer( ssl, 0 );
3883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3885 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003888 }
3889 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003891}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003892
3893/*
3894 * To be called when the last message of an outgoing flight is send.
3895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003897{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003898 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003899 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3902 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003905 }
3906 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003910
Paul Bakker5121ce52009-01-03 21:22:43 +00003911/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003912 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003913 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003914
3915/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003916 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003917 *
3918 * - fill in handshake headers
3919 * - update handshake checksum
3920 * - DTLS: save message for resending
3921 * - then pass to the record layer
3922 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003923 * DTLS: except for HelloRequest, messages are only queued, and will only be
3924 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003925 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926 * Inputs:
3927 * - ssl->out_msglen: 4 + actual handshake message len
3928 * (4 is the size of handshake headers for TLS)
3929 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3930 * - ssl->out_msg + 4: the handshake message body
3931 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003932 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003933 * - ssl->out_msglen: the length of the record contents
3934 * (including handshake headers but excluding record headers)
3935 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003936 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003937int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003938{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003939 int ret;
3940 const size_t hs_len = ssl->out_msglen - 4;
3941 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3944
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003945 /*
3946 * Sanity checks
3947 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003948 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003949 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3950 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003951 /* In SSLv3, the client might send a NoCertificate alert. */
3952#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3953 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3954 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3955 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3956#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3957 {
3958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3960 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003962
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003963 /* Whenever we send anything different from a
3964 * HelloRequest we should be in a handshake - double check. */
3965 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3966 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003967 ssl->handshake == NULL )
3968 {
3969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003974 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003977 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003980 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003981#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003982
Hanno Beckerb50a2532018-08-06 11:52:54 +01003983 /* Double-check that we did not exceed the bounds
3984 * of the outgoing record buffer.
3985 * This should never fail as the various message
3986 * writing functions must obey the bounds of the
3987 * outgoing record buffer, but better be safe.
3988 *
3989 * Note: We deliberately do not check for the MTU or MFL here.
3990 */
3991 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3992 {
3993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3994 "size %u, maximum %u",
3995 (unsigned) ssl->out_msglen,
3996 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3998 }
3999
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004000 /*
4001 * Fill handshake headers
4002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004005 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4006 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4007 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004008
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004009 /*
4010 * DTLS has additional fields in the Handshake layer,
4011 * between the length field and the actual payload:
4012 * uint16 message_seq;
4013 * uint24 fragment_offset;
4014 * uint24 fragment_length;
4015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004017 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004018 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004019 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004020 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004021 {
4022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4023 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004024 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004025 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4027 }
4028
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004029 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004030 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004031
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004032 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004033 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004034 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004035 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4036 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4037 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004038 }
4039 else
4040 {
4041 ssl->out_msg[4] = 0;
4042 ssl->out_msg[5] = 0;
4043 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004044
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004045 /* Handshake hashes are computed without fragmentation,
4046 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004047 memset( ssl->out_msg + 6, 0x00, 3 );
4048 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004049 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004051
Hanno Becker0207e532018-08-28 10:28:28 +01004052 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004053 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4054 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 }
4056
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004057 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004060 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4061 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004062 {
4063 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066 return( ret );
4067 }
4068 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004069 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004070#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004072 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004073 {
4074 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4075 return( ret );
4076 }
4077 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004078
4079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4080
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004081 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004082}
4083
4084/*
4085 * Record layer functions
4086 */
4087
4088/*
4089 * Write current record.
4090 *
4091 * Uses:
4092 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4093 * - ssl->out_msglen: length of the record content (excl headers)
4094 * - ssl->out_msg: record content
4095 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004096int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004097{
4098 int ret, done = 0;
4099 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004100 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004101
4102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004105 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004107 {
4108 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004111 return( ret );
4112 }
4113
4114 len = ssl->out_msglen;
4115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4119 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 ret = mbedtls_ssl_hw_record_write( ssl );
4124 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4127 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004128 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004129
4130 if( ret == 0 )
4131 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004132 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004134 if( !done )
4135 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004136 unsigned i;
4137 size_t protected_record_size;
4138
Hanno Becker6430faf2019-05-08 11:57:13 +01004139 /* Skip writing the record content type to after the encryption,
4140 * as it may change when using the CID extension. */
4141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004143 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004144
Hanno Becker19859472018-08-06 09:40:20 +01004145 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004146 ssl->out_len[0] = (unsigned char)( len >> 8 );
4147 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004148
Paul Bakker48916f92012-09-16 19:57:18 +00004149 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004150 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004151 mbedtls_record rec;
4152
4153 rec.buf = ssl->out_iv;
4154 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4155 ( ssl->out_iv - ssl->out_buf );
4156 rec.data_len = ssl->out_msglen;
4157 rec.data_offset = ssl->out_msg - rec.buf;
4158
4159 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4160 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4161 ssl->conf->transport, rec.ver );
4162 rec.type = ssl->out_msgtype;
4163
Hanno Becker43c24b82019-05-01 09:45:57 +01004164#if defined(MBEDTLS_SSL_CID)
4165 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004166 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004167#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004168
Hanno Beckera18d1322018-01-03 14:27:32 +00004169 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004170 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004173 return( ret );
4174 }
4175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004176 if( rec.data_offset != 0 )
4177 {
4178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4180 }
4181
Hanno Becker6430faf2019-05-08 11:57:13 +01004182 /* Update the record content type and CID. */
4183 ssl->out_msgtype = rec.type;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004184#if defined(MBEDTLS_SSL_CID )
4185 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
4186#endif /* MBEDTLS_SSL_CID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004187 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004188 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4189 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004190 }
4191
Hanno Becker5903de42019-05-03 14:46:38 +01004192 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004193
4194#if defined(MBEDTLS_SSL_PROTO_DTLS)
4195 /* In case of DTLS, double-check that we don't exceed
4196 * the remaining space in the datagram. */
4197 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4198 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004199 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004200 if( ret < 0 )
4201 return( ret );
4202
4203 if( protected_record_size > (size_t) ret )
4204 {
4205 /* Should never happen */
4206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4207 }
4208 }
4209#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004210
Hanno Becker6430faf2019-05-08 11:57:13 +01004211 /* Now write the potentially updated record content type. */
4212 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004215 "version = [%d:%d], msglen = %d",
4216 ssl->out_hdr[0], ssl->out_hdr[1],
4217 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004220 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004221
4222 ssl->out_left += protected_record_size;
4223 ssl->out_hdr += protected_record_size;
4224 ssl_update_out_pointers( ssl, ssl->transform_out );
4225
Hanno Becker04484622018-08-06 09:49:38 +01004226 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4227 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4228 break;
4229
4230 /* The loop goes to its end iff the counter is wrapping */
4231 if( i == ssl_ep_len( ssl ) )
4232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4234 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 }
4237
Hanno Becker67bc7c32018-08-06 11:33:50 +01004238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004239 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4240 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004241 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004242 size_t remaining;
4243 ret = ssl_get_remaining_payload_in_datagram( ssl );
4244 if( ret < 0 )
4245 {
4246 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4247 ret );
4248 return( ret );
4249 }
4250
4251 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004252 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004253 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004254 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004255 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004256 else
4257 {
Hanno Becker513815a2018-08-20 11:56:09 +01004258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004259 }
4260 }
4261#endif /* MBEDTLS_SSL_PROTO_DTLS */
4262
4263 if( ( flush == SSL_FORCE_FLUSH ) &&
4264 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 return( ret );
4268 }
4269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004271
4272 return( 0 );
4273}
4274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004276
4277static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4278{
4279 if( ssl->in_msglen < ssl->in_hslen ||
4280 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4281 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4282 {
4283 return( 1 );
4284 }
4285 return( 0 );
4286}
Hanno Becker44650b72018-08-16 12:51:11 +01004287
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004288static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004289{
4290 return( ( ssl->in_msg[9] << 16 ) |
4291 ( ssl->in_msg[10] << 8 ) |
4292 ssl->in_msg[11] );
4293}
4294
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004295static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004296{
4297 return( ( ssl->in_msg[6] << 16 ) |
4298 ( ssl->in_msg[7] << 8 ) |
4299 ssl->in_msg[8] );
4300}
4301
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004302static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004303{
4304 uint32_t msg_len, frag_off, frag_len;
4305
4306 msg_len = ssl_get_hs_total_len( ssl );
4307 frag_off = ssl_get_hs_frag_off( ssl );
4308 frag_len = ssl_get_hs_frag_len( ssl );
4309
4310 if( frag_off > msg_len )
4311 return( -1 );
4312
4313 if( frag_len > msg_len - frag_off )
4314 return( -1 );
4315
4316 if( frag_len + 12 > ssl->in_msglen )
4317 return( -1 );
4318
4319 return( 0 );
4320}
4321
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004322/*
4323 * Mark bits in bitmask (used for DTLS HS reassembly)
4324 */
4325static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4326{
4327 unsigned int start_bits, end_bits;
4328
4329 start_bits = 8 - ( offset % 8 );
4330 if( start_bits != 8 )
4331 {
4332 size_t first_byte_idx = offset / 8;
4333
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004334 /* Special case */
4335 if( len <= start_bits )
4336 {
4337 for( ; len != 0; len-- )
4338 mask[first_byte_idx] |= 1 << ( start_bits - len );
4339
4340 /* Avoid potential issues with offset or len becoming invalid */
4341 return;
4342 }
4343
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004344 offset += start_bits; /* Now offset % 8 == 0 */
4345 len -= start_bits;
4346
4347 for( ; start_bits != 0; start_bits-- )
4348 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4349 }
4350
4351 end_bits = len % 8;
4352 if( end_bits != 0 )
4353 {
4354 size_t last_byte_idx = ( offset + len ) / 8;
4355
4356 len -= end_bits; /* Now len % 8 == 0 */
4357
4358 for( ; end_bits != 0; end_bits-- )
4359 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4360 }
4361
4362 memset( mask + offset / 8, 0xFF, len / 8 );
4363}
4364
4365/*
4366 * Check that bitmask is full
4367 */
4368static int ssl_bitmask_check( unsigned char *mask, size_t len )
4369{
4370 size_t i;
4371
4372 for( i = 0; i < len / 8; i++ )
4373 if( mask[i] != 0xFF )
4374 return( -1 );
4375
4376 for( i = 0; i < len % 8; i++ )
4377 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4378 return( -1 );
4379
4380 return( 0 );
4381}
4382
Hanno Becker56e205e2018-08-16 09:06:12 +01004383/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004384static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004385 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004386{
Hanno Becker56e205e2018-08-16 09:06:12 +01004387 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004388
Hanno Becker56e205e2018-08-16 09:06:12 +01004389 alloc_len = 12; /* Handshake header */
4390 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004391
Hanno Beckerd07df862018-08-16 09:14:58 +01004392 if( add_bitmap )
4393 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004394
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004395 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004396}
Hanno Becker56e205e2018-08-16 09:06:12 +01004397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004399
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004400static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004401{
4402 return( ( ssl->in_msg[1] << 16 ) |
4403 ( ssl->in_msg[2] << 8 ) |
4404 ssl->in_msg[3] );
4405}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004406
Simon Butcher99000142016-10-13 17:21:01 +01004407int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004412 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004414 }
4415
Hanno Becker12555c62018-08-16 12:47:53 +01004416 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004419 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004420 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004424 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004425 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004426 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004427
Hanno Becker44650b72018-08-16 12:51:11 +01004428 if( ssl_check_hs_header( ssl ) != 0 )
4429 {
4430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4431 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4432 }
4433
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004434 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004435 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4436 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4437 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4438 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004439 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004440 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4441 {
4442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4443 recv_msg_seq,
4444 ssl->handshake->in_msg_seq ) );
4445 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4446 }
4447
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004448 /* Retransmit only on last message from previous flight, to avoid
4449 * too many retransmissions.
4450 * Besides, No sane server ever retransmits HelloVerifyRequest */
4451 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004455 "message_seq = %d, start_of_flight = %d",
4456 recv_msg_seq,
4457 ssl->handshake->in_flight_start_seq ) );
4458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004462 return( ret );
4463 }
4464 }
4465 else
4466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004468 "message_seq = %d, expected = %d",
4469 recv_msg_seq,
4470 ssl->handshake->in_msg_seq ) );
4471 }
4472
Hanno Becker90333da2017-10-10 11:27:13 +01004473 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004474 }
4475 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004476
Hanno Becker6d97ef52018-08-16 13:09:04 +01004477 /* Message reassembly is handled alongside buffering of future
4478 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004479 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004480 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004481 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004484 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004485 }
4486 }
4487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004489 /* With TLS we don't handle fragmentation (for now) */
4490 if( ssl->in_msglen < ssl->in_hslen )
4491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4493 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004494 }
4495
Simon Butcher99000142016-10-13 17:21:01 +01004496 return( 0 );
4497}
4498
4499void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4500{
Hanno Becker0271f962018-08-16 13:23:47 +01004501 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004502
Hanno Becker0271f962018-08-16 13:23:47 +01004503 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004504 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004505 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004506 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004507
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004508 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004511 ssl->handshake != NULL )
4512 {
Hanno Becker0271f962018-08-16 13:23:47 +01004513 unsigned offset;
4514 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004515
Hanno Becker0271f962018-08-16 13:23:47 +01004516 /* Increment handshake sequence number */
4517 hs->in_msg_seq++;
4518
4519 /*
4520 * Clear up handshake buffering and reassembly structure.
4521 */
4522
4523 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004524 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004525
4526 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004527 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4528 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004529 offset++, hs_buf++ )
4530 {
4531 *hs_buf = *(hs_buf + 1);
4532 }
4533
4534 /* Create a fresh last entry */
4535 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004536 }
4537#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004538}
4539
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004540/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004541 * DTLS anti-replay: RFC 6347 4.1.2.6
4542 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004543 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4544 * Bit n is set iff record number in_window_top - n has been seen.
4545 *
4546 * Usually, in_window_top is the last record number seen and the lsb of
4547 * in_window is set. The only exception is the initial state (record number 0
4548 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4551static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004552{
4553 ssl->in_window_top = 0;
4554 ssl->in_window = 0;
4555}
4556
4557static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4558{
4559 return( ( (uint64_t) buf[0] << 40 ) |
4560 ( (uint64_t) buf[1] << 32 ) |
4561 ( (uint64_t) buf[2] << 24 ) |
4562 ( (uint64_t) buf[3] << 16 ) |
4563 ( (uint64_t) buf[4] << 8 ) |
4564 ( (uint64_t) buf[5] ) );
4565}
4566
4567/*
4568 * Return 0 if sequence number is acceptable, -1 otherwise
4569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004571{
4572 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4573 uint64_t bit;
4574
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004575 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004576 return( 0 );
4577
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004578 if( rec_seqnum > ssl->in_window_top )
4579 return( 0 );
4580
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004581 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004582
4583 if( bit >= 64 )
4584 return( -1 );
4585
4586 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4587 return( -1 );
4588
4589 return( 0 );
4590}
4591
4592/*
4593 * Update replay window on new validated record
4594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596{
4597 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4598
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004599 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004600 return;
4601
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004602 if( rec_seqnum > ssl->in_window_top )
4603 {
4604 /* Update window_top and the contents of the window */
4605 uint64_t shift = rec_seqnum - ssl->in_window_top;
4606
4607 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004608 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004609 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004610 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004612 ssl->in_window |= 1;
4613 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004614
4615 ssl->in_window_top = rec_seqnum;
4616 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004617 else
4618 {
4619 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004620 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004621
4622 if( bit < 64 ) /* Always true, but be extra sure */
4623 ssl->in_window |= (uint64_t) 1 << bit;
4624 }
4625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004627
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004628#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004629/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004630static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4631
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004632/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004633 * Without any SSL context, check if a datagram looks like a ClientHello with
4634 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004635 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004636 *
4637 * - if cookie is valid, return 0
4638 * - if ClientHello looks superficially valid but cookie is not,
4639 * fill obuf and set olen, then
4640 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4641 * - otherwise return a specific error code
4642 */
4643static int ssl_check_dtls_clihlo_cookie(
4644 mbedtls_ssl_cookie_write_t *f_cookie_write,
4645 mbedtls_ssl_cookie_check_t *f_cookie_check,
4646 void *p_cookie,
4647 const unsigned char *cli_id, size_t cli_id_len,
4648 const unsigned char *in, size_t in_len,
4649 unsigned char *obuf, size_t buf_len, size_t *olen )
4650{
4651 size_t sid_len, cookie_len;
4652 unsigned char *p;
4653
4654 if( f_cookie_write == NULL || f_cookie_check == NULL )
4655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4656
4657 /*
4658 * Structure of ClientHello with record and handshake headers,
4659 * and expected values. We don't need to check a lot, more checks will be
4660 * done when actually parsing the ClientHello - skipping those checks
4661 * avoids code duplication and does not make cookie forging any easier.
4662 *
4663 * 0-0 ContentType type; copied, must be handshake
4664 * 1-2 ProtocolVersion version; copied
4665 * 3-4 uint16 epoch; copied, must be 0
4666 * 5-10 uint48 sequence_number; copied
4667 * 11-12 uint16 length; (ignored)
4668 *
4669 * 13-13 HandshakeType msg_type; (ignored)
4670 * 14-16 uint24 length; (ignored)
4671 * 17-18 uint16 message_seq; copied
4672 * 19-21 uint24 fragment_offset; copied, must be 0
4673 * 22-24 uint24 fragment_length; (ignored)
4674 *
4675 * 25-26 ProtocolVersion client_version; (ignored)
4676 * 27-58 Random random; (ignored)
4677 * 59-xx SessionID session_id; 1 byte len + sid_len content
4678 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4679 * ...
4680 *
4681 * Minimum length is 61 bytes.
4682 */
4683 if( in_len < 61 ||
4684 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4685 in[3] != 0 || in[4] != 0 ||
4686 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4687 {
4688 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4689 }
4690
4691 sid_len = in[59];
4692 if( sid_len > in_len - 61 )
4693 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4694
4695 cookie_len = in[60 + sid_len];
4696 if( cookie_len > in_len - 60 )
4697 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4698
4699 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4700 cli_id, cli_id_len ) == 0 )
4701 {
4702 /* Valid cookie */
4703 return( 0 );
4704 }
4705
4706 /*
4707 * If we get here, we've got an invalid cookie, let's prepare HVR.
4708 *
4709 * 0-0 ContentType type; copied
4710 * 1-2 ProtocolVersion version; copied
4711 * 3-4 uint16 epoch; copied
4712 * 5-10 uint48 sequence_number; copied
4713 * 11-12 uint16 length; olen - 13
4714 *
4715 * 13-13 HandshakeType msg_type; hello_verify_request
4716 * 14-16 uint24 length; olen - 25
4717 * 17-18 uint16 message_seq; copied
4718 * 19-21 uint24 fragment_offset; copied
4719 * 22-24 uint24 fragment_length; olen - 25
4720 *
4721 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4722 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4723 *
4724 * Minimum length is 28.
4725 */
4726 if( buf_len < 28 )
4727 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4728
4729 /* Copy most fields and adapt others */
4730 memcpy( obuf, in, 25 );
4731 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4732 obuf[25] = 0xfe;
4733 obuf[26] = 0xff;
4734
4735 /* Generate and write actual cookie */
4736 p = obuf + 28;
4737 if( f_cookie_write( p_cookie,
4738 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4739 {
4740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4741 }
4742
4743 *olen = p - obuf;
4744
4745 /* Go back and fill length fields */
4746 obuf[27] = (unsigned char)( *olen - 28 );
4747
4748 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4749 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4750 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4751
4752 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4753 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4754
4755 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4756}
4757
4758/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004759 * Handle possible client reconnect with the same UDP quadruplet
4760 * (RFC 6347 Section 4.2.8).
4761 *
4762 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4763 * that looks like a ClientHello.
4764 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004765 * - if the input looks like a ClientHello without cookies,
4766 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004767 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004768 * - if the input looks like a ClientHello with a valid cookie,
4769 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004770 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004771 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004772 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004774 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4775 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004776 */
4777static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4778{
4779 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004781
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004782 ret = ssl_check_dtls_clihlo_cookie(
4783 ssl->conf->f_cookie_write,
4784 ssl->conf->f_cookie_check,
4785 ssl->conf->p_cookie,
4786 ssl->cli_id, ssl->cli_id_len,
4787 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004788 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004789
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004790 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4791
4792 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004793 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004794 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004795 * If the error is permanent we'll catch it later,
4796 * if it's not, then hopefully it'll work next time. */
4797 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4798
4799 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004800 }
4801
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004802 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004803 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004804 /* Got a valid cookie, partially reset context */
4805 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4806 {
4807 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4808 return( ret );
4809 }
4810
4811 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004812 }
4813
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004814 return( ret );
4815}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004816#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004817
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004818static int ssl_check_record_type( uint8_t record_type )
4819{
4820 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4821 record_type != MBEDTLS_SSL_MSG_ALERT &&
4822 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4823 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4824 {
4825 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4826 }
4827
4828 return( 0 );
4829}
4830
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004831/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004832 * ContentType type;
4833 * ProtocolVersion version;
4834 * uint16 epoch; // DTLS only
4835 * uint48 sequence_number; // DTLS only
4836 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004837 *
4838 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004839 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004840 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4841 *
4842 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004843 * 1. proceed with the record if this function returns 0
4844 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4845 * 3. return CLIENT_RECONNECT if this function return that value
4846 * 4. drop the whole datagram if this function returns anything else.
4847 * Point 2 is needed when the peer is resending, and we have already received
4848 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004851{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004852 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004853
Hanno Becker5903de42019-05-03 14:46:38 +01004854 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004855
Paul Bakker5121ce52009-01-03 21:22:43 +00004856 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004857 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004858 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004862 ssl->in_msgtype,
4863 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004864
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004865 /* Check record type */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004866 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004869
4870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004871 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4872 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004873 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4874#endif /* MBEDTLS_SSL_PROTO_DTLS */
4875 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4876 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004879 }
4880
4881 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004882 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4885 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004886 }
4887
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004888 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4891 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 }
4893
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004894 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004895 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004896 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4899 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004900 }
4901
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004902 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004903 * DTLS-related tests.
4904 * Check epoch before checking length constraint because
4905 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4906 * message gets duplicated before the corresponding Finished message,
4907 * the second ChangeCipherSpec should be discarded because it belongs
4908 * to an old epoch, but not because its length is shorter than
4909 * the minimum record length for packets using the new record transform.
4910 * Note that these two kinds of failures are handled differently,
4911 * as an unexpected record is silently skipped but an invalid
4912 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004913 */
4914#if defined(MBEDTLS_SSL_PROTO_DTLS)
4915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4916 {
4917 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4918
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004919 /* Check epoch (and sequence number) with DTLS */
4920 if( rec_epoch != ssl->in_epoch )
4921 {
4922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4923 "expected %d, received %d",
4924 ssl->in_epoch, rec_epoch ) );
4925
4926#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4927 /*
4928 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4929 * access the first byte of record content (handshake type), as we
4930 * have an active transform (possibly iv_len != 0), so use the
4931 * fact that the record header len is 13 instead.
4932 */
4933 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4934 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4935 rec_epoch == 0 &&
4936 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4937 ssl->in_left > 13 &&
4938 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4939 {
4940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4941 "from the same port" ) );
4942 return( ssl_handle_possible_reconnect( ssl ) );
4943 }
4944 else
4945#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004946 {
4947 /* Consider buffering the record. */
4948 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4949 {
4950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4951 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4952 }
4953
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004954 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004955 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004956 }
4957
4958#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4959 /* Replay detection only works for the current epoch */
4960 if( rec_epoch == ssl->in_epoch &&
4961 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4962 {
4963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4964 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4965 }
4966#endif
4967 }
4968#endif /* MBEDTLS_SSL_PROTO_DTLS */
4969
Hanno Becker52c6dc62017-05-26 16:07:36 +01004970
4971 /* Check length against bounds of the current transform and version */
4972 if( ssl->transform_in == NULL )
4973 {
4974 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004975 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004976 {
4977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4978 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4979 }
4980 }
4981 else
4982 {
4983 if( ssl->in_msglen < ssl->transform_in->minlen )
4984 {
4985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4986 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4987 }
4988
4989#if defined(MBEDTLS_SSL_PROTO_SSL3)
4990 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004991 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004992 {
4993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4994 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4995 }
4996#endif
4997#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4998 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4999 /*
5000 * TLS encrypted messages can have up to 256 bytes of padding
5001 */
5002 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5003 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005004 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005005 {
5006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5007 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5008 }
5009#endif
5010 }
5011
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005012 return( 0 );
5013}
Paul Bakker5121ce52009-01-03 21:22:43 +00005014
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005015/*
5016 * If applicable, decrypt (and decompress) record content
5017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005019{
5020 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005023 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5026 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 ret = mbedtls_ssl_hw_record_read( ssl );
5031 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5034 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005035 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005036
5037 if( ret == 0 )
5038 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005041 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005042 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005043 mbedtls_record rec;
5044
5045 rec.buf = ssl->in_iv;
5046 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5047 - ( ssl->in_iv - ssl->in_buf );
5048 rec.data_len = ssl->in_msglen;
5049 rec.data_offset = 0;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005050#if defined(MBEDTLS_SSL_CID )
5051 rec.cid_len = ssl->in_len - ssl->in_cid;
5052 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
5053#endif /* MBEDTLS_SSL_CID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005054
5055 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5056 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5057 ssl->conf->transport, rec.ver );
5058 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005059 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5060 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005063 return( ret );
5064 }
5065
Hanno Becker6430faf2019-05-08 11:57:13 +01005066 if( ssl->in_msgtype != rec.type )
5067 {
5068 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5069 ssl->in_msgtype, rec.type ) );
5070 }
5071
5072 /* The record content type may change during decryption,
5073 * so re-read it. */
5074 ssl->in_msgtype = rec.type;
5075 /* Also update the input buffer, because unfortunately
5076 * the server-side ssl_parse_client_hello() reparses the
5077 * record header when receiving a ClientHello initiating
5078 * a renegotiation. */
5079 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005080 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005081 ssl->in_msglen = rec.data_len;
5082 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5083 ssl->in_len[1] = (unsigned char)( rec.data_len );
5084
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005085 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5086 ssl->in_msg, ssl->in_msglen );
5087
Hanno Becker6430faf2019-05-08 11:57:13 +01005088#if defined(MBEDTLS_SSL_CID)
5089 /* We have already checked the record content type
5090 * in ssl_parse_record_header(), failing or silently
5091 * dropping the record in the case of an unknown type.
5092 *
5093 * Since with the use of CIDs, the record content type
5094 * might change during decryption, re-check the record
5095 * content type, but treat a failure as fatal this time. */
5096 if( ssl_check_record_type( ssl->in_msgtype ) )
5097 {
5098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5099 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5100 }
5101#endif /* MBEDTLS_SSL_CID */
5102
Angus Grattond8213d02016-05-25 20:56:48 +10005103 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5106 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005108 else if( ssl->in_msglen == 0 )
5109 {
5110#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5111 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5112 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5113 {
5114 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5116 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5117 }
5118#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5119
5120 ssl->nb_zero++;
5121
5122 /*
5123 * Three or more empty messages may be a DoS attack
5124 * (excessive CPU consumption).
5125 */
5126 if( ssl->nb_zero > 3 )
5127 {
5128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005129 "messages, possible DoS attack" ) );
5130 /* Treat the records as if they were not properly authenticated,
5131 * thereby failing the connection if we see more than allowed
5132 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005133 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5134 }
5135 }
5136 else
5137 ssl->nb_zero = 0;
5138
5139#if defined(MBEDTLS_SSL_PROTO_DTLS)
5140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5141 {
5142 ; /* in_ctr read from peer, not maintained internally */
5143 }
5144 else
5145#endif
5146 {
5147 unsigned i;
5148 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5149 if( ++ssl->in_ctr[i - 1] != 0 )
5150 break;
5151
5152 /* The loop goes to its end iff the counter is wrapping */
5153 if( i == ssl_ep_len( ssl ) )
5154 {
5155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5156 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5157 }
5158 }
5159
Paul Bakker5121ce52009-01-03 21:22:43 +00005160 }
5161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005163 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005165 {
5166 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005169 return( ret );
5170 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005174#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005178 }
5179#endif
5180
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005181 return( 0 );
5182}
5183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005185
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005186/*
5187 * Read a record.
5188 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005189 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5190 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5191 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005192 */
Hanno Becker1097b342018-08-15 14:09:41 +01005193
5194/* Helper functions for mbedtls_ssl_read_record(). */
5195static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005196static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5197static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005198
Hanno Becker327c93b2018-08-15 13:56:18 +01005199int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005200 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005201{
5202 int ret;
5203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005205
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005206 if( ssl->keep_current_message == 0 )
5207 {
5208 do {
Simon Butcher99000142016-10-13 17:21:01 +01005209
Hanno Becker26994592018-08-15 14:14:59 +01005210 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005211 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005212 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005213
Hanno Beckere74d5562018-08-15 14:26:08 +01005214 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005215 {
Hanno Becker40f50842018-08-15 14:48:01 +01005216#if defined(MBEDTLS_SSL_PROTO_DTLS)
5217 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005218
Hanno Becker40f50842018-08-15 14:48:01 +01005219 /* We only check for buffered messages if the
5220 * current datagram is fully consumed. */
5221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005222 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005223 {
Hanno Becker40f50842018-08-15 14:48:01 +01005224 if( ssl_load_buffered_message( ssl ) == 0 )
5225 have_buffered = 1;
5226 }
5227
5228 if( have_buffered == 0 )
5229#endif /* MBEDTLS_SSL_PROTO_DTLS */
5230 {
5231 ret = ssl_get_next_record( ssl );
5232 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5233 continue;
5234
5235 if( ret != 0 )
5236 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005237 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005238 return( ret );
5239 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005240 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005241 }
5242
5243 ret = mbedtls_ssl_handle_message_type( ssl );
5244
Hanno Becker40f50842018-08-15 14:48:01 +01005245#if defined(MBEDTLS_SSL_PROTO_DTLS)
5246 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5247 {
5248 /* Buffer future message */
5249 ret = ssl_buffer_message( ssl );
5250 if( ret != 0 )
5251 return( ret );
5252
5253 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5254 }
5255#endif /* MBEDTLS_SSL_PROTO_DTLS */
5256
Hanno Becker90333da2017-10-10 11:27:13 +01005257 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5258 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005259
5260 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005261 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005262 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005263 return( ret );
5264 }
5265
Hanno Becker327c93b2018-08-15 13:56:18 +01005266 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005267 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005268 {
5269 mbedtls_ssl_update_handshake_status( ssl );
5270 }
Simon Butcher99000142016-10-13 17:21:01 +01005271 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005272 else
Simon Butcher99000142016-10-13 17:21:01 +01005273 {
Hanno Becker02f59072018-08-15 14:00:24 +01005274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005275 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005276 }
5277
5278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5279
5280 return( 0 );
5281}
5282
Hanno Becker40f50842018-08-15 14:48:01 +01005283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005285{
Hanno Becker40f50842018-08-15 14:48:01 +01005286 if( ssl->in_left > ssl->next_record_offset )
5287 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005288
Hanno Becker40f50842018-08-15 14:48:01 +01005289 return( 0 );
5290}
5291
5292static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5293{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005294 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005295 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005296 int ret = 0;
5297
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005298 if( hs == NULL )
5299 return( -1 );
5300
Hanno Beckere00ae372018-08-20 09:39:42 +01005301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5302
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005303 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5304 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5305 {
5306 /* Check if we have seen a ChangeCipherSpec before.
5307 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005308 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005309 {
5310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5311 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005312 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005313 }
5314
Hanno Becker39b8bc92018-08-28 17:17:13 +01005315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005316 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5317 ssl->in_msglen = 1;
5318 ssl->in_msg[0] = 1;
5319
5320 /* As long as they are equal, the exact value doesn't matter. */
5321 ssl->in_left = 0;
5322 ssl->next_record_offset = 0;
5323
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005324 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005325 goto exit;
5326 }
Hanno Becker37f95322018-08-16 13:55:32 +01005327
Hanno Beckerb8f50142018-08-28 10:01:34 +01005328#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005329 /* Debug only */
5330 {
5331 unsigned offset;
5332 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5333 {
5334 hs_buf = &hs->buffering.hs[offset];
5335 if( hs_buf->is_valid == 1 )
5336 {
5337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5338 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005339 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005340 }
5341 }
5342 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005343#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005344
5345 /* Check if we have buffered and/or fully reassembled the
5346 * next handshake message. */
5347 hs_buf = &hs->buffering.hs[0];
5348 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5349 {
5350 /* Synthesize a record containing the buffered HS message. */
5351 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5352 ( hs_buf->data[2] << 8 ) |
5353 hs_buf->data[3];
5354
5355 /* Double-check that we haven't accidentally buffered
5356 * a message that doesn't fit into the input buffer. */
5357 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5358 {
5359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5361 }
5362
5363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5364 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5365 hs_buf->data, msg_len + 12 );
5366
5367 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5368 ssl->in_hslen = msg_len + 12;
5369 ssl->in_msglen = msg_len + 12;
5370 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5371
5372 ret = 0;
5373 goto exit;
5374 }
5375 else
5376 {
5377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5378 hs->in_msg_seq ) );
5379 }
5380
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005381 ret = -1;
5382
5383exit:
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5386 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005387}
5388
Hanno Beckera02b0b42018-08-21 17:20:27 +01005389static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5390 size_t desired )
5391{
5392 int offset;
5393 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5395 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005396
Hanno Becker01315ea2018-08-21 17:22:17 +01005397 /* Get rid of future records epoch first, if such exist. */
5398 ssl_free_buffered_record( ssl );
5399
5400 /* Check if we have enough space available now. */
5401 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5402 hs->buffering.total_bytes_buffered ) )
5403 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005405 return( 0 );
5406 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005407
Hanno Becker4f432ad2018-08-28 10:02:32 +01005408 /* We don't have enough space to buffer the next expected handshake
5409 * message. Remove buffers used for future messages to gain space,
5410 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005411 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5412 offset >= 0; offset-- )
5413 {
5414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5415 offset ) );
5416
Hanno Beckerb309b922018-08-23 13:18:05 +01005417 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005418
5419 /* Check if we have enough space available now. */
5420 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5421 hs->buffering.total_bytes_buffered ) )
5422 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005424 return( 0 );
5425 }
5426 }
5427
5428 return( -1 );
5429}
5430
Hanno Becker40f50842018-08-15 14:48:01 +01005431static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5432{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005433 int ret = 0;
5434 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5435
5436 if( hs == NULL )
5437 return( 0 );
5438
5439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5440
5441 switch( ssl->in_msgtype )
5442 {
5443 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005445
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005446 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005447 break;
5448
5449 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005450 {
5451 unsigned recv_msg_seq_offset;
5452 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5453 mbedtls_ssl_hs_buffer *hs_buf;
5454 size_t msg_len = ssl->in_hslen - 12;
5455
5456 /* We should never receive an old handshake
5457 * message - double-check nonetheless. */
5458 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5459 {
5460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5462 }
5463
5464 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5465 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5466 {
5467 /* Silently ignore -- message too far in the future */
5468 MBEDTLS_SSL_DEBUG_MSG( 2,
5469 ( "Ignore future HS message with sequence number %u, "
5470 "buffering window %u - %u",
5471 recv_msg_seq, ssl->handshake->in_msg_seq,
5472 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5473
5474 goto exit;
5475 }
5476
5477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5478 recv_msg_seq, recv_msg_seq_offset ) );
5479
5480 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5481
5482 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005483 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005484 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005485 size_t reassembly_buf_sz;
5486
Hanno Becker37f95322018-08-16 13:55:32 +01005487 hs_buf->is_fragmented =
5488 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5489
5490 /* We copy the message back into the input buffer
5491 * after reassembly, so check that it's not too large.
5492 * This is an implementation-specific limitation
5493 * and not one from the standard, hence it is not
5494 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005495 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005496 {
5497 /* Ignore message */
5498 goto exit;
5499 }
5500
Hanno Beckere0b150f2018-08-21 15:51:03 +01005501 /* Check if we have enough space to buffer the message. */
5502 if( hs->buffering.total_bytes_buffered >
5503 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5504 {
5505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5507 }
5508
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005509 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5510 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005511
5512 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5513 hs->buffering.total_bytes_buffered ) )
5514 {
5515 if( recv_msg_seq_offset > 0 )
5516 {
5517 /* If we can't buffer a future message because
5518 * of space limitations -- ignore. */
5519 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",
5520 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5521 (unsigned) hs->buffering.total_bytes_buffered ) );
5522 goto exit;
5523 }
Hanno Beckere1801392018-08-21 16:51:05 +01005524 else
5525 {
5526 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",
5527 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5528 (unsigned) hs->buffering.total_bytes_buffered ) );
5529 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005530
Hanno Beckera02b0b42018-08-21 17:20:27 +01005531 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005532 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005533 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",
5534 (unsigned) msg_len,
5535 (unsigned) reassembly_buf_sz,
5536 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005537 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005538 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5539 goto exit;
5540 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005541 }
5542
5543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5544 msg_len ) );
5545
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005546 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5547 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005548 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005549 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005550 goto exit;
5551 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005552 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005553
5554 /* Prepare final header: copy msg_type, length and message_seq,
5555 * then add standardised fragment_offset and fragment_length */
5556 memcpy( hs_buf->data, ssl->in_msg, 6 );
5557 memset( hs_buf->data + 6, 0, 3 );
5558 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5559
5560 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005561
5562 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005563 }
5564 else
5565 {
5566 /* Make sure msg_type and length are consistent */
5567 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5568 {
5569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5570 /* Ignore */
5571 goto exit;
5572 }
5573 }
5574
Hanno Becker4422bbb2018-08-20 09:40:19 +01005575 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005576 {
5577 size_t frag_len, frag_off;
5578 unsigned char * const msg = hs_buf->data + 12;
5579
5580 /*
5581 * Check and copy current fragment
5582 */
5583
5584 /* Validation of header fields already done in
5585 * mbedtls_ssl_prepare_handshake_record(). */
5586 frag_off = ssl_get_hs_frag_off( ssl );
5587 frag_len = ssl_get_hs_frag_len( ssl );
5588
5589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5590 frag_off, frag_len ) );
5591 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5592
5593 if( hs_buf->is_fragmented )
5594 {
5595 unsigned char * const bitmask = msg + msg_len;
5596 ssl_bitmask_set( bitmask, frag_off, frag_len );
5597 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5598 msg_len ) == 0 );
5599 }
5600 else
5601 {
5602 hs_buf->is_complete = 1;
5603 }
5604
5605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5606 hs_buf->is_complete ? "" : "not yet " ) );
5607 }
5608
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005609 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005610 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005611
5612 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005613 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005614 break;
5615 }
5616
5617exit:
5618
5619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5620 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005621}
5622#endif /* MBEDTLS_SSL_PROTO_DTLS */
5623
Hanno Becker1097b342018-08-15 14:09:41 +01005624static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005625{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005626 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005627 * Consume last content-layer message and potentially
5628 * update in_msglen which keeps track of the contents'
5629 * consumption state.
5630 *
5631 * (1) Handshake messages:
5632 * Remove last handshake message, move content
5633 * and adapt in_msglen.
5634 *
5635 * (2) Alert messages:
5636 * Consume whole record content, in_msglen = 0.
5637 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005638 * (3) Change cipher spec:
5639 * Consume whole record content, in_msglen = 0.
5640 *
5641 * (4) Application data:
5642 * Don't do anything - the record layer provides
5643 * the application data as a stream transport
5644 * and consumes through mbedtls_ssl_read only.
5645 *
5646 */
5647
5648 /* Case (1): Handshake messages */
5649 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005650 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005651 /* Hard assertion to be sure that no application data
5652 * is in flight, as corrupting ssl->in_msglen during
5653 * ssl->in_offt != NULL is fatal. */
5654 if( ssl->in_offt != NULL )
5655 {
5656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5658 }
5659
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005660 /*
5661 * Get next Handshake message in the current record
5662 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005663
Hanno Becker4a810fb2017-05-24 16:27:30 +01005664 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005665 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005666 * current handshake content: If DTLS handshake
5667 * fragmentation is used, that's the fragment
5668 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005669 * size here is faulty and should be changed at
5670 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005671 * (2) While it doesn't seem to cause problems, one
5672 * has to be very careful not to assume that in_hslen
5673 * is always <= in_msglen in a sensible communication.
5674 * Again, it's wrong for DTLS handshake fragmentation.
5675 * The following check is therefore mandatory, and
5676 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005677 * Additionally, ssl->in_hslen might be arbitrarily out of
5678 * bounds after handling a DTLS message with an unexpected
5679 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005680 */
5681 if( ssl->in_hslen < ssl->in_msglen )
5682 {
5683 ssl->in_msglen -= ssl->in_hslen;
5684 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5685 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005686
Hanno Becker4a810fb2017-05-24 16:27:30 +01005687 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5688 ssl->in_msg, ssl->in_msglen );
5689 }
5690 else
5691 {
5692 ssl->in_msglen = 0;
5693 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005694
Hanno Becker4a810fb2017-05-24 16:27:30 +01005695 ssl->in_hslen = 0;
5696 }
5697 /* Case (4): Application data */
5698 else if( ssl->in_offt != NULL )
5699 {
5700 return( 0 );
5701 }
5702 /* Everything else (CCS & Alerts) */
5703 else
5704 {
5705 ssl->in_msglen = 0;
5706 }
5707
Hanno Becker1097b342018-08-15 14:09:41 +01005708 return( 0 );
5709}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005710
Hanno Beckere74d5562018-08-15 14:26:08 +01005711static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5712{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005713 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005714 return( 1 );
5715
5716 return( 0 );
5717}
5718
Hanno Becker5f066e72018-08-16 14:56:31 +01005719#if defined(MBEDTLS_SSL_PROTO_DTLS)
5720
5721static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5722{
5723 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5724 if( hs == NULL )
5725 return;
5726
Hanno Becker01315ea2018-08-21 17:22:17 +01005727 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005728 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005729 hs->buffering.total_bytes_buffered -=
5730 hs->buffering.future_record.len;
5731
5732 mbedtls_free( hs->buffering.future_record.data );
5733 hs->buffering.future_record.data = NULL;
5734 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005735}
5736
5737static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5738{
5739 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5740 unsigned char * rec;
5741 size_t rec_len;
5742 unsigned rec_epoch;
5743
5744 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5745 return( 0 );
5746
5747 if( hs == NULL )
5748 return( 0 );
5749
Hanno Becker5f066e72018-08-16 14:56:31 +01005750 rec = hs->buffering.future_record.data;
5751 rec_len = hs->buffering.future_record.len;
5752 rec_epoch = hs->buffering.future_record.epoch;
5753
5754 if( rec == NULL )
5755 return( 0 );
5756
Hanno Becker4cb782d2018-08-20 11:19:05 +01005757 /* Only consider loading future records if the
5758 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005759 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005760 return( 0 );
5761
Hanno Becker5f066e72018-08-16 14:56:31 +01005762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5763
5764 if( rec_epoch != ssl->in_epoch )
5765 {
5766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5767 goto exit;
5768 }
5769
5770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5771
5772 /* Double-check that the record is not too large */
5773 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5774 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5775 {
5776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5778 }
5779
5780 memcpy( ssl->in_hdr, rec, rec_len );
5781 ssl->in_left = rec_len;
5782 ssl->next_record_offset = 0;
5783
5784 ssl_free_buffered_record( ssl );
5785
5786exit:
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5788 return( 0 );
5789}
5790
5791static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5792{
5793 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5794 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005795 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005796
5797 /* Don't buffer future records outside handshakes. */
5798 if( hs == NULL )
5799 return( 0 );
5800
5801 /* Only buffer handshake records (we are only interested
5802 * in Finished messages). */
5803 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5804 return( 0 );
5805
5806 /* Don't buffer more than one future epoch record. */
5807 if( hs->buffering.future_record.data != NULL )
5808 return( 0 );
5809
Hanno Becker01315ea2018-08-21 17:22:17 +01005810 /* Don't buffer record if there's not enough buffering space remaining. */
5811 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5812 hs->buffering.total_bytes_buffered ) )
5813 {
5814 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",
5815 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5816 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005817 return( 0 );
5818 }
5819
Hanno Becker5f066e72018-08-16 14:56:31 +01005820 /* Buffer record */
5821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5822 ssl->in_epoch + 1 ) );
5823 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5824 rec_hdr_len + ssl->in_msglen );
5825
5826 /* ssl_parse_record_header() only considers records
5827 * of the next epoch as candidates for buffering. */
5828 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005829 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005830
5831 hs->buffering.future_record.data =
5832 mbedtls_calloc( 1, hs->buffering.future_record.len );
5833 if( hs->buffering.future_record.data == NULL )
5834 {
5835 /* If we run out of RAM trying to buffer a
5836 * record from the next epoch, just ignore. */
5837 return( 0 );
5838 }
5839
Hanno Becker01315ea2018-08-21 17:22:17 +01005840 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005841
Hanno Becker01315ea2018-08-21 17:22:17 +01005842 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005843 return( 0 );
5844}
5845
5846#endif /* MBEDTLS_SSL_PROTO_DTLS */
5847
Hanno Beckere74d5562018-08-15 14:26:08 +01005848static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005849{
5850 int ret;
5851
Hanno Becker5f066e72018-08-16 14:56:31 +01005852#if defined(MBEDTLS_SSL_PROTO_DTLS)
5853 /* We might have buffered a future record; if so,
5854 * and if the epoch matches now, load it.
5855 * On success, this call will set ssl->in_left to
5856 * the length of the buffered record, so that
5857 * the calls to ssl_fetch_input() below will
5858 * essentially be no-ops. */
5859 ret = ssl_load_buffered_record( ssl );
5860 if( ret != 0 )
5861 return( ret );
5862#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005863
Hanno Becker5903de42019-05-03 14:46:38 +01005864 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005867 return( ret );
5868 }
5869
5870 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005873 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5874 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005875 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005876 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5877 {
5878 ret = ssl_buffer_future_record( ssl );
5879 if( ret != 0 )
5880 return( ret );
5881
5882 /* Fall through to handling of unexpected records */
5883 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5884 }
5885
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005886 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5887 {
5888 /* Skip unexpected record (but not whole datagram) */
5889 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005890 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005891
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5893 "(header)" ) );
5894 }
5895 else
5896 {
5897 /* Skip invalid record and the rest of the datagram */
5898 ssl->next_record_offset = 0;
5899 ssl->in_left = 0;
5900
5901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5902 "(header)" ) );
5903 }
5904
5905 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005906 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005907 }
5908#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005909 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005910 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005911
5912 /*
5913 * Read and optionally decrypt the message contents
5914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005916 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005919 return( ret );
5920 }
5921
5922 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005924 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005925 {
Hanno Becker5903de42019-05-03 14:46:38 +01005926 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005927 if( ssl->next_record_offset < ssl->in_left )
5928 {
5929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5930 }
5931 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005932 else
5933#endif
5934 ssl->in_left = 0;
5935
5936 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005939 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005940 {
5941 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005942 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005943 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005944 /* Except when waiting for Finished as a bad mac here
5945 * probably means something went wrong in the handshake
5946 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5947 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5948 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5949 {
5950#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5951 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5952 {
5953 mbedtls_ssl_send_alert_message( ssl,
5954 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5955 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5956 }
5957#endif
5958 return( ret );
5959 }
5960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005962 if( ssl->conf->badmac_limit != 0 &&
5963 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5966 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005967 }
5968#endif
5969
Hanno Becker4a810fb2017-05-24 16:27:30 +01005970 /* As above, invalid records cause
5971 * dismissal of the whole datagram. */
5972
5973 ssl->next_record_offset = 0;
5974 ssl->in_left = 0;
5975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005977 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005978 }
5979
5980 return( ret );
5981 }
5982 else
5983#endif
5984 {
5985 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5987 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 mbedtls_ssl_send_alert_message( ssl,
5990 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5991 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005992 }
5993#endif
5994 return( ret );
5995 }
5996 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005997
Simon Butcher99000142016-10-13 17:21:01 +01005998 return( 0 );
5999}
6000
6001int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6002{
6003 int ret;
6004
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006005 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006006 * Handle particular types of records
6007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006009 {
Simon Butcher99000142016-10-13 17:21:01 +01006010 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6011 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006012 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006013 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 }
6015
Hanno Beckere678eaa2018-08-21 14:57:46 +01006016 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006017 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006018 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006019 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6021 ssl->in_msglen ) );
6022 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006023 }
6024
Hanno Beckere678eaa2018-08-21 14:57:46 +01006025 if( ssl->in_msg[0] != 1 )
6026 {
6027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6028 ssl->in_msg[0] ) );
6029 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6030 }
6031
6032#if defined(MBEDTLS_SSL_PROTO_DTLS)
6033 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6034 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6035 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6036 {
6037 if( ssl->handshake == NULL )
6038 {
6039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6040 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6041 }
6042
6043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6044 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6045 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006046#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006047 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006051 if( ssl->in_msglen != 2 )
6052 {
6053 /* Note: Standard allows for more than one 2 byte alert
6054 to be packed in a single message, but Mbed TLS doesn't
6055 currently support this. */
6056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6057 ssl->in_msglen ) );
6058 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6059 }
6060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 ssl->in_msg[0], ssl->in_msg[1] ) );
6063
6064 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006065 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006070 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006072 }
6073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6075 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6078 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006080
6081#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6082 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6083 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6084 {
Hanno Becker90333da2017-10-10 11:27:13 +01006085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006086 /* Will be handled when trying to parse ServerHello */
6087 return( 0 );
6088 }
6089#endif
6090
6091#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6092 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6093 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6094 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6095 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6096 {
6097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6098 /* Will be handled in mbedtls_ssl_parse_certificate() */
6099 return( 0 );
6100 }
6101#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6102
6103 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006104 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006105 }
6106
Hanno Beckerc76c6192017-06-06 10:03:17 +01006107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006109 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006110 /* Drop unexpected ApplicationData records,
6111 * except at the beginning of renegotiations */
6112 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6113 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6114#if defined(MBEDTLS_SSL_RENEGOTIATION)
6115 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6116 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006117#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006118 )
6119 {
6120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6121 return( MBEDTLS_ERR_SSL_NON_FATAL );
6122 }
6123
6124 if( ssl->handshake != NULL &&
6125 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6126 {
6127 ssl_handshake_wrapup_free_hs_transform( ssl );
6128 }
6129 }
6130#endif /* MBEDTLS_SSL_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006131
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 return( 0 );
6133}
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006136{
6137 int ret;
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6140 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6141 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006142 {
6143 return( ret );
6144 }
6145
6146 return( 0 );
6147}
6148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006150 unsigned char level,
6151 unsigned char message )
6152{
6153 int ret;
6154
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006155 if( ssl == NULL || ssl->conf == NULL )
6156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006162 ssl->out_msglen = 2;
6163 ssl->out_msg[0] = level;
6164 ssl->out_msg[1] = message;
6165
Hanno Becker67bc7c32018-08-06 11:33:50 +01006166 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006169 return( ret );
6170 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006172
6173 return( 0 );
6174}
6175
Hanno Beckerb9d44792019-02-08 07:19:04 +00006176#if defined(MBEDTLS_X509_CRT_PARSE_C)
6177static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6178{
6179#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6180 if( session->peer_cert != NULL )
6181 {
6182 mbedtls_x509_crt_free( session->peer_cert );
6183 mbedtls_free( session->peer_cert );
6184 session->peer_cert = NULL;
6185 }
6186#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6187 if( session->peer_cert_digest != NULL )
6188 {
6189 /* Zeroization is not necessary. */
6190 mbedtls_free( session->peer_cert_digest );
6191 session->peer_cert_digest = NULL;
6192 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6193 session->peer_cert_digest_len = 0;
6194 }
6195#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6196}
6197#endif /* MBEDTLS_X509_CRT_PARSE_C */
6198
Paul Bakker5121ce52009-01-03 21:22:43 +00006199/*
6200 * Handshake functions
6201 */
Hanno Becker21489932019-02-05 13:20:55 +00006202#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006203/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006205{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6207 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210
Hanno Becker7177a882019-02-05 13:36:46 +00006211 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006214 ssl->state++;
6215 return( 0 );
6216 }
6217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006220}
6221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006223{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006224 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6225 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006228
Hanno Becker7177a882019-02-05 13:36:46 +00006229 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006232 ssl->state++;
6233 return( 0 );
6234 }
6235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006238}
Gilles Peskinef9828522017-05-03 12:28:43 +02006239
Hanno Becker21489932019-02-05 13:20:55 +00006240#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006241/* Some certificate support -> implement write and parse */
6242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006246 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006248 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6249 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006252
Hanno Becker7177a882019-02-05 13:36:46 +00006253 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006256 ssl->state++;
6257 return( 0 );
6258 }
6259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 {
6263 if( ssl->client_auth == 0 )
6264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 ssl->state++;
6267 return( 0 );
6268 }
6269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006271 /*
6272 * If using SSLv3 and got no cert, send an Alert message
6273 * (otherwise an empty Certificate message will be sent).
6274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6276 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006277 {
6278 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6280 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6281 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 goto write_msg;
6285 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006287 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288#endif /* MBEDTLS_SSL_CLI_C */
6289#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006290 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6295 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 }
6297 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006298#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006301
6302 /*
6303 * 0 . 0 handshake type
6304 * 1 . 3 handshake length
6305 * 4 . 6 length of all certs
6306 * 7 . 9 length of cert. 1
6307 * 10 . n-1 peer certificate
6308 * n . n+2 length of cert. 2
6309 * n+3 . ... upper level cert, etc.
6310 */
6311 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006313
Paul Bakker29087132010-03-21 21:03:34 +00006314 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006315 {
6316 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006317 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006320 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006322 }
6323
6324 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6325 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6326 ssl->out_msg[i + 2] = (unsigned char)( n );
6327
6328 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6329 i += n; crt = crt->next;
6330 }
6331
6332 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6333 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6334 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6335
6336 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6338 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006340#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006341write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006342#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
6344 ssl->state++;
6345
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006346 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 return( ret );
6350 }
6351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006353
Paul Bakkered27a042013-04-18 22:46:23 +02006354 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006355}
6356
Hanno Becker84879e32019-01-31 07:44:03 +00006357#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006358
6359#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006360static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6361 unsigned char *crt_buf,
6362 size_t crt_buf_len )
6363{
6364 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6365
6366 if( peer_crt == NULL )
6367 return( -1 );
6368
6369 if( peer_crt->raw.len != crt_buf_len )
6370 return( -1 );
6371
Hanno Becker46f34d02019-02-08 14:00:04 +00006372 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006373}
Hanno Becker177475a2019-02-05 17:02:46 +00006374#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6375static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6376 unsigned char *crt_buf,
6377 size_t crt_buf_len )
6378{
6379 int ret;
6380 unsigned char const * const peer_cert_digest =
6381 ssl->session->peer_cert_digest;
6382 mbedtls_md_type_t const peer_cert_digest_type =
6383 ssl->session->peer_cert_digest_type;
6384 mbedtls_md_info_t const * const digest_info =
6385 mbedtls_md_info_from_type( peer_cert_digest_type );
6386 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6387 size_t digest_len;
6388
6389 if( peer_cert_digest == NULL || digest_info == NULL )
6390 return( -1 );
6391
6392 digest_len = mbedtls_md_get_size( digest_info );
6393 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6394 return( -1 );
6395
6396 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6397 if( ret != 0 )
6398 return( -1 );
6399
6400 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6401}
6402#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006403#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006404
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006405/*
6406 * Once the certificate message is read, parse it into a cert chain and
6407 * perform basic checks, but leave actual verification to the caller
6408 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006409static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6410 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006411{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006412 int ret;
6413#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6414 int crt_cnt=0;
6415#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006416 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006417 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006422 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6423 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006425 }
6426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6428 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006431 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6432 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006434 }
6435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006437
Paul Bakker5121ce52009-01-03 21:22:43 +00006438 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006440 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006441 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006442
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006443 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006447 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6448 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006450 }
6451
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006452 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6453 i += 3;
6454
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006455 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006456 while( i < ssl->in_hslen )
6457 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006458 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006459 if ( i + 3 > ssl->in_hslen ) {
6460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006461 mbedtls_ssl_send_alert_message( ssl,
6462 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6463 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006464 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6465 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006466 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6467 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006468 if( ssl->in_msg[i] != 0 )
6469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006471 mbedtls_ssl_send_alert_message( ssl,
6472 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6473 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006475 }
6476
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006477 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006478 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6479 | (unsigned int) ssl->in_msg[i + 2];
6480 i += 3;
6481
6482 if( n < 128 || i + n > ssl->in_hslen )
6483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006485 mbedtls_ssl_send_alert_message( ssl,
6486 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6487 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 }
6490
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006491 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006492#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6493 if( crt_cnt++ == 0 &&
6494 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6495 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006496 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006497 /* During client-side renegotiation, check that the server's
6498 * end-CRTs hasn't changed compared to the initial handshake,
6499 * mitigating the triple handshake attack. On success, reuse
6500 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6502 if( ssl_check_peer_crt_unchanged( ssl,
6503 &ssl->in_msg[i],
6504 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006505 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6507 mbedtls_ssl_send_alert_message( ssl,
6508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6509 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6510 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006511 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006512
6513 /* Now we can safely free the original chain. */
6514 ssl_clear_peer_cert( ssl->session );
6515 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006516#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6517
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006518 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006519#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006520 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006521#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006522 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006523 * it in-place from the input buffer instead of making a copy. */
6524 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6525#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006526 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006527 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006528 case 0: /*ok*/
6529 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6530 /* Ignore certificate with an unknown algorithm: maybe a
6531 prior certificate was already trusted. */
6532 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006533
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006534 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6535 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6536 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006537
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006538 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6539 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6540 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006541
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006542 default:
6543 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6544 crt_parse_der_failed:
6545 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6546 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6547 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 }
6549
6550 i += n;
6551 }
6552
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006553 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006554 return( 0 );
6555}
6556
Hanno Becker4a55f632019-02-05 12:49:06 +00006557#if defined(MBEDTLS_SSL_SRV_C)
6558static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6559{
6560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6561 return( -1 );
6562
6563#if defined(MBEDTLS_SSL_PROTO_SSL3)
6564 /*
6565 * Check if the client sent an empty certificate
6566 */
6567 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6568 {
6569 if( ssl->in_msglen == 2 &&
6570 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6571 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6572 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6573 {
6574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6575 return( 0 );
6576 }
6577
6578 return( -1 );
6579 }
6580#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6581
6582#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6583 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6584 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6585 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6586 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6587 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6588 {
6589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6590 return( 0 );
6591 }
6592
6593 return( -1 );
6594#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6595 MBEDTLS_SSL_PROTO_TLS1_2 */
6596}
6597#endif /* MBEDTLS_SSL_SRV_C */
6598
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006599/* Check if a certificate message is expected.
6600 * Return either
6601 * - SSL_CERTIFICATE_EXPECTED, or
6602 * - SSL_CERTIFICATE_SKIP
6603 * indicating whether a Certificate message is expected or not.
6604 */
6605#define SSL_CERTIFICATE_EXPECTED 0
6606#define SSL_CERTIFICATE_SKIP 1
6607static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6608 int authmode )
6609{
6610 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006611 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006612
6613 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6614 return( SSL_CERTIFICATE_SKIP );
6615
6616#if defined(MBEDTLS_SSL_SRV_C)
6617 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6618 {
6619 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6620 return( SSL_CERTIFICATE_SKIP );
6621
6622 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6623 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006624 ssl->session_negotiate->verify_result =
6625 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6626 return( SSL_CERTIFICATE_SKIP );
6627 }
6628 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006629#else
6630 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006631#endif /* MBEDTLS_SSL_SRV_C */
6632
6633 return( SSL_CERTIFICATE_EXPECTED );
6634}
6635
Hanno Becker68636192019-02-05 14:36:34 +00006636static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6637 int authmode,
6638 mbedtls_x509_crt *chain,
6639 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006640{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006641 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006642 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006643 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006644 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006645
Hanno Becker8927c832019-04-03 12:52:50 +01006646 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6647 void *p_vrfy;
6648
Hanno Becker68636192019-02-05 14:36:34 +00006649 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6650 return( 0 );
6651
Hanno Becker8927c832019-04-03 12:52:50 +01006652 if( ssl->f_vrfy != NULL )
6653 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006655 f_vrfy = ssl->f_vrfy;
6656 p_vrfy = ssl->p_vrfy;
6657 }
6658 else
6659 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006661 f_vrfy = ssl->conf->f_vrfy;
6662 p_vrfy = ssl->conf->p_vrfy;
6663 }
6664
Hanno Becker68636192019-02-05 14:36:34 +00006665 /*
6666 * Main check: verify certificate
6667 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006668#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6669 if( ssl->conf->f_ca_cb != NULL )
6670 {
6671 ((void) rs_ctx);
6672 have_ca_chain = 1;
6673
6674 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006675 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006676 chain,
6677 ssl->conf->f_ca_cb,
6678 ssl->conf->p_ca_cb,
6679 ssl->conf->cert_profile,
6680 ssl->hostname,
6681 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006682 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006683 }
6684 else
6685#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6686 {
6687 mbedtls_x509_crt *ca_chain;
6688 mbedtls_x509_crl *ca_crl;
6689
6690#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6691 if( ssl->handshake->sni_ca_chain != NULL )
6692 {
6693 ca_chain = ssl->handshake->sni_ca_chain;
6694 ca_crl = ssl->handshake->sni_ca_crl;
6695 }
6696 else
6697#endif
6698 {
6699 ca_chain = ssl->conf->ca_chain;
6700 ca_crl = ssl->conf->ca_crl;
6701 }
6702
6703 if( ca_chain != NULL )
6704 have_ca_chain = 1;
6705
6706 ret = mbedtls_x509_crt_verify_restartable(
6707 chain,
6708 ca_chain, ca_crl,
6709 ssl->conf->cert_profile,
6710 ssl->hostname,
6711 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006712 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006713 }
Hanno Becker68636192019-02-05 14:36:34 +00006714
6715 if( ret != 0 )
6716 {
6717 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6718 }
6719
6720#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6721 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6722 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6723#endif
6724
6725 /*
6726 * Secondary checks: always done, but change 'ret' only if it was 0
6727 */
6728
6729#if defined(MBEDTLS_ECP_C)
6730 {
6731 const mbedtls_pk_context *pk = &chain->pk;
6732
6733 /* If certificate uses an EC key, make sure the curve is OK */
6734 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6735 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6736 {
6737 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6738
6739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6740 if( ret == 0 )
6741 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6742 }
6743 }
6744#endif /* MBEDTLS_ECP_C */
6745
6746 if( mbedtls_ssl_check_cert_usage( chain,
6747 ciphersuite_info,
6748 ! ssl->conf->endpoint,
6749 &ssl->session_negotiate->verify_result ) != 0 )
6750 {
6751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6752 if( ret == 0 )
6753 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6754 }
6755
6756 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6757 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6758 * with details encoded in the verification flags. All other kinds
6759 * of error codes, including those from the user provided f_vrfy
6760 * functions, are treated as fatal and lead to a failure of
6761 * ssl_parse_certificate even if verification was optional. */
6762 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6763 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6764 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6765 {
6766 ret = 0;
6767 }
6768
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006769 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006770 {
6771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6772 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6773 }
6774
6775 if( ret != 0 )
6776 {
6777 uint8_t alert;
6778
6779 /* The certificate may have been rejected for several reasons.
6780 Pick one and send the corresponding alert. Which alert to send
6781 may be a subject of debate in some cases. */
6782 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6783 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6784 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6785 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6786 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6787 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6788 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6789 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6790 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6791 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6792 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6793 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6794 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6795 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6796 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6797 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6798 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6799 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6800 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6801 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6802 else
6803 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6804 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6805 alert );
6806 }
6807
6808#if defined(MBEDTLS_DEBUG_C)
6809 if( ssl->session_negotiate->verify_result != 0 )
6810 {
6811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6812 ssl->session_negotiate->verify_result ) );
6813 }
6814 else
6815 {
6816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6817 }
6818#endif /* MBEDTLS_DEBUG_C */
6819
6820 return( ret );
6821}
6822
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006823#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6824static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6825 unsigned char *start, size_t len )
6826{
6827 int ret;
6828 /* Remember digest of the peer's end-CRT. */
6829 ssl->session_negotiate->peer_cert_digest =
6830 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6831 if( ssl->session_negotiate->peer_cert_digest == NULL )
6832 {
6833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6834 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6835 mbedtls_ssl_send_alert_message( ssl,
6836 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6837 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6838
6839 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6840 }
6841
6842 ret = mbedtls_md( mbedtls_md_info_from_type(
6843 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6844 start, len,
6845 ssl->session_negotiate->peer_cert_digest );
6846
6847 ssl->session_negotiate->peer_cert_digest_type =
6848 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6849 ssl->session_negotiate->peer_cert_digest_len =
6850 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6851
6852 return( ret );
6853}
6854
6855static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6856 unsigned char *start, size_t len )
6857{
6858 unsigned char *end = start + len;
6859 int ret;
6860
6861 /* Make a copy of the peer's raw public key. */
6862 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6863 ret = mbedtls_pk_parse_subpubkey( &start, end,
6864 &ssl->handshake->peer_pubkey );
6865 if( ret != 0 )
6866 {
6867 /* We should have parsed the public key before. */
6868 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6869 }
6870
6871 return( 0 );
6872}
6873#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6874
Hanno Becker68636192019-02-05 14:36:34 +00006875int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6876{
6877 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006878 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006879#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6880 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6881 ? ssl->handshake->sni_authmode
6882 : ssl->conf->authmode;
6883#else
6884 const int authmode = ssl->conf->authmode;
6885#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006886 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006887 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006888
6889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6890
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006891 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6892 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006893 {
6894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006895 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006896 }
6897
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006898#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6899 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006900 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006901 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006902 chain = ssl->handshake->ecrs_peer_cert;
6903 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006904 goto crt_verify;
6905 }
6906#endif
6907
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006908 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006909 {
6910 /* mbedtls_ssl_read_record may have sent an alert already. We
6911 let it decide whether to alert. */
6912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006913 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006914 }
6915
Hanno Becker4a55f632019-02-05 12:49:06 +00006916#if defined(MBEDTLS_SSL_SRV_C)
6917 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6918 {
6919 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006920
6921 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006922 ret = 0;
6923 else
6924 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006925
Hanno Becker6bdfab22019-02-05 13:11:17 +00006926 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006927 }
6928#endif /* MBEDTLS_SSL_SRV_C */
6929
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006930 /* Clear existing peer CRT structure in case we tried to
6931 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006932 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006933
6934 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6935 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006936 {
6937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6938 sizeof( mbedtls_x509_crt ) ) );
6939 mbedtls_ssl_send_alert_message( ssl,
6940 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6941 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006942
Hanno Becker3dad3112019-02-05 17:19:52 +00006943 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6944 goto exit;
6945 }
6946 mbedtls_x509_crt_init( chain );
6947
6948 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006949 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006950 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006951
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006952#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6953 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006954 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006955
6956crt_verify:
6957 if( ssl->handshake->ecrs_enabled)
6958 rs_ctx = &ssl->handshake->ecrs_ctx;
6959#endif
6960
Hanno Becker68636192019-02-05 14:36:34 +00006961 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006962 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006963 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006964 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006965
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006966#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006967 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006968 unsigned char *crt_start, *pk_start;
6969 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006970
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006971 /* We parse the CRT chain without copying, so
6972 * these pointers point into the input buffer,
6973 * and are hence still valid after freeing the
6974 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006975
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006976 crt_start = chain->raw.p;
6977 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006978
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006979 pk_start = chain->pk_raw.p;
6980 pk_len = chain->pk_raw.len;
6981
6982 /* Free the CRT structures before computing
6983 * digest and copying the peer's public key. */
6984 mbedtls_x509_crt_free( chain );
6985 mbedtls_free( chain );
6986 chain = NULL;
6987
6988 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006989 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006990 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006991
6992 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6993 if( ret != 0 )
6994 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006995 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006996#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6997 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006998 ssl->session_negotiate->peer_cert = chain;
6999 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007000#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003
Hanno Becker6bdfab22019-02-05 13:11:17 +00007004exit:
7005
Hanno Becker3dad3112019-02-05 17:19:52 +00007006 if( ret == 0 )
7007 ssl->state++;
7008
7009#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7010 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7011 {
7012 ssl->handshake->ecrs_peer_cert = chain;
7013 chain = NULL;
7014 }
7015#endif
7016
7017 if( chain != NULL )
7018 {
7019 mbedtls_x509_crt_free( chain );
7020 mbedtls_free( chain );
7021 }
7022
Paul Bakker5121ce52009-01-03 21:22:43 +00007023 return( ret );
7024}
Hanno Becker21489932019-02-05 13:20:55 +00007025#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007028{
7029 int ret;
7030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007034 ssl->out_msglen = 1;
7035 ssl->out_msg[0] = 1;
7036
Paul Bakker5121ce52009-01-03 21:22:43 +00007037 ssl->state++;
7038
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007039 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007040 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007042 return( ret );
7043 }
7044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007046
7047 return( 0 );
7048}
7049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007051{
7052 int ret;
7053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007055
Hanno Becker327c93b2018-08-15 13:56:18 +01007056 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059 return( ret );
7060 }
7061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007065 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7066 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007068 }
7069
Hanno Beckere678eaa2018-08-21 14:57:46 +01007070 /* CCS records are only accepted if they have length 1 and content '1',
7071 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007073 /*
7074 * Switch to our negotiated transform and session parameters for inbound
7075 * data.
7076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007078 ssl->transform_in = ssl->transform_negotiate;
7079 ssl->session_in = ssl->session_negotiate;
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007085 ssl_dtls_replay_reset( ssl );
7086#endif
7087
7088 /* Increment epoch */
7089 if( ++ssl->in_epoch == 0 )
7090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007092 /* This is highly unlikely to happen for legitimate reasons, so
7093 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007095 }
7096 }
7097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007099 memset( ssl->in_ctr, 0, 8 );
7100
Hanno Becker79594fd2019-05-08 09:38:41 +01007101 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7104 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007109 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7110 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007112 }
7113 }
7114#endif
7115
Paul Bakker5121ce52009-01-03 21:22:43 +00007116 ssl->state++;
7117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
7120 return( 0 );
7121}
7122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7124 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007125{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007126 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7129 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7130 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007131 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007132 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7135#if defined(MBEDTLS_SHA512_C)
7136 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007137 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7138 else
7139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_SHA256_C)
7141 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007142 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007143 else
7144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007148 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007149 }
Paul Bakker380da532012-04-18 16:10:25 +00007150}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7155 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007156 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7157 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7160#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007161#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007162 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007163 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7164#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007165 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007166#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007170 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007171 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007172#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007173 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007174#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007177}
7178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007180 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007181{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7183 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007184 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7185 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7188#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007189#if defined(MBEDTLS_USE_PSA_CRYPTO)
7190 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7191#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007192 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007193#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007196#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007197 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007198#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007199 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007200#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007203}
7204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7206 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7207static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007208 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007209{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007210 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7211 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007212}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007213#endif
Paul Bakker380da532012-04-18 16:10:25 +00007214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7216#if defined(MBEDTLS_SHA256_C)
7217static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007218 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007219{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007220#if defined(MBEDTLS_USE_PSA_CRYPTO)
7221 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7222#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007223 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007224#endif
Paul Bakker380da532012-04-18 16:10:25 +00007225}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007226#endif
Paul Bakker380da532012-04-18 16:10:25 +00007227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SHA512_C)
7229static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007230 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007231{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007232#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007233 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007234#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007235 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007236#endif
Paul Bakker380da532012-04-18 16:10:25 +00007237}
Paul Bakker769075d2012-11-24 11:26:46 +01007238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007242static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007244{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007245 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007246 mbedtls_md5_context md5;
7247 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007248
Paul Bakker5121ce52009-01-03 21:22:43 +00007249 unsigned char padbuf[48];
7250 unsigned char md5sum[16];
7251 unsigned char sha1sum[20];
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007254 if( !session )
7255 session = ssl->session;
7256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007258
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007259 mbedtls_md5_init( &md5 );
7260 mbedtls_sha1_init( &sha1 );
7261
7262 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7263 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007264
7265 /*
7266 * SSLv3:
7267 * hash =
7268 * MD5( master + pad2 +
7269 * MD5( handshake + sender + master + pad1 ) )
7270 * + SHA1( master + pad2 +
7271 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007272 */
7273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007275 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7276 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007280 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7281 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007285 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007286
Paul Bakker1ef83d62012-04-11 12:09:53 +00007287 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007288
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007289 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7290 mbedtls_md5_update_ret( &md5, session->master, 48 );
7291 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7292 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007293
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007294 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7295 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7296 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7297 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007298
Paul Bakker1ef83d62012-04-11 12:09:53 +00007299 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007300
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007301 mbedtls_md5_starts_ret( &md5 );
7302 mbedtls_md5_update_ret( &md5, session->master, 48 );
7303 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7304 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7305 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007306
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007307 mbedtls_sha1_starts_ret( &sha1 );
7308 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7309 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7310 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7311 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007314
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007315 mbedtls_md5_free( &md5 );
7316 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007317
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007318 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7319 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7320 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007327static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007329{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007330 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007331 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007332 mbedtls_md5_context md5;
7333 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007334 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007337 if( !session )
7338 session = ssl->session;
7339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007341
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007342 mbedtls_md5_init( &md5 );
7343 mbedtls_sha1_init( &sha1 );
7344
7345 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7346 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Paul Bakker1ef83d62012-04-11 12:09:53 +00007348 /*
7349 * TLSv1:
7350 * hash = PRF( master, finished_label,
7351 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7352 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007355 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7356 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007357#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007360 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7361 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007362#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007365 ? "client finished"
7366 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007367
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007368 mbedtls_md5_finish_ret( &md5, padbuf );
7369 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007370
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007371 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007372 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007375
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007376 mbedtls_md5_free( &md5 );
7377 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007379 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7386#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007387static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007389{
7390 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007391 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007392 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007393#if defined(MBEDTLS_USE_PSA_CRYPTO)
7394 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007395 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007396 psa_status_t status;
7397#else
7398 mbedtls_sha256_context sha256;
7399#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007402 if( !session )
7403 session = ssl->session;
7404
Andrzej Kurekeb342242019-01-29 09:14:33 -05007405 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7406 ? "client finished"
7407 : "server finished";
7408
7409#if defined(MBEDTLS_USE_PSA_CRYPTO)
7410 sha256_psa = psa_hash_operation_init();
7411
7412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7413
7414 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7415 if( status != PSA_SUCCESS )
7416 {
7417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7418 return;
7419 }
7420
7421 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7422 if( status != PSA_SUCCESS )
7423 {
7424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7425 return;
7426 }
7427 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7428#else
7429
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007430 mbedtls_sha256_init( &sha256 );
7431
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007433
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007434 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007435
7436 /*
7437 * TLSv1.2:
7438 * hash = PRF( master, finished_label,
7439 * Hash( handshake ) )[0.11]
7440 */
7441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#if !defined(MBEDTLS_SHA256_ALT)
7443 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007444 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007445#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007447 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007448 mbedtls_sha256_free( &sha256 );
7449#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007450
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007451 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007452 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007455
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007456 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007459}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007463static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007465{
7466 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007467 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007468 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007469#if defined(MBEDTLS_USE_PSA_CRYPTO)
7470 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007471 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007472 psa_status_t status;
7473#else
7474 mbedtls_sha512_context sha512;
7475#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007478 if( !session )
7479 session = ssl->session;
7480
Andrzej Kurekeb342242019-01-29 09:14:33 -05007481 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7482 ? "client finished"
7483 : "server finished";
7484
7485#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007486 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007487
7488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7489
Andrzej Kurek972fba52019-01-30 03:29:12 -05007490 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007491 if( status != PSA_SUCCESS )
7492 {
7493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7494 return;
7495 }
7496
Andrzej Kurek972fba52019-01-30 03:29:12 -05007497 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007498 if( status != PSA_SUCCESS )
7499 {
7500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7501 return;
7502 }
7503 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7504#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007505 mbedtls_sha512_init( &sha512 );
7506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007508
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007509 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007510
7511 /*
7512 * TLSv1.2:
7513 * hash = PRF( master, finished_label,
7514 * Hash( handshake ) )[0.11]
7515 */
7516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007518 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7519 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007520#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007521
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007522 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007523 mbedtls_sha512_free( &sha512 );
7524#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007525
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007526 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007527 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007530
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007531 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535#endif /* MBEDTLS_SHA512_C */
7536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007541
7542 /*
7543 * Free our handshake params
7544 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007545 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007547 ssl->handshake = NULL;
7548
7549 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007550 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007551 */
7552 if( ssl->transform )
7553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 mbedtls_ssl_transform_free( ssl->transform );
7555 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007556 }
7557 ssl->transform = ssl->transform_negotiate;
7558 ssl->transform_negotiate = NULL;
7559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007561}
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007564{
7565 int resume = ssl->handshake->resume;
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#if defined(MBEDTLS_SSL_RENEGOTIATION)
7570 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007573 ssl->renego_records_seen = 0;
7574 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007575#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007576
7577 /*
7578 * Free the previous session and switch in the current one
7579 */
Paul Bakker0a597072012-09-25 21:55:46 +00007580 if( ssl->session )
7581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007583 /* RFC 7366 3.1: keep the EtM state */
7584 ssl->session_negotiate->encrypt_then_mac =
7585 ssl->session->encrypt_then_mac;
7586#endif
7587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 mbedtls_ssl_session_free( ssl->session );
7589 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007590 }
7591 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007592 ssl->session_negotiate = NULL;
7593
Paul Bakker0a597072012-09-25 21:55:46 +00007594 /*
7595 * Add cache entry
7596 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007597 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007598 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007599 resume == 0 )
7600 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007601 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007603 }
Paul Bakker0a597072012-09-25 21:55:46 +00007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007606 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007607 ssl->handshake->flight != NULL )
7608 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007609 /* Cancel handshake timer */
7610 ssl_set_timer( ssl, 0 );
7611
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007612 /* Keep last flight around in case we need to resend it:
7613 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007615 }
7616 else
7617#endif
7618 ssl_handshake_wrapup_free_hs_transform( ssl );
7619
Paul Bakker48916f92012-09-16 19:57:18 +00007620 ssl->state++;
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007623}
7624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007626{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007627 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007630
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007631 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007632
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007633 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007634
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007635 /*
7636 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7637 * may define some other value. Currently (early 2016), no defined
7638 * ciphersuite does this (and this is unlikely to change as activity has
7639 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007644 ssl->verify_data_len = hash_len;
7645 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007646#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007647
Paul Bakker5121ce52009-01-03 21:22:43 +00007648 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7650 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007651
7652 /*
7653 * In case of session resuming, invert the client and server
7654 * ChangeCipherSpec messages order.
7655 */
Paul Bakker0a597072012-09-25 21:55:46 +00007656 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007659 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007661#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007663 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007665#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007666 }
7667 else
7668 ssl->state++;
7669
Paul Bakker48916f92012-09-16 19:57:18 +00007670 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007671 * Switch to our negotiated transform and session parameters for outbound
7672 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007677 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007678 {
7679 unsigned char i;
7680
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007681 /* Remember current epoch settings for resending */
7682 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007683 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007684
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007685 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007686 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007687
7688 /* Increment epoch */
7689 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007690 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007691 break;
7692
7693 /* The loop goes to its end iff the counter is wrapping */
7694 if( i == 0 )
7695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7697 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007698 }
7699 }
7700 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007702 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007703
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007704 ssl->transform_out = ssl->transform_negotiate;
7705 ssl->session_out = ssl->session_negotiate;
7706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7708 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7713 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007714 }
7715 }
7716#endif
7717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007721#endif
7722
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007723 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 return( ret );
7727 }
7728
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007729#if defined(MBEDTLS_SSL_PROTO_DTLS)
7730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7731 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7732 {
7733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7734 return( ret );
7735 }
7736#endif
7737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007739
7740 return( 0 );
7741}
7742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007744#define SSL_MAX_HASH_LEN 36
7745#else
7746#define SSL_MAX_HASH_LEN 12
7747#endif
7748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007750{
Paul Bakker23986e52011-04-24 08:57:21 +00007751 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007752 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007753 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007756
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007757 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007758
Hanno Becker327c93b2018-08-15 13:56:18 +01007759 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007762 return( ret );
7763 }
7764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007768 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7769 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007771 }
7772
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007773 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_PROTO_SSL3)
7775 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007776 hash_len = 36;
7777 else
7778#endif
7779 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7782 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007785 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7786 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007788 }
7789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007791 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007794 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7795 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007797 }
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007800 ssl->verify_data_len = hash_len;
7801 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007802#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007803
Paul Bakker0a597072012-09-25 21:55:46 +00007804 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007807 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007809#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007811 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007813#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007814 }
7815 else
7816 ssl->state++;
7817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007819 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007821#endif
7822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007824
7825 return( 0 );
7826}
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7833 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7834 mbedtls_md5_init( &handshake->fin_md5 );
7835 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007836 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7837 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007838#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7840#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007841#if defined(MBEDTLS_USE_PSA_CRYPTO)
7842 handshake->fin_sha256_psa = psa_hash_operation_init();
7843 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7844#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007846 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007847#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007850#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007851 handshake->fin_sha384_psa = psa_hash_operation_init();
7852 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007853#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007855 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007856#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007859
7860 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007861
7862#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7863 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7864 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7865#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867#if defined(MBEDTLS_DHM_C)
7868 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870#if defined(MBEDTLS_ECDH_C)
7871 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007872#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007873#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007874 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007875#if defined(MBEDTLS_SSL_CLI_C)
7876 handshake->ecjpake_cache = NULL;
7877 handshake->ecjpake_cache_len = 0;
7878#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007879#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007880
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007881#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007882 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007883#endif
7884
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007885#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7886 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7887#endif
Hanno Becker75173122019-02-06 16:18:31 +00007888
7889#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7890 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7891 mbedtls_pk_init( &handshake->peer_pubkey );
7892#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007893}
7894
Hanno Beckera18d1322018-01-03 14:27:32 +00007895void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7900 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007901
Hanno Beckerd56ed242018-01-03 15:32:51 +00007902#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 mbedtls_md_init( &transform->md_ctx_enc );
7904 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007905#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007906}
7907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007911}
7912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007914{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007915 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007916 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007918 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007920 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007921 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007922
7923 /*
7924 * Either the pointers are now NULL or cleared properly and can be freed.
7925 * Now allocate missing structures.
7926 */
7927 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007928 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007929 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007930 }
Paul Bakker48916f92012-09-16 19:57:18 +00007931
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007932 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007933 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007934 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007935 }
Paul Bakker48916f92012-09-16 19:57:18 +00007936
Paul Bakker82788fb2014-10-20 13:59:19 +02007937 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007938 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007939 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007940 }
Paul Bakker48916f92012-09-16 19:57:18 +00007941
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007942 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007943 if( ssl->handshake == NULL ||
7944 ssl->transform_negotiate == NULL ||
7945 ssl->session_negotiate == NULL )
7946 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 mbedtls_free( ssl->handshake );
7950 mbedtls_free( ssl->transform_negotiate );
7951 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007952
7953 ssl->handshake = NULL;
7954 ssl->transform_negotiate = NULL;
7955 ssl->session_negotiate = NULL;
7956
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007957 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007958 }
7959
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007960 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007962 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007963 ssl_handshake_params_init( ssl->handshake );
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7967 {
7968 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007969
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007970 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7971 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7972 else
7973 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007974
7975 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007976 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007977#endif
7978
Paul Bakker48916f92012-09-16 19:57:18 +00007979 return( 0 );
7980}
7981
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007982#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007983/* Dummy cookie callbacks for defaults */
7984static int ssl_cookie_write_dummy( void *ctx,
7985 unsigned char **p, unsigned char *end,
7986 const unsigned char *cli_id, size_t cli_id_len )
7987{
7988 ((void) ctx);
7989 ((void) p);
7990 ((void) end);
7991 ((void) cli_id);
7992 ((void) cli_id_len);
7993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007995}
7996
7997static int ssl_cookie_check_dummy( void *ctx,
7998 const unsigned char *cookie, size_t cookie_len,
7999 const unsigned char *cli_id, size_t cli_id_len )
8000{
8001 ((void) ctx);
8002 ((void) cookie);
8003 ((void) cookie_len);
8004 ((void) cli_id);
8005 ((void) cli_id_len);
8006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008008}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008009#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008010
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008011/* Once ssl->out_hdr as the address of the beginning of the
8012 * next outgoing record is set, deduce the other pointers.
8013 *
8014 * Note: For TLS, we save the implicit record sequence number
8015 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8016 * and the caller has to make sure there's space for this.
8017 */
8018
8019static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8020 mbedtls_ssl_transform *transform )
8021{
8022#if defined(MBEDTLS_SSL_PROTO_DTLS)
8023 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8024 {
8025 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008026#if defined(MBEDTLS_SSL_CID)
8027 ssl->out_cid = ssl->out_ctr + 8;
8028 ssl->out_len = ssl->out_cid;
8029 if( transform != NULL )
8030 ssl->out_len += transform->out_cid_len;
8031#else /* MBEDTLS_SSL_CID */
8032 ssl->out_len = ssl->out_ctr + 8;
8033#endif /* MBEDTLS_SSL_CID */
8034 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008035 }
8036 else
8037#endif
8038 {
8039 ssl->out_ctr = ssl->out_hdr - 8;
8040 ssl->out_len = ssl->out_hdr + 3;
8041 ssl->out_iv = ssl->out_hdr + 5;
8042 }
8043
8044 /* Adjust out_msg to make space for explicit IV, if used. */
8045 if( transform != NULL &&
8046 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8047 {
8048 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8049 }
8050 else
8051 ssl->out_msg = ssl->out_iv;
8052}
8053
8054/* Once ssl->in_hdr as the address of the beginning of the
8055 * next incoming record is set, deduce the other pointers.
8056 *
8057 * Note: For TLS, we save the implicit record sequence number
8058 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8059 * and the caller has to make sure there's space for this.
8060 */
8061
Hanno Becker79594fd2019-05-08 09:38:41 +01008062static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008063{
Hanno Becker79594fd2019-05-08 09:38:41 +01008064 /* This function sets the pointers to match the case
8065 * of unprotected TLS/DTLS records, with both ssl->in_iv
8066 * and ssl->in_msg pointing to the beginning of the record
8067 * content.
8068 *
8069 * When decrypting a protected record, ssl->in_msg
8070 * will be shifted to point to the beginning of the
8071 * record plaintext.
8072 */
8073
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008074#if defined(MBEDTLS_SSL_PROTO_DTLS)
8075 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8076 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008077 /* This sets the header pointers to match records
8078 * without CID. When we receive a record containing
8079 * a CID, the fields are shifted accordingly in
8080 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008081 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008082#if defined(MBEDTLS_SSL_CID)
8083 ssl->in_cid = ssl->in_ctr + 8;
8084 ssl->in_len = ssl->in_cid; /* Default: no CID */
8085#else /* MBEDTLS_SSL_CID */
8086 ssl->in_len = ssl->in_ctr + 8;
8087#endif /* MBEDTLS_SSL_CID */
8088 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008089 }
8090 else
8091#endif
8092 {
8093 ssl->in_ctr = ssl->in_hdr - 8;
8094 ssl->in_len = ssl->in_hdr + 3;
8095 ssl->in_iv = ssl->in_hdr + 5;
8096 }
8097
Hanno Becker79594fd2019-05-08 09:38:41 +01008098 /* This will be adjusted at record decryption time. */
8099 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008100}
8101
Paul Bakker5121ce52009-01-03 21:22:43 +00008102/*
8103 * Initialize an SSL context
8104 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008105void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8106{
8107 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8108}
8109
8110/*
8111 * Setup an SSL context
8112 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008113
8114static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8115{
8116 /* Set the incoming and outgoing record pointers. */
8117#if defined(MBEDTLS_SSL_PROTO_DTLS)
8118 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8119 {
8120 ssl->out_hdr = ssl->out_buf;
8121 ssl->in_hdr = ssl->in_buf;
8122 }
8123 else
8124#endif /* MBEDTLS_SSL_PROTO_DTLS */
8125 {
8126 ssl->out_hdr = ssl->out_buf + 8;
8127 ssl->in_hdr = ssl->in_buf + 8;
8128 }
8129
8130 /* Derive other internal pointers. */
8131 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008132 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008133}
8134
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008135int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008136 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008137{
Paul Bakker48916f92012-09-16 19:57:18 +00008138 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008139
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008140 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008141
8142 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008143 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008144 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008145
8146 /* Set to NULL in case of an error condition */
8147 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008148
Angus Grattond8213d02016-05-25 20:56:48 +10008149 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8150 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008151 {
Angus Grattond8213d02016-05-25 20:56:48 +10008152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008153 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008154 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008155 }
8156
8157 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8158 if( ssl->out_buf == NULL )
8159 {
8160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008161 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008162 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008163 }
8164
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008165 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008166
Paul Bakker48916f92012-09-16 19:57:18 +00008167 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008168 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008169
8170 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008171
8172error:
8173 mbedtls_free( ssl->in_buf );
8174 mbedtls_free( ssl->out_buf );
8175
8176 ssl->conf = NULL;
8177
8178 ssl->in_buf = NULL;
8179 ssl->out_buf = NULL;
8180
8181 ssl->in_hdr = NULL;
8182 ssl->in_ctr = NULL;
8183 ssl->in_len = NULL;
8184 ssl->in_iv = NULL;
8185 ssl->in_msg = NULL;
8186
8187 ssl->out_hdr = NULL;
8188 ssl->out_ctr = NULL;
8189 ssl->out_len = NULL;
8190 ssl->out_iv = NULL;
8191 ssl->out_msg = NULL;
8192
k-stachowiak9f7798e2018-07-31 16:52:32 +02008193 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008194}
8195
8196/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008197 * Reset an initialized and used SSL context for re-use while retaining
8198 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008199 *
8200 * If partial is non-zero, keep data in the input buffer and client ID.
8201 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008202 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008203static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008204{
Paul Bakker48916f92012-09-16 19:57:18 +00008205 int ret;
8206
Hanno Becker7e772132018-08-10 12:38:21 +01008207#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8208 !defined(MBEDTLS_SSL_SRV_C)
8209 ((void) partial);
8210#endif
8211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008213
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008214 /* Cancel any possibly running timer */
8215 ssl_set_timer( ssl, 0 );
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#if defined(MBEDTLS_SSL_RENEGOTIATION)
8218 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008219 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008220
8221 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8223 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008226
Paul Bakker7eb013f2011-10-06 12:37:39 +00008227 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008228 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008229
8230 ssl->in_msgtype = 0;
8231 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008233 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008234 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008237 ssl_dtls_replay_reset( ssl );
8238#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008239
8240 ssl->in_hslen = 0;
8241 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008242
8243 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008244
8245 ssl->out_msgtype = 0;
8246 ssl->out_msglen = 0;
8247 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8249 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008250 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008251#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008252
Hanno Becker19859472018-08-06 09:40:20 +01008253 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8254
Paul Bakker48916f92012-09-16 19:57:18 +00008255 ssl->transform_in = NULL;
8256 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008257
Hanno Becker78640902018-08-13 16:35:15 +01008258 ssl->session_in = NULL;
8259 ssl->session_out = NULL;
8260
Angus Grattond8213d02016-05-25 20:56:48 +10008261 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008262
8263#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008264 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008265#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8266 {
8267 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008268 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008269 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8272 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8275 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8278 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008279 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008280 }
8281#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008282
Paul Bakker48916f92012-09-16 19:57:18 +00008283 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 mbedtls_ssl_transform_free( ssl->transform );
8286 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008287 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008288 }
Paul Bakker48916f92012-09-16 19:57:18 +00008289
Paul Bakkerc0463502013-02-14 11:19:38 +01008290 if( ssl->session )
8291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 mbedtls_ssl_session_free( ssl->session );
8293 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008294 ssl->session = NULL;
8295 }
8296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008298 ssl->alpn_chosen = NULL;
8299#endif
8300
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008301#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008302#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008303 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008304#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008305 {
8306 mbedtls_free( ssl->cli_id );
8307 ssl->cli_id = NULL;
8308 ssl->cli_id_len = 0;
8309 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008310#endif
8311
Paul Bakker48916f92012-09-16 19:57:18 +00008312 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8313 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008314
8315 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008316}
8317
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008318/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008319 * Reset an initialized and used SSL context for re-use while retaining
8320 * all application-set variables, function pointers and data.
8321 */
8322int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8323{
8324 return( ssl_session_reset_int( ssl, 0 ) );
8325}
8326
8327/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008328 * SSL set accessors
8329 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008330void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008331{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008332 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008333}
8334
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008335void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008336{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008337 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008338}
8339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008341void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008342{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008343 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008344}
8345#endif
8346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008348void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008349{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008350 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008351}
8352#endif
8353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008355
Hanno Becker1841b0a2018-08-24 11:13:57 +01008356void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8357 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008358{
8359 ssl->disable_datagram_packing = !allow_packing;
8360}
8361
8362void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8363 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008364{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008365 conf->hs_timeout_min = min;
8366 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008367}
8368#endif
8369
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008370void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008371{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008372 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008373}
8374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008376void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008377 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008378 void *p_vrfy )
8379{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008380 conf->f_vrfy = f_vrfy;
8381 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008384
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008385void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008386 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008387 void *p_rng )
8388{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008389 conf->f_rng = f_rng;
8390 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008391}
8392
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008394 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008395 void *p_dbg )
8396{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008397 conf->f_dbg = f_dbg;
8398 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008399}
8400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008402 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008403 mbedtls_ssl_send_t *f_send,
8404 mbedtls_ssl_recv_t *f_recv,
8405 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008406{
8407 ssl->p_bio = p_bio;
8408 ssl->f_send = f_send;
8409 ssl->f_recv = f_recv;
8410 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008411}
8412
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008413#if defined(MBEDTLS_SSL_PROTO_DTLS)
8414void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8415{
8416 ssl->mtu = mtu;
8417}
8418#endif
8419
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008420void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008421{
8422 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008423}
8424
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008425void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8426 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008427 mbedtls_ssl_set_timer_t *f_set_timer,
8428 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008429{
8430 ssl->p_timer = p_timer;
8431 ssl->f_set_timer = f_set_timer;
8432 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008433
8434 /* Make sure we start with no timer running */
8435 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008436}
8437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008439void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008440 void *p_cache,
8441 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8442 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008443{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008444 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008445 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008446 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450#if defined(MBEDTLS_SSL_CLI_C)
8451int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008452{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008453 int ret;
8454
8455 if( ssl == NULL ||
8456 session == NULL ||
8457 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008458 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008461 }
8462
Hanno Becker52055ae2019-02-06 14:30:46 +00008463 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8464 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008465 return( ret );
8466
Paul Bakker0a597072012-09-25 21:55:46 +00008467 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008468
8469 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008472
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008473void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008474 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008475{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008476 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8477 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8478 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8479 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008480}
8481
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008482void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008483 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008484 int major, int minor )
8485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008487 return;
8488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008490 return;
8491
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008492 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008493}
8494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008496void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008497 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008498{
8499 conf->cert_profile = profile;
8500}
8501
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008502/* Append a new keycert entry to a (possibly empty) list */
8503static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8504 mbedtls_x509_crt *cert,
8505 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008506{
niisato8ee24222018-06-25 19:05:48 +09008507 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008508
niisato8ee24222018-06-25 19:05:48 +09008509 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8510 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008511 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008512
niisato8ee24222018-06-25 19:05:48 +09008513 new_cert->cert = cert;
8514 new_cert->key = key;
8515 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008516
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008517 /* Update head is the list was null, else add to the end */
8518 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008519 {
niisato8ee24222018-06-25 19:05:48 +09008520 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008521 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008522 else
8523 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008524 mbedtls_ssl_key_cert *cur = *head;
8525 while( cur->next != NULL )
8526 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008527 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008528 }
8529
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008530 return( 0 );
8531}
8532
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008533int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008534 mbedtls_x509_crt *own_cert,
8535 mbedtls_pk_context *pk_key )
8536{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008537 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008538}
8539
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008540void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008541 mbedtls_x509_crt *ca_chain,
8542 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008543{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008544 conf->ca_chain = ca_chain;
8545 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008546
8547#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8548 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8549 * cannot be used together. */
8550 conf->f_ca_cb = NULL;
8551 conf->p_ca_cb = NULL;
8552#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008553}
Hanno Becker5adaad92019-03-27 16:54:37 +00008554
8555#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8556void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008557 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008558 void *p_ca_cb )
8559{
8560 conf->f_ca_cb = f_ca_cb;
8561 conf->p_ca_cb = p_ca_cb;
8562
8563 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8564 * cannot be used together. */
8565 conf->ca_chain = NULL;
8566 conf->ca_crl = NULL;
8567}
8568#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008570
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008571#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8572int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8573 mbedtls_x509_crt *own_cert,
8574 mbedtls_pk_context *pk_key )
8575{
8576 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8577 own_cert, pk_key ) );
8578}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008579
8580void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8581 mbedtls_x509_crt *ca_chain,
8582 mbedtls_x509_crl *ca_crl )
8583{
8584 ssl->handshake->sni_ca_chain = ca_chain;
8585 ssl->handshake->sni_ca_crl = ca_crl;
8586}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008587
8588void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8589 int authmode )
8590{
8591 ssl->handshake->sni_authmode = authmode;
8592}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008593#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8594
Hanno Becker8927c832019-04-03 12:52:50 +01008595#if defined(MBEDTLS_X509_CRT_PARSE_C)
8596void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8597 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8598 void *p_vrfy )
8599{
8600 ssl->f_vrfy = f_vrfy;
8601 ssl->p_vrfy = p_vrfy;
8602}
8603#endif
8604
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008605#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008606/*
8607 * Set EC J-PAKE password for current handshake
8608 */
8609int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8610 const unsigned char *pw,
8611 size_t pw_len )
8612{
8613 mbedtls_ecjpake_role role;
8614
Janos Follath8eb64132016-06-03 15:40:57 +01008615 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8617
8618 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8619 role = MBEDTLS_ECJPAKE_SERVER;
8620 else
8621 role = MBEDTLS_ECJPAKE_CLIENT;
8622
8623 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8624 role,
8625 MBEDTLS_MD_SHA256,
8626 MBEDTLS_ECP_DP_SECP256R1,
8627 pw, pw_len ) );
8628}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008629#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008632
8633static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8634{
8635 /* Remove reference to existing PSK, if any. */
8636#if defined(MBEDTLS_USE_PSA_CRYPTO)
8637 if( conf->psk_opaque != 0 )
8638 {
8639 /* The maintenance of the PSK key slot is the
8640 * user's responsibility. */
8641 conf->psk_opaque = 0;
8642 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008643 /* This and the following branch should never
8644 * be taken simultaenously as we maintain the
8645 * invariant that raw and opaque PSKs are never
8646 * configured simultaneously. As a safeguard,
8647 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008648#endif /* MBEDTLS_USE_PSA_CRYPTO */
8649 if( conf->psk != NULL )
8650 {
8651 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8652
8653 mbedtls_free( conf->psk );
8654 conf->psk = NULL;
8655 conf->psk_len = 0;
8656 }
8657
8658 /* Remove reference to PSK identity, if any. */
8659 if( conf->psk_identity != NULL )
8660 {
8661 mbedtls_free( conf->psk_identity );
8662 conf->psk_identity = NULL;
8663 conf->psk_identity_len = 0;
8664 }
8665}
8666
Hanno Becker7390c712018-11-15 13:33:04 +00008667/* This function assumes that PSK identity in the SSL config is unset.
8668 * It checks that the provided identity is well-formed and attempts
8669 * to make a copy of it in the SSL config.
8670 * On failure, the PSK identity in the config remains unset. */
8671static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8672 unsigned char const *psk_identity,
8673 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008674{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008675 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008676 if( psk_identity == NULL ||
8677 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008678 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008679 {
8680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8681 }
8682
Hanno Becker7390c712018-11-15 13:33:04 +00008683 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8684 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008685 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008686
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008687 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008688 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008689
8690 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008691}
8692
Hanno Becker7390c712018-11-15 13:33:04 +00008693int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8694 const unsigned char *psk, size_t psk_len,
8695 const unsigned char *psk_identity, size_t psk_identity_len )
8696{
8697 int ret;
8698 /* Remove opaque/raw PSK + PSK Identity */
8699 ssl_conf_remove_psk( conf );
8700
8701 /* Check and set raw PSK */
8702 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8704 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8705 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8706 conf->psk_len = psk_len;
8707 memcpy( conf->psk, psk, conf->psk_len );
8708
8709 /* Check and set PSK Identity */
8710 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8711 if( ret != 0 )
8712 ssl_conf_remove_psk( conf );
8713
8714 return( ret );
8715}
8716
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008717static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8718{
8719#if defined(MBEDTLS_USE_PSA_CRYPTO)
8720 if( ssl->handshake->psk_opaque != 0 )
8721 {
8722 ssl->handshake->psk_opaque = 0;
8723 }
8724 else
8725#endif /* MBEDTLS_USE_PSA_CRYPTO */
8726 if( ssl->handshake->psk != NULL )
8727 {
8728 mbedtls_platform_zeroize( ssl->handshake->psk,
8729 ssl->handshake->psk_len );
8730 mbedtls_free( ssl->handshake->psk );
8731 ssl->handshake->psk_len = 0;
8732 }
8733}
8734
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008735int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8736 const unsigned char *psk, size_t psk_len )
8737{
8738 if( psk == NULL || ssl->handshake == NULL )
8739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8740
8741 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8743
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008744 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008745
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008746 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008747 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008748
8749 ssl->handshake->psk_len = psk_len;
8750 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8751
8752 return( 0 );
8753}
8754
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008755#if defined(MBEDTLS_USE_PSA_CRYPTO)
8756int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008757 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008758 const unsigned char *psk_identity,
8759 size_t psk_identity_len )
8760{
Hanno Becker7390c712018-11-15 13:33:04 +00008761 int ret;
8762 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008763 ssl_conf_remove_psk( conf );
8764
Hanno Becker7390c712018-11-15 13:33:04 +00008765 /* Check and set opaque PSK */
8766 if( psk_slot == 0 )
8767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008768 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008769
8770 /* Check and set PSK Identity */
8771 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8772 psk_identity_len );
8773 if( ret != 0 )
8774 ssl_conf_remove_psk( conf );
8775
8776 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008777}
8778
8779int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008780 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008781{
8782 if( psk_slot == 0 || ssl->handshake == NULL )
8783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8784
8785 ssl_remove_psk( ssl );
8786 ssl->handshake->psk_opaque = psk_slot;
8787 return( 0 );
8788}
8789#endif /* MBEDTLS_USE_PSA_CRYPTO */
8790
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008791void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008793 size_t),
8794 void *p_psk )
8795{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008796 conf->f_psk = f_psk;
8797 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008798}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008800
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008801#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008802
8803#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008804int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008805{
8806 int ret;
8807
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008808 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8809 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8810 {
8811 mbedtls_mpi_free( &conf->dhm_P );
8812 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008813 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008815
8816 return( 0 );
8817}
Hanno Becker470a8c42017-10-04 15:28:46 +01008818#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008819
Hanno Beckera90658f2017-10-04 15:29:08 +01008820int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8821 const unsigned char *dhm_P, size_t P_len,
8822 const unsigned char *dhm_G, size_t G_len )
8823{
8824 int ret;
8825
8826 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8827 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8828 {
8829 mbedtls_mpi_free( &conf->dhm_P );
8830 mbedtls_mpi_free( &conf->dhm_G );
8831 return( ret );
8832 }
8833
8834 return( 0 );
8835}
Paul Bakker5121ce52009-01-03 21:22:43 +00008836
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008837int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008838{
8839 int ret;
8840
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008841 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8842 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8843 {
8844 mbedtls_mpi_free( &conf->dhm_P );
8845 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008846 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008847 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008848
8849 return( 0 );
8850}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008851#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008852
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008853#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8854/*
8855 * Set the minimum length for Diffie-Hellman parameters
8856 */
8857void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8858 unsigned int bitlen )
8859{
8860 conf->dhm_min_bitlen = bitlen;
8861}
8862#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8863
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008864#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008865/*
8866 * Set allowed/preferred hashes for handshake signatures
8867 */
8868void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8869 const int *hashes )
8870{
8871 conf->sig_hashes = hashes;
8872}
Hanno Becker947194e2017-04-07 13:25:49 +01008873#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008874
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008875#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008876/*
8877 * Set the allowed elliptic curves
8878 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008879void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008880 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008881{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008882 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008883}
Hanno Becker947194e2017-04-07 13:25:49 +01008884#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008885
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008886#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008888{
Hanno Becker947194e2017-04-07 13:25:49 +01008889 /* Initialize to suppress unnecessary compiler warning */
8890 size_t hostname_len = 0;
8891
8892 /* Check if new hostname is valid before
8893 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008894 if( hostname != NULL )
8895 {
8896 hostname_len = strlen( hostname );
8897
8898 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8900 }
8901
8902 /* Now it's clear that we will overwrite the old hostname,
8903 * so we can free it safely */
8904
8905 if( ssl->hostname != NULL )
8906 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008907 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008908 mbedtls_free( ssl->hostname );
8909 }
8910
8911 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008912
Paul Bakker5121ce52009-01-03 21:22:43 +00008913 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008914 {
8915 ssl->hostname = NULL;
8916 }
8917 else
8918 {
8919 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008920 if( ssl->hostname == NULL )
8921 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008922
Hanno Becker947194e2017-04-07 13:25:49 +01008923 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008924
Hanno Becker947194e2017-04-07 13:25:49 +01008925 ssl->hostname[hostname_len] = '\0';
8926 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008927
8928 return( 0 );
8929}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008930#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008931
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008932#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008933void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008935 const unsigned char *, size_t),
8936 void *p_sni )
8937{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008938 conf->f_sni = f_sni;
8939 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008944int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008945{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008946 size_t cur_len, tot_len;
8947 const char **p;
8948
8949 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008950 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8951 * MUST NOT be truncated."
8952 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008953 */
8954 tot_len = 0;
8955 for( p = protos; *p != NULL; p++ )
8956 {
8957 cur_len = strlen( *p );
8958 tot_len += cur_len;
8959
8960 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008962 }
8963
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008964 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008965
8966 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008967}
8968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008970{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008971 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008974
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008975void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008976{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008977 conf->max_major_ver = major;
8978 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008979}
8980
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008981void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008982{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008983 conf->min_major_ver = major;
8984 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008985}
8986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008988void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008989{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008990 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008991}
8992#endif
8993
Janos Follath088ce432017-04-10 12:42:31 +01008994#if defined(MBEDTLS_SSL_SRV_C)
8995void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8996 char cert_req_ca_list )
8997{
8998 conf->cert_req_ca_list = cert_req_ca_list;
8999}
9000#endif
9001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009003void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009004{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009005 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009006}
9007#endif
9008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009010void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009011{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009012 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009013}
9014#endif
9015
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009016#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009017void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009018{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009019 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009020}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009021#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009024int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009027 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009030 }
9031
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009032 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009033
9034 return( 0 );
9035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009039void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009040{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009041 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009046void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009047{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009048 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009049}
9050#endif
9051
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009052void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009054 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009055}
9056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009058void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009060 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009061}
9062
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009063void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009064{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009065 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009066}
9067
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009068void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009069 const unsigned char period[8] )
9070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009071 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009076#if defined(MBEDTLS_SSL_CLI_C)
9077void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009078{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009079 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009080}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009081#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009082
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009083#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009084void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9085 mbedtls_ssl_ticket_write_t *f_ticket_write,
9086 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9087 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009088{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009089 conf->f_ticket_write = f_ticket_write;
9090 conf->f_ticket_parse = f_ticket_parse;
9091 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009092}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009095
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009096#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9097void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9098 mbedtls_ssl_export_keys_t *f_export_keys,
9099 void *p_export_keys )
9100{
9101 conf->f_export_keys = f_export_keys;
9102 conf->p_export_keys = p_export_keys;
9103}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009104
9105void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9106 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9107 void *p_export_keys )
9108{
9109 conf->f_export_keys_ext = f_export_keys_ext;
9110 conf->p_export_keys = p_export_keys;
9111}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009112#endif
9113
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009114#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009115void mbedtls_ssl_conf_async_private_cb(
9116 mbedtls_ssl_config *conf,
9117 mbedtls_ssl_async_sign_t *f_async_sign,
9118 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9119 mbedtls_ssl_async_resume_t *f_async_resume,
9120 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009121 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009122{
9123 conf->f_async_sign_start = f_async_sign;
9124 conf->f_async_decrypt_start = f_async_decrypt;
9125 conf->f_async_resume = f_async_resume;
9126 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009127 conf->p_async_config_data = async_config_data;
9128}
9129
Gilles Peskine8f97af72018-04-26 11:46:10 +02009130void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9131{
9132 return( conf->p_async_config_data );
9133}
9134
Gilles Peskine1febfef2018-04-30 11:54:39 +02009135void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009136{
9137 if( ssl->handshake == NULL )
9138 return( NULL );
9139 else
9140 return( ssl->handshake->user_async_ctx );
9141}
9142
Gilles Peskine1febfef2018-04-30 11:54:39 +02009143void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009144 void *ctx )
9145{
9146 if( ssl->handshake != NULL )
9147 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009148}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009149#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009150
Paul Bakker5121ce52009-01-03 21:22:43 +00009151/*
9152 * SSL get accessors
9153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009155{
9156 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9157}
9158
Hanno Becker8b170a02017-10-10 11:51:19 +01009159int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9160{
9161 /*
9162 * Case A: We're currently holding back
9163 * a message for further processing.
9164 */
9165
9166 if( ssl->keep_current_message == 1 )
9167 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009169 return( 1 );
9170 }
9171
9172 /*
9173 * Case B: Further records are pending in the current datagram.
9174 */
9175
9176#if defined(MBEDTLS_SSL_PROTO_DTLS)
9177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9178 ssl->in_left > ssl->next_record_offset )
9179 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009181 return( 1 );
9182 }
9183#endif /* MBEDTLS_SSL_PROTO_DTLS */
9184
9185 /*
9186 * Case C: A handshake message is being processed.
9187 */
9188
Hanno Becker8b170a02017-10-10 11:51:19 +01009189 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9190 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009192 return( 1 );
9193 }
9194
9195 /*
9196 * Case D: An application data message is being processed
9197 */
9198 if( ssl->in_offt != NULL )
9199 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009201 return( 1 );
9202 }
9203
9204 /*
9205 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009206 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009207 * we implement support for multiple alerts in single records.
9208 */
9209
9210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9211 return( 0 );
9212}
9213
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009214uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009215{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009216 if( ssl->session != NULL )
9217 return( ssl->session->verify_result );
9218
9219 if( ssl->session_negotiate != NULL )
9220 return( ssl->session_negotiate->verify_result );
9221
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009222 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009223}
9224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009226{
Paul Bakker926c8e42013-03-06 10:23:34 +01009227 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009228 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009231}
9232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009237 {
9238 switch( ssl->minor_ver )
9239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009240 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009241 return( "DTLSv1.0" );
9242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009244 return( "DTLSv1.2" );
9245
9246 default:
9247 return( "unknown (DTLS)" );
9248 }
9249 }
9250#endif
9251
Paul Bakker43ca69c2011-01-15 17:35:19 +00009252 switch( ssl->minor_ver )
9253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009255 return( "SSLv3.0" );
9256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009258 return( "TLSv1.0" );
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009261 return( "TLSv1.1" );
9262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009264 return( "TLSv1.2" );
9265
Paul Bakker43ca69c2011-01-15 17:35:19 +00009266 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009267 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009268 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009269}
9270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009272{
Hanno Becker3136ede2018-08-17 15:28:19 +01009273 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009275 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009276
Hanno Becker5903de42019-05-03 14:46:38 +01009277 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9278
Hanno Becker78640902018-08-13 16:35:15 +01009279 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009280 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282#if defined(MBEDTLS_ZLIB_SUPPORT)
9283 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9284 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009285#endif
9286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 case MBEDTLS_MODE_GCM:
9290 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009291 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009293 transform_expansion = transform->minlen;
9294 break;
9295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009297
9298 block_size = mbedtls_cipher_get_block_size(
9299 &transform->cipher_ctx_enc );
9300
Hanno Becker3136ede2018-08-17 15:28:19 +01009301 /* Expansion due to the addition of the MAC. */
9302 transform_expansion += transform->maclen;
9303
9304 /* Expansion due to the addition of CBC padding;
9305 * Theoretically up to 256 bytes, but we never use
9306 * more than the block size of the underlying cipher. */
9307 transform_expansion += block_size;
9308
9309 /* For TLS 1.1 or higher, an explicit IV is added
9310 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009311#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9312 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009313 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009314#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009315
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009316 break;
9317
9318 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009321 }
9322
Hanno Becker6cbad552019-05-08 15:40:11 +01009323#if defined(MBEDTLS_SSL_CID)
9324 if( transform->out_cid_len != 0 )
9325 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
9326#endif /* MBEDTLS_SSL_CID */
9327
Hanno Becker5903de42019-05-03 14:46:38 +01009328 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009329}
9330
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009331#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9332size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9333{
9334 size_t max_len;
9335
9336 /*
9337 * Assume mfl_code is correct since it was checked when set
9338 */
Angus Grattond8213d02016-05-25 20:56:48 +10009339 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009340
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009341 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009342 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009343 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009344 {
Angus Grattond8213d02016-05-25 20:56:48 +10009345 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009346 }
9347
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009348 /* During a handshake, use the value being negotiated */
9349 if( ssl->session_negotiate != NULL &&
9350 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9351 {
9352 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9353 }
9354
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009355 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009356}
9357#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9358
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009359#if defined(MBEDTLS_SSL_PROTO_DTLS)
9360static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9361{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009362 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9364 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9365 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9366 return ( 0 );
9367
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009368 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9369 return( ssl->mtu );
9370
9371 if( ssl->mtu == 0 )
9372 return( ssl->handshake->mtu );
9373
9374 return( ssl->mtu < ssl->handshake->mtu ?
9375 ssl->mtu : ssl->handshake->mtu );
9376}
9377#endif /* MBEDTLS_SSL_PROTO_DTLS */
9378
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009379int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9380{
9381 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9382
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009383#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9384 !defined(MBEDTLS_SSL_PROTO_DTLS)
9385 (void) ssl;
9386#endif
9387
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009388#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9389 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9390
9391 if( max_len > mfl )
9392 max_len = mfl;
9393#endif
9394
9395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009396 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009397 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009398 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009399 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9400 const size_t overhead = (size_t) ret;
9401
9402 if( ret < 0 )
9403 return( ret );
9404
9405 if( mtu <= overhead )
9406 {
9407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9408 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9409 }
9410
9411 if( max_len > mtu - overhead )
9412 max_len = mtu - overhead;
9413 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009414#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009415
Hanno Becker0defedb2018-08-10 12:35:02 +01009416#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9417 !defined(MBEDTLS_SSL_PROTO_DTLS)
9418 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009419#endif
9420
9421 return( (int) max_len );
9422}
9423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424#if defined(MBEDTLS_X509_CRT_PARSE_C)
9425const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009426{
9427 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009428 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009429
Hanno Beckere6824572019-02-07 13:18:46 +00009430#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009431 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009432#else
9433 return( NULL );
9434#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009439int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9440 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009441{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009442 if( ssl == NULL ||
9443 dst == NULL ||
9444 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009445 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009448 }
9449
Hanno Becker52055ae2019-02-06 14:30:46 +00009450 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009453
Paul Bakker5121ce52009-01-03 21:22:43 +00009454/*
Paul Bakker1961b702013-01-25 14:49:24 +01009455 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009460
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009461 if( ssl == NULL || ssl->conf == NULL )
9462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009465 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009469 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009471#endif
9472
Paul Bakker1961b702013-01-25 14:49:24 +01009473 return( ret );
9474}
9475
9476/*
9477 * Perform the SSL handshake
9478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009480{
9481 int ret = 0;
9482
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009483 if( ssl == NULL || ssl->conf == NULL )
9484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009491
9492 if( ret != 0 )
9493 break;
9494 }
9495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009497
9498 return( ret );
9499}
9500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#if defined(MBEDTLS_SSL_RENEGOTIATION)
9502#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009503/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009504 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009507{
9508 int ret;
9509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009511
9512 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9514 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009515
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009516 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009517 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009519 return( ret );
9520 }
9521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009523
9524 return( 0 );
9525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009527
9528/*
9529 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 * - any side: calling mbedtls_ssl_renegotiate(),
9531 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9532 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009533 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009534 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009538{
9539 int ret;
9540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009542
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009543 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9544 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009545
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009546 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9547 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009549 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009551 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009553 ssl->handshake->out_msg_seq = 1;
9554 else
9555 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009556 }
9557#endif
9558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9560 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009565 return( ret );
9566 }
9567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009569
9570 return( 0 );
9571}
9572
9573/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009574 * Renegotiate current connection on client,
9575 * or request renegotiation on server
9576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009580
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009581 if( ssl == NULL || ssl->conf == NULL )
9582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009585 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009586 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009592
9593 /* Did we already try/start sending HelloRequest? */
9594 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009596
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009597 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009602 /*
9603 * On client, either start the renegotiation process or,
9604 * if already in progress, continue the handshake
9605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009610
9611 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009614 return( ret );
9615 }
9616 }
9617 else
9618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009622 return( ret );
9623 }
9624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009626
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009627 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009628}
9629
9630/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009631 * Check record counters and renegotiate if they're above the limit.
9632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009634{
Andres AG2196c7f2016-12-15 17:01:16 +00009635 size_t ep_len = ssl_ep_len( ssl );
9636 int in_ctr_cmp;
9637 int out_ctr_cmp;
9638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9640 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009641 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009642 {
9643 return( 0 );
9644 }
9645
Andres AG2196c7f2016-12-15 17:01:16 +00009646 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9647 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009648 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009649 ssl->conf->renego_period + ep_len, 8 - ep_len );
9650
9651 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009652 {
9653 return( 0 );
9654 }
9655
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009660
9661/*
9662 * Receive application data decrypted from the SSL layer
9663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009665{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009666 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009667 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009668
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009669 if( ssl == NULL || ssl->conf == NULL )
9670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009678 return( ret );
9679
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009680 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009682 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009683 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009684 return( ret );
9685 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009686 }
9687#endif
9688
Hanno Becker4a810fb2017-05-24 16:27:30 +01009689 /*
9690 * Check if renegotiation is necessary and/or handshake is
9691 * in process. If yes, perform/continue, and fall through
9692 * if an unexpected packet is received while the client
9693 * is waiting for the ServerHello.
9694 *
9695 * (There is no equivalent to the last condition on
9696 * the server-side as it is not treated as within
9697 * a handshake while waiting for the ClientHello
9698 * after a renegotiation request.)
9699 */
9700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009702 ret = ssl_check_ctr_renegotiate( ssl );
9703 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9704 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009707 return( ret );
9708 }
9709#endif
9710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009714 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9715 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009718 return( ret );
9719 }
9720 }
9721
Hanno Beckere41158b2017-10-23 13:30:32 +01009722 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009723 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009724 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009725 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009726 if( ssl->f_get_timer != NULL &&
9727 ssl->f_get_timer( ssl->p_timer ) == -1 )
9728 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009729 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009730 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009731
Hanno Becker327c93b2018-08-15 13:56:18 +01009732 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009733 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009734 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9735 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009736
Hanno Becker4a810fb2017-05-24 16:27:30 +01009737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9738 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009739 }
9740
9741 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009743 {
9744 /*
9745 * OpenSSL sends empty messages to randomize the IV
9746 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009747 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009750 return( 0 );
9751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009753 return( ret );
9754 }
9755 }
9756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009760
Hanno Becker4a810fb2017-05-24 16:27:30 +01009761 /*
9762 * - For client-side, expect SERVER_HELLO_REQUEST.
9763 * - For server-side, expect CLIENT_HELLO.
9764 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9765 */
9766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009768 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009770 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009773
9774 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009777 {
9778 continue;
9779 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009782 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009783#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009784
Hanno Becker4a810fb2017-05-24 16:27:30 +01009785#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009786 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009790
9791 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009794 {
9795 continue;
9796 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009799 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009800#endif /* MBEDTLS_SSL_SRV_C */
9801
Hanno Becker21df7f92017-10-17 11:03:26 +01009802#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009803 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009804 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9805 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9806 ssl->conf->allow_legacy_renegotiation ==
9807 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9808 {
9809 /*
9810 * Accept renegotiation request
9811 */
Paul Bakker48916f92012-09-16 19:57:18 +00009812
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009813 /* DTLS clients need to know renego is server-initiated */
9814#if defined(MBEDTLS_SSL_PROTO_DTLS)
9815 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9816 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9817 {
9818 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9819 }
9820#endif
9821 ret = ssl_start_renegotiation( ssl );
9822 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9823 ret != 0 )
9824 {
9825 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9826 return( ret );
9827 }
9828 }
9829 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009830#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009831 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009832 /*
9833 * Refuse renegotiation
9834 */
9835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838#if defined(MBEDTLS_SSL_PROTO_SSL3)
9839 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009840 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009841 /* SSLv3 does not have a "no_renegotiation" warning, so
9842 we send a fatal alert and abort the connection. */
9843 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9844 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9845 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009846 }
9847 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9849#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9850 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9851 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9854 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9855 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009856 {
9857 return( ret );
9858 }
Paul Bakker48916f92012-09-16 19:57:18 +00009859 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009860 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9862 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009866 }
Paul Bakker48916f92012-09-16 19:57:18 +00009867 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009868
Hanno Becker90333da2017-10-10 11:27:13 +01009869 /* At this point, we don't know whether the renegotiation has been
9870 * completed or not. The cases to consider are the following:
9871 * 1) The renegotiation is complete. In this case, no new record
9872 * has been read yet.
9873 * 2) The renegotiation is incomplete because the client received
9874 * an application data record while awaiting the ServerHello.
9875 * 3) The renegotiation is incomplete because the client received
9876 * a non-handshake, non-application data message while awaiting
9877 * the ServerHello.
9878 * In each of these case, looping will be the proper action:
9879 * - For 1), the next iteration will read a new record and check
9880 * if it's application data.
9881 * - For 2), the loop condition isn't satisfied as application data
9882 * is present, hence continue is the same as break
9883 * - For 3), the loop condition is satisfied and read_record
9884 * will re-deliver the message that was held back by the client
9885 * when expecting the ServerHello.
9886 */
9887 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009888 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009889#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009891 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009892 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009893 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009894 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009897 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009899 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009900 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009901 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9905 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009908 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009909 }
9910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9914 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009915 }
9916
9917 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009918
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009919 /* We're going to return something now, cancel timer,
9920 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009922 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009923
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009924#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009925 /* If we requested renego but received AppData, resend HelloRequest.
9926 * Do it now, after setting in_offt, to avoid taking this branch
9927 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009929 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009931 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009932 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009935 return( ret );
9936 }
9937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009939#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009940 }
9941
9942 n = ( len < ssl->in_msglen )
9943 ? len : ssl->in_msglen;
9944
9945 memcpy( buf, ssl->in_offt, n );
9946 ssl->in_msglen -= n;
9947
9948 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009949 {
9950 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009951 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009952 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009953 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009955 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009956 /* more data available */
9957 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009961
Paul Bakker23986e52011-04-24 08:57:21 +00009962 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009963}
9964
9965/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009966 * Send application data to be encrypted by the SSL layer, taking care of max
9967 * fragment length and buffer size.
9968 *
9969 * According to RFC 5246 Section 6.2.1:
9970 *
9971 * Zero-length fragments of Application data MAY be sent as they are
9972 * potentially useful as a traffic analysis countermeasure.
9973 *
9974 * Therefore, it is possible that the input message length is 0 and the
9975 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009976 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009977static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009978 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009979{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009980 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9981 const size_t max_len = (size_t) ret;
9982
9983 if( ret < 0 )
9984 {
9985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9986 return( ret );
9987 }
9988
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009989 if( len > max_len )
9990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009991#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009992 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009995 "maximum fragment length: %d > %d",
9996 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009998 }
9999 else
10000#endif
10001 len = max_len;
10002 }
Paul Bakker887bd502011-06-08 13:10:54 +000010003
Paul Bakker5121ce52009-01-03 21:22:43 +000010004 if( ssl->out_left != 0 )
10005 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010006 /*
10007 * The user has previously tried to send the data and
10008 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10009 * written. In this case, we expect the high-level write function
10010 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010015 return( ret );
10016 }
10017 }
Paul Bakker887bd502011-06-08 13:10:54 +000010018 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010019 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010020 /*
10021 * The user is trying to send a message the first time, so we need to
10022 * copy the data into the internal buffers and setup the data structure
10023 * to keep track of partial writes
10024 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010025 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010027 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010028
Hanno Becker67bc7c32018-08-06 11:33:50 +010010029 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010032 return( ret );
10033 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010034 }
10035
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010036 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010037}
10038
10039/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010040 * Write application data, doing 1/n-1 splitting if necessary.
10041 *
10042 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010043 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010044 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010047static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010048 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010049{
10050 int ret;
10051
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010052 if( ssl->conf->cbc_record_splitting ==
10053 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010054 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10056 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10057 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010058 {
10059 return( ssl_write_real( ssl, buf, len ) );
10060 }
10061
10062 if( ssl->split_done == 0 )
10063 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010064 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010065 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010066 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010067 }
10068
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010069 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10070 return( ret );
10071 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010072
10073 return( ret + 1 );
10074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010076
10077/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010078 * Write application data (public-facing wrapper)
10079 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010080int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010081{
10082 int ret;
10083
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010085
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010086 if( ssl == NULL || ssl->conf == NULL )
10087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10088
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010089#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010090 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10091 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010092 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010093 return( ret );
10094 }
10095#endif
10096
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010097 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010098 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010099 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010100 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010102 return( ret );
10103 }
10104 }
10105
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010106#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010107 ret = ssl_write_split( ssl, buf, len );
10108#else
10109 ret = ssl_write_real( ssl, buf, len );
10110#endif
10111
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010113
10114 return( ret );
10115}
10116
10117/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010118 * Notify the peer that the connection is being closed
10119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010121{
10122 int ret;
10123
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010124 if( ssl == NULL || ssl->conf == NULL )
10125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010128
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010129 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10135 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10136 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010139 return( ret );
10140 }
10141 }
10142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010144
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010145 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010146}
10147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010149{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010150 if( transform == NULL )
10151 return;
10152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010154 deflateEnd( &transform->ctx_deflate );
10155 inflateEnd( &transform->ctx_inflate );
10156#endif
10157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010158 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10159 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010160
Hanno Beckerd56ed242018-01-03 15:32:51 +000010161#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010162 mbedtls_md_free( &transform->md_ctx_enc );
10163 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010164#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010165
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010166 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010167}
10168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169#if defined(MBEDTLS_X509_CRT_PARSE_C)
10170static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010173
10174 while( cur != NULL )
10175 {
10176 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010178 cur = next;
10179 }
10180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010182
Hanno Becker0271f962018-08-16 13:23:47 +010010183#if defined(MBEDTLS_SSL_PROTO_DTLS)
10184
10185static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10186{
10187 unsigned offset;
10188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10189
10190 if( hs == NULL )
10191 return;
10192
Hanno Becker283f5ef2018-08-24 09:34:47 +010010193 ssl_free_buffered_record( ssl );
10194
Hanno Becker0271f962018-08-16 13:23:47 +010010195 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010196 ssl_buffering_free_slot( ssl, offset );
10197}
10198
10199static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10200 uint8_t slot )
10201{
10202 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10203 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010204
10205 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10206 return;
10207
Hanno Beckere605b192018-08-21 15:59:07 +010010208 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010209 {
Hanno Beckere605b192018-08-21 15:59:07 +010010210 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010211 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010212 mbedtls_free( hs_buf->data );
10213 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010214 }
10215}
10216
10217#endif /* MBEDTLS_SSL_PROTO_DTLS */
10218
Gilles Peskine9b562d52018-04-25 20:32:43 +020010219void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010220{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010221 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10222
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010223 if( handshake == NULL )
10224 return;
10225
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010226#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10227 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10228 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010229 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010230 handshake->async_in_progress = 0;
10231 }
10232#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10233
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010234#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10235 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10236 mbedtls_md5_free( &handshake->fin_md5 );
10237 mbedtls_sha1_free( &handshake->fin_sha1 );
10238#endif
10239#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10240#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010241#if defined(MBEDTLS_USE_PSA_CRYPTO)
10242 psa_hash_abort( &handshake->fin_sha256_psa );
10243#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010244 mbedtls_sha256_free( &handshake->fin_sha256 );
10245#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010246#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010247#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010248#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010249 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010250#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010251 mbedtls_sha512_free( &handshake->fin_sha512 );
10252#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010253#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010254#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256#if defined(MBEDTLS_DHM_C)
10257 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259#if defined(MBEDTLS_ECDH_C)
10260 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010261#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010262#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010263 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010264#if defined(MBEDTLS_SSL_CLI_C)
10265 mbedtls_free( handshake->ecjpake_cache );
10266 handshake->ecjpake_cache = NULL;
10267 handshake->ecjpake_cache_len = 0;
10268#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010269#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010270
Janos Follath4ae5c292016-02-10 11:27:43 +000010271#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10272 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010273 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010275#endif
10276
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010277#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10278 if( handshake->psk != NULL )
10279 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010280 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010281 mbedtls_free( handshake->psk );
10282 }
10283#endif
10284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10286 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010287 /*
10288 * Free only the linked list wrapper, not the keys themselves
10289 * since the belong to the SNI callback
10290 */
10291 if( handshake->sni_key_cert != NULL )
10292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010294
10295 while( cur != NULL )
10296 {
10297 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010299 cur = next;
10300 }
10301 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010303
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010304#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010305 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010306 if( handshake->ecrs_peer_cert != NULL )
10307 {
10308 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10309 mbedtls_free( handshake->ecrs_peer_cert );
10310 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010311#endif
10312
Hanno Becker75173122019-02-06 16:18:31 +000010313#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10314 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10315 mbedtls_pk_free( &handshake->peer_pubkey );
10316#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318#if defined(MBEDTLS_SSL_PROTO_DTLS)
10319 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010320 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010321 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010322#endif
10323
Hanno Becker4a63ed42019-01-08 11:39:35 +000010324#if defined(MBEDTLS_ECDH_C) && \
10325 defined(MBEDTLS_USE_PSA_CRYPTO)
10326 psa_destroy_key( handshake->ecdh_psa_privkey );
10327#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10328
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010329 mbedtls_platform_zeroize( handshake,
10330 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010331}
10332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010334{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010335 if( session == NULL )
10336 return;
10337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010339 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010340#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010341
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010342#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010344#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010346 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010347}
10348
Paul Bakker5121ce52009-01-03 21:22:43 +000010349/*
10350 * Free an SSL context
10351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010353{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010354 if( ssl == NULL )
10355 return;
10356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010358
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010359 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010360 {
Angus Grattond8213d02016-05-25 20:56:48 +100010361 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010363 }
10364
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010365 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010366 {
Angus Grattond8213d02016-05-25 20:56:48 +100010367 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010369 }
10370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010372 if( ssl->compress_buf != NULL )
10373 {
Angus Grattond8213d02016-05-25 20:56:48 +100010374 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010376 }
10377#endif
10378
Paul Bakker48916f92012-09-16 19:57:18 +000010379 if( ssl->transform )
10380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381 mbedtls_ssl_transform_free( ssl->transform );
10382 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010383 }
10384
10385 if( ssl->handshake )
10386 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010387 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10389 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391 mbedtls_free( ssl->handshake );
10392 mbedtls_free( ssl->transform_negotiate );
10393 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010394 }
10395
Paul Bakkerc0463502013-02-14 11:19:38 +010010396 if( ssl->session )
10397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398 mbedtls_ssl_session_free( ssl->session );
10399 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010400 }
10401
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010402#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010403 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010404 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010405 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010407 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010408#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10411 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10414 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010415 }
10416#endif
10417
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010418#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010420#endif
10421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010423
Paul Bakker86f04f42013-02-14 11:20:09 +010010424 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010425 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010426}
10427
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010428/*
10429 * Initialze mbedtls_ssl_config
10430 */
10431void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10432{
10433 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10434}
10435
Simon Butcherc97b6972015-12-27 23:48:17 +000010436#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010437static int ssl_preset_default_hashes[] = {
10438#if defined(MBEDTLS_SHA512_C)
10439 MBEDTLS_MD_SHA512,
10440 MBEDTLS_MD_SHA384,
10441#endif
10442#if defined(MBEDTLS_SHA256_C)
10443 MBEDTLS_MD_SHA256,
10444 MBEDTLS_MD_SHA224,
10445#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010446#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010447 MBEDTLS_MD_SHA1,
10448#endif
10449 MBEDTLS_MD_NONE
10450};
Simon Butcherc97b6972015-12-27 23:48:17 +000010451#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010452
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010453static int ssl_preset_suiteb_ciphersuites[] = {
10454 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10455 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10456 0
10457};
10458
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010459#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010460static int ssl_preset_suiteb_hashes[] = {
10461 MBEDTLS_MD_SHA256,
10462 MBEDTLS_MD_SHA384,
10463 MBEDTLS_MD_NONE
10464};
10465#endif
10466
10467#if defined(MBEDTLS_ECP_C)
10468static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10469 MBEDTLS_ECP_DP_SECP256R1,
10470 MBEDTLS_ECP_DP_SECP384R1,
10471 MBEDTLS_ECP_DP_NONE
10472};
10473#endif
10474
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010475/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010476 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010477 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010478int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010479 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010480{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010481#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010482 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010483#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010484
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010485 /* Use the functions here so that they are covered in tests,
10486 * but otherwise access member directly for efficiency */
10487 mbedtls_ssl_conf_endpoint( conf, endpoint );
10488 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010489
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010490 /*
10491 * Things that are common to all presets
10492 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010493#if defined(MBEDTLS_SSL_CLI_C)
10494 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10495 {
10496 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10497#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10498 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10499#endif
10500 }
10501#endif
10502
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010503#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010504 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010505#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010506
10507#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10508 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10509#endif
10510
10511#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10512 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10513#endif
10514
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010515#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10516 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10517#endif
10518
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010519#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010520 conf->f_cookie_write = ssl_cookie_write_dummy;
10521 conf->f_cookie_check = ssl_cookie_check_dummy;
10522#endif
10523
10524#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10525 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10526#endif
10527
Janos Follath088ce432017-04-10 12:42:31 +010010528#if defined(MBEDTLS_SSL_SRV_C)
10529 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10530#endif
10531
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010532#if defined(MBEDTLS_SSL_PROTO_DTLS)
10533 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10534 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10535#endif
10536
10537#if defined(MBEDTLS_SSL_RENEGOTIATION)
10538 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010539 memset( conf->renego_period, 0x00, 2 );
10540 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010541#endif
10542
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010543#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10544 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10545 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010546 const unsigned char dhm_p[] =
10547 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10548 const unsigned char dhm_g[] =
10549 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10550
Hanno Beckera90658f2017-10-04 15:29:08 +010010551 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10552 dhm_p, sizeof( dhm_p ),
10553 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010554 {
10555 return( ret );
10556 }
10557 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010558#endif
10559
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010560 /*
10561 * Preset-specific defaults
10562 */
10563 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010564 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010565 /*
10566 * NSA Suite B
10567 */
10568 case MBEDTLS_SSL_PRESET_SUITEB:
10569 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10570 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10571 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10572 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10573
10574 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10575 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10576 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10577 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10578 ssl_preset_suiteb_ciphersuites;
10579
10580#if defined(MBEDTLS_X509_CRT_PARSE_C)
10581 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010582#endif
10583
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010584#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010585 conf->sig_hashes = ssl_preset_suiteb_hashes;
10586#endif
10587
10588#if defined(MBEDTLS_ECP_C)
10589 conf->curve_list = ssl_preset_suiteb_curves;
10590#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010591 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010592
10593 /*
10594 * Default
10595 */
10596 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010597 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10598 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10599 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10600 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10601 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10602 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10603 MBEDTLS_SSL_MIN_MINOR_VERSION :
10604 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010605 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10606 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10607
10608#if defined(MBEDTLS_SSL_PROTO_DTLS)
10609 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10610 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10611#endif
10612
10613 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10614 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10615 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10616 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10617 mbedtls_ssl_list_ciphersuites();
10618
10619#if defined(MBEDTLS_X509_CRT_PARSE_C)
10620 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10621#endif
10622
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010623#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010624 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010625#endif
10626
10627#if defined(MBEDTLS_ECP_C)
10628 conf->curve_list = mbedtls_ecp_grp_id_list();
10629#endif
10630
10631#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10632 conf->dhm_min_bitlen = 1024;
10633#endif
10634 }
10635
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010636 return( 0 );
10637}
10638
10639/*
10640 * Free mbedtls_ssl_config
10641 */
10642void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10643{
10644#if defined(MBEDTLS_DHM_C)
10645 mbedtls_mpi_free( &conf->dhm_P );
10646 mbedtls_mpi_free( &conf->dhm_G );
10647#endif
10648
10649#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10650 if( conf->psk != NULL )
10651 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010652 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010653 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010654 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010655 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010656 }
10657
10658 if( conf->psk_identity != NULL )
10659 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010660 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010661 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010662 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010663 conf->psk_identity_len = 0;
10664 }
10665#endif
10666
10667#if defined(MBEDTLS_X509_CRT_PARSE_C)
10668 ssl_key_cert_free( conf->key_cert );
10669#endif
10670
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010671 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010672}
10673
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010674#if defined(MBEDTLS_PK_C) && \
10675 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010676/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010680{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681#if defined(MBEDTLS_RSA_C)
10682 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10683 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010684#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685#if defined(MBEDTLS_ECDSA_C)
10686 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10687 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010688#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010690}
10691
Hanno Becker7e5437a2017-04-28 17:15:26 +010010692unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10693{
10694 switch( type ) {
10695 case MBEDTLS_PK_RSA:
10696 return( MBEDTLS_SSL_SIG_RSA );
10697 case MBEDTLS_PK_ECDSA:
10698 case MBEDTLS_PK_ECKEY:
10699 return( MBEDTLS_SSL_SIG_ECDSA );
10700 default:
10701 return( MBEDTLS_SSL_SIG_ANON );
10702 }
10703}
10704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010706{
10707 switch( sig )
10708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709#if defined(MBEDTLS_RSA_C)
10710 case MBEDTLS_SSL_SIG_RSA:
10711 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713#if defined(MBEDTLS_ECDSA_C)
10714 case MBEDTLS_SSL_SIG_ECDSA:
10715 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010716#endif
10717 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010719 }
10720}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010721#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010722
Hanno Becker7e5437a2017-04-28 17:15:26 +010010723#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10724 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10725
10726/* Find an entry in a signature-hash set matching a given hash algorithm. */
10727mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10728 mbedtls_pk_type_t sig_alg )
10729{
10730 switch( sig_alg )
10731 {
10732 case MBEDTLS_PK_RSA:
10733 return( set->rsa );
10734 case MBEDTLS_PK_ECDSA:
10735 return( set->ecdsa );
10736 default:
10737 return( MBEDTLS_MD_NONE );
10738 }
10739}
10740
10741/* Add a signature-hash-pair to a signature-hash set */
10742void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10743 mbedtls_pk_type_t sig_alg,
10744 mbedtls_md_type_t md_alg )
10745{
10746 switch( sig_alg )
10747 {
10748 case MBEDTLS_PK_RSA:
10749 if( set->rsa == MBEDTLS_MD_NONE )
10750 set->rsa = md_alg;
10751 break;
10752
10753 case MBEDTLS_PK_ECDSA:
10754 if( set->ecdsa == MBEDTLS_MD_NONE )
10755 set->ecdsa = md_alg;
10756 break;
10757
10758 default:
10759 break;
10760 }
10761}
10762
10763/* Allow exactly one hash algorithm for each signature. */
10764void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10765 mbedtls_md_type_t md_alg )
10766{
10767 set->rsa = md_alg;
10768 set->ecdsa = md_alg;
10769}
10770
10771#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10772 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10773
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010774/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010775 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010778{
10779 switch( hash )
10780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#if defined(MBEDTLS_MD5_C)
10782 case MBEDTLS_SSL_HASH_MD5:
10783 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785#if defined(MBEDTLS_SHA1_C)
10786 case MBEDTLS_SSL_HASH_SHA1:
10787 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010788#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010789#if defined(MBEDTLS_SHA256_C)
10790 case MBEDTLS_SSL_HASH_SHA224:
10791 return( MBEDTLS_MD_SHA224 );
10792 case MBEDTLS_SSL_HASH_SHA256:
10793 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795#if defined(MBEDTLS_SHA512_C)
10796 case MBEDTLS_SSL_HASH_SHA384:
10797 return( MBEDTLS_MD_SHA384 );
10798 case MBEDTLS_SSL_HASH_SHA512:
10799 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010800#endif
10801 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010803 }
10804}
10805
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010806/*
10807 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10808 */
10809unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10810{
10811 switch( md )
10812 {
10813#if defined(MBEDTLS_MD5_C)
10814 case MBEDTLS_MD_MD5:
10815 return( MBEDTLS_SSL_HASH_MD5 );
10816#endif
10817#if defined(MBEDTLS_SHA1_C)
10818 case MBEDTLS_MD_SHA1:
10819 return( MBEDTLS_SSL_HASH_SHA1 );
10820#endif
10821#if defined(MBEDTLS_SHA256_C)
10822 case MBEDTLS_MD_SHA224:
10823 return( MBEDTLS_SSL_HASH_SHA224 );
10824 case MBEDTLS_MD_SHA256:
10825 return( MBEDTLS_SSL_HASH_SHA256 );
10826#endif
10827#if defined(MBEDTLS_SHA512_C)
10828 case MBEDTLS_MD_SHA384:
10829 return( MBEDTLS_SSL_HASH_SHA384 );
10830 case MBEDTLS_MD_SHA512:
10831 return( MBEDTLS_SSL_HASH_SHA512 );
10832#endif
10833 default:
10834 return( MBEDTLS_SSL_HASH_NONE );
10835 }
10836}
10837
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010838#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010839/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010840 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010841 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010842 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010843int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010846
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010847 if( ssl->conf->curve_list == NULL )
10848 return( -1 );
10849
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010850 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010851 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010852 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010853
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010854 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010855}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010856#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010857
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010858#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010859/*
10860 * Check if a hash proposed by the peer is in our list.
10861 * Return 0 if we're willing to use it, -1 otherwise.
10862 */
10863int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10864 mbedtls_md_type_t md )
10865{
10866 const int *cur;
10867
10868 if( ssl->conf->sig_hashes == NULL )
10869 return( -1 );
10870
10871 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10872 if( *cur == (int) md )
10873 return( 0 );
10874
10875 return( -1 );
10876}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010877#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879#if defined(MBEDTLS_X509_CRT_PARSE_C)
10880int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10881 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010882 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010883 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010884{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010885 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010887 int usage = 0;
10888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010889#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010890 const char *ext_oid;
10891 size_t ext_len;
10892#endif
10893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10895 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010896 ((void) cert);
10897 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010898 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010899#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10902 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010903 {
10904 /* Server part of the key exchange */
10905 switch( ciphersuite->key_exchange )
10906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 case MBEDTLS_KEY_EXCHANGE_RSA:
10908 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010909 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010910 break;
10911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10913 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10914 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10915 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010916 break;
10917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10919 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010920 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010921 break;
10922
10923 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 case MBEDTLS_KEY_EXCHANGE_NONE:
10925 case MBEDTLS_KEY_EXCHANGE_PSK:
10926 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10927 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010928 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010929 usage = 0;
10930 }
10931 }
10932 else
10933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010934 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10935 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010936 }
10937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010938 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010939 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010940 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010941 ret = -1;
10942 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010943#else
10944 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10948 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10951 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010952 }
10953 else
10954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10956 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010957 }
10958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010960 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010961 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010962 ret = -1;
10963 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010965
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010966 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010969
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010970/*
10971 * Convert version numbers to/from wire format
10972 * and, for DTLS, to/from TLS equivalent.
10973 *
10974 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010975 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010976 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10977 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010979void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010980 unsigned char ver[2] )
10981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982#if defined(MBEDTLS_SSL_PROTO_DTLS)
10983 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010986 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10987
10988 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10989 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10990 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010991 else
10992#else
10993 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010994#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010995 {
10996 ver[0] = (unsigned char) major;
10997 ver[1] = (unsigned char) minor;
10998 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010999}
11000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011002 const unsigned char ver[2] )
11003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004#if defined(MBEDTLS_SSL_PROTO_DTLS)
11005 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011006 {
11007 *major = 255 - ver[0] + 2;
11008 *minor = 255 - ver[1] + 1;
11009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011011 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11012 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011013 else
11014#else
11015 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011016#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011017 {
11018 *major = ver[0];
11019 *minor = ver[1];
11020 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011021}
11022
Simon Butcher99000142016-10-13 17:21:01 +010011023int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11024{
11025#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11026 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11027 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11028
11029 switch( md )
11030 {
11031#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11032#if defined(MBEDTLS_MD5_C)
11033 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011034 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011035#endif
11036#if defined(MBEDTLS_SHA1_C)
11037 case MBEDTLS_SSL_HASH_SHA1:
11038 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11039 break;
11040#endif
11041#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11042#if defined(MBEDTLS_SHA512_C)
11043 case MBEDTLS_SSL_HASH_SHA384:
11044 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11045 break;
11046#endif
11047#if defined(MBEDTLS_SHA256_C)
11048 case MBEDTLS_SSL_HASH_SHA256:
11049 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11050 break;
11051#endif
11052 default:
11053 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11054 }
11055
11056 return 0;
11057#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11058 (void) ssl;
11059 (void) md;
11060
11061 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11062#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11063}
11064
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011065#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11066 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11067int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11068 unsigned char *output,
11069 unsigned char *data, size_t data_len )
11070{
11071 int ret = 0;
11072 mbedtls_md5_context mbedtls_md5;
11073 mbedtls_sha1_context mbedtls_sha1;
11074
11075 mbedtls_md5_init( &mbedtls_md5 );
11076 mbedtls_sha1_init( &mbedtls_sha1 );
11077
11078 /*
11079 * digitally-signed struct {
11080 * opaque md5_hash[16];
11081 * opaque sha_hash[20];
11082 * };
11083 *
11084 * md5_hash
11085 * MD5(ClientHello.random + ServerHello.random
11086 * + ServerParams);
11087 * sha_hash
11088 * SHA(ClientHello.random + ServerHello.random
11089 * + ServerParams);
11090 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011091 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011092 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011094 goto exit;
11095 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011096 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011097 ssl->handshake->randbytes, 64 ) ) != 0 )
11098 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011100 goto exit;
11101 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011102 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011103 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011105 goto exit;
11106 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011107 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011108 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011110 goto exit;
11111 }
11112
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011113 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011114 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011116 goto exit;
11117 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011118 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011119 ssl->handshake->randbytes, 64 ) ) != 0 )
11120 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011122 goto exit;
11123 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011124 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011125 data_len ) ) != 0 )
11126 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011128 goto exit;
11129 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011130 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011131 output + 16 ) ) != 0 )
11132 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011134 goto exit;
11135 }
11136
11137exit:
11138 mbedtls_md5_free( &mbedtls_md5 );
11139 mbedtls_sha1_free( &mbedtls_sha1 );
11140
11141 if( ret != 0 )
11142 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11143 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11144
11145 return( ret );
11146
11147}
11148#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11149 MBEDTLS_SSL_PROTO_TLS1_1 */
11150
11151#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11152 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011153
11154#if defined(MBEDTLS_USE_PSA_CRYPTO)
11155int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11156 unsigned char *hash, size_t *hashlen,
11157 unsigned char *data, size_t data_len,
11158 mbedtls_md_type_t md_alg )
11159{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011160 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011161 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011162 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11163
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011165
11166 if( ( status = psa_hash_setup( &hash_operation,
11167 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011168 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011169 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011170 goto exit;
11171 }
11172
Andrzej Kurek814feff2019-01-14 04:35:19 -050011173 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11174 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011175 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011176 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011177 goto exit;
11178 }
11179
Andrzej Kurek814feff2019-01-14 04:35:19 -050011180 if( ( status = psa_hash_update( &hash_operation,
11181 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011182 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011183 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011184 goto exit;
11185 }
11186
Andrzej Kurek814feff2019-01-14 04:35:19 -050011187 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11188 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011189 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011190 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011191 goto exit;
11192 }
11193
11194exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011195 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011196 {
11197 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11198 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011199 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011200 {
11201 case PSA_ERROR_NOT_SUPPORTED:
11202 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011203 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011204 case PSA_ERROR_BUFFER_TOO_SMALL:
11205 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11206 case PSA_ERROR_INSUFFICIENT_MEMORY:
11207 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11208 default:
11209 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11210 }
11211 }
11212 return( 0 );
11213}
11214
11215#else
11216
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011217int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011218 unsigned char *hash, size_t *hashlen,
11219 unsigned char *data, size_t data_len,
11220 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011221{
11222 int ret = 0;
11223 mbedtls_md_context_t ctx;
11224 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011225 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011226
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011228
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011229 mbedtls_md_init( &ctx );
11230
11231 /*
11232 * digitally-signed struct {
11233 * opaque client_random[32];
11234 * opaque server_random[32];
11235 * ServerDHParams params;
11236 * };
11237 */
11238 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11239 {
11240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11241 goto exit;
11242 }
11243 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11244 {
11245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11246 goto exit;
11247 }
11248 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11249 {
11250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11251 goto exit;
11252 }
11253 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11254 {
11255 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11256 goto exit;
11257 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011258 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011259 {
11260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11261 goto exit;
11262 }
11263
11264exit:
11265 mbedtls_md_free( &ctx );
11266
11267 if( ret != 0 )
11268 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11269 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11270
11271 return( ret );
11272}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011273#endif /* MBEDTLS_USE_PSA_CRYPTO */
11274
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011275#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11276 MBEDTLS_SSL_PROTO_TLS1_2 */
11277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011278#endif /* MBEDTLS_SSL_TLS_C */