blob: b7e6cea572990fca3f0b0e41aa7c56faa10c00de [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
Paul Bakker05ef8352012-05-08 09:17:57 +00004139 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004141 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004142
Hanno Becker19859472018-08-06 09:40:20 +01004143 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004144 ssl->out_len[0] = (unsigned char)( len >> 8 );
4145 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004146
Paul Bakker48916f92012-09-16 19:57:18 +00004147 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004148 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004149 mbedtls_record rec;
4150
4151 rec.buf = ssl->out_iv;
4152 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4153 ( ssl->out_iv - ssl->out_buf );
4154 rec.data_len = ssl->out_msglen;
4155 rec.data_offset = ssl->out_msg - rec.buf;
4156
4157 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4158 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4159 ssl->conf->transport, rec.ver );
4160 rec.type = ssl->out_msgtype;
4161
Hanno Becker43c24b82019-05-01 09:45:57 +01004162#if defined(MBEDTLS_SSL_CID)
4163 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004164 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004165#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004166
Hanno Beckera18d1322018-01-03 14:27:32 +00004167 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004168 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004171 return( ret );
4172 }
4173
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004174 if( rec.data_offset != 0 )
4175 {
4176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4178 }
4179
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004180#if defined(MBEDTLS_SSL_CID )
4181 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
4182#endif /* MBEDTLS_SSL_CID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004183 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004184 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4185 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004186 }
4187
Hanno Becker5903de42019-05-03 14:46:38 +01004188 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004189
4190#if defined(MBEDTLS_SSL_PROTO_DTLS)
4191 /* In case of DTLS, double-check that we don't exceed
4192 * the remaining space in the datagram. */
4193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4194 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004195 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004196 if( ret < 0 )
4197 return( ret );
4198
4199 if( protected_record_size > (size_t) ret )
4200 {
4201 /* Should never happen */
4202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4203 }
4204 }
4205#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004208 "version = [%d:%d], msglen = %d",
4209 ssl->out_hdr[0], ssl->out_hdr[1],
4210 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004213 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004214
4215 ssl->out_left += protected_record_size;
4216 ssl->out_hdr += protected_record_size;
4217 ssl_update_out_pointers( ssl, ssl->transform_out );
4218
Hanno Becker04484622018-08-06 09:49:38 +01004219 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4220 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4221 break;
4222
4223 /* The loop goes to its end iff the counter is wrapping */
4224 if( i == ssl_ep_len( ssl ) )
4225 {
4226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4227 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 }
4230
Hanno Becker67bc7c32018-08-06 11:33:50 +01004231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004232 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4233 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004234 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004235 size_t remaining;
4236 ret = ssl_get_remaining_payload_in_datagram( ssl );
4237 if( ret < 0 )
4238 {
4239 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4240 ret );
4241 return( ret );
4242 }
4243
4244 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004245 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004246 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004247 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004248 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004249 else
4250 {
Hanno Becker513815a2018-08-20 11:56:09 +01004251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004252 }
4253 }
4254#endif /* MBEDTLS_SSL_PROTO_DTLS */
4255
4256 if( ( flush == SSL_FORCE_FLUSH ) &&
4257 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 return( ret );
4261 }
4262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004264
4265 return( 0 );
4266}
4267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004269
4270static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4271{
4272 if( ssl->in_msglen < ssl->in_hslen ||
4273 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4274 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4275 {
4276 return( 1 );
4277 }
4278 return( 0 );
4279}
Hanno Becker44650b72018-08-16 12:51:11 +01004280
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004281static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004282{
4283 return( ( ssl->in_msg[9] << 16 ) |
4284 ( ssl->in_msg[10] << 8 ) |
4285 ssl->in_msg[11] );
4286}
4287
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004288static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004289{
4290 return( ( ssl->in_msg[6] << 16 ) |
4291 ( ssl->in_msg[7] << 8 ) |
4292 ssl->in_msg[8] );
4293}
4294
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004295static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004296{
4297 uint32_t msg_len, frag_off, frag_len;
4298
4299 msg_len = ssl_get_hs_total_len( ssl );
4300 frag_off = ssl_get_hs_frag_off( ssl );
4301 frag_len = ssl_get_hs_frag_len( ssl );
4302
4303 if( frag_off > msg_len )
4304 return( -1 );
4305
4306 if( frag_len > msg_len - frag_off )
4307 return( -1 );
4308
4309 if( frag_len + 12 > ssl->in_msglen )
4310 return( -1 );
4311
4312 return( 0 );
4313}
4314
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004315/*
4316 * Mark bits in bitmask (used for DTLS HS reassembly)
4317 */
4318static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4319{
4320 unsigned int start_bits, end_bits;
4321
4322 start_bits = 8 - ( offset % 8 );
4323 if( start_bits != 8 )
4324 {
4325 size_t first_byte_idx = offset / 8;
4326
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004327 /* Special case */
4328 if( len <= start_bits )
4329 {
4330 for( ; len != 0; len-- )
4331 mask[first_byte_idx] |= 1 << ( start_bits - len );
4332
4333 /* Avoid potential issues with offset or len becoming invalid */
4334 return;
4335 }
4336
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004337 offset += start_bits; /* Now offset % 8 == 0 */
4338 len -= start_bits;
4339
4340 for( ; start_bits != 0; start_bits-- )
4341 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4342 }
4343
4344 end_bits = len % 8;
4345 if( end_bits != 0 )
4346 {
4347 size_t last_byte_idx = ( offset + len ) / 8;
4348
4349 len -= end_bits; /* Now len % 8 == 0 */
4350
4351 for( ; end_bits != 0; end_bits-- )
4352 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4353 }
4354
4355 memset( mask + offset / 8, 0xFF, len / 8 );
4356}
4357
4358/*
4359 * Check that bitmask is full
4360 */
4361static int ssl_bitmask_check( unsigned char *mask, size_t len )
4362{
4363 size_t i;
4364
4365 for( i = 0; i < len / 8; i++ )
4366 if( mask[i] != 0xFF )
4367 return( -1 );
4368
4369 for( i = 0; i < len % 8; i++ )
4370 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4371 return( -1 );
4372
4373 return( 0 );
4374}
4375
Hanno Becker56e205e2018-08-16 09:06:12 +01004376/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004377static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004378 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004379{
Hanno Becker56e205e2018-08-16 09:06:12 +01004380 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004381
Hanno Becker56e205e2018-08-16 09:06:12 +01004382 alloc_len = 12; /* Handshake header */
4383 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004384
Hanno Beckerd07df862018-08-16 09:14:58 +01004385 if( add_bitmap )
4386 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004387
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004388 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004389}
Hanno Becker56e205e2018-08-16 09:06:12 +01004390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004392
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004393static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004394{
4395 return( ( ssl->in_msg[1] << 16 ) |
4396 ( ssl->in_msg[2] << 8 ) |
4397 ssl->in_msg[3] );
4398}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004399
Simon Butcher99000142016-10-13 17:21:01 +01004400int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004405 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004407 }
4408
Hanno Becker12555c62018-08-16 12:47:53 +01004409 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004412 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004413 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004417 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004418 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004419 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004420
Hanno Becker44650b72018-08-16 12:51:11 +01004421 if( ssl_check_hs_header( ssl ) != 0 )
4422 {
4423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4424 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4425 }
4426
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004427 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004428 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4429 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4430 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4431 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004432 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004433 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4434 {
4435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4436 recv_msg_seq,
4437 ssl->handshake->in_msg_seq ) );
4438 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4439 }
4440
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004441 /* Retransmit only on last message from previous flight, to avoid
4442 * too many retransmissions.
4443 * Besides, No sane server ever retransmits HelloVerifyRequest */
4444 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004448 "message_seq = %d, start_of_flight = %d",
4449 recv_msg_seq,
4450 ssl->handshake->in_flight_start_seq ) );
4451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004455 return( ret );
4456 }
4457 }
4458 else
4459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004461 "message_seq = %d, expected = %d",
4462 recv_msg_seq,
4463 ssl->handshake->in_msg_seq ) );
4464 }
4465
Hanno Becker90333da2017-10-10 11:27:13 +01004466 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004467 }
4468 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004469
Hanno Becker6d97ef52018-08-16 13:09:04 +01004470 /* Message reassembly is handled alongside buffering of future
4471 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004472 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004473 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004474 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004477 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004478 }
4479 }
4480 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004482 /* With TLS we don't handle fragmentation (for now) */
4483 if( ssl->in_msglen < ssl->in_hslen )
4484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4486 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004487 }
4488
Simon Butcher99000142016-10-13 17:21:01 +01004489 return( 0 );
4490}
4491
4492void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4493{
Hanno Becker0271f962018-08-16 13:23:47 +01004494 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004495
Hanno Becker0271f962018-08-16 13:23:47 +01004496 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004497 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004498 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004499 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004500
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004501 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004503 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004504 ssl->handshake != NULL )
4505 {
Hanno Becker0271f962018-08-16 13:23:47 +01004506 unsigned offset;
4507 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004508
Hanno Becker0271f962018-08-16 13:23:47 +01004509 /* Increment handshake sequence number */
4510 hs->in_msg_seq++;
4511
4512 /*
4513 * Clear up handshake buffering and reassembly structure.
4514 */
4515
4516 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004517 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004518
4519 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004520 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4521 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004522 offset++, hs_buf++ )
4523 {
4524 *hs_buf = *(hs_buf + 1);
4525 }
4526
4527 /* Create a fresh last entry */
4528 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004529 }
4530#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004531}
4532
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004534 * DTLS anti-replay: RFC 6347 4.1.2.6
4535 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004536 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4537 * Bit n is set iff record number in_window_top - n has been seen.
4538 *
4539 * Usually, in_window_top is the last record number seen and the lsb of
4540 * in_window is set. The only exception is the initial state (record number 0
4541 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4544static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004545{
4546 ssl->in_window_top = 0;
4547 ssl->in_window = 0;
4548}
4549
4550static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4551{
4552 return( ( (uint64_t) buf[0] << 40 ) |
4553 ( (uint64_t) buf[1] << 32 ) |
4554 ( (uint64_t) buf[2] << 24 ) |
4555 ( (uint64_t) buf[3] << 16 ) |
4556 ( (uint64_t) buf[4] << 8 ) |
4557 ( (uint64_t) buf[5] ) );
4558}
4559
4560/*
4561 * Return 0 if sequence number is acceptable, -1 otherwise
4562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004564{
4565 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4566 uint64_t bit;
4567
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004568 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004569 return( 0 );
4570
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004571 if( rec_seqnum > ssl->in_window_top )
4572 return( 0 );
4573
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004574 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004575
4576 if( bit >= 64 )
4577 return( -1 );
4578
4579 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4580 return( -1 );
4581
4582 return( 0 );
4583}
4584
4585/*
4586 * Update replay window on new validated record
4587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004589{
4590 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4591
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004592 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004593 return;
4594
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004595 if( rec_seqnum > ssl->in_window_top )
4596 {
4597 /* Update window_top and the contents of the window */
4598 uint64_t shift = rec_seqnum - ssl->in_window_top;
4599
4600 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004601 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004602 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004603 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004604 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004605 ssl->in_window |= 1;
4606 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004607
4608 ssl->in_window_top = rec_seqnum;
4609 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004610 else
4611 {
4612 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004613 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004614
4615 if( bit < 64 ) /* Always true, but be extra sure */
4616 ssl->in_window |= (uint64_t) 1 << bit;
4617 }
4618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004620
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004621#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004622/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004623static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4624
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004625/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004626 * Without any SSL context, check if a datagram looks like a ClientHello with
4627 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004628 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004629 *
4630 * - if cookie is valid, return 0
4631 * - if ClientHello looks superficially valid but cookie is not,
4632 * fill obuf and set olen, then
4633 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4634 * - otherwise return a specific error code
4635 */
4636static int ssl_check_dtls_clihlo_cookie(
4637 mbedtls_ssl_cookie_write_t *f_cookie_write,
4638 mbedtls_ssl_cookie_check_t *f_cookie_check,
4639 void *p_cookie,
4640 const unsigned char *cli_id, size_t cli_id_len,
4641 const unsigned char *in, size_t in_len,
4642 unsigned char *obuf, size_t buf_len, size_t *olen )
4643{
4644 size_t sid_len, cookie_len;
4645 unsigned char *p;
4646
4647 if( f_cookie_write == NULL || f_cookie_check == NULL )
4648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4649
4650 /*
4651 * Structure of ClientHello with record and handshake headers,
4652 * and expected values. We don't need to check a lot, more checks will be
4653 * done when actually parsing the ClientHello - skipping those checks
4654 * avoids code duplication and does not make cookie forging any easier.
4655 *
4656 * 0-0 ContentType type; copied, must be handshake
4657 * 1-2 ProtocolVersion version; copied
4658 * 3-4 uint16 epoch; copied, must be 0
4659 * 5-10 uint48 sequence_number; copied
4660 * 11-12 uint16 length; (ignored)
4661 *
4662 * 13-13 HandshakeType msg_type; (ignored)
4663 * 14-16 uint24 length; (ignored)
4664 * 17-18 uint16 message_seq; copied
4665 * 19-21 uint24 fragment_offset; copied, must be 0
4666 * 22-24 uint24 fragment_length; (ignored)
4667 *
4668 * 25-26 ProtocolVersion client_version; (ignored)
4669 * 27-58 Random random; (ignored)
4670 * 59-xx SessionID session_id; 1 byte len + sid_len content
4671 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4672 * ...
4673 *
4674 * Minimum length is 61 bytes.
4675 */
4676 if( in_len < 61 ||
4677 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4678 in[3] != 0 || in[4] != 0 ||
4679 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4680 {
4681 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4682 }
4683
4684 sid_len = in[59];
4685 if( sid_len > in_len - 61 )
4686 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4687
4688 cookie_len = in[60 + sid_len];
4689 if( cookie_len > in_len - 60 )
4690 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4691
4692 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4693 cli_id, cli_id_len ) == 0 )
4694 {
4695 /* Valid cookie */
4696 return( 0 );
4697 }
4698
4699 /*
4700 * If we get here, we've got an invalid cookie, let's prepare HVR.
4701 *
4702 * 0-0 ContentType type; copied
4703 * 1-2 ProtocolVersion version; copied
4704 * 3-4 uint16 epoch; copied
4705 * 5-10 uint48 sequence_number; copied
4706 * 11-12 uint16 length; olen - 13
4707 *
4708 * 13-13 HandshakeType msg_type; hello_verify_request
4709 * 14-16 uint24 length; olen - 25
4710 * 17-18 uint16 message_seq; copied
4711 * 19-21 uint24 fragment_offset; copied
4712 * 22-24 uint24 fragment_length; olen - 25
4713 *
4714 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4715 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4716 *
4717 * Minimum length is 28.
4718 */
4719 if( buf_len < 28 )
4720 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4721
4722 /* Copy most fields and adapt others */
4723 memcpy( obuf, in, 25 );
4724 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4725 obuf[25] = 0xfe;
4726 obuf[26] = 0xff;
4727
4728 /* Generate and write actual cookie */
4729 p = obuf + 28;
4730 if( f_cookie_write( p_cookie,
4731 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4732 {
4733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4734 }
4735
4736 *olen = p - obuf;
4737
4738 /* Go back and fill length fields */
4739 obuf[27] = (unsigned char)( *olen - 28 );
4740
4741 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4742 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4743 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4744
4745 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4746 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4747
4748 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4749}
4750
4751/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004752 * Handle possible client reconnect with the same UDP quadruplet
4753 * (RFC 6347 Section 4.2.8).
4754 *
4755 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4756 * that looks like a ClientHello.
4757 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004758 * - if the input looks like a ClientHello without cookies,
4759 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004760 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004761 * - if the input looks like a ClientHello with a valid cookie,
4762 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004763 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004764 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004765 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004766 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004767 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4768 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004769 */
4770static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4771{
4772 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004774
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004775 ret = ssl_check_dtls_clihlo_cookie(
4776 ssl->conf->f_cookie_write,
4777 ssl->conf->f_cookie_check,
4778 ssl->conf->p_cookie,
4779 ssl->cli_id, ssl->cli_id_len,
4780 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004781 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004783 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4784
4785 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004786 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004787 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004788 * If the error is permanent we'll catch it later,
4789 * if it's not, then hopefully it'll work next time. */
4790 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4791
4792 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004793 }
4794
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004795 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004796 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004797 /* Got a valid cookie, partially reset context */
4798 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4799 {
4800 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4801 return( ret );
4802 }
4803
4804 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004805 }
4806
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004807 return( ret );
4808}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004809#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004810
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004811static int ssl_check_record_type( uint8_t record_type )
4812{
4813 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4814 record_type != MBEDTLS_SSL_MSG_ALERT &&
4815 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4816 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4817 {
4818 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4819 }
4820
4821 return( 0 );
4822}
4823
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004824/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004825 * ContentType type;
4826 * ProtocolVersion version;
4827 * uint16 epoch; // DTLS only
4828 * uint48 sequence_number; // DTLS only
4829 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004830 *
4831 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004832 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004833 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4834 *
4835 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004836 * 1. proceed with the record if this function returns 0
4837 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4838 * 3. return CLIENT_RECONNECT if this function return that value
4839 * 4. drop the whole datagram if this function returns anything else.
4840 * Point 2 is needed when the peer is resending, and we have already received
4841 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004844{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004845 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004846
Hanno Becker5903de42019-05-03 14:46:38 +01004847 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 +02004848
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004850 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004851 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004854 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004855 ssl->in_msgtype,
4856 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004857
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004858 /* Check record type */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004859 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004862
4863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004864 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4865 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004866 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4867#endif /* MBEDTLS_SSL_PROTO_DTLS */
4868 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4869 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004872 }
4873
4874 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004875 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4878 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004879 }
4880
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004881 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4884 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004885 }
4886
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004887 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004888 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004889 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4892 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004893 }
4894
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004895 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004896 * DTLS-related tests.
4897 * Check epoch before checking length constraint because
4898 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4899 * message gets duplicated before the corresponding Finished message,
4900 * the second ChangeCipherSpec should be discarded because it belongs
4901 * to an old epoch, but not because its length is shorter than
4902 * the minimum record length for packets using the new record transform.
4903 * Note that these two kinds of failures are handled differently,
4904 * as an unexpected record is silently skipped but an invalid
4905 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004906 */
4907#if defined(MBEDTLS_SSL_PROTO_DTLS)
4908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4909 {
4910 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4911
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004912 /* Check epoch (and sequence number) with DTLS */
4913 if( rec_epoch != ssl->in_epoch )
4914 {
4915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4916 "expected %d, received %d",
4917 ssl->in_epoch, rec_epoch ) );
4918
4919#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4920 /*
4921 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4922 * access the first byte of record content (handshake type), as we
4923 * have an active transform (possibly iv_len != 0), so use the
4924 * fact that the record header len is 13 instead.
4925 */
4926 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4927 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4928 rec_epoch == 0 &&
4929 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4930 ssl->in_left > 13 &&
4931 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4932 {
4933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4934 "from the same port" ) );
4935 return( ssl_handle_possible_reconnect( ssl ) );
4936 }
4937 else
4938#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004939 {
4940 /* Consider buffering the record. */
4941 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4942 {
4943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4944 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4945 }
4946
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004947 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004948 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004949 }
4950
4951#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4952 /* Replay detection only works for the current epoch */
4953 if( rec_epoch == ssl->in_epoch &&
4954 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4955 {
4956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4957 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4958 }
4959#endif
4960 }
4961#endif /* MBEDTLS_SSL_PROTO_DTLS */
4962
Hanno Becker52c6dc62017-05-26 16:07:36 +01004963
4964 /* Check length against bounds of the current transform and version */
4965 if( ssl->transform_in == NULL )
4966 {
4967 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004968 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004969 {
4970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4971 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4972 }
4973 }
4974 else
4975 {
4976 if( ssl->in_msglen < ssl->transform_in->minlen )
4977 {
4978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4979 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4980 }
4981
4982#if defined(MBEDTLS_SSL_PROTO_SSL3)
4983 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004984 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004985 {
4986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4987 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4988 }
4989#endif
4990#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4991 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4992 /*
4993 * TLS encrypted messages can have up to 256 bytes of padding
4994 */
4995 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4996 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004997 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004998 {
4999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5000 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5001 }
5002#endif
5003 }
5004
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005005 return( 0 );
5006}
Paul Bakker5121ce52009-01-03 21:22:43 +00005007
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005008/*
5009 * If applicable, decrypt (and decompress) record content
5010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005012{
5013 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005016 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5019 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 ret = mbedtls_ssl_hw_record_read( ssl );
5024 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5027 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005028 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005029
5030 if( ret == 0 )
5031 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005032 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005034 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005035 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005036 mbedtls_record rec;
5037
5038 rec.buf = ssl->in_iv;
5039 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5040 - ( ssl->in_iv - ssl->in_buf );
5041 rec.data_len = ssl->in_msglen;
5042 rec.data_offset = 0;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005043#if defined(MBEDTLS_SSL_CID )
5044 rec.cid_len = ssl->in_len - ssl->in_cid;
5045 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
5046#endif /* MBEDTLS_SSL_CID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005047
5048 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5049 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5050 ssl->conf->transport, rec.ver );
5051 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005052 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5053 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 return( ret );
5057 }
5058
Hanno Becker79594fd2019-05-08 09:38:41 +01005059 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005060 ssl->in_msglen = rec.data_len;
5061 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5062 ssl->in_len[1] = (unsigned char)( rec.data_len );
5063
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005064 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5065 ssl->in_msg, ssl->in_msglen );
5066
Angus Grattond8213d02016-05-25 20:56:48 +10005067 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5070 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005072 else if( ssl->in_msglen == 0 )
5073 {
5074#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5075 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5076 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5077 {
5078 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5080 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5081 }
5082#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5083
5084 ssl->nb_zero++;
5085
5086 /*
5087 * Three or more empty messages may be a DoS attack
5088 * (excessive CPU consumption).
5089 */
5090 if( ssl->nb_zero > 3 )
5091 {
5092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005093 "messages, possible DoS attack" ) );
5094 /* Treat the records as if they were not properly authenticated,
5095 * thereby failing the connection if we see more than allowed
5096 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005097 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5098 }
5099 }
5100 else
5101 ssl->nb_zero = 0;
5102
5103#if defined(MBEDTLS_SSL_PROTO_DTLS)
5104 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5105 {
5106 ; /* in_ctr read from peer, not maintained internally */
5107 }
5108 else
5109#endif
5110 {
5111 unsigned i;
5112 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5113 if( ++ssl->in_ctr[i - 1] != 0 )
5114 break;
5115
5116 /* The loop goes to its end iff the counter is wrapping */
5117 if( i == ssl_ep_len( ssl ) )
5118 {
5119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5120 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5121 }
5122 }
5123
Paul Bakker5121ce52009-01-03 21:22:43 +00005124 }
5125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005127 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005129 {
5130 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005133 return( ret );
5134 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005139 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005141 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005142 }
5143#endif
5144
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005145 return( 0 );
5146}
5147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005149
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005150/*
5151 * Read a record.
5152 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005153 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5154 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5155 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005156 */
Hanno Becker1097b342018-08-15 14:09:41 +01005157
5158/* Helper functions for mbedtls_ssl_read_record(). */
5159static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005160static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5161static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005162
Hanno Becker327c93b2018-08-15 13:56:18 +01005163int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005164 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005165{
5166 int ret;
5167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005169
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005170 if( ssl->keep_current_message == 0 )
5171 {
5172 do {
Simon Butcher99000142016-10-13 17:21:01 +01005173
Hanno Becker26994592018-08-15 14:14:59 +01005174 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005175 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005176 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005177
Hanno Beckere74d5562018-08-15 14:26:08 +01005178 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005179 {
Hanno Becker40f50842018-08-15 14:48:01 +01005180#if defined(MBEDTLS_SSL_PROTO_DTLS)
5181 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005182
Hanno Becker40f50842018-08-15 14:48:01 +01005183 /* We only check for buffered messages if the
5184 * current datagram is fully consumed. */
5185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005186 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005187 {
Hanno Becker40f50842018-08-15 14:48:01 +01005188 if( ssl_load_buffered_message( ssl ) == 0 )
5189 have_buffered = 1;
5190 }
5191
5192 if( have_buffered == 0 )
5193#endif /* MBEDTLS_SSL_PROTO_DTLS */
5194 {
5195 ret = ssl_get_next_record( ssl );
5196 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5197 continue;
5198
5199 if( ret != 0 )
5200 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005201 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005202 return( ret );
5203 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005204 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005205 }
5206
5207 ret = mbedtls_ssl_handle_message_type( ssl );
5208
Hanno Becker40f50842018-08-15 14:48:01 +01005209#if defined(MBEDTLS_SSL_PROTO_DTLS)
5210 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5211 {
5212 /* Buffer future message */
5213 ret = ssl_buffer_message( ssl );
5214 if( ret != 0 )
5215 return( ret );
5216
5217 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5218 }
5219#endif /* MBEDTLS_SSL_PROTO_DTLS */
5220
Hanno Becker90333da2017-10-10 11:27:13 +01005221 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5222 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005223
5224 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005225 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005226 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005227 return( ret );
5228 }
5229
Hanno Becker327c93b2018-08-15 13:56:18 +01005230 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005231 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005232 {
5233 mbedtls_ssl_update_handshake_status( ssl );
5234 }
Simon Butcher99000142016-10-13 17:21:01 +01005235 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005236 else
Simon Butcher99000142016-10-13 17:21:01 +01005237 {
Hanno Becker02f59072018-08-15 14:00:24 +01005238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005239 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005240 }
5241
5242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5243
5244 return( 0 );
5245}
5246
Hanno Becker40f50842018-08-15 14:48:01 +01005247#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005248static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005249{
Hanno Becker40f50842018-08-15 14:48:01 +01005250 if( ssl->in_left > ssl->next_record_offset )
5251 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005252
Hanno Becker40f50842018-08-15 14:48:01 +01005253 return( 0 );
5254}
5255
5256static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5257{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005258 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005259 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005260 int ret = 0;
5261
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005262 if( hs == NULL )
5263 return( -1 );
5264
Hanno Beckere00ae372018-08-20 09:39:42 +01005265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5266
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005267 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5268 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5269 {
5270 /* Check if we have seen a ChangeCipherSpec before.
5271 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005272 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005273 {
5274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5275 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005276 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005277 }
5278
Hanno Becker39b8bc92018-08-28 17:17:13 +01005279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005280 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5281 ssl->in_msglen = 1;
5282 ssl->in_msg[0] = 1;
5283
5284 /* As long as they are equal, the exact value doesn't matter. */
5285 ssl->in_left = 0;
5286 ssl->next_record_offset = 0;
5287
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005288 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005289 goto exit;
5290 }
Hanno Becker37f95322018-08-16 13:55:32 +01005291
Hanno Beckerb8f50142018-08-28 10:01:34 +01005292#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005293 /* Debug only */
5294 {
5295 unsigned offset;
5296 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5297 {
5298 hs_buf = &hs->buffering.hs[offset];
5299 if( hs_buf->is_valid == 1 )
5300 {
5301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5302 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005303 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005304 }
5305 }
5306 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005307#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005308
5309 /* Check if we have buffered and/or fully reassembled the
5310 * next handshake message. */
5311 hs_buf = &hs->buffering.hs[0];
5312 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5313 {
5314 /* Synthesize a record containing the buffered HS message. */
5315 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5316 ( hs_buf->data[2] << 8 ) |
5317 hs_buf->data[3];
5318
5319 /* Double-check that we haven't accidentally buffered
5320 * a message that doesn't fit into the input buffer. */
5321 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5322 {
5323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5325 }
5326
5327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5328 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5329 hs_buf->data, msg_len + 12 );
5330
5331 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5332 ssl->in_hslen = msg_len + 12;
5333 ssl->in_msglen = msg_len + 12;
5334 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5335
5336 ret = 0;
5337 goto exit;
5338 }
5339 else
5340 {
5341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5342 hs->in_msg_seq ) );
5343 }
5344
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005345 ret = -1;
5346
5347exit:
5348
5349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5350 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005351}
5352
Hanno Beckera02b0b42018-08-21 17:20:27 +01005353static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5354 size_t desired )
5355{
5356 int offset;
5357 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5359 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005360
Hanno Becker01315ea2018-08-21 17:22:17 +01005361 /* Get rid of future records epoch first, if such exist. */
5362 ssl_free_buffered_record( ssl );
5363
5364 /* Check if we have enough space available now. */
5365 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5366 hs->buffering.total_bytes_buffered ) )
5367 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005369 return( 0 );
5370 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005371
Hanno Becker4f432ad2018-08-28 10:02:32 +01005372 /* We don't have enough space to buffer the next expected handshake
5373 * message. Remove buffers used for future messages to gain space,
5374 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005375 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5376 offset >= 0; offset-- )
5377 {
5378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5379 offset ) );
5380
Hanno Beckerb309b922018-08-23 13:18:05 +01005381 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005382
5383 /* Check if we have enough space available now. */
5384 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5385 hs->buffering.total_bytes_buffered ) )
5386 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005388 return( 0 );
5389 }
5390 }
5391
5392 return( -1 );
5393}
5394
Hanno Becker40f50842018-08-15 14:48:01 +01005395static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5396{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005397 int ret = 0;
5398 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5399
5400 if( hs == NULL )
5401 return( 0 );
5402
5403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5404
5405 switch( ssl->in_msgtype )
5406 {
5407 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005409
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005410 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005411 break;
5412
5413 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005414 {
5415 unsigned recv_msg_seq_offset;
5416 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5417 mbedtls_ssl_hs_buffer *hs_buf;
5418 size_t msg_len = ssl->in_hslen - 12;
5419
5420 /* We should never receive an old handshake
5421 * message - double-check nonetheless. */
5422 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5423 {
5424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5426 }
5427
5428 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5429 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5430 {
5431 /* Silently ignore -- message too far in the future */
5432 MBEDTLS_SSL_DEBUG_MSG( 2,
5433 ( "Ignore future HS message with sequence number %u, "
5434 "buffering window %u - %u",
5435 recv_msg_seq, ssl->handshake->in_msg_seq,
5436 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5437
5438 goto exit;
5439 }
5440
5441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5442 recv_msg_seq, recv_msg_seq_offset ) );
5443
5444 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5445
5446 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005447 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005448 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005449 size_t reassembly_buf_sz;
5450
Hanno Becker37f95322018-08-16 13:55:32 +01005451 hs_buf->is_fragmented =
5452 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5453
5454 /* We copy the message back into the input buffer
5455 * after reassembly, so check that it's not too large.
5456 * This is an implementation-specific limitation
5457 * and not one from the standard, hence it is not
5458 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005459 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005460 {
5461 /* Ignore message */
5462 goto exit;
5463 }
5464
Hanno Beckere0b150f2018-08-21 15:51:03 +01005465 /* Check if we have enough space to buffer the message. */
5466 if( hs->buffering.total_bytes_buffered >
5467 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5468 {
5469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5471 }
5472
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005473 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5474 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005475
5476 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5477 hs->buffering.total_bytes_buffered ) )
5478 {
5479 if( recv_msg_seq_offset > 0 )
5480 {
5481 /* If we can't buffer a future message because
5482 * of space limitations -- ignore. */
5483 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",
5484 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5485 (unsigned) hs->buffering.total_bytes_buffered ) );
5486 goto exit;
5487 }
Hanno Beckere1801392018-08-21 16:51:05 +01005488 else
5489 {
5490 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",
5491 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5492 (unsigned) hs->buffering.total_bytes_buffered ) );
5493 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005494
Hanno Beckera02b0b42018-08-21 17:20:27 +01005495 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005496 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005497 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",
5498 (unsigned) msg_len,
5499 (unsigned) reassembly_buf_sz,
5500 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005501 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005502 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5503 goto exit;
5504 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005505 }
5506
5507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5508 msg_len ) );
5509
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005510 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5511 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005512 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005513 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005514 goto exit;
5515 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005516 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005517
5518 /* Prepare final header: copy msg_type, length and message_seq,
5519 * then add standardised fragment_offset and fragment_length */
5520 memcpy( hs_buf->data, ssl->in_msg, 6 );
5521 memset( hs_buf->data + 6, 0, 3 );
5522 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5523
5524 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005525
5526 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005527 }
5528 else
5529 {
5530 /* Make sure msg_type and length are consistent */
5531 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5532 {
5533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5534 /* Ignore */
5535 goto exit;
5536 }
5537 }
5538
Hanno Becker4422bbb2018-08-20 09:40:19 +01005539 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005540 {
5541 size_t frag_len, frag_off;
5542 unsigned char * const msg = hs_buf->data + 12;
5543
5544 /*
5545 * Check and copy current fragment
5546 */
5547
5548 /* Validation of header fields already done in
5549 * mbedtls_ssl_prepare_handshake_record(). */
5550 frag_off = ssl_get_hs_frag_off( ssl );
5551 frag_len = ssl_get_hs_frag_len( ssl );
5552
5553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5554 frag_off, frag_len ) );
5555 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5556
5557 if( hs_buf->is_fragmented )
5558 {
5559 unsigned char * const bitmask = msg + msg_len;
5560 ssl_bitmask_set( bitmask, frag_off, frag_len );
5561 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5562 msg_len ) == 0 );
5563 }
5564 else
5565 {
5566 hs_buf->is_complete = 1;
5567 }
5568
5569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5570 hs_buf->is_complete ? "" : "not yet " ) );
5571 }
5572
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005573 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005574 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005575
5576 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005577 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005578 break;
5579 }
5580
5581exit:
5582
5583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5584 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005585}
5586#endif /* MBEDTLS_SSL_PROTO_DTLS */
5587
Hanno Becker1097b342018-08-15 14:09:41 +01005588static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005589{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005590 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005591 * Consume last content-layer message and potentially
5592 * update in_msglen which keeps track of the contents'
5593 * consumption state.
5594 *
5595 * (1) Handshake messages:
5596 * Remove last handshake message, move content
5597 * and adapt in_msglen.
5598 *
5599 * (2) Alert messages:
5600 * Consume whole record content, in_msglen = 0.
5601 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005602 * (3) Change cipher spec:
5603 * Consume whole record content, in_msglen = 0.
5604 *
5605 * (4) Application data:
5606 * Don't do anything - the record layer provides
5607 * the application data as a stream transport
5608 * and consumes through mbedtls_ssl_read only.
5609 *
5610 */
5611
5612 /* Case (1): Handshake messages */
5613 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005614 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005615 /* Hard assertion to be sure that no application data
5616 * is in flight, as corrupting ssl->in_msglen during
5617 * ssl->in_offt != NULL is fatal. */
5618 if( ssl->in_offt != NULL )
5619 {
5620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5622 }
5623
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005624 /*
5625 * Get next Handshake message in the current record
5626 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005627
Hanno Becker4a810fb2017-05-24 16:27:30 +01005628 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005629 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005630 * current handshake content: If DTLS handshake
5631 * fragmentation is used, that's the fragment
5632 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005633 * size here is faulty and should be changed at
5634 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005635 * (2) While it doesn't seem to cause problems, one
5636 * has to be very careful not to assume that in_hslen
5637 * is always <= in_msglen in a sensible communication.
5638 * Again, it's wrong for DTLS handshake fragmentation.
5639 * The following check is therefore mandatory, and
5640 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005641 * Additionally, ssl->in_hslen might be arbitrarily out of
5642 * bounds after handling a DTLS message with an unexpected
5643 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005644 */
5645 if( ssl->in_hslen < ssl->in_msglen )
5646 {
5647 ssl->in_msglen -= ssl->in_hslen;
5648 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5649 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005650
Hanno Becker4a810fb2017-05-24 16:27:30 +01005651 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5652 ssl->in_msg, ssl->in_msglen );
5653 }
5654 else
5655 {
5656 ssl->in_msglen = 0;
5657 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005658
Hanno Becker4a810fb2017-05-24 16:27:30 +01005659 ssl->in_hslen = 0;
5660 }
5661 /* Case (4): Application data */
5662 else if( ssl->in_offt != NULL )
5663 {
5664 return( 0 );
5665 }
5666 /* Everything else (CCS & Alerts) */
5667 else
5668 {
5669 ssl->in_msglen = 0;
5670 }
5671
Hanno Becker1097b342018-08-15 14:09:41 +01005672 return( 0 );
5673}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005674
Hanno Beckere74d5562018-08-15 14:26:08 +01005675static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5676{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005677 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005678 return( 1 );
5679
5680 return( 0 );
5681}
5682
Hanno Becker5f066e72018-08-16 14:56:31 +01005683#if defined(MBEDTLS_SSL_PROTO_DTLS)
5684
5685static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5686{
5687 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5688 if( hs == NULL )
5689 return;
5690
Hanno Becker01315ea2018-08-21 17:22:17 +01005691 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005692 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005693 hs->buffering.total_bytes_buffered -=
5694 hs->buffering.future_record.len;
5695
5696 mbedtls_free( hs->buffering.future_record.data );
5697 hs->buffering.future_record.data = NULL;
5698 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005699}
5700
5701static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5702{
5703 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5704 unsigned char * rec;
5705 size_t rec_len;
5706 unsigned rec_epoch;
5707
5708 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5709 return( 0 );
5710
5711 if( hs == NULL )
5712 return( 0 );
5713
Hanno Becker5f066e72018-08-16 14:56:31 +01005714 rec = hs->buffering.future_record.data;
5715 rec_len = hs->buffering.future_record.len;
5716 rec_epoch = hs->buffering.future_record.epoch;
5717
5718 if( rec == NULL )
5719 return( 0 );
5720
Hanno Becker4cb782d2018-08-20 11:19:05 +01005721 /* Only consider loading future records if the
5722 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005723 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005724 return( 0 );
5725
Hanno Becker5f066e72018-08-16 14:56:31 +01005726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5727
5728 if( rec_epoch != ssl->in_epoch )
5729 {
5730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5731 goto exit;
5732 }
5733
5734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5735
5736 /* Double-check that the record is not too large */
5737 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5738 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5739 {
5740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5741 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5742 }
5743
5744 memcpy( ssl->in_hdr, rec, rec_len );
5745 ssl->in_left = rec_len;
5746 ssl->next_record_offset = 0;
5747
5748 ssl_free_buffered_record( ssl );
5749
5750exit:
5751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5752 return( 0 );
5753}
5754
5755static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5756{
5757 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5758 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005759 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005760
5761 /* Don't buffer future records outside handshakes. */
5762 if( hs == NULL )
5763 return( 0 );
5764
5765 /* Only buffer handshake records (we are only interested
5766 * in Finished messages). */
5767 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5768 return( 0 );
5769
5770 /* Don't buffer more than one future epoch record. */
5771 if( hs->buffering.future_record.data != NULL )
5772 return( 0 );
5773
Hanno Becker01315ea2018-08-21 17:22:17 +01005774 /* Don't buffer record if there's not enough buffering space remaining. */
5775 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5776 hs->buffering.total_bytes_buffered ) )
5777 {
5778 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",
5779 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5780 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005781 return( 0 );
5782 }
5783
Hanno Becker5f066e72018-08-16 14:56:31 +01005784 /* Buffer record */
5785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5786 ssl->in_epoch + 1 ) );
5787 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5788 rec_hdr_len + ssl->in_msglen );
5789
5790 /* ssl_parse_record_header() only considers records
5791 * of the next epoch as candidates for buffering. */
5792 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005793 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005794
5795 hs->buffering.future_record.data =
5796 mbedtls_calloc( 1, hs->buffering.future_record.len );
5797 if( hs->buffering.future_record.data == NULL )
5798 {
5799 /* If we run out of RAM trying to buffer a
5800 * record from the next epoch, just ignore. */
5801 return( 0 );
5802 }
5803
Hanno Becker01315ea2018-08-21 17:22:17 +01005804 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005805
Hanno Becker01315ea2018-08-21 17:22:17 +01005806 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005807 return( 0 );
5808}
5809
5810#endif /* MBEDTLS_SSL_PROTO_DTLS */
5811
Hanno Beckere74d5562018-08-15 14:26:08 +01005812static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005813{
5814 int ret;
5815
Hanno Becker5f066e72018-08-16 14:56:31 +01005816#if defined(MBEDTLS_SSL_PROTO_DTLS)
5817 /* We might have buffered a future record; if so,
5818 * and if the epoch matches now, load it.
5819 * On success, this call will set ssl->in_left to
5820 * the length of the buffered record, so that
5821 * the calls to ssl_fetch_input() below will
5822 * essentially be no-ops. */
5823 ret = ssl_load_buffered_record( ssl );
5824 if( ret != 0 )
5825 return( ret );
5826#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005827
Hanno Becker5903de42019-05-03 14:46:38 +01005828 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005831 return( ret );
5832 }
5833
5834 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005837 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5838 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005839 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005840 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5841 {
5842 ret = ssl_buffer_future_record( ssl );
5843 if( ret != 0 )
5844 return( ret );
5845
5846 /* Fall through to handling of unexpected records */
5847 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5848 }
5849
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005850 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5851 {
5852 /* Skip unexpected record (but not whole datagram) */
5853 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005854 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005855
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5857 "(header)" ) );
5858 }
5859 else
5860 {
5861 /* Skip invalid record and the rest of the datagram */
5862 ssl->next_record_offset = 0;
5863 ssl->in_left = 0;
5864
5865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5866 "(header)" ) );
5867 }
5868
5869 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005870 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005871 }
5872#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005873 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005874 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005875
5876 /*
5877 * Read and optionally decrypt the message contents
5878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005880 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005883 return( ret );
5884 }
5885
5886 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005888 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005889 {
Hanno Becker5903de42019-05-03 14:46:38 +01005890 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005891 if( ssl->next_record_offset < ssl->in_left )
5892 {
5893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5894 }
5895 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005896 else
5897#endif
5898 ssl->in_left = 0;
5899
5900 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005903 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005904 {
5905 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005906 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005907 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005908 /* Except when waiting for Finished as a bad mac here
5909 * probably means something went wrong in the handshake
5910 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5911 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5912 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5913 {
5914#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5915 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5916 {
5917 mbedtls_ssl_send_alert_message( ssl,
5918 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5919 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5920 }
5921#endif
5922 return( ret );
5923 }
5924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005926 if( ssl->conf->badmac_limit != 0 &&
5927 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5930 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005931 }
5932#endif
5933
Hanno Becker4a810fb2017-05-24 16:27:30 +01005934 /* As above, invalid records cause
5935 * dismissal of the whole datagram. */
5936
5937 ssl->next_record_offset = 0;
5938 ssl->in_left = 0;
5939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005941 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005942 }
5943
5944 return( ret );
5945 }
5946 else
5947#endif
5948 {
5949 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5951 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 mbedtls_ssl_send_alert_message( ssl,
5954 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5955 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005956 }
5957#endif
5958 return( ret );
5959 }
5960 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005961
Simon Butcher99000142016-10-13 17:21:01 +01005962 return( 0 );
5963}
5964
5965int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5966{
5967 int ret;
5968
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005969 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005970 * Handle particular types of records
5971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 {
Simon Butcher99000142016-10-13 17:21:01 +01005974 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5975 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005976 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005978 }
5979
Hanno Beckere678eaa2018-08-21 14:57:46 +01005980 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005981 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005982 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005983 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5985 ssl->in_msglen ) );
5986 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005987 }
5988
Hanno Beckere678eaa2018-08-21 14:57:46 +01005989 if( ssl->in_msg[0] != 1 )
5990 {
5991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5992 ssl->in_msg[0] ) );
5993 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5994 }
5995
5996#if defined(MBEDTLS_SSL_PROTO_DTLS)
5997 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5998 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5999 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6000 {
6001 if( ssl->handshake == NULL )
6002 {
6003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6004 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6005 }
6006
6007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6008 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6009 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006010#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006011 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006015 if( ssl->in_msglen != 2 )
6016 {
6017 /* Note: Standard allows for more than one 2 byte alert
6018 to be packed in a single message, but Mbed TLS doesn't
6019 currently support this. */
6020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6021 ssl->in_msglen ) );
6022 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6023 }
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006026 ssl->in_msg[0], ssl->in_msg[1] ) );
6027
6028 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006029 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006034 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 }
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6039 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6042 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006043 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006044
6045#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6046 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6047 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6048 {
Hanno Becker90333da2017-10-10 11:27:13 +01006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006050 /* Will be handled when trying to parse ServerHello */
6051 return( 0 );
6052 }
6053#endif
6054
6055#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6056 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6057 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6058 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6059 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6060 {
6061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6062 /* Will be handled in mbedtls_ssl_parse_certificate() */
6063 return( 0 );
6064 }
6065#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6066
6067 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006068 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 }
6070
Hanno Beckerc76c6192017-06-06 10:03:17 +01006071#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006072 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006073 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006074 /* Drop unexpected ApplicationData records,
6075 * except at the beginning of renegotiations */
6076 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6077 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6078#if defined(MBEDTLS_SSL_RENEGOTIATION)
6079 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6080 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006081#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006082 )
6083 {
6084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6085 return( MBEDTLS_ERR_SSL_NON_FATAL );
6086 }
6087
6088 if( ssl->handshake != NULL &&
6089 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6090 {
6091 ssl_handshake_wrapup_free_hs_transform( ssl );
6092 }
6093 }
6094#endif /* MBEDTLS_SSL_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006095
Paul Bakker5121ce52009-01-03 21:22:43 +00006096 return( 0 );
6097}
6098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006100{
6101 int ret;
6102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6104 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6105 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006106 {
6107 return( ret );
6108 }
6109
6110 return( 0 );
6111}
6112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006114 unsigned char level,
6115 unsigned char message )
6116{
6117 int ret;
6118
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006119 if( ssl == NULL || ssl->conf == NULL )
6120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006126 ssl->out_msglen = 2;
6127 ssl->out_msg[0] = level;
6128 ssl->out_msg[1] = message;
6129
Hanno Becker67bc7c32018-08-06 11:33:50 +01006130 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006133 return( ret );
6134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006136
6137 return( 0 );
6138}
6139
Hanno Beckerb9d44792019-02-08 07:19:04 +00006140#if defined(MBEDTLS_X509_CRT_PARSE_C)
6141static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6142{
6143#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6144 if( session->peer_cert != NULL )
6145 {
6146 mbedtls_x509_crt_free( session->peer_cert );
6147 mbedtls_free( session->peer_cert );
6148 session->peer_cert = NULL;
6149 }
6150#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6151 if( session->peer_cert_digest != NULL )
6152 {
6153 /* Zeroization is not necessary. */
6154 mbedtls_free( session->peer_cert_digest );
6155 session->peer_cert_digest = NULL;
6156 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6157 session->peer_cert_digest_len = 0;
6158 }
6159#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6160}
6161#endif /* MBEDTLS_X509_CRT_PARSE_C */
6162
Paul Bakker5121ce52009-01-03 21:22:43 +00006163/*
6164 * Handshake functions
6165 */
Hanno Becker21489932019-02-05 13:20:55 +00006166#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006167/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006169{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006170 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6171 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006174
Hanno Becker7177a882019-02-05 13:36:46 +00006175 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006178 ssl->state++;
6179 return( 0 );
6180 }
6181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006184}
6185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006187{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006188 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6189 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006192
Hanno Becker7177a882019-02-05 13:36:46 +00006193 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006196 ssl->state++;
6197 return( 0 );
6198 }
6199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006202}
Gilles Peskinef9828522017-05-03 12:28:43 +02006203
Hanno Becker21489932019-02-05 13:20:55 +00006204#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006205/* Some certificate support -> implement write and parse */
6206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006210 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006212 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6213 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006216
Hanno Becker7177a882019-02-05 13:36:46 +00006217 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006220 ssl->state++;
6221 return( 0 );
6222 }
6223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006225 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 {
6227 if( ssl->client_auth == 0 )
6228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 ssl->state++;
6231 return( 0 );
6232 }
6233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 /*
6236 * If using SSLv3 and got no cert, send an Alert message
6237 * (otherwise an empty Certificate message will be sent).
6238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6240 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 {
6242 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6244 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6245 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006248 goto write_msg;
6249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006251 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252#endif /* MBEDTLS_SSL_CLI_C */
6253#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006254 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6259 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 }
6261 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265
6266 /*
6267 * 0 . 0 handshake type
6268 * 1 . 3 handshake length
6269 * 4 . 6 length of all certs
6270 * 7 . 9 length of cert. 1
6271 * 10 . n-1 peer certificate
6272 * n . n+2 length of cert. 2
6273 * n+3 . ... upper level cert, etc.
6274 */
6275 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006277
Paul Bakker29087132010-03-21 21:03:34 +00006278 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006279 {
6280 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006281 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006284 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 }
6287
6288 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6289 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6290 ssl->out_msg[i + 2] = (unsigned char)( n );
6291
6292 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6293 i += n; crt = crt->next;
6294 }
6295
6296 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6297 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6298 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6299
6300 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6302 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006303
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006304#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006305write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006306#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006307
6308 ssl->state++;
6309
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006310 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006311 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006313 return( ret );
6314 }
6315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006317
Paul Bakkered27a042013-04-18 22:46:23 +02006318 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006319}
6320
Hanno Becker84879e32019-01-31 07:44:03 +00006321#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006322
6323#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006324static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6325 unsigned char *crt_buf,
6326 size_t crt_buf_len )
6327{
6328 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6329
6330 if( peer_crt == NULL )
6331 return( -1 );
6332
6333 if( peer_crt->raw.len != crt_buf_len )
6334 return( -1 );
6335
Hanno Becker46f34d02019-02-08 14:00:04 +00006336 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006337}
Hanno Becker177475a2019-02-05 17:02:46 +00006338#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6339static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6340 unsigned char *crt_buf,
6341 size_t crt_buf_len )
6342{
6343 int ret;
6344 unsigned char const * const peer_cert_digest =
6345 ssl->session->peer_cert_digest;
6346 mbedtls_md_type_t const peer_cert_digest_type =
6347 ssl->session->peer_cert_digest_type;
6348 mbedtls_md_info_t const * const digest_info =
6349 mbedtls_md_info_from_type( peer_cert_digest_type );
6350 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6351 size_t digest_len;
6352
6353 if( peer_cert_digest == NULL || digest_info == NULL )
6354 return( -1 );
6355
6356 digest_len = mbedtls_md_get_size( digest_info );
6357 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6358 return( -1 );
6359
6360 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6361 if( ret != 0 )
6362 return( -1 );
6363
6364 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6365}
6366#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006367#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006368
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006369/*
6370 * Once the certificate message is read, parse it into a cert chain and
6371 * perform basic checks, but leave actual verification to the caller
6372 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006373static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6374 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006375{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006376 int ret;
6377#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6378 int crt_cnt=0;
6379#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006380 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006381 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006386 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6387 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006389 }
6390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6392 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6396 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006398 }
6399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006401
Paul Bakker5121ce52009-01-03 21:22:43 +00006402 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006404 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006405 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006406
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006407 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006411 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6412 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006414 }
6415
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006416 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6417 i += 3;
6418
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006419 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006420 while( i < ssl->in_hslen )
6421 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006422 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006423 if ( i + 3 > ssl->in_hslen ) {
6424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006425 mbedtls_ssl_send_alert_message( ssl,
6426 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6427 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006428 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6429 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006430 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6431 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 if( ssl->in_msg[i] != 0 )
6433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006435 mbedtls_ssl_send_alert_message( ssl,
6436 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6437 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439 }
6440
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006441 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006442 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6443 | (unsigned int) ssl->in_msg[i + 2];
6444 i += 3;
6445
6446 if( n < 128 || i + n > ssl->in_hslen )
6447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006449 mbedtls_ssl_send_alert_message( ssl,
6450 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6451 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006453 }
6454
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006455 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006456#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6457 if( crt_cnt++ == 0 &&
6458 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6459 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006460 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006461 /* During client-side renegotiation, check that the server's
6462 * end-CRTs hasn't changed compared to the initial handshake,
6463 * mitigating the triple handshake attack. On success, reuse
6464 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6466 if( ssl_check_peer_crt_unchanged( ssl,
6467 &ssl->in_msg[i],
6468 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006469 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6471 mbedtls_ssl_send_alert_message( ssl,
6472 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6473 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6474 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006475 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006476
6477 /* Now we can safely free the original chain. */
6478 ssl_clear_peer_cert( ssl->session );
6479 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006480#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6481
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006482 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006483#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006484 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006485#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006486 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006487 * it in-place from the input buffer instead of making a copy. */
6488 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6489#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006490 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006492 case 0: /*ok*/
6493 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6494 /* Ignore certificate with an unknown algorithm: maybe a
6495 prior certificate was already trusted. */
6496 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006497
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006498 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6499 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6500 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006501
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006502 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6503 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6504 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006505
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006506 default:
6507 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6508 crt_parse_der_failed:
6509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6510 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6511 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 }
6513
6514 i += n;
6515 }
6516
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006517 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006518 return( 0 );
6519}
6520
Hanno Becker4a55f632019-02-05 12:49:06 +00006521#if defined(MBEDTLS_SSL_SRV_C)
6522static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6523{
6524 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6525 return( -1 );
6526
6527#if defined(MBEDTLS_SSL_PROTO_SSL3)
6528 /*
6529 * Check if the client sent an empty certificate
6530 */
6531 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6532 {
6533 if( ssl->in_msglen == 2 &&
6534 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6535 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6536 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6539 return( 0 );
6540 }
6541
6542 return( -1 );
6543 }
6544#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6545
6546#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6547 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6548 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6549 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6550 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6551 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6552 {
6553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6554 return( 0 );
6555 }
6556
6557 return( -1 );
6558#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6559 MBEDTLS_SSL_PROTO_TLS1_2 */
6560}
6561#endif /* MBEDTLS_SSL_SRV_C */
6562
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006563/* Check if a certificate message is expected.
6564 * Return either
6565 * - SSL_CERTIFICATE_EXPECTED, or
6566 * - SSL_CERTIFICATE_SKIP
6567 * indicating whether a Certificate message is expected or not.
6568 */
6569#define SSL_CERTIFICATE_EXPECTED 0
6570#define SSL_CERTIFICATE_SKIP 1
6571static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6572 int authmode )
6573{
6574 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006575 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006576
6577 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6578 return( SSL_CERTIFICATE_SKIP );
6579
6580#if defined(MBEDTLS_SSL_SRV_C)
6581 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6582 {
6583 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6584 return( SSL_CERTIFICATE_SKIP );
6585
6586 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6587 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006588 ssl->session_negotiate->verify_result =
6589 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6590 return( SSL_CERTIFICATE_SKIP );
6591 }
6592 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006593#else
6594 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006595#endif /* MBEDTLS_SSL_SRV_C */
6596
6597 return( SSL_CERTIFICATE_EXPECTED );
6598}
6599
Hanno Becker68636192019-02-05 14:36:34 +00006600static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6601 int authmode,
6602 mbedtls_x509_crt *chain,
6603 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006604{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006605 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006606 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006607 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006608 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006609
Hanno Becker8927c832019-04-03 12:52:50 +01006610 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6611 void *p_vrfy;
6612
Hanno Becker68636192019-02-05 14:36:34 +00006613 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6614 return( 0 );
6615
Hanno Becker8927c832019-04-03 12:52:50 +01006616 if( ssl->f_vrfy != NULL )
6617 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006619 f_vrfy = ssl->f_vrfy;
6620 p_vrfy = ssl->p_vrfy;
6621 }
6622 else
6623 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006625 f_vrfy = ssl->conf->f_vrfy;
6626 p_vrfy = ssl->conf->p_vrfy;
6627 }
6628
Hanno Becker68636192019-02-05 14:36:34 +00006629 /*
6630 * Main check: verify certificate
6631 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006632#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6633 if( ssl->conf->f_ca_cb != NULL )
6634 {
6635 ((void) rs_ctx);
6636 have_ca_chain = 1;
6637
6638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006639 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006640 chain,
6641 ssl->conf->f_ca_cb,
6642 ssl->conf->p_ca_cb,
6643 ssl->conf->cert_profile,
6644 ssl->hostname,
6645 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006646 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006647 }
6648 else
6649#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6650 {
6651 mbedtls_x509_crt *ca_chain;
6652 mbedtls_x509_crl *ca_crl;
6653
6654#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6655 if( ssl->handshake->sni_ca_chain != NULL )
6656 {
6657 ca_chain = ssl->handshake->sni_ca_chain;
6658 ca_crl = ssl->handshake->sni_ca_crl;
6659 }
6660 else
6661#endif
6662 {
6663 ca_chain = ssl->conf->ca_chain;
6664 ca_crl = ssl->conf->ca_crl;
6665 }
6666
6667 if( ca_chain != NULL )
6668 have_ca_chain = 1;
6669
6670 ret = mbedtls_x509_crt_verify_restartable(
6671 chain,
6672 ca_chain, ca_crl,
6673 ssl->conf->cert_profile,
6674 ssl->hostname,
6675 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006676 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006677 }
Hanno Becker68636192019-02-05 14:36:34 +00006678
6679 if( ret != 0 )
6680 {
6681 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6682 }
6683
6684#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6685 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6686 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6687#endif
6688
6689 /*
6690 * Secondary checks: always done, but change 'ret' only if it was 0
6691 */
6692
6693#if defined(MBEDTLS_ECP_C)
6694 {
6695 const mbedtls_pk_context *pk = &chain->pk;
6696
6697 /* If certificate uses an EC key, make sure the curve is OK */
6698 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6699 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6700 {
6701 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6702
6703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6704 if( ret == 0 )
6705 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6706 }
6707 }
6708#endif /* MBEDTLS_ECP_C */
6709
6710 if( mbedtls_ssl_check_cert_usage( chain,
6711 ciphersuite_info,
6712 ! ssl->conf->endpoint,
6713 &ssl->session_negotiate->verify_result ) != 0 )
6714 {
6715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6716 if( ret == 0 )
6717 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6718 }
6719
6720 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6721 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6722 * with details encoded in the verification flags. All other kinds
6723 * of error codes, including those from the user provided f_vrfy
6724 * functions, are treated as fatal and lead to a failure of
6725 * ssl_parse_certificate even if verification was optional. */
6726 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6727 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6728 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6729 {
6730 ret = 0;
6731 }
6732
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006733 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006734 {
6735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6736 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6737 }
6738
6739 if( ret != 0 )
6740 {
6741 uint8_t alert;
6742
6743 /* The certificate may have been rejected for several reasons.
6744 Pick one and send the corresponding alert. Which alert to send
6745 may be a subject of debate in some cases. */
6746 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6747 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6748 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6749 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6750 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6751 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6752 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6753 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6754 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6755 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6756 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6757 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6758 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6759 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6760 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6761 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6762 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6763 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6764 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6765 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6766 else
6767 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6768 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6769 alert );
6770 }
6771
6772#if defined(MBEDTLS_DEBUG_C)
6773 if( ssl->session_negotiate->verify_result != 0 )
6774 {
6775 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6776 ssl->session_negotiate->verify_result ) );
6777 }
6778 else
6779 {
6780 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6781 }
6782#endif /* MBEDTLS_DEBUG_C */
6783
6784 return( ret );
6785}
6786
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006787#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6788static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6789 unsigned char *start, size_t len )
6790{
6791 int ret;
6792 /* Remember digest of the peer's end-CRT. */
6793 ssl->session_negotiate->peer_cert_digest =
6794 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6795 if( ssl->session_negotiate->peer_cert_digest == NULL )
6796 {
6797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6798 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6799 mbedtls_ssl_send_alert_message( ssl,
6800 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6801 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6802
6803 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6804 }
6805
6806 ret = mbedtls_md( mbedtls_md_info_from_type(
6807 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6808 start, len,
6809 ssl->session_negotiate->peer_cert_digest );
6810
6811 ssl->session_negotiate->peer_cert_digest_type =
6812 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6813 ssl->session_negotiate->peer_cert_digest_len =
6814 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6815
6816 return( ret );
6817}
6818
6819static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6820 unsigned char *start, size_t len )
6821{
6822 unsigned char *end = start + len;
6823 int ret;
6824
6825 /* Make a copy of the peer's raw public key. */
6826 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6827 ret = mbedtls_pk_parse_subpubkey( &start, end,
6828 &ssl->handshake->peer_pubkey );
6829 if( ret != 0 )
6830 {
6831 /* We should have parsed the public key before. */
6832 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6833 }
6834
6835 return( 0 );
6836}
6837#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6838
Hanno Becker68636192019-02-05 14:36:34 +00006839int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6840{
6841 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006842 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006843#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6844 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6845 ? ssl->handshake->sni_authmode
6846 : ssl->conf->authmode;
6847#else
6848 const int authmode = ssl->conf->authmode;
6849#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006850 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006851 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006852
6853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6854
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006855 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6856 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006857 {
6858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006859 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006860 }
6861
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006862#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6863 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006864 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006865 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006866 chain = ssl->handshake->ecrs_peer_cert;
6867 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006868 goto crt_verify;
6869 }
6870#endif
6871
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006872 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006873 {
6874 /* mbedtls_ssl_read_record may have sent an alert already. We
6875 let it decide whether to alert. */
6876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006877 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006878 }
6879
Hanno Becker4a55f632019-02-05 12:49:06 +00006880#if defined(MBEDTLS_SSL_SRV_C)
6881 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6882 {
6883 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006884
6885 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006886 ret = 0;
6887 else
6888 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006889
Hanno Becker6bdfab22019-02-05 13:11:17 +00006890 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006891 }
6892#endif /* MBEDTLS_SSL_SRV_C */
6893
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006894 /* Clear existing peer CRT structure in case we tried to
6895 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006896 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006897
6898 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6899 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006900 {
6901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6902 sizeof( mbedtls_x509_crt ) ) );
6903 mbedtls_ssl_send_alert_message( ssl,
6904 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6905 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006906
Hanno Becker3dad3112019-02-05 17:19:52 +00006907 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6908 goto exit;
6909 }
6910 mbedtls_x509_crt_init( chain );
6911
6912 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006913 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006914 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006915
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006916#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6917 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006918 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006919
6920crt_verify:
6921 if( ssl->handshake->ecrs_enabled)
6922 rs_ctx = &ssl->handshake->ecrs_ctx;
6923#endif
6924
Hanno Becker68636192019-02-05 14:36:34 +00006925 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006926 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006927 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006928 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006929
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006930#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006931 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006932 unsigned char *crt_start, *pk_start;
6933 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006934
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006935 /* We parse the CRT chain without copying, so
6936 * these pointers point into the input buffer,
6937 * and are hence still valid after freeing the
6938 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006939
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006940 crt_start = chain->raw.p;
6941 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006942
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006943 pk_start = chain->pk_raw.p;
6944 pk_len = chain->pk_raw.len;
6945
6946 /* Free the CRT structures before computing
6947 * digest and copying the peer's public key. */
6948 mbedtls_x509_crt_free( chain );
6949 mbedtls_free( chain );
6950 chain = NULL;
6951
6952 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006953 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006954 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006955
6956 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6957 if( ret != 0 )
6958 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006959 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006960#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6961 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006962 ssl->session_negotiate->peer_cert = chain;
6963 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006964#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006967
Hanno Becker6bdfab22019-02-05 13:11:17 +00006968exit:
6969
Hanno Becker3dad3112019-02-05 17:19:52 +00006970 if( ret == 0 )
6971 ssl->state++;
6972
6973#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6974 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6975 {
6976 ssl->handshake->ecrs_peer_cert = chain;
6977 chain = NULL;
6978 }
6979#endif
6980
6981 if( chain != NULL )
6982 {
6983 mbedtls_x509_crt_free( chain );
6984 mbedtls_free( chain );
6985 }
6986
Paul Bakker5121ce52009-01-03 21:22:43 +00006987 return( ret );
6988}
Hanno Becker21489932019-02-05 13:20:55 +00006989#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006992{
6993 int ret;
6994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006998 ssl->out_msglen = 1;
6999 ssl->out_msg[0] = 1;
7000
Paul Bakker5121ce52009-01-03 21:22:43 +00007001 ssl->state++;
7002
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007003 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007004 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007006 return( ret );
7007 }
7008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007010
7011 return( 0 );
7012}
7013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007015{
7016 int ret;
7017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007019
Hanno Becker327c93b2018-08-15 13:56:18 +01007020 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007023 return( ret );
7024 }
7025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007029 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7030 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007032 }
7033
Hanno Beckere678eaa2018-08-21 14:57:46 +01007034 /* CCS records are only accepted if they have length 1 and content '1',
7035 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007036
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007037 /*
7038 * Switch to our negotiated transform and session parameters for inbound
7039 * data.
7040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007042 ssl->transform_in = ssl->transform_negotiate;
7043 ssl->session_in = ssl->session_negotiate;
7044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007049 ssl_dtls_replay_reset( ssl );
7050#endif
7051
7052 /* Increment epoch */
7053 if( ++ssl->in_epoch == 0 )
7054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007056 /* This is highly unlikely to happen for legitimate reasons, so
7057 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007059 }
7060 }
7061 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007063 memset( ssl->in_ctr, 0, 8 );
7064
Hanno Becker79594fd2019-05-08 09:38:41 +01007065 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7068 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007073 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7074 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007076 }
7077 }
7078#endif
7079
Paul Bakker5121ce52009-01-03 21:22:43 +00007080 ssl->state++;
7081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007083
7084 return( 0 );
7085}
7086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7088 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007089{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007090 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7093 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7094 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007095 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007096 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7099#if defined(MBEDTLS_SHA512_C)
7100 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007101 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7102 else
7103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104#if defined(MBEDTLS_SHA256_C)
7105 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007106 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007107 else
7108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007112 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007113 }
Paul Bakker380da532012-04-18 16:10:25 +00007114}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7119 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007120 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7121 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7124#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007125#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007126 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007127 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7128#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007129 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007130#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007133#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007134 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007135 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007136#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007137 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007138#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007141}
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007144 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7147 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007148 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7149 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7152#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007153#if defined(MBEDTLS_USE_PSA_CRYPTO)
7154 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7155#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007156 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007157#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007160#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007161 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007162#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007163 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007164#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007167}
7168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7170 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7171static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007172 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007173{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007174 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7175 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007176}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007177#endif
Paul Bakker380da532012-04-18 16:10:25 +00007178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7180#if defined(MBEDTLS_SHA256_C)
7181static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007182 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007183{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007184#if defined(MBEDTLS_USE_PSA_CRYPTO)
7185 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7186#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007187 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007188#endif
Paul Bakker380da532012-04-18 16:10:25 +00007189}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007190#endif
Paul Bakker380da532012-04-18 16:10:25 +00007191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192#if defined(MBEDTLS_SHA512_C)
7193static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007194 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007195{
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 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007200#endif
Paul Bakker380da532012-04-18 16:10:25 +00007201}
Paul Bakker769075d2012-11-24 11:26:46 +01007202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007206static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007208{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007209 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007210 mbedtls_md5_context md5;
7211 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007212
Paul Bakker5121ce52009-01-03 21:22:43 +00007213 unsigned char padbuf[48];
7214 unsigned char md5sum[16];
7215 unsigned char sha1sum[20];
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007218 if( !session )
7219 session = ssl->session;
7220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007222
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007223 mbedtls_md5_init( &md5 );
7224 mbedtls_sha1_init( &sha1 );
7225
7226 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7227 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
7229 /*
7230 * SSLv3:
7231 * hash =
7232 * MD5( master + pad2 +
7233 * MD5( handshake + sender + master + pad1 ) )
7234 * + SHA1( master + pad2 +
7235 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007236 */
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007239 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7240 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007244 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7245 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007246#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007249 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007250
Paul Bakker1ef83d62012-04-11 12:09:53 +00007251 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007252
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007253 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7254 mbedtls_md5_update_ret( &md5, session->master, 48 );
7255 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7256 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007257
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007258 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7259 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7260 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7261 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007262
Paul Bakker1ef83d62012-04-11 12:09:53 +00007263 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007264
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007265 mbedtls_md5_starts_ret( &md5 );
7266 mbedtls_md5_update_ret( &md5, session->master, 48 );
7267 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7268 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7269 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007270
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007271 mbedtls_sha1_starts_ret( &sha1 );
7272 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7273 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7274 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7275 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007279 mbedtls_md5_free( &md5 );
7280 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007281
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007282 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7283 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7284 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007291static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007293{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007294 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007295 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007296 mbedtls_md5_context md5;
7297 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007298 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007301 if( !session )
7302 session = ssl->session;
7303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007305
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007306 mbedtls_md5_init( &md5 );
7307 mbedtls_sha1_init( &sha1 );
7308
7309 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7310 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007311
Paul Bakker1ef83d62012-04-11 12:09:53 +00007312 /*
7313 * TLSv1:
7314 * hash = PRF( master, finished_label,
7315 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7316 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007319 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7320 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007321#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007324 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7325 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007326#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007329 ? "client finished"
7330 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007331
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007332 mbedtls_md5_finish_ret( &md5, padbuf );
7333 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007334
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007335 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007336 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007339
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007340 mbedtls_md5_free( &md5 );
7341 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007342
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007343 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7350#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007351static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007353{
7354 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007355 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007356 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007357#if defined(MBEDTLS_USE_PSA_CRYPTO)
7358 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007359 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007360 psa_status_t status;
7361#else
7362 mbedtls_sha256_context sha256;
7363#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007366 if( !session )
7367 session = ssl->session;
7368
Andrzej Kurekeb342242019-01-29 09:14:33 -05007369 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7370 ? "client finished"
7371 : "server finished";
7372
7373#if defined(MBEDTLS_USE_PSA_CRYPTO)
7374 sha256_psa = psa_hash_operation_init();
7375
7376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7377
7378 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7379 if( status != PSA_SUCCESS )
7380 {
7381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7382 return;
7383 }
7384
7385 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7386 if( status != PSA_SUCCESS )
7387 {
7388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7389 return;
7390 }
7391 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7392#else
7393
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007394 mbedtls_sha256_init( &sha256 );
7395
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007397
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007398 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007399
7400 /*
7401 * TLSv1.2:
7402 * hash = PRF( master, finished_label,
7403 * Hash( handshake ) )[0.11]
7404 */
7405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#if !defined(MBEDTLS_SHA256_ALT)
7407 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007408 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007409#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007410
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007411 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007412 mbedtls_sha256_free( &sha256 );
7413#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007414
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007415 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007416 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007419
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007420 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007427static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007429{
7430 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007431 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007432 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007433#if defined(MBEDTLS_USE_PSA_CRYPTO)
7434 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007435 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007436 psa_status_t status;
7437#else
7438 mbedtls_sha512_context sha512;
7439#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007442 if( !session )
7443 session = ssl->session;
7444
Andrzej Kurekeb342242019-01-29 09:14:33 -05007445 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7446 ? "client finished"
7447 : "server finished";
7448
7449#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007450 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007451
7452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7453
Andrzej Kurek972fba52019-01-30 03:29:12 -05007454 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007455 if( status != PSA_SUCCESS )
7456 {
7457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7458 return;
7459 }
7460
Andrzej Kurek972fba52019-01-30 03:29:12 -05007461 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007462 if( status != PSA_SUCCESS )
7463 {
7464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7465 return;
7466 }
7467 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7468#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007469 mbedtls_sha512_init( &sha512 );
7470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007472
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007473 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007474
7475 /*
7476 * TLSv1.2:
7477 * hash = PRF( master, finished_label,
7478 * Hash( handshake ) )[0.11]
7479 */
7480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007482 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7483 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007484#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007485
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007486 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007487 mbedtls_sha512_free( &sha512 );
7488#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007489
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007490 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007491 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007494
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007495 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499#endif /* MBEDTLS_SHA512_C */
7500#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007505
7506 /*
7507 * Free our handshake params
7508 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007509 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007511 ssl->handshake = NULL;
7512
7513 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007514 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007515 */
7516 if( ssl->transform )
7517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 mbedtls_ssl_transform_free( ssl->transform );
7519 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007520 }
7521 ssl->transform = ssl->transform_negotiate;
7522 ssl->transform_negotiate = NULL;
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007525}
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007528{
7529 int resume = ssl->handshake->resume;
7530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_RENEGOTIATION)
7534 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007537 ssl->renego_records_seen = 0;
7538 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007539#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007540
7541 /*
7542 * Free the previous session and switch in the current one
7543 */
Paul Bakker0a597072012-09-25 21:55:46 +00007544 if( ssl->session )
7545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007547 /* RFC 7366 3.1: keep the EtM state */
7548 ssl->session_negotiate->encrypt_then_mac =
7549 ssl->session->encrypt_then_mac;
7550#endif
7551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552 mbedtls_ssl_session_free( ssl->session );
7553 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007554 }
7555 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007556 ssl->session_negotiate = NULL;
7557
Paul Bakker0a597072012-09-25 21:55:46 +00007558 /*
7559 * Add cache entry
7560 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007561 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007562 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007563 resume == 0 )
7564 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007565 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007567 }
Paul Bakker0a597072012-09-25 21:55:46 +00007568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007570 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007571 ssl->handshake->flight != NULL )
7572 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007573 /* Cancel handshake timer */
7574 ssl_set_timer( ssl, 0 );
7575
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007576 /* Keep last flight around in case we need to resend it:
7577 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007579 }
7580 else
7581#endif
7582 ssl_handshake_wrapup_free_hs_transform( ssl );
7583
Paul Bakker48916f92012-09-16 19:57:18 +00007584 ssl->state++;
7585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007587}
7588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007590{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007591 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007594
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007595 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007596
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007597 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007598
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007599 /*
7600 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7601 * may define some other value. Currently (early 2016), no defined
7602 * ciphersuite does this (and this is unlikely to change as activity has
7603 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007608 ssl->verify_data_len = hash_len;
7609 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007610#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007611
Paul Bakker5121ce52009-01-03 21:22:43 +00007612 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7614 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007615
7616 /*
7617 * In case of session resuming, invert the client and server
7618 * ChangeCipherSpec messages order.
7619 */
Paul Bakker0a597072012-09-25 21:55:46 +00007620 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007623 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007627 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007629#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007630 }
7631 else
7632 ssl->state++;
7633
Paul Bakker48916f92012-09-16 19:57:18 +00007634 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007635 * Switch to our negotiated transform and session parameters for outbound
7636 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007641 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007642 {
7643 unsigned char i;
7644
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007645 /* Remember current epoch settings for resending */
7646 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007647 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007648
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007649 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007650 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007651
7652 /* Increment epoch */
7653 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007654 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007655 break;
7656
7657 /* The loop goes to its end iff the counter is wrapping */
7658 if( i == 0 )
7659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7661 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007662 }
7663 }
7664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007666 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007667
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007668 ssl->transform_out = ssl->transform_negotiate;
7669 ssl->session_out = ssl->session_negotiate;
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7672 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7677 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007678 }
7679 }
7680#endif
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007683 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007685#endif
7686
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007687 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007688 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007690 return( ret );
7691 }
7692
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007693#if defined(MBEDTLS_SSL_PROTO_DTLS)
7694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7695 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7696 {
7697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7698 return( ret );
7699 }
7700#endif
7701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007703
7704 return( 0 );
7705}
7706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007708#define SSL_MAX_HASH_LEN 36
7709#else
7710#define SSL_MAX_HASH_LEN 12
7711#endif
7712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007714{
Paul Bakker23986e52011-04-24 08:57:21 +00007715 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007716 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007717 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007720
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007721 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007722
Hanno Becker327c93b2018-08-15 13:56:18 +01007723 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 return( ret );
7727 }
7728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7733 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 }
7736
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007737 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_PROTO_SSL3)
7739 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007740 hash_len = 36;
7741 else
7742#endif
7743 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7746 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007749 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7750 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 }
7753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007755 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007758 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7759 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 }
7762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007764 ssl->verify_data_len = hash_len;
7765 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007766#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007767
Paul Bakker0a597072012-09-25 21:55:46 +00007768 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007775 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007777#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007778 }
7779 else
7780 ssl->state++;
7781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007785#endif
7786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007788
7789 return( 0 );
7790}
7791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007793{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7797 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7798 mbedtls_md5_init( &handshake->fin_md5 );
7799 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007800 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7801 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7804#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007805#if defined(MBEDTLS_USE_PSA_CRYPTO)
7806 handshake->fin_sha256_psa = psa_hash_operation_init();
7807 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7808#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007810 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007811#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007812#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007814#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007815 handshake->fin_sha384_psa = psa_hash_operation_init();
7816 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007817#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007819 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007820#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007821#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007823
7824 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007825
7826#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7827 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7828 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7829#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#if defined(MBEDTLS_DHM_C)
7832 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#if defined(MBEDTLS_ECDH_C)
7835 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007836#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007837#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007838 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007839#if defined(MBEDTLS_SSL_CLI_C)
7840 handshake->ecjpake_cache = NULL;
7841 handshake->ecjpake_cache_len = 0;
7842#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007843#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007844
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007845#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007846 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007847#endif
7848
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007849#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7850 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7851#endif
Hanno Becker75173122019-02-06 16:18:31 +00007852
7853#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7854 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7855 mbedtls_pk_init( &handshake->peer_pubkey );
7856#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007857}
7858
Hanno Beckera18d1322018-01-03 14:27:32 +00007859void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7864 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007865
Hanno Beckerd56ed242018-01-03 15:32:51 +00007866#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 mbedtls_md_init( &transform->md_ctx_enc );
7868 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007869#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007870}
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007873{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007875}
7876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007878{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007879 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007880 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007882 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007884 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007885 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007886
7887 /*
7888 * Either the pointers are now NULL or cleared properly and can be freed.
7889 * Now allocate missing structures.
7890 */
7891 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007892 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007893 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007894 }
Paul Bakker48916f92012-09-16 19:57:18 +00007895
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007896 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007897 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007898 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007899 }
Paul Bakker48916f92012-09-16 19:57:18 +00007900
Paul Bakker82788fb2014-10-20 13:59:19 +02007901 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007902 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007903 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007904 }
Paul Bakker48916f92012-09-16 19:57:18 +00007905
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007906 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007907 if( ssl->handshake == NULL ||
7908 ssl->transform_negotiate == NULL ||
7909 ssl->session_negotiate == NULL )
7910 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 mbedtls_free( ssl->handshake );
7914 mbedtls_free( ssl->transform_negotiate );
7915 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007916
7917 ssl->handshake = NULL;
7918 ssl->transform_negotiate = NULL;
7919 ssl->session_negotiate = NULL;
7920
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007921 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007922 }
7923
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007924 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007926 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007927 ssl_handshake_params_init( ssl->handshake );
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007930 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7931 {
7932 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007933
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007934 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7935 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7936 else
7937 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007938
7939 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007940 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007941#endif
7942
Paul Bakker48916f92012-09-16 19:57:18 +00007943 return( 0 );
7944}
7945
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007946#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007947/* Dummy cookie callbacks for defaults */
7948static int ssl_cookie_write_dummy( void *ctx,
7949 unsigned char **p, unsigned char *end,
7950 const unsigned char *cli_id, size_t cli_id_len )
7951{
7952 ((void) ctx);
7953 ((void) p);
7954 ((void) end);
7955 ((void) cli_id);
7956 ((void) cli_id_len);
7957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007959}
7960
7961static int ssl_cookie_check_dummy( void *ctx,
7962 const unsigned char *cookie, size_t cookie_len,
7963 const unsigned char *cli_id, size_t cli_id_len )
7964{
7965 ((void) ctx);
7966 ((void) cookie);
7967 ((void) cookie_len);
7968 ((void) cli_id);
7969 ((void) cli_id_len);
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007972}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007973#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007974
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007975/* Once ssl->out_hdr as the address of the beginning of the
7976 * next outgoing record is set, deduce the other pointers.
7977 *
7978 * Note: For TLS, we save the implicit record sequence number
7979 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7980 * and the caller has to make sure there's space for this.
7981 */
7982
7983static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7984 mbedtls_ssl_transform *transform )
7985{
7986#if defined(MBEDTLS_SSL_PROTO_DTLS)
7987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7988 {
7989 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01007990#if defined(MBEDTLS_SSL_CID)
7991 ssl->out_cid = ssl->out_ctr + 8;
7992 ssl->out_len = ssl->out_cid;
7993 if( transform != NULL )
7994 ssl->out_len += transform->out_cid_len;
7995#else /* MBEDTLS_SSL_CID */
7996 ssl->out_len = ssl->out_ctr + 8;
7997#endif /* MBEDTLS_SSL_CID */
7998 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007999 }
8000 else
8001#endif
8002 {
8003 ssl->out_ctr = ssl->out_hdr - 8;
8004 ssl->out_len = ssl->out_hdr + 3;
8005 ssl->out_iv = ssl->out_hdr + 5;
8006 }
8007
8008 /* Adjust out_msg to make space for explicit IV, if used. */
8009 if( transform != NULL &&
8010 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8011 {
8012 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8013 }
8014 else
8015 ssl->out_msg = ssl->out_iv;
8016}
8017
8018/* Once ssl->in_hdr as the address of the beginning of the
8019 * next incoming record is set, deduce the other pointers.
8020 *
8021 * Note: For TLS, we save the implicit record sequence number
8022 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8023 * and the caller has to make sure there's space for this.
8024 */
8025
Hanno Becker79594fd2019-05-08 09:38:41 +01008026static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008027{
Hanno Becker79594fd2019-05-08 09:38:41 +01008028 /* This function sets the pointers to match the case
8029 * of unprotected TLS/DTLS records, with both ssl->in_iv
8030 * and ssl->in_msg pointing to the beginning of the record
8031 * content.
8032 *
8033 * When decrypting a protected record, ssl->in_msg
8034 * will be shifted to point to the beginning of the
8035 * record plaintext.
8036 */
8037
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008038#if defined(MBEDTLS_SSL_PROTO_DTLS)
8039 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8040 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008041 /* This sets the header pointers to match records
8042 * without CID. When we receive a record containing
8043 * a CID, the fields are shifted accordingly in
8044 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008045 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008046#if defined(MBEDTLS_SSL_CID)
8047 ssl->in_cid = ssl->in_ctr + 8;
8048 ssl->in_len = ssl->in_cid; /* Default: no CID */
8049#else /* MBEDTLS_SSL_CID */
8050 ssl->in_len = ssl->in_ctr + 8;
8051#endif /* MBEDTLS_SSL_CID */
8052 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008053 }
8054 else
8055#endif
8056 {
8057 ssl->in_ctr = ssl->in_hdr - 8;
8058 ssl->in_len = ssl->in_hdr + 3;
8059 ssl->in_iv = ssl->in_hdr + 5;
8060 }
8061
Hanno Becker79594fd2019-05-08 09:38:41 +01008062 /* This will be adjusted at record decryption time. */
8063 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008064}
8065
Paul Bakker5121ce52009-01-03 21:22:43 +00008066/*
8067 * Initialize an SSL context
8068 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008069void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8070{
8071 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8072}
8073
8074/*
8075 * Setup an SSL context
8076 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008077
8078static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8079{
8080 /* Set the incoming and outgoing record pointers. */
8081#if defined(MBEDTLS_SSL_PROTO_DTLS)
8082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8083 {
8084 ssl->out_hdr = ssl->out_buf;
8085 ssl->in_hdr = ssl->in_buf;
8086 }
8087 else
8088#endif /* MBEDTLS_SSL_PROTO_DTLS */
8089 {
8090 ssl->out_hdr = ssl->out_buf + 8;
8091 ssl->in_hdr = ssl->in_buf + 8;
8092 }
8093
8094 /* Derive other internal pointers. */
8095 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008096 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008097}
8098
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008099int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008100 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008101{
Paul Bakker48916f92012-09-16 19:57:18 +00008102 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008103
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008104 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008105
8106 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008107 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008108 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008109
8110 /* Set to NULL in case of an error condition */
8111 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008112
Angus Grattond8213d02016-05-25 20:56:48 +10008113 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8114 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008115 {
Angus Grattond8213d02016-05-25 20:56:48 +10008116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008117 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008118 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008119 }
8120
8121 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8122 if( ssl->out_buf == NULL )
8123 {
8124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008125 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008126 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008127 }
8128
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008129 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008130
Paul Bakker48916f92012-09-16 19:57:18 +00008131 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008132 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008133
8134 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008135
8136error:
8137 mbedtls_free( ssl->in_buf );
8138 mbedtls_free( ssl->out_buf );
8139
8140 ssl->conf = NULL;
8141
8142 ssl->in_buf = NULL;
8143 ssl->out_buf = NULL;
8144
8145 ssl->in_hdr = NULL;
8146 ssl->in_ctr = NULL;
8147 ssl->in_len = NULL;
8148 ssl->in_iv = NULL;
8149 ssl->in_msg = NULL;
8150
8151 ssl->out_hdr = NULL;
8152 ssl->out_ctr = NULL;
8153 ssl->out_len = NULL;
8154 ssl->out_iv = NULL;
8155 ssl->out_msg = NULL;
8156
k-stachowiak9f7798e2018-07-31 16:52:32 +02008157 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008158}
8159
8160/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008161 * Reset an initialized and used SSL context for re-use while retaining
8162 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008163 *
8164 * If partial is non-zero, keep data in the input buffer and client ID.
8165 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008166 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008167static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008168{
Paul Bakker48916f92012-09-16 19:57:18 +00008169 int ret;
8170
Hanno Becker7e772132018-08-10 12:38:21 +01008171#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8172 !defined(MBEDTLS_SSL_SRV_C)
8173 ((void) partial);
8174#endif
8175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008177
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008178 /* Cancel any possibly running timer */
8179 ssl_set_timer( ssl, 0 );
8180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_SSL_RENEGOTIATION)
8182 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008183 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008184
8185 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8187 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008190
Paul Bakker7eb013f2011-10-06 12:37:39 +00008191 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008192 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008193
8194 ssl->in_msgtype = 0;
8195 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008197 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008198 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008201 ssl_dtls_replay_reset( ssl );
8202#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008203
8204 ssl->in_hslen = 0;
8205 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008206
8207 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008208
8209 ssl->out_msgtype = 0;
8210 ssl->out_msglen = 0;
8211 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8213 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008214 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008215#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008216
Hanno Becker19859472018-08-06 09:40:20 +01008217 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8218
Paul Bakker48916f92012-09-16 19:57:18 +00008219 ssl->transform_in = NULL;
8220 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008221
Hanno Becker78640902018-08-13 16:35:15 +01008222 ssl->session_in = NULL;
8223 ssl->session_out = NULL;
8224
Angus Grattond8213d02016-05-25 20:56:48 +10008225 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008226
8227#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008228 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008229#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8230 {
8231 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008232 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008233 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8236 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8239 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8242 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008243 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008244 }
8245#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008246
Paul Bakker48916f92012-09-16 19:57:18 +00008247 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 mbedtls_ssl_transform_free( ssl->transform );
8250 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008251 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008252 }
Paul Bakker48916f92012-09-16 19:57:18 +00008253
Paul Bakkerc0463502013-02-14 11:19:38 +01008254 if( ssl->session )
8255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 mbedtls_ssl_session_free( ssl->session );
8257 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008258 ssl->session = NULL;
8259 }
8260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008262 ssl->alpn_chosen = NULL;
8263#endif
8264
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008265#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008266#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008267 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008268#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008269 {
8270 mbedtls_free( ssl->cli_id );
8271 ssl->cli_id = NULL;
8272 ssl->cli_id_len = 0;
8273 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008274#endif
8275
Paul Bakker48916f92012-09-16 19:57:18 +00008276 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8277 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008278
8279 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008280}
8281
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008282/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008283 * Reset an initialized and used SSL context for re-use while retaining
8284 * all application-set variables, function pointers and data.
8285 */
8286int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8287{
8288 return( ssl_session_reset_int( ssl, 0 ) );
8289}
8290
8291/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008292 * SSL set accessors
8293 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008294void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008296 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008297}
8298
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008299void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008300{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008301 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008302}
8303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008305void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008307 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008308}
8309#endif
8310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008312void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008314 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008315}
8316#endif
8317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008319
Hanno Becker1841b0a2018-08-24 11:13:57 +01008320void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8321 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008322{
8323 ssl->disable_datagram_packing = !allow_packing;
8324}
8325
8326void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8327 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008328{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008329 conf->hs_timeout_min = min;
8330 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008331}
8332#endif
8333
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008334void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008335{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008336 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008337}
8338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008340void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008341 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008342 void *p_vrfy )
8343{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008344 conf->f_vrfy = f_vrfy;
8345 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008348
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008349void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008350 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008351 void *p_rng )
8352{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008353 conf->f_rng = f_rng;
8354 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008355}
8356
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008357void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008358 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008359 void *p_dbg )
8360{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008361 conf->f_dbg = f_dbg;
8362 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008363}
8364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008366 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008367 mbedtls_ssl_send_t *f_send,
8368 mbedtls_ssl_recv_t *f_recv,
8369 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008370{
8371 ssl->p_bio = p_bio;
8372 ssl->f_send = f_send;
8373 ssl->f_recv = f_recv;
8374 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008375}
8376
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008377#if defined(MBEDTLS_SSL_PROTO_DTLS)
8378void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8379{
8380 ssl->mtu = mtu;
8381}
8382#endif
8383
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008384void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008385{
8386 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008387}
8388
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008389void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8390 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008391 mbedtls_ssl_set_timer_t *f_set_timer,
8392 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008393{
8394 ssl->p_timer = p_timer;
8395 ssl->f_set_timer = f_set_timer;
8396 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008397
8398 /* Make sure we start with no timer running */
8399 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008400}
8401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008403void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008404 void *p_cache,
8405 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8406 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008407{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008408 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008409 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008410 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_SSL_CLI_C)
8415int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008416{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008417 int ret;
8418
8419 if( ssl == NULL ||
8420 session == NULL ||
8421 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008422 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008425 }
8426
Hanno Becker52055ae2019-02-06 14:30:46 +00008427 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8428 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008429 return( ret );
8430
Paul Bakker0a597072012-09-25 21:55:46 +00008431 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008432
8433 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008436
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008437void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008438 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8441 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8442 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8443 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008444}
8445
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008446void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008447 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008448 int major, int minor )
8449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008451 return;
8452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008454 return;
8455
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008456 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008457}
8458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008460void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008461 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008462{
8463 conf->cert_profile = profile;
8464}
8465
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008466/* Append a new keycert entry to a (possibly empty) list */
8467static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8468 mbedtls_x509_crt *cert,
8469 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008470{
niisato8ee24222018-06-25 19:05:48 +09008471 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008472
niisato8ee24222018-06-25 19:05:48 +09008473 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8474 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008475 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008476
niisato8ee24222018-06-25 19:05:48 +09008477 new_cert->cert = cert;
8478 new_cert->key = key;
8479 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008480
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008481 /* Update head is the list was null, else add to the end */
8482 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008483 {
niisato8ee24222018-06-25 19:05:48 +09008484 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008485 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008486 else
8487 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008488 mbedtls_ssl_key_cert *cur = *head;
8489 while( cur->next != NULL )
8490 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008491 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008492 }
8493
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008494 return( 0 );
8495}
8496
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008497int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008498 mbedtls_x509_crt *own_cert,
8499 mbedtls_pk_context *pk_key )
8500{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008501 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008502}
8503
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008504void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008505 mbedtls_x509_crt *ca_chain,
8506 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008507{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008508 conf->ca_chain = ca_chain;
8509 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008510
8511#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8512 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8513 * cannot be used together. */
8514 conf->f_ca_cb = NULL;
8515 conf->p_ca_cb = NULL;
8516#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008517}
Hanno Becker5adaad92019-03-27 16:54:37 +00008518
8519#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8520void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008521 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008522 void *p_ca_cb )
8523{
8524 conf->f_ca_cb = f_ca_cb;
8525 conf->p_ca_cb = p_ca_cb;
8526
8527 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8528 * cannot be used together. */
8529 conf->ca_chain = NULL;
8530 conf->ca_crl = NULL;
8531}
8532#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008534
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008535#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8536int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8537 mbedtls_x509_crt *own_cert,
8538 mbedtls_pk_context *pk_key )
8539{
8540 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8541 own_cert, pk_key ) );
8542}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008543
8544void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8545 mbedtls_x509_crt *ca_chain,
8546 mbedtls_x509_crl *ca_crl )
8547{
8548 ssl->handshake->sni_ca_chain = ca_chain;
8549 ssl->handshake->sni_ca_crl = ca_crl;
8550}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008551
8552void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8553 int authmode )
8554{
8555 ssl->handshake->sni_authmode = authmode;
8556}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008557#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8558
Hanno Becker8927c832019-04-03 12:52:50 +01008559#if defined(MBEDTLS_X509_CRT_PARSE_C)
8560void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8561 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8562 void *p_vrfy )
8563{
8564 ssl->f_vrfy = f_vrfy;
8565 ssl->p_vrfy = p_vrfy;
8566}
8567#endif
8568
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008569#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008570/*
8571 * Set EC J-PAKE password for current handshake
8572 */
8573int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8574 const unsigned char *pw,
8575 size_t pw_len )
8576{
8577 mbedtls_ecjpake_role role;
8578
Janos Follath8eb64132016-06-03 15:40:57 +01008579 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8581
8582 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8583 role = MBEDTLS_ECJPAKE_SERVER;
8584 else
8585 role = MBEDTLS_ECJPAKE_CLIENT;
8586
8587 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8588 role,
8589 MBEDTLS_MD_SHA256,
8590 MBEDTLS_ECP_DP_SECP256R1,
8591 pw, pw_len ) );
8592}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008593#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008596
8597static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8598{
8599 /* Remove reference to existing PSK, if any. */
8600#if defined(MBEDTLS_USE_PSA_CRYPTO)
8601 if( conf->psk_opaque != 0 )
8602 {
8603 /* The maintenance of the PSK key slot is the
8604 * user's responsibility. */
8605 conf->psk_opaque = 0;
8606 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008607 /* This and the following branch should never
8608 * be taken simultaenously as we maintain the
8609 * invariant that raw and opaque PSKs are never
8610 * configured simultaneously. As a safeguard,
8611 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008612#endif /* MBEDTLS_USE_PSA_CRYPTO */
8613 if( conf->psk != NULL )
8614 {
8615 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8616
8617 mbedtls_free( conf->psk );
8618 conf->psk = NULL;
8619 conf->psk_len = 0;
8620 }
8621
8622 /* Remove reference to PSK identity, if any. */
8623 if( conf->psk_identity != NULL )
8624 {
8625 mbedtls_free( conf->psk_identity );
8626 conf->psk_identity = NULL;
8627 conf->psk_identity_len = 0;
8628 }
8629}
8630
Hanno Becker7390c712018-11-15 13:33:04 +00008631/* This function assumes that PSK identity in the SSL config is unset.
8632 * It checks that the provided identity is well-formed and attempts
8633 * to make a copy of it in the SSL config.
8634 * On failure, the PSK identity in the config remains unset. */
8635static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8636 unsigned char const *psk_identity,
8637 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008638{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008639 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008640 if( psk_identity == NULL ||
8641 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008642 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008643 {
8644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8645 }
8646
Hanno Becker7390c712018-11-15 13:33:04 +00008647 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8648 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008649 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008650
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008651 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008652 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008653
8654 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008655}
8656
Hanno Becker7390c712018-11-15 13:33:04 +00008657int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8658 const unsigned char *psk, size_t psk_len,
8659 const unsigned char *psk_identity, size_t psk_identity_len )
8660{
8661 int ret;
8662 /* Remove opaque/raw PSK + PSK Identity */
8663 ssl_conf_remove_psk( conf );
8664
8665 /* Check and set raw PSK */
8666 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8668 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8669 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8670 conf->psk_len = psk_len;
8671 memcpy( conf->psk, psk, conf->psk_len );
8672
8673 /* Check and set PSK Identity */
8674 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8675 if( ret != 0 )
8676 ssl_conf_remove_psk( conf );
8677
8678 return( ret );
8679}
8680
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008681static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8682{
8683#if defined(MBEDTLS_USE_PSA_CRYPTO)
8684 if( ssl->handshake->psk_opaque != 0 )
8685 {
8686 ssl->handshake->psk_opaque = 0;
8687 }
8688 else
8689#endif /* MBEDTLS_USE_PSA_CRYPTO */
8690 if( ssl->handshake->psk != NULL )
8691 {
8692 mbedtls_platform_zeroize( ssl->handshake->psk,
8693 ssl->handshake->psk_len );
8694 mbedtls_free( ssl->handshake->psk );
8695 ssl->handshake->psk_len = 0;
8696 }
8697}
8698
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008699int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8700 const unsigned char *psk, size_t psk_len )
8701{
8702 if( psk == NULL || ssl->handshake == NULL )
8703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8704
8705 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8707
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008708 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008709
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008710 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008711 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008712
8713 ssl->handshake->psk_len = psk_len;
8714 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8715
8716 return( 0 );
8717}
8718
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008719#if defined(MBEDTLS_USE_PSA_CRYPTO)
8720int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008721 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008722 const unsigned char *psk_identity,
8723 size_t psk_identity_len )
8724{
Hanno Becker7390c712018-11-15 13:33:04 +00008725 int ret;
8726 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008727 ssl_conf_remove_psk( conf );
8728
Hanno Becker7390c712018-11-15 13:33:04 +00008729 /* Check and set opaque PSK */
8730 if( psk_slot == 0 )
8731 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008732 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008733
8734 /* Check and set PSK Identity */
8735 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8736 psk_identity_len );
8737 if( ret != 0 )
8738 ssl_conf_remove_psk( conf );
8739
8740 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008741}
8742
8743int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008744 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008745{
8746 if( psk_slot == 0 || ssl->handshake == NULL )
8747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8748
8749 ssl_remove_psk( ssl );
8750 ssl->handshake->psk_opaque = psk_slot;
8751 return( 0 );
8752}
8753#endif /* MBEDTLS_USE_PSA_CRYPTO */
8754
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008755void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008757 size_t),
8758 void *p_psk )
8759{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008760 conf->f_psk = f_psk;
8761 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008764
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008765#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008766
8767#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008768int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008769{
8770 int ret;
8771
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008772 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8773 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8774 {
8775 mbedtls_mpi_free( &conf->dhm_P );
8776 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008777 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008778 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008779
8780 return( 0 );
8781}
Hanno Becker470a8c42017-10-04 15:28:46 +01008782#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008783
Hanno Beckera90658f2017-10-04 15:29:08 +01008784int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8785 const unsigned char *dhm_P, size_t P_len,
8786 const unsigned char *dhm_G, size_t G_len )
8787{
8788 int ret;
8789
8790 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8791 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8792 {
8793 mbedtls_mpi_free( &conf->dhm_P );
8794 mbedtls_mpi_free( &conf->dhm_G );
8795 return( ret );
8796 }
8797
8798 return( 0 );
8799}
Paul Bakker5121ce52009-01-03 21:22:43 +00008800
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008801int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008802{
8803 int ret;
8804
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008805 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8806 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8807 {
8808 mbedtls_mpi_free( &conf->dhm_P );
8809 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008810 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008811 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008812
8813 return( 0 );
8814}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008815#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008816
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008817#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8818/*
8819 * Set the minimum length for Diffie-Hellman parameters
8820 */
8821void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8822 unsigned int bitlen )
8823{
8824 conf->dhm_min_bitlen = bitlen;
8825}
8826#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8827
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008828#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008829/*
8830 * Set allowed/preferred hashes for handshake signatures
8831 */
8832void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8833 const int *hashes )
8834{
8835 conf->sig_hashes = hashes;
8836}
Hanno Becker947194e2017-04-07 13:25:49 +01008837#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008838
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008839#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008840/*
8841 * Set the allowed elliptic curves
8842 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008843void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008844 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008845{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008846 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008847}
Hanno Becker947194e2017-04-07 13:25:49 +01008848#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008849
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008850#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008852{
Hanno Becker947194e2017-04-07 13:25:49 +01008853 /* Initialize to suppress unnecessary compiler warning */
8854 size_t hostname_len = 0;
8855
8856 /* Check if new hostname is valid before
8857 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008858 if( hostname != NULL )
8859 {
8860 hostname_len = strlen( hostname );
8861
8862 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8864 }
8865
8866 /* Now it's clear that we will overwrite the old hostname,
8867 * so we can free it safely */
8868
8869 if( ssl->hostname != NULL )
8870 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008871 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008872 mbedtls_free( ssl->hostname );
8873 }
8874
8875 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008876
Paul Bakker5121ce52009-01-03 21:22:43 +00008877 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008878 {
8879 ssl->hostname = NULL;
8880 }
8881 else
8882 {
8883 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008884 if( ssl->hostname == NULL )
8885 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008886
Hanno Becker947194e2017-04-07 13:25:49 +01008887 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008888
Hanno Becker947194e2017-04-07 13:25:49 +01008889 ssl->hostname[hostname_len] = '\0';
8890 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008891
8892 return( 0 );
8893}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008894#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008895
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008896#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008897void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008899 const unsigned char *, size_t),
8900 void *p_sni )
8901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008902 conf->f_sni = f_sni;
8903 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008907#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008908int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008909{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008910 size_t cur_len, tot_len;
8911 const char **p;
8912
8913 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008914 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8915 * MUST NOT be truncated."
8916 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008917 */
8918 tot_len = 0;
8919 for( p = protos; *p != NULL; p++ )
8920 {
8921 cur_len = strlen( *p );
8922 tot_len += cur_len;
8923
8924 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008926 }
8927
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008928 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008929
8930 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008931}
8932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008934{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008935 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008938
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008939void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008940{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008941 conf->max_major_ver = major;
8942 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008943}
8944
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008945void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008946{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008947 conf->min_major_ver = major;
8948 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008949}
8950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008952void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008953{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008954 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008955}
8956#endif
8957
Janos Follath088ce432017-04-10 12:42:31 +01008958#if defined(MBEDTLS_SSL_SRV_C)
8959void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8960 char cert_req_ca_list )
8961{
8962 conf->cert_req_ca_list = cert_req_ca_list;
8963}
8964#endif
8965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008967void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008968{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008969 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008970}
8971#endif
8972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008974void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008975{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008976 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008977}
8978#endif
8979
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008980#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008981void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008982{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008983 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008984}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008985#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008988int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008991 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008994 }
8995
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008996 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008997
8998 return( 0 );
8999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009003void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009004{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009005 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009010void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009011{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009012 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009013}
9014#endif
9015
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009016void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009017{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009018 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009019}
9020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009022void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009023{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009024 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009025}
9026
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009027void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009028{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009029 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009030}
9031
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009032void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009033 const unsigned char period[8] )
9034{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009035 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009040#if defined(MBEDTLS_SSL_CLI_C)
9041void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009042{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009043 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009044}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009045#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009046
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009047#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009048void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9049 mbedtls_ssl_ticket_write_t *f_ticket_write,
9050 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9051 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009052{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009053 conf->f_ticket_write = f_ticket_write;
9054 conf->f_ticket_parse = f_ticket_parse;
9055 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009056}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009059
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009060#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9061void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9062 mbedtls_ssl_export_keys_t *f_export_keys,
9063 void *p_export_keys )
9064{
9065 conf->f_export_keys = f_export_keys;
9066 conf->p_export_keys = p_export_keys;
9067}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009068
9069void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9070 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9071 void *p_export_keys )
9072{
9073 conf->f_export_keys_ext = f_export_keys_ext;
9074 conf->p_export_keys = p_export_keys;
9075}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009076#endif
9077
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009078#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009079void mbedtls_ssl_conf_async_private_cb(
9080 mbedtls_ssl_config *conf,
9081 mbedtls_ssl_async_sign_t *f_async_sign,
9082 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9083 mbedtls_ssl_async_resume_t *f_async_resume,
9084 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009085 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009086{
9087 conf->f_async_sign_start = f_async_sign;
9088 conf->f_async_decrypt_start = f_async_decrypt;
9089 conf->f_async_resume = f_async_resume;
9090 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009091 conf->p_async_config_data = async_config_data;
9092}
9093
Gilles Peskine8f97af72018-04-26 11:46:10 +02009094void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9095{
9096 return( conf->p_async_config_data );
9097}
9098
Gilles Peskine1febfef2018-04-30 11:54:39 +02009099void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009100{
9101 if( ssl->handshake == NULL )
9102 return( NULL );
9103 else
9104 return( ssl->handshake->user_async_ctx );
9105}
9106
Gilles Peskine1febfef2018-04-30 11:54:39 +02009107void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009108 void *ctx )
9109{
9110 if( ssl->handshake != NULL )
9111 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009112}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009113#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009114
Paul Bakker5121ce52009-01-03 21:22:43 +00009115/*
9116 * SSL get accessors
9117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009119{
9120 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9121}
9122
Hanno Becker8b170a02017-10-10 11:51:19 +01009123int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9124{
9125 /*
9126 * Case A: We're currently holding back
9127 * a message for further processing.
9128 */
9129
9130 if( ssl->keep_current_message == 1 )
9131 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009133 return( 1 );
9134 }
9135
9136 /*
9137 * Case B: Further records are pending in the current datagram.
9138 */
9139
9140#if defined(MBEDTLS_SSL_PROTO_DTLS)
9141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9142 ssl->in_left > ssl->next_record_offset )
9143 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009145 return( 1 );
9146 }
9147#endif /* MBEDTLS_SSL_PROTO_DTLS */
9148
9149 /*
9150 * Case C: A handshake message is being processed.
9151 */
9152
Hanno Becker8b170a02017-10-10 11:51:19 +01009153 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9154 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009156 return( 1 );
9157 }
9158
9159 /*
9160 * Case D: An application data message is being processed
9161 */
9162 if( ssl->in_offt != NULL )
9163 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009165 return( 1 );
9166 }
9167
9168 /*
9169 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009170 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009171 * we implement support for multiple alerts in single records.
9172 */
9173
9174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9175 return( 0 );
9176}
9177
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009178uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009179{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009180 if( ssl->session != NULL )
9181 return( ssl->session->verify_result );
9182
9183 if( ssl->session_negotiate != NULL )
9184 return( ssl->session_negotiate->verify_result );
9185
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009186 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009187}
9188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009190{
Paul Bakker926c8e42013-03-06 10:23:34 +01009191 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009192 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009195}
9196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009200 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009201 {
9202 switch( ssl->minor_ver )
9203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009205 return( "DTLSv1.0" );
9206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009208 return( "DTLSv1.2" );
9209
9210 default:
9211 return( "unknown (DTLS)" );
9212 }
9213 }
9214#endif
9215
Paul Bakker43ca69c2011-01-15 17:35:19 +00009216 switch( ssl->minor_ver )
9217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009219 return( "SSLv3.0" );
9220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009222 return( "TLSv1.0" );
9223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009225 return( "TLSv1.1" );
9226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009228 return( "TLSv1.2" );
9229
Paul Bakker43ca69c2011-01-15 17:35:19 +00009230 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009231 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009232 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009233}
9234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009236{
Hanno Becker3136ede2018-08-17 15:28:19 +01009237 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009239 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009240
Hanno Becker5903de42019-05-03 14:46:38 +01009241 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9242
Hanno Becker78640902018-08-13 16:35:15 +01009243 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009244 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246#if defined(MBEDTLS_ZLIB_SUPPORT)
9247 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9248 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009249#endif
9250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009253 case MBEDTLS_MODE_GCM:
9254 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009255 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009257 transform_expansion = transform->minlen;
9258 break;
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009261
9262 block_size = mbedtls_cipher_get_block_size(
9263 &transform->cipher_ctx_enc );
9264
Hanno Becker3136ede2018-08-17 15:28:19 +01009265 /* Expansion due to the addition of the MAC. */
9266 transform_expansion += transform->maclen;
9267
9268 /* Expansion due to the addition of CBC padding;
9269 * Theoretically up to 256 bytes, but we never use
9270 * more than the block size of the underlying cipher. */
9271 transform_expansion += block_size;
9272
9273 /* For TLS 1.1 or higher, an explicit IV is added
9274 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009275#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9276 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009277 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009278#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009279
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009280 break;
9281
9282 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009285 }
9286
Hanno Becker6cbad552019-05-08 15:40:11 +01009287#if defined(MBEDTLS_SSL_CID)
9288 if( transform->out_cid_len != 0 )
9289 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
9290#endif /* MBEDTLS_SSL_CID */
9291
Hanno Becker5903de42019-05-03 14:46:38 +01009292 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009293}
9294
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009295#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9296size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9297{
9298 size_t max_len;
9299
9300 /*
9301 * Assume mfl_code is correct since it was checked when set
9302 */
Angus Grattond8213d02016-05-25 20:56:48 +10009303 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009304
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009305 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009306 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009307 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009308 {
Angus Grattond8213d02016-05-25 20:56:48 +10009309 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009310 }
9311
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009312 /* During a handshake, use the value being negotiated */
9313 if( ssl->session_negotiate != NULL &&
9314 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9315 {
9316 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9317 }
9318
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009319 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009320}
9321#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9322
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009323#if defined(MBEDTLS_SSL_PROTO_DTLS)
9324static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9325{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009326 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9327 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9328 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9329 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9330 return ( 0 );
9331
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009332 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9333 return( ssl->mtu );
9334
9335 if( ssl->mtu == 0 )
9336 return( ssl->handshake->mtu );
9337
9338 return( ssl->mtu < ssl->handshake->mtu ?
9339 ssl->mtu : ssl->handshake->mtu );
9340}
9341#endif /* MBEDTLS_SSL_PROTO_DTLS */
9342
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009343int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9344{
9345 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9346
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009347#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9348 !defined(MBEDTLS_SSL_PROTO_DTLS)
9349 (void) ssl;
9350#endif
9351
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009352#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9353 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9354
9355 if( max_len > mfl )
9356 max_len = mfl;
9357#endif
9358
9359#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009360 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009361 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009362 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009363 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9364 const size_t overhead = (size_t) ret;
9365
9366 if( ret < 0 )
9367 return( ret );
9368
9369 if( mtu <= overhead )
9370 {
9371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9372 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9373 }
9374
9375 if( max_len > mtu - overhead )
9376 max_len = mtu - overhead;
9377 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009378#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009379
Hanno Becker0defedb2018-08-10 12:35:02 +01009380#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9381 !defined(MBEDTLS_SSL_PROTO_DTLS)
9382 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009383#endif
9384
9385 return( (int) max_len );
9386}
9387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388#if defined(MBEDTLS_X509_CRT_PARSE_C)
9389const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009390{
9391 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009392 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009393
Hanno Beckere6824572019-02-07 13:18:46 +00009394#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009395 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009396#else
9397 return( NULL );
9398#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009403int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9404 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009405{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009406 if( ssl == NULL ||
9407 dst == NULL ||
9408 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009409 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009412 }
9413
Hanno Becker52055ae2019-02-06 14:30:46 +00009414 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009417
Paul Bakker5121ce52009-01-03 21:22:43 +00009418/*
Paul Bakker1961b702013-01-25 14:49:24 +01009419 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009424
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009425 if( ssl == NULL || ssl->conf == NULL )
9426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009429 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009435#endif
9436
Paul Bakker1961b702013-01-25 14:49:24 +01009437 return( ret );
9438}
9439
9440/*
9441 * Perform the SSL handshake
9442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009444{
9445 int ret = 0;
9446
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009447 if( ssl == NULL || ssl->conf == NULL )
9448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009455
9456 if( ret != 0 )
9457 break;
9458 }
9459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009461
9462 return( ret );
9463}
9464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#if defined(MBEDTLS_SSL_RENEGOTIATION)
9466#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009467/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009468 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009471{
9472 int ret;
9473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009475
9476 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9478 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009479
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009480 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009481 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009483 return( ret );
9484 }
9485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009487
9488 return( 0 );
9489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009491
9492/*
9493 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494 * - any side: calling mbedtls_ssl_renegotiate(),
9495 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9496 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009497 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009498 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009502{
9503 int ret;
9504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009506
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009507 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9508 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009509
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009510 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9511 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009515 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009516 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009517 ssl->handshake->out_msg_seq = 1;
9518 else
9519 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009520 }
9521#endif
9522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9524 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009529 return( ret );
9530 }
9531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009533
9534 return( 0 );
9535}
9536
9537/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009538 * Renegotiate current connection on client,
9539 * or request renegotiation on server
9540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009544
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009545 if( ssl == NULL || ssl->conf == NULL )
9546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009549 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009550 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009556
9557 /* Did we already try/start sending HelloRequest? */
9558 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009560
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009561 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009566 /*
9567 * On client, either start the renegotiation process or,
9568 * if already in progress, continue the handshake
9569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009574
9575 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009578 return( ret );
9579 }
9580 }
9581 else
9582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009586 return( ret );
9587 }
9588 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009590
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009591 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009592}
9593
9594/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009595 * Check record counters and renegotiate if they're above the limit.
9596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009598{
Andres AG2196c7f2016-12-15 17:01:16 +00009599 size_t ep_len = ssl_ep_len( ssl );
9600 int in_ctr_cmp;
9601 int out_ctr_cmp;
9602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9604 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009605 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009606 {
9607 return( 0 );
9608 }
9609
Andres AG2196c7f2016-12-15 17:01:16 +00009610 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9611 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009612 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009613 ssl->conf->renego_period + ep_len, 8 - ep_len );
9614
9615 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009616 {
9617 return( 0 );
9618 }
9619
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009622}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009624
9625/*
9626 * Receive application data decrypted from the SSL layer
9627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009629{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009630 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009631 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009632
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009633 if( ssl == NULL || ssl->conf == NULL )
9634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009639 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009642 return( ret );
9643
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009644 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009646 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009647 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009648 return( ret );
9649 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009650 }
9651#endif
9652
Hanno Becker4a810fb2017-05-24 16:27:30 +01009653 /*
9654 * Check if renegotiation is necessary and/or handshake is
9655 * in process. If yes, perform/continue, and fall through
9656 * if an unexpected packet is received while the client
9657 * is waiting for the ServerHello.
9658 *
9659 * (There is no equivalent to the last condition on
9660 * the server-side as it is not treated as within
9661 * a handshake while waiting for the ClientHello
9662 * after a renegotiation request.)
9663 */
9664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009666 ret = ssl_check_ctr_renegotiate( ssl );
9667 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9668 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009671 return( ret );
9672 }
9673#endif
9674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009678 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9679 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009682 return( ret );
9683 }
9684 }
9685
Hanno Beckere41158b2017-10-23 13:30:32 +01009686 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009687 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009688 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009689 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009690 if( ssl->f_get_timer != NULL &&
9691 ssl->f_get_timer( ssl->p_timer ) == -1 )
9692 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009693 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009694 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009695
Hanno Becker327c93b2018-08-15 13:56:18 +01009696 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009697 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009698 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9699 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009700
Hanno Becker4a810fb2017-05-24 16:27:30 +01009701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9702 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009703 }
9704
9705 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009707 {
9708 /*
9709 * OpenSSL sends empty messages to randomize the IV
9710 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009711 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009714 return( 0 );
9715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009717 return( ret );
9718 }
9719 }
9720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009724
Hanno Becker4a810fb2017-05-24 16:27:30 +01009725 /*
9726 * - For client-side, expect SERVER_HELLO_REQUEST.
9727 * - For server-side, expect CLIENT_HELLO.
9728 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9729 */
9730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009734 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009737
9738 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009740 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009741 {
9742 continue;
9743 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009746 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009747#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009748
Hanno Becker4a810fb2017-05-24 16:27:30 +01009749#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009750 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009754
9755 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009757 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009758 {
9759 continue;
9760 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009763 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009764#endif /* MBEDTLS_SSL_SRV_C */
9765
Hanno Becker21df7f92017-10-17 11:03:26 +01009766#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009767 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009768 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9769 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9770 ssl->conf->allow_legacy_renegotiation ==
9771 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9772 {
9773 /*
9774 * Accept renegotiation request
9775 */
Paul Bakker48916f92012-09-16 19:57:18 +00009776
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009777 /* DTLS clients need to know renego is server-initiated */
9778#if defined(MBEDTLS_SSL_PROTO_DTLS)
9779 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9780 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9781 {
9782 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9783 }
9784#endif
9785 ret = ssl_start_renegotiation( ssl );
9786 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9787 ret != 0 )
9788 {
9789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9790 return( ret );
9791 }
9792 }
9793 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009794#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009795 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009796 /*
9797 * Refuse renegotiation
9798 */
9799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802#if defined(MBEDTLS_SSL_PROTO_SSL3)
9803 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009804 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009805 /* SSLv3 does not have a "no_renegotiation" warning, so
9806 we send a fatal alert and abort the connection. */
9807 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9808 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9809 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009810 }
9811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9813#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9814 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9815 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9818 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9819 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009820 {
9821 return( ret );
9822 }
Paul Bakker48916f92012-09-16 19:57:18 +00009823 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009824 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9826 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009830 }
Paul Bakker48916f92012-09-16 19:57:18 +00009831 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009832
Hanno Becker90333da2017-10-10 11:27:13 +01009833 /* At this point, we don't know whether the renegotiation has been
9834 * completed or not. The cases to consider are the following:
9835 * 1) The renegotiation is complete. In this case, no new record
9836 * has been read yet.
9837 * 2) The renegotiation is incomplete because the client received
9838 * an application data record while awaiting the ServerHello.
9839 * 3) The renegotiation is incomplete because the client received
9840 * a non-handshake, non-application data message while awaiting
9841 * the ServerHello.
9842 * In each of these case, looping will be the proper action:
9843 * - For 1), the next iteration will read a new record and check
9844 * if it's application data.
9845 * - For 2), the loop condition isn't satisfied as application data
9846 * is present, hence continue is the same as break
9847 * - For 3), the loop condition is satisfied and read_record
9848 * will re-deliver the message that was held back by the client
9849 * when expecting the ServerHello.
9850 */
9851 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009852 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009853#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009855 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009856 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009857 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009858 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009861 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009863 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009864 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009865 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9869 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009872 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009873 }
9874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9878 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009879 }
9880
9881 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009882
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009883 /* We're going to return something now, cancel timer,
9884 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009886 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009887
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009889 /* If we requested renego but received AppData, resend HelloRequest.
9890 * Do it now, after setting in_offt, to avoid taking this branch
9891 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009895 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009896 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009899 return( ret );
9900 }
9901 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009904 }
9905
9906 n = ( len < ssl->in_msglen )
9907 ? len : ssl->in_msglen;
9908
9909 memcpy( buf, ssl->in_offt, n );
9910 ssl->in_msglen -= n;
9911
9912 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009913 {
9914 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009915 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009916 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009918 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009919 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009920 /* more data available */
9921 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009925
Paul Bakker23986e52011-04-24 08:57:21 +00009926 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009927}
9928
9929/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009930 * Send application data to be encrypted by the SSL layer, taking care of max
9931 * fragment length and buffer size.
9932 *
9933 * According to RFC 5246 Section 6.2.1:
9934 *
9935 * Zero-length fragments of Application data MAY be sent as they are
9936 * potentially useful as a traffic analysis countermeasure.
9937 *
9938 * Therefore, it is possible that the input message length is 0 and the
9939 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009940 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009941static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009942 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009943{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009944 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9945 const size_t max_len = (size_t) ret;
9946
9947 if( ret < 0 )
9948 {
9949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9950 return( ret );
9951 }
9952
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009953 if( len > max_len )
9954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009959 "maximum fragment length: %d > %d",
9960 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009962 }
9963 else
9964#endif
9965 len = max_len;
9966 }
Paul Bakker887bd502011-06-08 13:10:54 +00009967
Paul Bakker5121ce52009-01-03 21:22:43 +00009968 if( ssl->out_left != 0 )
9969 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009970 /*
9971 * The user has previously tried to send the data and
9972 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9973 * written. In this case, we expect the high-level write function
9974 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009979 return( ret );
9980 }
9981 }
Paul Bakker887bd502011-06-08 13:10:54 +00009982 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009983 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009984 /*
9985 * The user is trying to send a message the first time, so we need to
9986 * copy the data into the internal buffers and setup the data structure
9987 * to keep track of partial writes
9988 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009989 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009991 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009992
Hanno Becker67bc7c32018-08-06 11:33:50 +01009993 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009996 return( ret );
9997 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009998 }
9999
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010000 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010001}
10002
10003/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010004 * Write application data, doing 1/n-1 splitting if necessary.
10005 *
10006 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010007 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010008 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010011static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010012 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010013{
10014 int ret;
10015
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010016 if( ssl->conf->cbc_record_splitting ==
10017 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010018 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10020 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10021 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010022 {
10023 return( ssl_write_real( ssl, buf, len ) );
10024 }
10025
10026 if( ssl->split_done == 0 )
10027 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010028 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010029 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010030 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010031 }
10032
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010033 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10034 return( ret );
10035 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010036
10037 return( ret + 1 );
10038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010040
10041/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010042 * Write application data (public-facing wrapper)
10043 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010044int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010045{
10046 int ret;
10047
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010049
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010050 if( ssl == NULL || ssl->conf == NULL )
10051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10052
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010053#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010054 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10055 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010056 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010057 return( ret );
10058 }
10059#endif
10060
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010061 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010062 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010063 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010064 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010066 return( ret );
10067 }
10068 }
10069
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010070#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010071 ret = ssl_write_split( ssl, buf, len );
10072#else
10073 ret = ssl_write_real( ssl, buf, len );
10074#endif
10075
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010077
10078 return( ret );
10079}
10080
10081/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010082 * Notify the peer that the connection is being closed
10083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010085{
10086 int ret;
10087
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010088 if( ssl == NULL || ssl->conf == NULL )
10089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010092
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010093 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10099 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10100 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010103 return( ret );
10104 }
10105 }
10106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010108
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010109 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010110}
10111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010113{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010114 if( transform == NULL )
10115 return;
10116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010118 deflateEnd( &transform->ctx_deflate );
10119 inflateEnd( &transform->ctx_inflate );
10120#endif
10121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10123 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010124
Hanno Beckerd56ed242018-01-03 15:32:51 +000010125#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 mbedtls_md_free( &transform->md_ctx_enc );
10127 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010128#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010129
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010130 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010131}
10132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133#if defined(MBEDTLS_X509_CRT_PARSE_C)
10134static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010137
10138 while( cur != NULL )
10139 {
10140 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010141 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010142 cur = next;
10143 }
10144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010145#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010146
Hanno Becker0271f962018-08-16 13:23:47 +010010147#if defined(MBEDTLS_SSL_PROTO_DTLS)
10148
10149static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10150{
10151 unsigned offset;
10152 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10153
10154 if( hs == NULL )
10155 return;
10156
Hanno Becker283f5ef2018-08-24 09:34:47 +010010157 ssl_free_buffered_record( ssl );
10158
Hanno Becker0271f962018-08-16 13:23:47 +010010159 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010160 ssl_buffering_free_slot( ssl, offset );
10161}
10162
10163static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10164 uint8_t slot )
10165{
10166 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10167 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010168
10169 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10170 return;
10171
Hanno Beckere605b192018-08-21 15:59:07 +010010172 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010173 {
Hanno Beckere605b192018-08-21 15:59:07 +010010174 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010175 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010176 mbedtls_free( hs_buf->data );
10177 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010178 }
10179}
10180
10181#endif /* MBEDTLS_SSL_PROTO_DTLS */
10182
Gilles Peskine9b562d52018-04-25 20:32:43 +020010183void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010184{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010185 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10186
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010187 if( handshake == NULL )
10188 return;
10189
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010190#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10191 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10192 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010193 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010194 handshake->async_in_progress = 0;
10195 }
10196#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10197
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010198#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10199 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10200 mbedtls_md5_free( &handshake->fin_md5 );
10201 mbedtls_sha1_free( &handshake->fin_sha1 );
10202#endif
10203#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10204#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010205#if defined(MBEDTLS_USE_PSA_CRYPTO)
10206 psa_hash_abort( &handshake->fin_sha256_psa );
10207#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010208 mbedtls_sha256_free( &handshake->fin_sha256 );
10209#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010210#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010211#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010212#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010213 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010214#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010215 mbedtls_sha512_free( &handshake->fin_sha512 );
10216#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010217#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010218#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#if defined(MBEDTLS_DHM_C)
10221 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223#if defined(MBEDTLS_ECDH_C)
10224 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010225#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010226#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010227 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010228#if defined(MBEDTLS_SSL_CLI_C)
10229 mbedtls_free( handshake->ecjpake_cache );
10230 handshake->ecjpake_cache = NULL;
10231 handshake->ecjpake_cache_len = 0;
10232#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010233#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010234
Janos Follath4ae5c292016-02-10 11:27:43 +000010235#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10236 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010237 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010239#endif
10240
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010241#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10242 if( handshake->psk != NULL )
10243 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010244 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010245 mbedtls_free( handshake->psk );
10246 }
10247#endif
10248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10250 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010251 /*
10252 * Free only the linked list wrapper, not the keys themselves
10253 * since the belong to the SNI callback
10254 */
10255 if( handshake->sni_key_cert != NULL )
10256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010258
10259 while( cur != NULL )
10260 {
10261 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010263 cur = next;
10264 }
10265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010267
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010268#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010269 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010270 if( handshake->ecrs_peer_cert != NULL )
10271 {
10272 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10273 mbedtls_free( handshake->ecrs_peer_cert );
10274 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010275#endif
10276
Hanno Becker75173122019-02-06 16:18:31 +000010277#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10278 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10279 mbedtls_pk_free( &handshake->peer_pubkey );
10280#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282#if defined(MBEDTLS_SSL_PROTO_DTLS)
10283 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010284 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010285 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010286#endif
10287
Hanno Becker4a63ed42019-01-08 11:39:35 +000010288#if defined(MBEDTLS_ECDH_C) && \
10289 defined(MBEDTLS_USE_PSA_CRYPTO)
10290 psa_destroy_key( handshake->ecdh_psa_privkey );
10291#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10292
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010293 mbedtls_platform_zeroize( handshake,
10294 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010295}
10296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010298{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010299 if( session == NULL )
10300 return;
10301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010303 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010304#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010305
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010306#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010308#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010309
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010310 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010311}
10312
Paul Bakker5121ce52009-01-03 21:22:43 +000010313/*
10314 * Free an SSL context
10315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010317{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010318 if( ssl == NULL )
10319 return;
10320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010322
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010323 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010324 {
Angus Grattond8213d02016-05-25 20:56:48 +100010325 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010327 }
10328
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010329 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010330 {
Angus Grattond8213d02016-05-25 20:56:48 +100010331 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010333 }
10334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010336 if( ssl->compress_buf != NULL )
10337 {
Angus Grattond8213d02016-05-25 20:56:48 +100010338 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010340 }
10341#endif
10342
Paul Bakker48916f92012-09-16 19:57:18 +000010343 if( ssl->transform )
10344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 mbedtls_ssl_transform_free( ssl->transform );
10346 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010347 }
10348
10349 if( ssl->handshake )
10350 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010351 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10353 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355 mbedtls_free( ssl->handshake );
10356 mbedtls_free( ssl->transform_negotiate );
10357 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010358 }
10359
Paul Bakkerc0463502013-02-14 11:19:38 +010010360 if( ssl->session )
10361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362 mbedtls_ssl_session_free( ssl->session );
10363 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010364 }
10365
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010366#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010367 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010368 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010369 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010371 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10375 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10378 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010379 }
10380#endif
10381
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010382#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010384#endif
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010387
Paul Bakker86f04f42013-02-14 11:20:09 +010010388 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010389 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010390}
10391
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010392/*
10393 * Initialze mbedtls_ssl_config
10394 */
10395void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10396{
10397 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10398}
10399
Simon Butcherc97b6972015-12-27 23:48:17 +000010400#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010401static int ssl_preset_default_hashes[] = {
10402#if defined(MBEDTLS_SHA512_C)
10403 MBEDTLS_MD_SHA512,
10404 MBEDTLS_MD_SHA384,
10405#endif
10406#if defined(MBEDTLS_SHA256_C)
10407 MBEDTLS_MD_SHA256,
10408 MBEDTLS_MD_SHA224,
10409#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010410#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010411 MBEDTLS_MD_SHA1,
10412#endif
10413 MBEDTLS_MD_NONE
10414};
Simon Butcherc97b6972015-12-27 23:48:17 +000010415#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010416
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010417static int ssl_preset_suiteb_ciphersuites[] = {
10418 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10419 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10420 0
10421};
10422
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010423#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010424static int ssl_preset_suiteb_hashes[] = {
10425 MBEDTLS_MD_SHA256,
10426 MBEDTLS_MD_SHA384,
10427 MBEDTLS_MD_NONE
10428};
10429#endif
10430
10431#if defined(MBEDTLS_ECP_C)
10432static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10433 MBEDTLS_ECP_DP_SECP256R1,
10434 MBEDTLS_ECP_DP_SECP384R1,
10435 MBEDTLS_ECP_DP_NONE
10436};
10437#endif
10438
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010439/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010440 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010441 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010442int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010443 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010444{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010445#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010446 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010447#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010448
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010449 /* Use the functions here so that they are covered in tests,
10450 * but otherwise access member directly for efficiency */
10451 mbedtls_ssl_conf_endpoint( conf, endpoint );
10452 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010453
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010454 /*
10455 * Things that are common to all presets
10456 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010457#if defined(MBEDTLS_SSL_CLI_C)
10458 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10459 {
10460 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10461#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10462 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10463#endif
10464 }
10465#endif
10466
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010467#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010468 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010469#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010470
10471#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10472 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10473#endif
10474
10475#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10476 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10477#endif
10478
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010479#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10480 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10481#endif
10482
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010483#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010484 conf->f_cookie_write = ssl_cookie_write_dummy;
10485 conf->f_cookie_check = ssl_cookie_check_dummy;
10486#endif
10487
10488#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10489 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10490#endif
10491
Janos Follath088ce432017-04-10 12:42:31 +010010492#if defined(MBEDTLS_SSL_SRV_C)
10493 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10494#endif
10495
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010496#if defined(MBEDTLS_SSL_PROTO_DTLS)
10497 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10498 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10499#endif
10500
10501#if defined(MBEDTLS_SSL_RENEGOTIATION)
10502 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010503 memset( conf->renego_period, 0x00, 2 );
10504 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010505#endif
10506
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010507#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10508 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10509 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010510 const unsigned char dhm_p[] =
10511 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10512 const unsigned char dhm_g[] =
10513 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10514
Hanno Beckera90658f2017-10-04 15:29:08 +010010515 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10516 dhm_p, sizeof( dhm_p ),
10517 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010518 {
10519 return( ret );
10520 }
10521 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010522#endif
10523
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010524 /*
10525 * Preset-specific defaults
10526 */
10527 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010528 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010529 /*
10530 * NSA Suite B
10531 */
10532 case MBEDTLS_SSL_PRESET_SUITEB:
10533 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10534 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10535 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10536 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10537
10538 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10539 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10540 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10541 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10542 ssl_preset_suiteb_ciphersuites;
10543
10544#if defined(MBEDTLS_X509_CRT_PARSE_C)
10545 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010546#endif
10547
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010548#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010549 conf->sig_hashes = ssl_preset_suiteb_hashes;
10550#endif
10551
10552#if defined(MBEDTLS_ECP_C)
10553 conf->curve_list = ssl_preset_suiteb_curves;
10554#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010555 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010556
10557 /*
10558 * Default
10559 */
10560 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010561 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10562 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10563 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10564 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10565 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10566 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10567 MBEDTLS_SSL_MIN_MINOR_VERSION :
10568 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010569 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10570 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10571
10572#if defined(MBEDTLS_SSL_PROTO_DTLS)
10573 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10574 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10575#endif
10576
10577 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10578 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10579 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10580 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10581 mbedtls_ssl_list_ciphersuites();
10582
10583#if defined(MBEDTLS_X509_CRT_PARSE_C)
10584 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10585#endif
10586
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010587#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010588 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010589#endif
10590
10591#if defined(MBEDTLS_ECP_C)
10592 conf->curve_list = mbedtls_ecp_grp_id_list();
10593#endif
10594
10595#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10596 conf->dhm_min_bitlen = 1024;
10597#endif
10598 }
10599
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010600 return( 0 );
10601}
10602
10603/*
10604 * Free mbedtls_ssl_config
10605 */
10606void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10607{
10608#if defined(MBEDTLS_DHM_C)
10609 mbedtls_mpi_free( &conf->dhm_P );
10610 mbedtls_mpi_free( &conf->dhm_G );
10611#endif
10612
10613#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10614 if( conf->psk != NULL )
10615 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010616 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010617 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010618 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010619 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010620 }
10621
10622 if( conf->psk_identity != NULL )
10623 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010624 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010625 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010626 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010627 conf->psk_identity_len = 0;
10628 }
10629#endif
10630
10631#if defined(MBEDTLS_X509_CRT_PARSE_C)
10632 ssl_key_cert_free( conf->key_cert );
10633#endif
10634
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010635 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010636}
10637
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010638#if defined(MBEDTLS_PK_C) && \
10639 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010640/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645#if defined(MBEDTLS_RSA_C)
10646 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10647 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010648#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649#if defined(MBEDTLS_ECDSA_C)
10650 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10651 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010652#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010654}
10655
Hanno Becker7e5437a2017-04-28 17:15:26 +010010656unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10657{
10658 switch( type ) {
10659 case MBEDTLS_PK_RSA:
10660 return( MBEDTLS_SSL_SIG_RSA );
10661 case MBEDTLS_PK_ECDSA:
10662 case MBEDTLS_PK_ECKEY:
10663 return( MBEDTLS_SSL_SIG_ECDSA );
10664 default:
10665 return( MBEDTLS_SSL_SIG_ANON );
10666 }
10667}
10668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010670{
10671 switch( sig )
10672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673#if defined(MBEDTLS_RSA_C)
10674 case MBEDTLS_SSL_SIG_RSA:
10675 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010676#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677#if defined(MBEDTLS_ECDSA_C)
10678 case MBEDTLS_SSL_SIG_ECDSA:
10679 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010680#endif
10681 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010683 }
10684}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010685#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010686
Hanno Becker7e5437a2017-04-28 17:15:26 +010010687#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10688 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10689
10690/* Find an entry in a signature-hash set matching a given hash algorithm. */
10691mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10692 mbedtls_pk_type_t sig_alg )
10693{
10694 switch( sig_alg )
10695 {
10696 case MBEDTLS_PK_RSA:
10697 return( set->rsa );
10698 case MBEDTLS_PK_ECDSA:
10699 return( set->ecdsa );
10700 default:
10701 return( MBEDTLS_MD_NONE );
10702 }
10703}
10704
10705/* Add a signature-hash-pair to a signature-hash set */
10706void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10707 mbedtls_pk_type_t sig_alg,
10708 mbedtls_md_type_t md_alg )
10709{
10710 switch( sig_alg )
10711 {
10712 case MBEDTLS_PK_RSA:
10713 if( set->rsa == MBEDTLS_MD_NONE )
10714 set->rsa = md_alg;
10715 break;
10716
10717 case MBEDTLS_PK_ECDSA:
10718 if( set->ecdsa == MBEDTLS_MD_NONE )
10719 set->ecdsa = md_alg;
10720 break;
10721
10722 default:
10723 break;
10724 }
10725}
10726
10727/* Allow exactly one hash algorithm for each signature. */
10728void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10729 mbedtls_md_type_t md_alg )
10730{
10731 set->rsa = md_alg;
10732 set->ecdsa = md_alg;
10733}
10734
10735#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10736 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10737
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010738/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010739 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010742{
10743 switch( hash )
10744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745#if defined(MBEDTLS_MD5_C)
10746 case MBEDTLS_SSL_HASH_MD5:
10747 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749#if defined(MBEDTLS_SHA1_C)
10750 case MBEDTLS_SSL_HASH_SHA1:
10751 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753#if defined(MBEDTLS_SHA256_C)
10754 case MBEDTLS_SSL_HASH_SHA224:
10755 return( MBEDTLS_MD_SHA224 );
10756 case MBEDTLS_SSL_HASH_SHA256:
10757 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759#if defined(MBEDTLS_SHA512_C)
10760 case MBEDTLS_SSL_HASH_SHA384:
10761 return( MBEDTLS_MD_SHA384 );
10762 case MBEDTLS_SSL_HASH_SHA512:
10763 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010764#endif
10765 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010766 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010767 }
10768}
10769
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010770/*
10771 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10772 */
10773unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10774{
10775 switch( md )
10776 {
10777#if defined(MBEDTLS_MD5_C)
10778 case MBEDTLS_MD_MD5:
10779 return( MBEDTLS_SSL_HASH_MD5 );
10780#endif
10781#if defined(MBEDTLS_SHA1_C)
10782 case MBEDTLS_MD_SHA1:
10783 return( MBEDTLS_SSL_HASH_SHA1 );
10784#endif
10785#if defined(MBEDTLS_SHA256_C)
10786 case MBEDTLS_MD_SHA224:
10787 return( MBEDTLS_SSL_HASH_SHA224 );
10788 case MBEDTLS_MD_SHA256:
10789 return( MBEDTLS_SSL_HASH_SHA256 );
10790#endif
10791#if defined(MBEDTLS_SHA512_C)
10792 case MBEDTLS_MD_SHA384:
10793 return( MBEDTLS_SSL_HASH_SHA384 );
10794 case MBEDTLS_MD_SHA512:
10795 return( MBEDTLS_SSL_HASH_SHA512 );
10796#endif
10797 default:
10798 return( MBEDTLS_SSL_HASH_NONE );
10799 }
10800}
10801
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010802#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010803/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010804 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010805 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010806 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010807int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010810
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010811 if( ssl->conf->curve_list == NULL )
10812 return( -1 );
10813
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010814 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010815 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010816 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010817
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010818 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010819}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010820#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010821
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010822#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010823/*
10824 * Check if a hash proposed by the peer is in our list.
10825 * Return 0 if we're willing to use it, -1 otherwise.
10826 */
10827int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10828 mbedtls_md_type_t md )
10829{
10830 const int *cur;
10831
10832 if( ssl->conf->sig_hashes == NULL )
10833 return( -1 );
10834
10835 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10836 if( *cur == (int) md )
10837 return( 0 );
10838
10839 return( -1 );
10840}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010841#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010843#if defined(MBEDTLS_X509_CRT_PARSE_C)
10844int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10845 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010846 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010847 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010848{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010849 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010850#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010851 int usage = 0;
10852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010854 const char *ext_oid;
10855 size_t ext_len;
10856#endif
10857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10859 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010860 ((void) cert);
10861 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010862 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010863#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10866 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010867 {
10868 /* Server part of the key exchange */
10869 switch( ciphersuite->key_exchange )
10870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871 case MBEDTLS_KEY_EXCHANGE_RSA:
10872 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010873 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010874 break;
10875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10877 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10878 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10879 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010880 break;
10881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010882 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10883 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010884 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010885 break;
10886
10887 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888 case MBEDTLS_KEY_EXCHANGE_NONE:
10889 case MBEDTLS_KEY_EXCHANGE_PSK:
10890 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10891 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010892 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010893 usage = 0;
10894 }
10895 }
10896 else
10897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10899 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010900 }
10901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010903 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010904 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010905 ret = -1;
10906 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010907#else
10908 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010909#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10912 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10915 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010916 }
10917 else
10918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10920 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010921 }
10922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010923 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010924 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010925 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010926 ret = -1;
10927 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010928#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010929
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010930 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010933
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010934/*
10935 * Convert version numbers to/from wire format
10936 * and, for DTLS, to/from TLS equivalent.
10937 *
10938 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010939 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010940 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10941 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010943void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010944 unsigned char ver[2] )
10945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946#if defined(MBEDTLS_SSL_PROTO_DTLS)
10947 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010950 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10951
10952 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10953 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10954 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010955 else
10956#else
10957 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010958#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010959 {
10960 ver[0] = (unsigned char) major;
10961 ver[1] = (unsigned char) minor;
10962 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010963}
10964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010966 const unsigned char ver[2] )
10967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968#if defined(MBEDTLS_SSL_PROTO_DTLS)
10969 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010970 {
10971 *major = 255 - ver[0] + 2;
10972 *minor = 255 - ver[1] + 1;
10973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010975 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10976 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010977 else
10978#else
10979 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010980#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010981 {
10982 *major = ver[0];
10983 *minor = ver[1];
10984 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010985}
10986
Simon Butcher99000142016-10-13 17:21:01 +010010987int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10988{
10989#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10990 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10991 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10992
10993 switch( md )
10994 {
10995#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10996#if defined(MBEDTLS_MD5_C)
10997 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010998 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010999#endif
11000#if defined(MBEDTLS_SHA1_C)
11001 case MBEDTLS_SSL_HASH_SHA1:
11002 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11003 break;
11004#endif
11005#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11006#if defined(MBEDTLS_SHA512_C)
11007 case MBEDTLS_SSL_HASH_SHA384:
11008 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11009 break;
11010#endif
11011#if defined(MBEDTLS_SHA256_C)
11012 case MBEDTLS_SSL_HASH_SHA256:
11013 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11014 break;
11015#endif
11016 default:
11017 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11018 }
11019
11020 return 0;
11021#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11022 (void) ssl;
11023 (void) md;
11024
11025 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11026#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11027}
11028
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011029#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11030 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11031int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11032 unsigned char *output,
11033 unsigned char *data, size_t data_len )
11034{
11035 int ret = 0;
11036 mbedtls_md5_context mbedtls_md5;
11037 mbedtls_sha1_context mbedtls_sha1;
11038
11039 mbedtls_md5_init( &mbedtls_md5 );
11040 mbedtls_sha1_init( &mbedtls_sha1 );
11041
11042 /*
11043 * digitally-signed struct {
11044 * opaque md5_hash[16];
11045 * opaque sha_hash[20];
11046 * };
11047 *
11048 * md5_hash
11049 * MD5(ClientHello.random + ServerHello.random
11050 * + ServerParams);
11051 * sha_hash
11052 * SHA(ClientHello.random + ServerHello.random
11053 * + ServerParams);
11054 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011055 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011056 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011058 goto exit;
11059 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011060 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011061 ssl->handshake->randbytes, 64 ) ) != 0 )
11062 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011064 goto exit;
11065 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011066 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011067 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011069 goto exit;
11070 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011071 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011072 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011074 goto exit;
11075 }
11076
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011077 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011078 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011080 goto exit;
11081 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011082 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011083 ssl->handshake->randbytes, 64 ) ) != 0 )
11084 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011086 goto exit;
11087 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011088 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011089 data_len ) ) != 0 )
11090 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011092 goto exit;
11093 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011094 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011095 output + 16 ) ) != 0 )
11096 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011098 goto exit;
11099 }
11100
11101exit:
11102 mbedtls_md5_free( &mbedtls_md5 );
11103 mbedtls_sha1_free( &mbedtls_sha1 );
11104
11105 if( ret != 0 )
11106 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11107 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11108
11109 return( ret );
11110
11111}
11112#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11113 MBEDTLS_SSL_PROTO_TLS1_1 */
11114
11115#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11116 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011117
11118#if defined(MBEDTLS_USE_PSA_CRYPTO)
11119int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11120 unsigned char *hash, size_t *hashlen,
11121 unsigned char *data, size_t data_len,
11122 mbedtls_md_type_t md_alg )
11123{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011124 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011125 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011126 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11127
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011129
11130 if( ( status = psa_hash_setup( &hash_operation,
11131 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011132 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011133 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011134 goto exit;
11135 }
11136
Andrzej Kurek814feff2019-01-14 04:35:19 -050011137 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11138 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011139 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011140 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011141 goto exit;
11142 }
11143
Andrzej Kurek814feff2019-01-14 04:35:19 -050011144 if( ( status = psa_hash_update( &hash_operation,
11145 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011146 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011147 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011148 goto exit;
11149 }
11150
Andrzej Kurek814feff2019-01-14 04:35:19 -050011151 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11152 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011153 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011154 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011155 goto exit;
11156 }
11157
11158exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011159 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011160 {
11161 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11162 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011163 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011164 {
11165 case PSA_ERROR_NOT_SUPPORTED:
11166 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011167 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011168 case PSA_ERROR_BUFFER_TOO_SMALL:
11169 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11170 case PSA_ERROR_INSUFFICIENT_MEMORY:
11171 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11172 default:
11173 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11174 }
11175 }
11176 return( 0 );
11177}
11178
11179#else
11180
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011181int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011182 unsigned char *hash, size_t *hashlen,
11183 unsigned char *data, size_t data_len,
11184 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011185{
11186 int ret = 0;
11187 mbedtls_md_context_t ctx;
11188 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011189 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011190
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011192
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011193 mbedtls_md_init( &ctx );
11194
11195 /*
11196 * digitally-signed struct {
11197 * opaque client_random[32];
11198 * opaque server_random[32];
11199 * ServerDHParams params;
11200 * };
11201 */
11202 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11203 {
11204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11205 goto exit;
11206 }
11207 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11208 {
11209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11210 goto exit;
11211 }
11212 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11213 {
11214 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11215 goto exit;
11216 }
11217 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11218 {
11219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11220 goto exit;
11221 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011222 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011223 {
11224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11225 goto exit;
11226 }
11227
11228exit:
11229 mbedtls_md_free( &ctx );
11230
11231 if( ret != 0 )
11232 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11233 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11234
11235 return( ret );
11236}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011237#endif /* MBEDTLS_USE_PSA_CRYPTO */
11238
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011239#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11240 MBEDTLS_SSL_PROTO_TLS1_2 */
11241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011242#endif /* MBEDTLS_SSL_TLS_C */