blob: 7e7d5a0b433c361516445ed40b19f52412bb83a3 [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
170int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
171 int *enabled,
172 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
173 size_t *peer_cid_len )
174{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100175 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100176
Hanno Becker76a79ab2019-05-03 14:38:32 +0100177 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
178 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
179 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100181 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100182
Hanno Beckerc5f24222019-05-03 12:54:52 +0100183 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
184 * were used, but client and server requested the empty CID.
185 * This is indistinguishable from not using the CID extension
186 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100187 if( ssl->transform_in->in_cid_len == 0 &&
188 ssl->transform_in->out_cid_len == 0 )
189 {
190 return( 0 );
191 }
192
193 *peer_cid_len = ssl->transform_in->out_cid_len;
194 memcpy( peer_cid, ssl->transform_in->out_cid,
195 ssl->transform_in->out_cid_len );
196
197 *enabled = MBEDTLS_SSL_CID_ENABLED;
198
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100199 return( 0 );
200}
Hanno Becker35c36a62019-04-23 12:31:42 +0100201#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202
Hanno Beckerd5847772018-08-28 10:09:23 +0100203/* Forward declarations for functions related to message buffering. */
204static void ssl_buffering_free( mbedtls_ssl_context *ssl );
205static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
206 uint8_t slot );
207static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
208static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
209static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
210static int ssl_buffer_message( mbedtls_ssl_context *ssl );
211static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100212static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100213
Hanno Beckera67dee22018-08-22 10:05:20 +0100214static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100215static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100216{
Hanno Becker11682cc2018-08-22 14:41:02 +0100217 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100218
219 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100220 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100221
222 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
223}
224
Hanno Becker67bc7c32018-08-06 11:33:50 +0100225static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
226{
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 size_t const bytes_written = ssl->out_left;
228 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100229
230 /* Double-check that the write-index hasn't gone
231 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100232 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233 {
234 /* Should never happen... */
235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
236 }
237
238 return( (int) ( mtu - bytes_written ) );
239}
240
241static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
242{
243 int ret;
244 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400245 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100246
247#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
248 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
249
250 if( max_len > mfl )
251 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100252
253 /* By the standard (RFC 6066 Sect. 4), the MFL extension
254 * only limits the maximum record payload size, so in theory
255 * we would be allowed to pack multiple records of payload size
256 * MFL into a single datagram. However, this would mean that there's
257 * no way to explicitly communicate MTU restrictions to the peer.
258 *
259 * The following reduction of max_len makes sure that we never
260 * write datagrams larger than MFL + Record Expansion Overhead.
261 */
262 if( max_len <= ssl->out_left )
263 return( 0 );
264
265 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100266#endif
267
268 ret = ssl_get_remaining_space_in_datagram( ssl );
269 if( ret < 0 )
270 return( ret );
271 remaining = (size_t) ret;
272
273 ret = mbedtls_ssl_get_record_expansion( ssl );
274 if( ret < 0 )
275 return( ret );
276 expansion = (size_t) ret;
277
278 if( remaining <= expansion )
279 return( 0 );
280
281 remaining -= expansion;
282 if( remaining >= max_len )
283 remaining = max_len;
284
285 return( (int) remaining );
286}
287
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200288/*
289 * Double the retransmit timeout value, within the allowed range,
290 * returning -1 if the maximum value has already been reached.
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200293{
294 uint32_t new_timeout;
295
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200296 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200297 return( -1 );
298
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200299 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
300 * in the following way: after the initial transmission and a first
301 * retransmission, back off to a temporary estimated MTU of 508 bytes.
302 * This value is guaranteed to be deliverable (if not guaranteed to be
303 * delivered) of any compliant IPv4 (and IPv6) network, and should work
304 * on most non-IP stacks too. */
305 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400306 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200307 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
309 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200310
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200311 new_timeout = 2 * ssl->handshake->retransmit_timeout;
312
313 /* Avoid arithmetic overflow and range overflow */
314 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200315 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200316 {
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 }
319
320 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200322 ssl->handshake->retransmit_timeout ) );
323
324 return( 0 );
325}
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200328{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200329 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200331 ssl->handshake->retransmit_timeout ) );
332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200336/*
337 * Convert max_fragment_length codes to length.
338 * RFC 6066 says:
339 * enum{
340 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
341 * } MaxFragmentLength;
342 * and we add 0 -> extension unused
343 */
Angus Grattond8213d02016-05-25 20:56:48 +1000344static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200345{
Angus Grattond8213d02016-05-25 20:56:48 +1000346 switch( mfl )
347 {
348 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
349 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
350 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
351 return 512;
352 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
353 return 1024;
354 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
355 return 2048;
356 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
357 return 4096;
358 default:
359 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
360 }
361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200363
Hanno Becker52055ae2019-02-06 14:30:46 +0000364int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
365 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_ssl_session_free( dst );
368 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000371
372#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373 if( src->peer_cert != NULL )
374 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200375 int ret;
376
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200377 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200378 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200384 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387 dst->peer_cert = NULL;
388 return( ret );
389 }
390 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000391#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000392 if( src->peer_cert_digest != NULL )
393 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000394 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000395 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000396 if( dst->peer_cert_digest == NULL )
397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
398
399 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
400 src->peer_cert_digest_len );
401 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000402 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000403 }
404#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200408#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409 if( src->ticket != NULL )
410 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200411 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200412 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200413 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414
415 memcpy( dst->ticket, src->ticket, src->ticket_len );
416 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200417#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200418
419 return( 0 );
420}
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
423int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200424 const unsigned char *key_enc, const unsigned char *key_dec,
425 size_t keylen,
426 const unsigned char *iv_enc, const unsigned char *iv_dec,
427 size_t ivlen,
428 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200429 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
431int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
432int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
433int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
434int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
435#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000436
Paul Bakker5121ce52009-01-03 21:22:43 +0000437/*
438 * Key material generation
439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200441static int ssl3_prf( const unsigned char *secret, size_t slen,
442 const char *label,
443 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000444 unsigned char *dstbuf, size_t dlen )
445{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100446 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000447 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200448 mbedtls_md5_context md5;
449 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000450 unsigned char padding[16];
451 unsigned char sha1sum[20];
452 ((void)label);
453
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200454 mbedtls_md5_init( &md5 );
455 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200456
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 /*
458 * SSLv3:
459 * block =
460 * MD5( secret + SHA1( 'A' + secret + random ) ) +
461 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
462 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
463 * ...
464 */
465 for( i = 0; i < dlen / 16; i++ )
466 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200467 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000468
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100469 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100470 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 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, secret, slen ) ) != 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, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000479
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 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, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100486 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000488 }
489
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200491 mbedtls_md5_free( &md5 );
492 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000493
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500494 mbedtls_platform_zeroize( padding, sizeof( padding ) );
495 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000496
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200502static int tls1_prf( const unsigned char *secret, size_t slen,
503 const char *label,
504 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000505 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000506{
Paul Bakker23986e52011-04-24 08:57:21 +0000507 size_t nb, hs;
508 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200509 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300510 unsigned char *tmp;
511 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 const mbedtls_md_info_t *md_info;
514 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100515 int ret;
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Ron Eldor3b350852019-05-07 18:31:49 +0300519 tmp_len = 20 + strlen( label ) + rlen;
520 tmp = mbedtls_calloc( 1, tmp_len );
521 if( tmp == NULL )
522 {
523 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
524 goto exit;
525 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 hs = ( slen + 1 ) / 2;
528 S1 = secret;
529 S2 = secret + slen - hs;
530
531 nb = strlen( label );
532 memcpy( tmp + 20, label, nb );
533 memcpy( tmp + 20 + nb, random, rlen );
534 nb += rlen;
535
536 /*
537 * First compute P_md5(secret,label+random)[0..dlen]
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300540 {
541 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
542 goto exit;
543 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300546 {
547 goto exit;
548 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
551 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
552 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554 for( i = 0; i < dlen; i += 16 )
555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_reset ( &md_ctx );
557 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
558 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_hmac_reset ( &md_ctx );
561 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
562 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
565
566 for( j = 0; j < k; j++ )
567 dstbuf[i + j] = h_i[j];
568 }
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100571
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 /*
573 * XOR out with P_sha1(secret,label+random)[0..dlen]
574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300576 {
577 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
578 goto exit;
579 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300582 {
583 goto exit;
584 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
587 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
588 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 for( i = 0; i < dlen; i += 20 )
591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_hmac_reset ( &md_ctx );
593 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
594 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_hmac_reset ( &md_ctx );
597 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
598 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
601
602 for( j = 0; j < k; j++ )
603 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
604 }
605
Ron Eldor3b350852019-05-07 18:31:49 +0300606exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100608
Ron Eldor3b350852019-05-07 18:31:49 +0300609 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500610 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Ron Eldor3b350852019-05-07 18:31:49 +0300612 mbedtls_free( tmp );
613 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500618#if defined(MBEDTLS_USE_PSA_CRYPTO)
619static int tls_prf_generic( mbedtls_md_type_t md_type,
620 const unsigned char *secret, size_t slen,
621 const char *label,
622 const unsigned char *random, size_t rlen,
623 unsigned char *dstbuf, size_t dlen )
624{
625 psa_status_t status;
626 psa_algorithm_t alg;
627 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500628 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500629 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
630
Andrzej Kurek2f760752019-01-28 08:08:15 -0500631 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500632 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500633
Andrzej Kurekc929a822019-01-14 03:51:11 -0500634 if( md_type == MBEDTLS_MD_SHA384 )
635 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
636 else
637 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
638
Andrzej Kurek2f760752019-01-28 08:08:15 -0500639 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500640 psa_key_policy_set_usage( &policy,
641 PSA_KEY_USAGE_DERIVE,
642 alg );
643 status = psa_set_key_policy( master_slot, &policy );
644 if( status != PSA_SUCCESS )
645 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
646
647 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
648 if( status != PSA_SUCCESS )
649 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
650
651 status = psa_key_derivation( &generator,
652 master_slot, alg,
653 random, rlen,
654 (unsigned char const *) label,
655 (size_t) strlen( label ),
656 dlen );
657 if( status != PSA_SUCCESS )
658 {
659 psa_generator_abort( &generator );
660 psa_destroy_key( master_slot );
661 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
662 }
663
664 status = psa_generator_read( &generator, dstbuf, dlen );
665 if( status != PSA_SUCCESS )
666 {
667 psa_generator_abort( &generator );
668 psa_destroy_key( master_slot );
669 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
670 }
671
672 status = psa_generator_abort( &generator );
673 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500674 {
675 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500676 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500677 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500678
679 status = psa_destroy_key( master_slot );
680 if( status != PSA_SUCCESS )
681 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
682
Andrzej Kurek33171262019-01-15 03:25:18 -0500683 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500684}
685
686#else /* MBEDTLS_USE_PSA_CRYPTO */
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100689 const unsigned char *secret, size_t slen,
690 const char *label,
691 const unsigned char *random, size_t rlen,
692 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000693{
694 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100695 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300696 unsigned char *tmp;
697 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
699 const mbedtls_md_info_t *md_info;
700 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100701 int ret;
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100709
Ron Eldor3b350852019-05-07 18:31:49 +0300710 tmp_len = md_len + strlen( label ) + rlen;
711 tmp = mbedtls_calloc( 1, tmp_len );
712 if( tmp == NULL )
713 {
714 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
715 goto exit;
716 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000717
718 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100719 memcpy( tmp + md_len, label, nb );
720 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000721 nb += rlen;
722
723 /*
724 * Compute P_<hash>(secret, label + random)[0..dlen]
725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300727 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
730 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
731 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100732
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100733 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_md_hmac_reset ( &md_ctx );
736 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
737 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_md_hmac_reset ( &md_ctx );
740 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
741 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000742
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100743 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744
745 for( j = 0; j < k; j++ )
746 dstbuf[i + j] = h_i[j];
747 }
748
Ron Eldor3b350852019-05-07 18:31:49 +0300749exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100751
Ron Eldor3b350852019-05-07 18:31:49 +0300752 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500753 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000754
Ron Eldor3b350852019-05-07 18:31:49 +0300755 mbedtls_free( tmp );
756
757 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000758}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500759#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100761static int tls_prf_sha256( const unsigned char *secret, size_t slen,
762 const char *label,
763 const unsigned char *random, size_t rlen,
764 unsigned char *dstbuf, size_t dlen )
765{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100767 label, random, rlen, dstbuf, dlen ) );
768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200772static int tls_prf_sha384( const unsigned char *secret, size_t slen,
773 const char *label,
774 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000775 unsigned char *dstbuf, size_t dlen )
776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100778 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#endif /* MBEDTLS_SHA512_C */
781#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
786 defined(MBEDTLS_SSL_PROTO_TLS1_1)
787static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788#endif
Paul Bakker380da532012-04-18 16:10:25 +0000789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SSL_PROTO_SSL3)
791static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
792static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200793#endif
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
796static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
797static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200798#endif
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
801#if defined(MBEDTLS_SHA256_C)
802static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
803static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
804static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SHA512_C)
808static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
809static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
810static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100811#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000813
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100814#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100815 defined(MBEDTLS_USE_PSA_CRYPTO)
816static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
817{
818 if( ssl->conf->f_psk != NULL )
819 {
820 /* If we've used a callback to select the PSK,
821 * the static configuration is irrelevant. */
822 if( ssl->handshake->psk_opaque != 0 )
823 return( 1 );
824
825 return( 0 );
826 }
827
828 if( ssl->conf->psk_opaque != 0 )
829 return( 1 );
830
831 return( 0 );
832}
833#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100834 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100835
Ron Eldorcf280092019-05-14 20:19:13 +0300836#if defined(MBEDTLS_SSL_EXPORT_KEYS)
837static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
838{
839#if defined(MBEDTLS_SSL_PROTO_SSL3)
840 if( tls_prf == ssl3_prf )
841 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300842 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300843 }
844 else
845#endif
846#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
847 if( tls_prf == tls1_prf )
848 {
849 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
850 }
851 else
852#endif
853#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
854#if defined(MBEDTLS_SHA512_C)
855 if( tls_prf == tls_prf_sha384 )
856 {
857 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
858 }
859 else
860#endif
861#if defined(MBEDTLS_SHA256_C)
862 if( tls_prf == tls_prf_sha256 )
863 {
864 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
865 }
866 else
867#endif
868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
869 return( MBEDTLS_SSL_TLS_PRF_NONE );
870}
871#endif /* MBEDTLS_SSL_EXPORT_KEYS */
872
Ron Eldor51d3ab52019-05-12 14:54:30 +0300873int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
874 const unsigned char *secret, size_t slen,
875 const char *label,
876 const unsigned char *random, size_t rlen,
877 unsigned char *dstbuf, size_t dlen )
878{
879 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
880
881 switch( prf )
882 {
883#if defined(MBEDTLS_SSL_PROTO_SSL3)
884 case MBEDTLS_SSL_TLS_PRF_SSL3:
885 tls_prf = ssl3_prf;
886 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300887#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300888#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
889 case MBEDTLS_SSL_TLS_PRF_TLS1:
890 tls_prf = tls1_prf;
891 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300892#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
893
894#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300895#if defined(MBEDTLS_SHA512_C)
896 case MBEDTLS_SSL_TLS_PRF_SHA384:
897 tls_prf = tls_prf_sha384;
898 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300899#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300900#if defined(MBEDTLS_SHA256_C)
901 case MBEDTLS_SSL_TLS_PRF_SHA256:
902 tls_prf = tls_prf_sha256;
903 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300904#endif /* MBEDTLS_SHA256_C */
905#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300906 default:
907 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
908 }
909
910 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
911}
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000914{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200915 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000916#if defined(MBEDTLS_USE_PSA_CRYPTO)
917 int psa_fallthrough;
918#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 unsigned char keyblk[256];
921 unsigned char *key1;
922 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100923 unsigned char *mac_enc;
924 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000925 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200926 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000927 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000928 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 const mbedtls_cipher_info_t *cipher_info;
930 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100931
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000932 /* cf. RFC 5246, Section 8.1:
933 * "The master secret is always exactly 48 bytes in length." */
934 size_t const master_secret_len = 48;
935
Hanno Becker35b23c72018-10-23 12:10:41 +0100936#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
937 unsigned char session_hash[48];
938#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_ssl_session *session = ssl->session_negotiate;
941 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
942 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
947 transform->encrypt_then_mac = session->encrypt_then_mac;
948#endif
949 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000950
951 ciphersuite_info = handshake->ciphersuite_info;
952 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100953 if( cipher_info == NULL )
954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000956 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100958 }
959
Hanno Beckere694c3e2017-12-27 21:34:08 +0000960 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100961 if( md_info == NULL )
962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000964 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100966 }
967
Hanno Becker4bf74652019-04-26 16:22:27 +0100968#if defined(MBEDTLS_SSL_CID)
969 /* Copy own and peer's CID if the use of the CID
970 * extension has been negotiated. */
971 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
972 {
973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100974
Hanno Becker05154c32019-05-03 15:23:51 +0100975 transform->in_cid_len = ssl->own_cid_len;
976 transform->out_cid_len = ssl->handshake->peer_cid_len;
977 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
978 memcpy( transform->out_cid, ssl->handshake->peer_cid,
979 ssl->handshake->peer_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100980
981 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
982 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100983 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100984 transform->in_cid_len );
985 }
986#endif /* MBEDTLS_SSL_CID */
987
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000989 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#if defined(MBEDTLS_SSL_PROTO_SSL3)
992 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 {
Paul Bakker48916f92012-09-16 19:57:18 +0000994 handshake->tls_prf = ssl3_prf;
995 handshake->calc_verify = ssl_calc_verify_ssl;
996 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000997 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200998 else
999#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1001 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001002 {
Paul Bakker48916f92012-09-16 19:57:18 +00001003 handshake->tls_prf = tls1_prf;
1004 handshake->calc_verify = ssl_calc_verify_tls;
1005 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001006 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001007 else
1008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1010#if defined(MBEDTLS_SHA512_C)
1011 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001012 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 {
Paul Bakker48916f92012-09-16 19:57:18 +00001014 handshake->tls_prf = tls_prf_sha384;
1015 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1016 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001017 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001018 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_SHA256_C)
1021 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001022 {
Paul Bakker48916f92012-09-16 19:57:18 +00001023 handshake->tls_prf = tls_prf_sha256;
1024 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1025 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001026 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001027 else
1028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1032 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001033 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001034
1035 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 * SSLv3:
1037 * master =
1038 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1039 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1040 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001041 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001042 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 * master = PRF( premaster, "master secret", randbytes )[0..47]
1044 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001045 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1048 }
1049 else
1050 {
1051 /* The label for the KDF used for key expansion.
1052 * This is either "master secret" or "extended master secret"
1053 * depending on whether the Extended Master Secret extension
1054 * is used. */
1055 char const *lbl = "master secret";
1056
1057 /* The salt for the KDF used for key expansion.
1058 * - If the Extended Master Secret extension is not used,
1059 * this is ClientHello.Random + ServerHello.Random
1060 * (see Sect. 8.1 in RFC 5246).
1061 * - If the Extended Master Secret extension is used,
1062 * this is the transcript of the handshake so far.
1063 * (see Sect. 4 in RFC 7627). */
1064 unsigned char const *salt = handshake->randbytes;
1065 size_t salt_len = 64;
1066
1067#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001071
Hanno Becker35b23c72018-10-23 12:10:41 +01001072 lbl = "extended master secret";
1073 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001074 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1076 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001079 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1080 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001081 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001082 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001083 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001084#endif /* MBEDTLS_SHA512_C */
1085 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001086 }
1087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001089 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001090
Hanno Becker35b23c72018-10-23 12:10:41 +01001091 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001092 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001093#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1094
Hanno Becker7d0a5692018-10-23 15:26:22 +01001095#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1096 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1097 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1098 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1099 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001100 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001101 /* Perform PSK-to-MS expansion in a single step. */
1102 psa_status_t status;
1103 psa_algorithm_t alg;
1104 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001105 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001106
1107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1108
1109 psk = ssl->conf->psk_opaque;
1110 if( ssl->handshake->psk_opaque != 0 )
1111 psk = ssl->handshake->psk_opaque;
1112
Hanno Becker22bf1452019-04-05 11:21:08 +01001113 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001114 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1115 else
1116 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1117
1118 status = psa_key_derivation( &generator, psk, alg,
1119 salt, salt_len,
1120 (unsigned char const *) lbl,
1121 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001122 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001123 if( status != PSA_SUCCESS )
1124 {
1125 psa_generator_abort( &generator );
1126 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1127 }
1128
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001129 status = psa_generator_read( &generator, session->master,
1130 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001131 if( status != PSA_SUCCESS )
1132 {
1133 psa_generator_abort( &generator );
1134 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1135 }
1136
1137 status = psa_generator_abort( &generator );
1138 if( status != PSA_SUCCESS )
1139 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001140 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 else
1142#endif
1143 {
1144 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1145 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001146 session->master,
1147 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001148 if( ret != 0 )
1149 {
1150 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1151 return( ret );
1152 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001153
Hanno Becker7d0a5692018-10-23 15:26:22 +01001154 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1155 handshake->premaster,
1156 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001157
Hanno Becker7d0a5692018-10-23 15:26:22 +01001158 mbedtls_platform_zeroize( handshake->premaster,
1159 sizeof(handshake->premaster) );
1160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
1163 /*
1164 * Swap the client and server random values.
1165 */
Paul Bakker48916f92012-09-16 19:57:18 +00001166 memcpy( tmp, handshake->randbytes, 64 );
1167 memcpy( handshake->randbytes, tmp + 32, 32 );
1168 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001169 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
1171 /*
1172 * SSLv3:
1173 * key block =
1174 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1175 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1176 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1177 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1178 * ...
1179 *
1180 * TLSv1:
1181 * key block = PRF( master, "key expansion", randbytes )
1182 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001183 ret = handshake->tls_prf( session->master, 48, "key expansion",
1184 handshake->randbytes, 64, keyblk, 256 );
1185 if( ret != 0 )
1186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001188 return( ret );
1189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1192 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1193 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1194 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1195 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 /*
1198 * Determine the appropriate key, IV and MAC length.
1199 */
Paul Bakker68884e32013-01-07 18:20:04 +01001200
Hanno Becker88aaf652017-12-27 08:17:40 +00001201 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001202
Hanno Becker8031d062018-01-03 15:32:31 +00001203#if defined(MBEDTLS_GCM_C) || \
1204 defined(MBEDTLS_CCM_C) || \
1205 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001207 cipher_info->mode == MBEDTLS_MODE_CCM ||
1208 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001210 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001211
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001212 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001213 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001214 transform->taglen =
1215 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001216
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001217 /* All modes haves 96-bit IVs;
1218 * GCM and CCM has 4 implicit and 8 explicit bytes
1219 * ChachaPoly has all 12 bytes implicit
1220 */
Paul Bakker68884e32013-01-07 18:20:04 +01001221 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001222 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1223 transform->fixed_ivlen = 12;
1224 else
1225 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001226
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001227 /* Minimum length of encrypted record */
1228 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001229 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001230 }
1231 else
Hanno Becker8031d062018-01-03 15:32:31 +00001232#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1233#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1234 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1235 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001236 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001237 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1239 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001242 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001245 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001246 mac_key_len = mbedtls_md_get_size( md_info );
1247 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001250 /*
1251 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1252 * (rfc 6066 page 13 or rfc 2104 section 4),
1253 * so we only need to adjust the length here.
1254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001258
1259#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1260 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001261 * HMAC implementation which also truncates the key
1262 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001263 mac_key_len = transform->maclen;
1264#endif
1265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001267
1268 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001269 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001271 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001273 transform->minlen = transform->maclen;
1274 else
Paul Bakker68884e32013-01-07 18:20:04 +01001275 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001276 /*
1277 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001278 * 1. if EtM is in use: one block plus MAC
1279 * otherwise: * first multiple of blocklen greater than maclen
1280 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1283 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001284 {
1285 transform->minlen = transform->maclen
1286 + cipher_info->block_size;
1287 }
1288 else
1289#endif
1290 {
1291 transform->minlen = transform->maclen
1292 + cipher_info->block_size
1293 - transform->maclen % cipher_info->block_size;
1294 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1297 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1298 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001299 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001300 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1303 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1304 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305 {
1306 transform->minlen += transform->ivlen;
1307 }
1308 else
1309#endif
1310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001312 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1313 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001314 }
Paul Bakker68884e32013-01-07 18:20:04 +01001315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 }
Hanno Becker8031d062018-01-03 15:32:31 +00001317 else
1318#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1319 {
1320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Hanno Becker88aaf652017-12-27 08:17:40 +00001324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1325 (unsigned) keylen,
1326 (unsigned) transform->minlen,
1327 (unsigned) transform->ivlen,
1328 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 /*
1331 * Finally setup the cipher contexts, IVs and MAC secrets.
1332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001334 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001336 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001337 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Paul Bakker68884e32013-01-07 18:20:04 +01001339 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001340 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001342 /*
1343 * This is not used in TLS v1.1.
1344 */
Paul Bakker48916f92012-09-16 19:57:18 +00001345 iv_copy_len = ( transform->fixed_ivlen ) ?
1346 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001347 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1348 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001349 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
1351 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#endif /* MBEDTLS_SSL_CLI_C */
1353#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001354 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001356 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001357 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
Hanno Becker81c7b182017-11-09 18:39:33 +00001359 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001360 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001362 /*
1363 * This is not used in TLS v1.1.
1364 */
Paul Bakker48916f92012-09-16 19:57:18 +00001365 iv_copy_len = ( transform->fixed_ivlen ) ?
1366 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001367 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1368 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001369 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001371 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001375 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1376 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Hanno Beckerd56ed242018-01-03 15:32:51 +00001379#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_PROTO_SSL3)
1381 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001382 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001383 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001386 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1387 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001388 }
1389
Hanno Becker81c7b182017-11-09 18:39:33 +00001390 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1391 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001392 }
1393 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1395#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1396 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1397 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001398 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001399 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1400 For AEAD-based ciphersuites, there is nothing to do here. */
1401 if( mac_key_len != 0 )
1402 {
1403 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1404 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1405 }
Paul Bakker68884e32013-01-07 18:20:04 +01001406 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001407 else
1408#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001411 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1412 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001413 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001414#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1417 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001418 {
1419 int ret = 0;
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001422
Hanno Becker88aaf652017-12-27 08:17:40 +00001423 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001424 transform->iv_enc, transform->iv_dec,
1425 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001426 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001427 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001430 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1431 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001432 }
1433 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001434#else
1435 ((void) mac_dec);
1436 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001438
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001439#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1440 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001441 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001442 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1443 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001444 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001445 iv_copy_len );
1446 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001447
1448 if( ssl->conf->f_export_keys_ext != NULL )
1449 {
1450 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1451 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001452 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001453 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001454 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001455 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001456 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001457 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001458#endif
1459
Hanno Beckerf704bef2018-11-16 15:21:18 +00001460#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001461
1462 /* Only use PSA-based ciphers for TLS-1.2.
1463 * That's relevant at least for TLS-1.0, where
1464 * we assume that mbedtls_cipher_crypt() updates
1465 * the structure field for the IV, which the PSA-based
1466 * implementation currently doesn't. */
1467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1468 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001469 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001470 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001471 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001472 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1473 {
1474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001475 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001476 }
1477
1478 if( ret == 0 )
1479 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001480 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001481 psa_fallthrough = 0;
1482 }
1483 else
1484 {
1485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1486 psa_fallthrough = 1;
1487 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001488 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001489 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001490 psa_fallthrough = 1;
1491#else
1492 psa_fallthrough = 1;
1493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001494
Hanno Beckercb1cc802018-11-17 22:27:38 +00001495 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001496#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001497 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001498 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001501 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001502 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001503
Hanno Beckerf704bef2018-11-16 15:21:18 +00001504#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 /* Only use PSA-based ciphers for TLS-1.2.
1506 * That's relevant at least for TLS-1.0, where
1507 * we assume that mbedtls_cipher_crypt() updates
1508 * the structure field for the IV, which the PSA-based
1509 * implementation currently doesn't. */
1510#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1511 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001512 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001513 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001514 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001515 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1516 {
1517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001518 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001519 }
1520
1521 if( ret == 0 )
1522 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001524 psa_fallthrough = 0;
1525 }
1526 else
1527 {
1528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1529 psa_fallthrough = 1;
1530 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001531 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001532 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001533 psa_fallthrough = 1;
1534#else
1535 psa_fallthrough = 1;
1536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001537
Hanno Beckercb1cc802018-11-17 22:27:38 +00001538 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001539#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001540 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 cipher_info ) ) != 0 )
1542 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001544 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001548 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001552 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001553 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001556 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001560 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001561 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_CIPHER_MODE_CBC)
1564 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1567 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001570 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001571 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1574 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001577 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001584 // Initialize compression
1585 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001587 {
Paul Bakker16770332013-10-11 09:59:44 +02001588 if( ssl->compress_buf == NULL )
1589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001591 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001592 if( ssl->compress_buf == NULL )
1593 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001595 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001596 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1597 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001598 }
1599 }
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001602
Paul Bakker48916f92012-09-16 19:57:18 +00001603 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1604 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001605
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001606 if( deflateInit( &transform->ctx_deflate,
1607 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001608 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001611 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1612 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 }
1614 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001618end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001619 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1620 mbedtls_platform_zeroize( handshake->randbytes,
1621 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001622 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001623}
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#if defined(MBEDTLS_SSL_PROTO_SSL3)
1626void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001627{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001628 mbedtls_md5_context md5;
1629 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 unsigned char pad_1[48];
1631 unsigned char pad_2[48];
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001635 mbedtls_md5_init( &md5 );
1636 mbedtls_sha1_init( &sha1 );
1637
1638 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1639 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
Paul Bakker380da532012-04-18 16:10:25 +00001641 memset( pad_1, 0x36, 48 );
1642 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001644 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1645 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1646 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001648 mbedtls_md5_starts_ret( &md5 );
1649 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1650 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1651 mbedtls_md5_update_ret( &md5, hash, 16 );
1652 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001654 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1655 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1656 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001658 mbedtls_sha1_starts_ret( &sha1 );
1659 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1660 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1661 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1662 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001666
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001667 mbedtls_md5_free( &md5 );
1668 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001669
Paul Bakker380da532012-04-18 16:10:25 +00001670 return;
1671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1675void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001676{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001677 mbedtls_md5_context md5;
1678 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001681
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001682 mbedtls_md5_init( &md5 );
1683 mbedtls_sha1_init( &sha1 );
1684
1685 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1686 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001687
Andrzej Kurekeb342242019-01-29 09:14:33 -05001688 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001689 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001693
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001694 mbedtls_md5_free( &md5 );
1695 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001696
Paul Bakker380da532012-04-18 16:10:25 +00001697 return;
1698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1702#if defined(MBEDTLS_SHA256_C)
1703void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001704{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001705#if defined(MBEDTLS_USE_PSA_CRYPTO)
1706 size_t hash_size;
1707 psa_status_t status;
1708 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1709
1710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1711 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1712 if( status != PSA_SUCCESS )
1713 {
1714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1715 return;
1716 }
1717
1718 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1719 if( status != PSA_SUCCESS )
1720 {
1721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1722 return;
1723 }
1724 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1726#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001727 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001728
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001729 mbedtls_sha256_init( &sha256 );
1730
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001732
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001733 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001734 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001738
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001739 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001740#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001741 return;
1742}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if defined(MBEDTLS_SHA512_C)
1746void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001747{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001748#if defined(MBEDTLS_USE_PSA_CRYPTO)
1749 size_t hash_size;
1750 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001751 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001752
1753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001754 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001755 if( status != PSA_SUCCESS )
1756 {
1757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1758 return;
1759 }
1760
Andrzej Kurek972fba52019-01-30 03:29:12 -05001761 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001762 if( status != PSA_SUCCESS )
1763 {
1764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1765 return;
1766 }
1767 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1769#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001770 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001771
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001772 mbedtls_sha512_init( &sha512 );
1773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001775
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001776 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001777 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001782 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001783#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 return;
1785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#endif /* MBEDTLS_SHA512_C */
1787#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1790int 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 +02001791{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001792 unsigned char *p = ssl->handshake->premaster;
1793 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001794 const unsigned char *psk = ssl->conf->psk;
1795 size_t psk_len = ssl->conf->psk_len;
1796
1797 /* If the psk callback was called, use its result */
1798 if( ssl->handshake->psk != NULL )
1799 {
1800 psk = ssl->handshake->psk;
1801 psk_len = ssl->handshake->psk_len;
1802 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001803
1804 /*
1805 * PMS = struct {
1806 * opaque other_secret<0..2^16-1>;
1807 * opaque psk<0..2^16-1>;
1808 * };
1809 * with "other_secret" depending on the particular key exchange
1810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1812 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001813 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001814 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001816
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001817 *(p++) = (unsigned char)( psk_len >> 8 );
1818 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001819
1820 if( end < p || (size_t)( end - p ) < psk_len )
1821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1822
1823 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001824 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001825 }
1826 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1828#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1829 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001830 {
1831 /*
1832 * other_secret already set by the ClientKeyExchange message,
1833 * and is 48 bytes long
1834 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001835 if( end - p < 2 )
1836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1837
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001838 *p++ = 0;
1839 *p++ = 48;
1840 p += 48;
1841 }
1842 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1844#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1845 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001846 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001847 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001848 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001849
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001850 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001852 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001853 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001856 return( ret );
1857 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001858 *(p++) = (unsigned char)( len >> 8 );
1859 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860 p += len;
1861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001863 }
1864 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1866#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1867 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001868 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001869 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 size_t zlen;
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001873 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001874 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001877 return( ret );
1878 }
1879
1880 *(p++) = (unsigned char)( zlen >> 8 );
1881 *(p++) = (unsigned char)( zlen );
1882 p += zlen;
1883
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001884 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1885 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886 }
1887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001892 }
1893
1894 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001895 if( end - p < 2 )
1896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001897
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001898 *(p++) = (unsigned char)( psk_len >> 8 );
1899 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001900
1901 if( end < p || (size_t)( end - p ) < psk_len )
1902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1903
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001904 memcpy( p, psk, psk_len );
1905 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001906
1907 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1908
1909 return( 0 );
1910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001914/*
1915 * SSLv3.0 MAC functions
1916 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001917#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001918static void ssl_mac( mbedtls_md_context_t *md_ctx,
1919 const unsigned char *secret,
1920 const unsigned char *buf, size_t len,
1921 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001922 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001923{
1924 unsigned char header[11];
1925 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001926 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1928 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001929
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001930 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001932 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001933 else
Paul Bakker68884e32013-01-07 18:20:04 +01001934 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
1936 memcpy( header, ctr, 8 );
1937 header[ 8] = (unsigned char) type;
1938 header[ 9] = (unsigned char)( len >> 8 );
1939 header[10] = (unsigned char)( len );
1940
Paul Bakker68884e32013-01-07 18:20:04 +01001941 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 mbedtls_md_starts( md_ctx );
1943 mbedtls_md_update( md_ctx, secret, md_size );
1944 mbedtls_md_update( md_ctx, padding, padlen );
1945 mbedtls_md_update( md_ctx, header, 11 );
1946 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001947 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
Paul Bakker68884e32013-01-07 18:20:04 +01001949 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 mbedtls_md_starts( md_ctx );
1951 mbedtls_md_update( md_ctx, secret, md_size );
1952 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001953 mbedtls_md_update( md_ctx, out, md_size );
1954 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001957
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001958/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001959 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001960#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001961 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1962 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1963 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1964/* This function makes sure every byte in the memory region is accessed
1965 * (in ascending addresses order) */
1966static void ssl_read_memory( unsigned char *p, size_t len )
1967{
1968 unsigned char acc = 0;
1969 volatile unsigned char force;
1970
1971 for( ; len != 0; p++, len-- )
1972 acc ^= *p;
1973
1974 force = acc;
1975 (void) force;
1976}
1977#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1978
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001979/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001981 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001982
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001983#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001984/* This functions transforms a DTLS plaintext fragment and a record content
1985 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001986 *
1987 * struct {
1988 * opaque content[DTLSPlaintext.length];
1989 * ContentType real_type;
1990 * uint8 zeros[length_of_padding];
1991 * } DTLSInnerPlaintext;
1992 *
1993 * Input:
1994 * - `content`: The beginning of the buffer holding the
1995 * plaintext to be wrapped.
1996 * - `*content_size`: The length of the plaintext in Bytes.
1997 * - `max_len`: The number of Bytes available starting from
1998 * `content`. This must be `>= *content_size`.
1999 * - `rec_type`: The desired record content type.
2000 *
2001 * Output:
2002 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2003 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2004 *
2005 * Returns:
2006 * - `0` on success.
2007 * - A negative error code if `max_len` didn't offer enough space
2008 * for the expansion.
2009 */
2010static int ssl_cid_build_inner_plaintext( unsigned char *content,
2011 size_t *content_size,
2012 size_t remaining,
2013 uint8_t rec_type )
2014{
2015 size_t len = *content_size;
2016 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2017
2018 /* Write real content type */
2019 if( remaining == 0 )
2020 return( -1 );
2021 content[ len ] = rec_type;
2022 len++;
2023 remaining--;
2024
2025 if( remaining < pad )
2026 return( -1 );
2027 memset( content + len, 0, pad );
2028 len += pad;
2029 remaining -= pad;
2030
2031 *content_size = len;
2032 return( 0 );
2033}
2034
Hanno Becker07dc97d2019-05-20 15:08:01 +01002035/* This function parses a DTLSInnerPlaintext structure.
2036 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002037static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2038 size_t *content_size,
2039 uint8_t *rec_type )
2040{
2041 size_t remaining = *content_size;
2042
2043 /* Determine length of padding by skipping zeroes from the back. */
2044 do
2045 {
2046 if( remaining == 0 )
2047 return( -1 );
2048 remaining--;
2049 } while( content[ remaining ] == 0 );
2050
2051 *content_size = remaining;
2052 *rec_type = content[ remaining ];
2053
2054 return( 0 );
2055}
2056#endif /* MBEDTLS_SSL_CID */
2057
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002058/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002059 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002060static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002061 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002062 mbedtls_record *rec )
2063{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002064 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002065 *
2066 * additional_data = seq_num + TLSCompressed.type +
2067 * TLSCompressed.version + TLSCompressed.length;
2068 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002069 * For the CID extension, this is extended as follows
2070 * (quoting draft-ietf-tls-dtls-connection-id-05,
2071 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002072 *
2073 * additional_data = seq_num + DTLSPlaintext.type +
2074 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002075 * cid +
2076 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002077 * length_of_DTLSInnerPlaintext;
2078 */
2079
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002080 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2081 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002082 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002083
2084#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002085 if( rec->cid_len != 0 )
2086 {
2087 memcpy( add_data + 11, rec->cid, rec->cid_len );
2088 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2089 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2090 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2091 *add_data_len = 13 + 1 + rec->cid_len;
2092 }
2093 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002094#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002095 {
2096 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2097 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2098 *add_data_len = 13;
2099 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002100}
2101
Hanno Beckera18d1322018-01-03 14:27:32 +00002102int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2103 mbedtls_ssl_transform *transform,
2104 mbedtls_record *rec,
2105 int (*f_rng)(void *, unsigned char *, size_t),
2106 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002109 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002110 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002111 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002112 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002113 size_t post_avail;
2114
2115 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002116#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002117 ((void) ssl);
2118#endif
2119
2120 /* The PRNG is used for dynamic IV generation that's used
2121 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2122#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2123 ( defined(MBEDTLS_AES_C) || \
2124 defined(MBEDTLS_ARIA_C) || \
2125 defined(MBEDTLS_CAMELLIA_C) ) && \
2126 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2127 ((void) f_rng);
2128 ((void) p_rng);
2129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002133 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002134 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2137 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002138 if( rec == NULL
2139 || rec->buf == NULL
2140 || rec->buf_len < rec->data_offset
2141 || rec->buf_len - rec->data_offset < rec->data_len
2142#if defined(MBEDTLS_SSL_CID)
2143 || rec->cid_len != 0
2144#endif
2145 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002146 {
2147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002149 }
2150
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002151 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002152 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002154 data, rec->data_len );
2155
2156 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2157
2158 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2161 (unsigned) rec->data_len,
2162 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2164 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002165
Hanno Beckercab87e62019-04-29 13:52:53 +01002166#if defined(MBEDTLS_SSL_CID)
2167 /*
2168 * Add CID information
2169 */
2170 rec->cid_len = transform->out_cid_len;
2171 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2172 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002173
2174 if( rec->cid_len != 0 )
2175 {
2176 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002177 * Wrap plaintext into DTLSInnerPlaintext structure.
2178 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002179 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002180 * Note that this changes `rec->data_len`, and hence
2181 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002182 */
2183 if( ssl_cid_build_inner_plaintext( data,
2184 &rec->data_len,
2185 post_avail,
2186 rec->type ) != 0 )
2187 {
2188 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2189 }
2190
2191 rec->type = MBEDTLS_SSL_MSG_CID;
2192 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002193#endif /* MBEDTLS_SSL_CID */
2194
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002195 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2196
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002198 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 */
Hanno Becker52344c22018-01-03 15:24:20 +00002200#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 if( mode == MBEDTLS_MODE_STREAM ||
2202 ( mode == MBEDTLS_MODE_CBC
2203#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002204 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002205#endif
2206 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002208 if( post_avail < transform->maclen )
2209 {
2210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2211 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2212 }
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002215 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002216 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002217 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002218 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2219 data, rec->data_len, rec->ctr, rec->type, mac );
2220 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002221 }
2222 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2225 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002226 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002227 {
Hanno Becker992b6872017-11-09 18:57:39 +00002228 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2229
Hanno Beckercab87e62019-04-29 13:52:53 +01002230 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002231
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002233 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2235 data, rec->data_len );
2236 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2237 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2238
2239 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002240 }
2241 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002242#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002246 }
2247
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002248 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2249 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002250
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002251 rec->data_len += transform->maclen;
2252 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002253 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002254 }
Hanno Becker52344c22018-01-03 15:24:20 +00002255#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002257 /*
2258 * Encrypt
2259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2261 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002263 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002264 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002266 "including %d bytes of padding",
2267 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002269 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2270 transform->iv_enc, transform->ivlen,
2271 data, rec->data_len,
2272 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002275 return( ret );
2276 }
2277
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002278 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 }
Paul Bakker68884e32013-01-07 18:20:04 +01002284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002286
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002287#if defined(MBEDTLS_GCM_C) || \
2288 defined(MBEDTLS_CCM_C) || \
2289 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002291 mode == MBEDTLS_MODE_CCM ||
2292 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002293 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002294 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002295 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002296 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002297
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002298 /* Check that there's space for both the authentication tag
2299 * and the explicit IV before and after the record content. */
2300 if( post_avail < transform->taglen ||
2301 rec->data_offset < explicit_iv_len )
2302 {
2303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2304 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2305 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002306
Paul Bakker68884e32013-01-07 18:20:04 +01002307 /*
2308 * Generate IV
2309 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002310 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2311 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002312 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002313 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002314 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2315 explicit_iv_len );
2316 /* Prefix record content with explicit IV. */
2317 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002318 }
2319 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2320 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002321 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002322 unsigned char i;
2323
2324 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2325
2326 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002327 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002328 }
2329 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002330 {
2331 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002334 }
2335
Hanno Beckercab87e62019-04-29 13:52:53 +01002336 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002337
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2339 iv, transform->ivlen );
2340 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002341 data - explicit_iv_len, explicit_iv_len );
2342 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002343 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002345 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002346 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002347
Paul Bakker68884e32013-01-07 18:20:04 +01002348 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002349 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002350 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002351
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002353 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002354 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355 data, rec->data_len, /* source */
2356 data, &rec->data_len, /* destination */
2357 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002360 return( ret );
2361 }
2362
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002363 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2364 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002365
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002366 rec->data_len += transform->taglen + explicit_iv_len;
2367 rec->data_offset -= explicit_iv_len;
2368 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002369 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2373#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002374 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002377 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 size_t padlen, i;
2379 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002380
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 /* Currently we're always using minimal padding
2382 * (up to 255 bytes would be allowed). */
2383 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2384 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 padlen = 0;
2386
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 /* Check there's enough space in the buffer for the padding. */
2388 if( post_avail < padlen + 1 )
2389 {
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2391 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2392 }
2393
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002395 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002397 rec->data_len += padlen + 1;
2398 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002401 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002402 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2403 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002404 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002405 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002407 if( f_rng == NULL )
2408 {
2409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2410 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2411 }
2412
2413 if( rec->data_offset < transform->ivlen )
2414 {
2415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2416 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2417 }
2418
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002419 /*
2420 * Generate IV
2421 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002422 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002423 if( ret != 0 )
2424 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002425
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002426 memcpy( data - transform->ivlen, transform->iv_enc,
2427 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002433 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002435 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002437 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2438 transform->iv_enc,
2439 transform->ivlen,
2440 data, rec->data_len,
2441 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002444 return( ret );
2445 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002446
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002451 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002454 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002455 {
2456 /*
2457 * Save IV in SSL3 and TLS1
2458 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2460 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002463#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002464 {
2465 data -= transform->ivlen;
2466 rec->data_offset -= transform->ivlen;
2467 rec->data_len += transform->ivlen;
2468 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002471 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002472 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002473 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2474
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002475 /*
2476 * MAC(MAC_write_key, seq_num +
2477 * TLSCipherText.type +
2478 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002479 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002480 * IV + // except for TLS 1.0
2481 * ENC(content + padding + padding_length));
2482 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002483
2484 if( post_avail < transform->maclen)
2485 {
2486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2487 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2488 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002489
Hanno Beckercab87e62019-04-29 13:52:53 +01002490 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002493 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002494 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002497 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002498 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2499 data, rec->data_len );
2500 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2501 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002503 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 rec->data_len += transform->maclen;
2506 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002507 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002513 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002519 /* Make extra sure authentication was performed, exactly once */
2520 if( auth_done != 1 )
2521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002524 }
2525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 return( 0 );
2529}
2530
Hanno Beckera18d1322018-01-03 14:27:32 +00002531int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2532 mbedtls_ssl_transform *transform,
2533 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002534{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002535 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002537 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002538#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002539 size_t padlen = 0, correct = 1;
2540#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002541 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002542 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002543 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002544
Hanno Beckera18d1322018-01-03 14:27:32 +00002545#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 ((void) ssl);
2547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002551 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2554 }
2555 if( rec == NULL ||
2556 rec->buf == NULL ||
2557 rec->buf_len < rec->data_offset ||
2558 rec->buf_len - rec->data_offset < rec->data_len )
2559 {
2560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002562 }
2563
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002564 data = rec->buf + rec->data_offset;
2565 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Hanno Beckercab87e62019-04-29 13:52:53 +01002567#if defined(MBEDTLS_SSL_CID)
2568 /*
2569 * Match record's CID with incoming CID.
2570 */
2571
Hanno Becker938489a2019-05-08 13:02:22 +01002572 if( rec->cid_len != transform->in_cid_len ||
2573 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2574 {
Hanno Becker16ded982019-05-08 13:02:55 +01002575 /* Silently skip over record with mismatching CID. */
2576 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker938489a2019-05-08 13:02:22 +01002577 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002578#endif /* MBEDTLS_SSL_CID */
2579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2581 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002582 {
2583 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002584 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2585 transform->iv_dec,
2586 transform->ivlen,
2587 data, rec->data_len,
2588 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002591 return( ret );
2592 }
2593
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002594 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
Paul Bakker68884e32013-01-07 18:20:04 +01002600 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002602#if defined(MBEDTLS_GCM_C) || \
2603 defined(MBEDTLS_CCM_C) || \
2604 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002606 mode == MBEDTLS_MODE_CCM ||
2607 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002608 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002610 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002611
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002612 /*
2613 * Compute and update sizes
2614 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002615 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 "+ taglen (%d)", rec->data_len,
2619 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002621 }
Paul Bakker68884e32013-01-07 18:20:04 +01002622
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002623 /*
2624 * Prepare IV
2625 */
2626 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2627 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002628 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002630 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002631
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 }
2633 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2634 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002635 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002636 unsigned char i;
2637
2638 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2639
2640 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002641 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 }
2643 else
2644 {
2645 /* Reminder if we ever add an AEAD mode with a different size */
2646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2648 }
2649
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002650 data += explicit_iv_len;
2651 rec->data_offset += explicit_iv_len;
2652 rec->data_len -= explicit_iv_len + transform->taglen;
2653
Hanno Beckercab87e62019-04-29 13:52:53 +01002654 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002655 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002656 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657
2658 memcpy( transform->iv_dec + transform->fixed_ivlen,
2659 data - explicit_iv_len, explicit_iv_len );
2660
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002661 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002662 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002663 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002664
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002666 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002667 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002668 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002669 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2670 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002671 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672 data, rec->data_len,
2673 data, &olen,
2674 data + rec->data_len,
2675 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2680 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002681
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002682 return( ret );
2683 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002684 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002685
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002686 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002690 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2694#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002695 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002698 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002699
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 /*
Paul Bakker45829992013-01-03 14:52:21 +01002701 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002704 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2705 {
2706 /* The ciphertext is prefixed with the CBC IV. */
2707 minlen += transform->ivlen;
2708 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002709#endif
Paul Bakker45829992013-01-03 14:52:21 +01002710
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002711 /* Size considerations:
2712 *
2713 * - The CBC cipher text must not be empty and hence
2714 * at least of size transform->ivlen.
2715 *
2716 * Together with the potential IV-prefix, this explains
2717 * the first of the two checks below.
2718 *
2719 * - The record must contain a MAC, either in plain or
2720 * encrypted, depending on whether Encrypt-then-MAC
2721 * is used or not.
2722 * - If it is, the message contains the IV-prefix,
2723 * the CBC ciphertext, and the MAC.
2724 * - If it is not, the padded plaintext, and hence
2725 * the CBC ciphertext, has at least length maclen + 1
2726 * because there is at least the padding length byte.
2727 *
2728 * As the CBC ciphertext is not empty, both cases give the
2729 * lower bound minlen + maclen + 1 on the record size, which
2730 * we test for in the second check below.
2731 */
2732 if( rec->data_len < minlen + transform->ivlen ||
2733 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002736 "+ 1 ) ( + expl IV )", rec->data_len,
2737 transform->ivlen,
2738 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002740 }
2741
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002742 /*
2743 * Authenticate before decrypt if enabled
2744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002746 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002747 {
Hanno Becker992b6872017-11-09 18:57:39 +00002748 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002751
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002752 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2753 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002754
Hanno Beckercab87e62019-04-29 13:52:53 +01002755 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002756
Hanno Beckercab87e62019-04-29 13:52:53 +01002757 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2758 add_data_len );
2759 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2760 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2762 data, rec->data_len );
2763 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2764 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002765
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002766 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2767 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002768 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002770
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2772 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002776 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002777 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780
2781 /*
2782 * Check length sanity
2783 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002784 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002787 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002789 }
2790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002793 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002794 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002795 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002796 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002797 /* This is safe because data_len >= minlen + maclen + 1 initially,
2798 * and at this point we have at most subtracted maclen (note that
2799 * minlen == transform->ivlen here). */
2800 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002801
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002802 data += transform->ivlen;
2803 rec->data_offset += transform->ivlen;
2804 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002807
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002808 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2809 transform->iv_dec, transform->ivlen,
2810 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002813 return( ret );
2814 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002815
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002816 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2819 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002820 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002823 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002824 {
2825 /*
2826 * Save IV in SSL3 and TLS1
2827 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002828 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2829 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002830 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002831#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002833 /* Safe since data_len >= minlen + maclen + 1, so after having
2834 * subtracted at most minlen and maclen up to this point,
2835 * data_len > 0. */
2836 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002837
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838 if( auth_done == 1 )
2839 {
2840 correct *= ( rec->data_len >= padlen + 1 );
2841 padlen *= ( rec->data_len >= padlen + 1 );
2842 }
2843 else
Paul Bakker45829992013-01-03 14:52:21 +01002844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846 if( rec->data_len < transform->maclen + padlen + 1 )
2847 {
2848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2849 rec->data_len,
2850 transform->maclen,
2851 padlen + 1 ) );
2852 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002853#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002854
2855 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2856 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002859 padlen++;
2860
2861 /* Regardless of the validity of the padding,
2862 * we have data_len >= padlen here. */
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002865 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002867 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869#if defined(MBEDTLS_SSL_DEBUG_ALL)
2870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002871 "should be no more than %d",
2872 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002873#endif
Paul Bakker45829992013-01-03 14:52:21 +01002874 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 }
2876 }
2877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2879#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2880 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 /* The padding check involves a series of up to 256
2884 * consecutive memory reads at the end of the record
2885 * plaintext buffer. In order to hide the length and
2886 * validity of the padding, always perform exactly
2887 * `min(256,plaintext_len)` reads (but take into account
2888 * only the last `padlen` bytes for the padding check). */
2889 size_t pad_count = 0;
2890 size_t real_count = 0;
2891 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002892
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002893 /* Index of first padding byte; it has been ensured above
2894 * that the subtraction is safe. */
2895 size_t const padding_idx = rec->data_len - padlen;
2896 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2897 size_t const start_idx = rec->data_len - num_checks;
2898 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002899
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002900 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002901 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002902 real_count |= ( idx >= padding_idx );
2903 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002904 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002905 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002908 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002910#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002911 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2915 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002919 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002920
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002921 /* If the padding was found to be invalid, padlen == 0
2922 * and the subtraction is safe. If the padding was found valid,
2923 * padlen hasn't been changed and the previous assertion
2924 * data_len >= padlen still holds. */
2925 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002927 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002929 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002935#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002937 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002938#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
2940 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002941 * Authenticate if not done yet.
2942 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 */
Hanno Becker52344c22018-01-03 15:24:20 +00002944#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002945 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002946 {
Hanno Becker992b6872017-11-09 18:57:39 +00002947 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002948
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002949 /* If the initial value of padlen was such that
2950 * data_len < maclen + padlen + 1, then padlen
2951 * got reset to 1, and the initial check
2952 * data_len >= minlen + maclen + 1
2953 * guarantees that at this point we still
2954 * have at least data_len >= maclen.
2955 *
2956 * If the initial value of padlen was such that
2957 * data_len >= maclen + padlen + 1, then we have
2958 * subtracted either padlen + 1 (if the padding was correct)
2959 * or 0 (if the padding was incorrect) since then,
2960 * hence data_len >= maclen in any case.
2961 */
2962 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Hanno Beckercab87e62019-04-29 13:52:53 +01002964 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002968 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002969 ssl_mac( &transform->md_ctx_dec,
2970 transform->mac_dec,
2971 data, rec->data_len,
2972 rec->ctr, rec->type,
2973 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002974 }
2975 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2977#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2978 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002980 {
2981 /*
2982 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002983 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002984 *
2985 * Known timing attacks:
2986 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2987 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002988 * To compensate for different timings for the MAC calculation
2989 * depending on how much padding was removed (which is determined
2990 * by padlen), process extra_run more blocks through the hash
2991 * function.
2992 *
2993 * The formula in the paper is
2994 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2995 * where L1 is the size of the header plus the decrypted message
2996 * plus CBC padding and L2 is the size of the header plus the
2997 * decrypted message. This is for an underlying hash function
2998 * with 64-byte blocks.
2999 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3000 * correctly. We round down instead of up, so -56 is the correct
3001 * value for our calculations instead of -55.
3002 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003003 * Repeat the formula rather than defining a block_size variable.
3004 * This avoids requiring division by a variable at runtime
3005 * (which would be marginally less efficient and would require
3006 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003007 */
3008 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003009 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003010
3011 /*
3012 * The next two sizes are the minimum and maximum values of
3013 * in_msglen over all padlen values.
3014 *
3015 * They're independent of padlen, since we previously did
3016 * in_msglen -= padlen.
3017 *
3018 * Note that max_len + maclen is never more than the buffer
3019 * length, as we previously did in_msglen -= maclen too.
3020 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003021 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003022 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3023
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003024 memset( tmp, 0, sizeof( tmp ) );
3025
3026 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003027 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003028#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3029 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003030 case MBEDTLS_MD_MD5:
3031 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003032 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003033 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003034 extra_run =
3035 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3036 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 break;
3038#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003039#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003040 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003041 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003042 extra_run =
3043 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3044 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003045 break;
3046#endif
3047 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3050 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003051
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003052 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003053
Hanno Beckercab87e62019-04-29 13:52:53 +01003054 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3055 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003056 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3057 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003058 /* Make sure we access everything even when padlen > 0. This
3059 * makes the synchronisation requirements for just-in-time
3060 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003061 ssl_read_memory( data + rec->data_len, padlen );
3062 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003063
3064 /* Call mbedtls_md_process at least once due to cache attacks
3065 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003066 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003067 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003068
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003069 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003070
3071 /* Make sure we access all the memory that could contain the MAC,
3072 * before we check it in the next code block. This makes the
3073 * synchronisation requirements for just-in-time Prime+Probe
3074 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003075 ssl_read_memory( data + min_len,
3076 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003077 }
3078 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3080 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003086#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003087 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3088 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003091 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3092 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_DEBUG_ALL)
3095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003096#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003097 correct = 0;
3098 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003099 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003100 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003101
3102 /*
3103 * Finally check the correct flag
3104 */
3105 if( correct == 0 )
3106 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003107#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003108
3109 /* Make extra sure authentication was performed, exactly once */
3110 if( auth_done != 1 )
3111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3113 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003116#if defined(MBEDTLS_SSL_CID)
3117 if( rec->cid_len != 0 )
3118 {
3119 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3120 &rec->type );
3121 if( ret != 0 )
3122 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3123 }
3124#endif /* MBEDTLS_SSL_CID */
3125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
3128 return( 0 );
3129}
3130
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003131#undef MAC_NONE
3132#undef MAC_PLAINTEXT
3133#undef MAC_CIPHERTEXT
3134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003136/*
3137 * Compression/decompression functions
3138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003140{
3141 int ret;
3142 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003143 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003144 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003145 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003148
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003149 if( len_pre == 0 )
3150 return( 0 );
3151
Paul Bakker2770fbd2012-07-03 13:30:23 +00003152 memcpy( msg_pre, ssl->out_msg, len_pre );
3153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155 ssl->out_msglen ) );
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003158 ssl->out_msg, ssl->out_msglen );
3159
Paul Bakker48916f92012-09-16 19:57:18 +00003160 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3161 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3162 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003163 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003164
Paul Bakker48916f92012-09-16 19:57:18 +00003165 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003166 if( ret != Z_OK )
3167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3169 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003170 }
3171
Angus Grattond8213d02016-05-25 20:56:48 +10003172 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003173 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003176 ssl->out_msglen ) );
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179 ssl->out_msg, ssl->out_msglen );
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003182
3183 return( 0 );
3184}
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003187{
3188 int ret;
3189 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003190 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003192 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003195
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003196 if( len_pre == 0 )
3197 return( 0 );
3198
Paul Bakker2770fbd2012-07-03 13:30:23 +00003199 memcpy( msg_pre, ssl->in_msg, len_pre );
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 ssl->in_msglen ) );
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205 ssl->in_msg, ssl->in_msglen );
3206
Paul Bakker48916f92012-09-16 19:57:18 +00003207 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3208 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3209 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003210 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003211 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003212
Paul Bakker48916f92012-09-16 19:57:18 +00003213 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214 if( ret != Z_OK )
3215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3217 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003218 }
3219
Angus Grattond8213d02016-05-25 20:56:48 +10003220 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003221 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003224 ssl->in_msglen ) );
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003227 ssl->in_msg, ssl->in_msglen );
3228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003230
3231 return( 0 );
3232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3236static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_SSL_PROTO_DTLS)
3239static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003240{
3241 /* If renegotiation is not enforced, retransmit until we would reach max
3242 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003243 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003244 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003245 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003246 unsigned char doublings = 1;
3247
3248 while( ratio != 0 )
3249 {
3250 ++doublings;
3251 ratio >>= 1;
3252 }
3253
3254 if( ++ssl->renego_records_seen > doublings )
3255 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003257 return( 0 );
3258 }
3259 }
3260
3261 return( ssl_write_hello_request( ssl ) );
3262}
3263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003265
Paul Bakker5121ce52009-01-03 21:22:43 +00003266/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003267 * Fill the input message buffer by appending data to it.
3268 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003269 *
3270 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3271 * available (from this read and/or a previous one). Otherwise, an error code
3272 * is returned (possibly EOF or WANT_READ).
3273 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003274 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3275 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3276 * since we always read a whole datagram at once.
3277 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003278 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003279 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282{
Paul Bakker23986e52011-04-24 08:57:21 +00003283 int ret;
3284 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003288 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003291 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003293 }
3294
Angus Grattond8213d02016-05-25 20:56:48 +10003295 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003299 }
3300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003304 uint32_t timeout;
3305
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003306 /* Just to be sure */
3307 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3308 {
3309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3310 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3312 }
3313
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003314 /*
3315 * The point is, we need to always read a full datagram at once, so we
3316 * sometimes read more then requested, and handle the additional data.
3317 * It could be the rest of the current record (while fetching the
3318 * header) and/or some other records in the same datagram.
3319 */
3320
3321 /*
3322 * Move to the next record in the already read datagram if applicable
3323 */
3324 if( ssl->next_record_offset != 0 )
3325 {
3326 if( ssl->in_left < ssl->next_record_offset )
3327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003330 }
3331
3332 ssl->in_left -= ssl->next_record_offset;
3333
3334 if( ssl->in_left != 0 )
3335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003337 ssl->next_record_offset ) );
3338 memmove( ssl->in_hdr,
3339 ssl->in_hdr + ssl->next_record_offset,
3340 ssl->in_left );
3341 }
3342
3343 ssl->next_record_offset = 0;
3344 }
3345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003348
3349 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003350 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003351 */
3352 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003355 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003356 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003357
3358 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003359 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003360 * are not at the beginning of a new record, the caller did something
3361 * wrong.
3362 */
3363 if( ssl->in_left != 0 )
3364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003367 }
3368
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003369 /*
3370 * Don't even try to read if time's out already.
3371 * This avoids by-passing the timer when repeatedly receiving messages
3372 * that will end up being dropped.
3373 */
3374 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003375 {
3376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003377 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003378 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003379 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003380 {
Angus Grattond8213d02016-05-25 20:56:48 +10003381 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003384 timeout = ssl->handshake->retransmit_timeout;
3385 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003386 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003389
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003390 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003391 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3392 timeout );
3393 else
3394 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397
3398 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400 }
3401
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003402 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003405 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003408 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003409 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003412 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003413 }
3414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003418 return( ret );
3419 }
3420
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003421 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003422 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003424 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003426 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003427 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003430 return( ret );
3431 }
3432
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003433 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003436 }
3437
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 if( ret < 0 )
3439 return( ret );
3440
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003441 ssl->in_left = ret;
3442 }
3443 else
3444#endif
3445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003447 ssl->in_left, nb_want ) );
3448
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003449 while( ssl->in_left < nb_want )
3450 {
3451 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003452
3453 if( ssl_check_timer( ssl ) != 0 )
3454 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3455 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003456 {
3457 if( ssl->f_recv_timeout != NULL )
3458 {
3459 ret = ssl->f_recv_timeout( ssl->p_bio,
3460 ssl->in_hdr + ssl->in_left, len,
3461 ssl->conf->read_timeout );
3462 }
3463 else
3464 {
3465 ret = ssl->f_recv( ssl->p_bio,
3466 ssl->in_hdr + ssl->in_left, len );
3467 }
3468 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003471 ssl->in_left, nb_want ) );
3472 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003473
3474 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003476
3477 if( ret < 0 )
3478 return( ret );
3479
mohammad160352aecb92018-03-28 23:41:40 -07003480 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003481 {
Darryl Green11999bb2018-03-13 15:22:58 +00003482 MBEDTLS_SSL_DEBUG_MSG( 1,
3483 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003484 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3486 }
3487
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003488 ssl->in_left += ret;
3489 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003490 }
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
3494 return( 0 );
3495}
3496
3497/*
3498 * Flush any data not yet written
3499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003501{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003502 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003503 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003506
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003507 if( ssl->f_send == NULL )
3508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003510 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003512 }
3513
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003514 /* Avoid incrementing counter if data is flushed */
3515 if( ssl->out_left == 0 )
3516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003518 return( 0 );
3519 }
3520
Paul Bakker5121ce52009-01-03 21:22:43 +00003521 while( ssl->out_left > 0 )
3522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003524 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Hanno Becker2b1e3542018-08-06 11:19:13 +01003526 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003527 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003530
3531 if( ret <= 0 )
3532 return( ret );
3533
mohammad160352aecb92018-03-28 23:41:40 -07003534 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003535 {
Darryl Green11999bb2018-03-13 15:22:58 +00003536 MBEDTLS_SSL_DEBUG_MSG( 1,
3537 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003538 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3540 }
3541
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 ssl->out_left -= ret;
3543 }
3544
Hanno Becker2b1e3542018-08-06 11:19:13 +01003545#if defined(MBEDTLS_SSL_PROTO_DTLS)
3546 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003547 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003548 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003549 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003550 else
3551#endif
3552 {
3553 ssl->out_hdr = ssl->out_buf + 8;
3554 }
3555 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
3559 return( 0 );
3560}
3561
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003562/*
3563 * Functions to handle the DTLS retransmission state machine
3564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003566/*
3567 * Append current handshake message to current outgoing flight
3568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3573 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3574 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575
3576 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003577 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003581 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582 }
3583
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003584 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003588 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589 }
3590
3591 /* Copy current handshake message with headers */
3592 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3593 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003594 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003595 msg->next = NULL;
3596
3597 /* Append to the current flight */
3598 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003599 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003600 else
3601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003603 while( cur->next != NULL )
3604 cur = cur->next;
3605 cur->next = msg;
3606 }
3607
Hanno Becker3b235902018-08-06 09:54:53 +01003608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003609 return( 0 );
3610}
3611
3612/*
3613 * Free the current flight of handshake messages
3614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003616{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 mbedtls_ssl_flight_item *cur = flight;
3618 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003619
3620 while( cur != NULL )
3621 {
3622 next = cur->next;
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 mbedtls_free( cur->p );
3625 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626
3627 cur = next;
3628 }
3629}
3630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3632static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003633#endif
3634
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003635/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003636 * Swap transform_out and out_ctr with the alternative ones
3637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003641 unsigned char tmp_out_ctr[8];
3642
3643 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003646 return;
3647 }
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003651 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003652 tmp_transform = ssl->transform_out;
3653 ssl->transform_out = ssl->handshake->alt_transform_out;
3654 ssl->handshake->alt_transform_out = tmp_transform;
3655
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003656 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003657 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3658 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003659 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003660
3661 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003662 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3665 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3670 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003671 }
3672 }
3673#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003674}
3675
3676/*
3677 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003678 */
3679int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3680{
3681 int ret = 0;
3682
3683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3684
3685 ret = mbedtls_ssl_flight_transmit( ssl );
3686
3687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3688
3689 return( ret );
3690}
3691
3692/*
3693 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003694 *
3695 * Need to remember the current message in case flush_output returns
3696 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003697 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003699int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003700{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003701 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003705 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003707
3708 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003709 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003710 ssl_swap_epochs( ssl );
3711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003713 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714
3715 while( ssl->handshake->cur_msg != NULL )
3716 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003717 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003718 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003719
Hanno Beckere1dcb032018-08-17 16:47:58 +01003720 int const is_finished =
3721 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3722 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3723
Hanno Becker04da1892018-08-14 13:22:10 +01003724 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3725 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3726
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003727 /* Swap epochs before sending Finished: we can't do it after
3728 * sending ChangeCipherSpec, in case write returns WANT_READ.
3729 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003730 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003731 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003733 ssl_swap_epochs( ssl );
3734 }
3735
Hanno Becker67bc7c32018-08-06 11:33:50 +01003736 ret = ssl_get_remaining_payload_in_datagram( ssl );
3737 if( ret < 0 )
3738 return( ret );
3739 max_frag_len = (size_t) ret;
3740
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003741 /* CCS is copied as is, while HS messages may need fragmentation */
3742 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3743 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003744 if( max_frag_len == 0 )
3745 {
3746 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3747 return( ret );
3748
3749 continue;
3750 }
3751
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003752 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003753 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003754 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003755
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003756 /* Update position inside current message */
3757 ssl->handshake->cur_msg_p += cur->len;
3758 }
3759 else
3760 {
3761 const unsigned char * const p = ssl->handshake->cur_msg_p;
3762 const size_t hs_len = cur->len - 12;
3763 const size_t frag_off = p - ( cur->p + 12 );
3764 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003765 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003766
Hanno Beckere1dcb032018-08-17 16:47:58 +01003767 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003768 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003769 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003770 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003771
Hanno Becker67bc7c32018-08-06 11:33:50 +01003772 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3773 return( ret );
3774
3775 continue;
3776 }
3777 max_hs_frag_len = max_frag_len - 12;
3778
3779 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3780 max_hs_frag_len : rem_len;
3781
3782 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003783 {
3784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003785 (unsigned) cur_hs_frag_len,
3786 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003787 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003788
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003789 /* Messages are stored with handshake headers as if not fragmented,
3790 * copy beginning of headers then fill fragmentation fields.
3791 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3792 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003793
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003794 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3795 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3796 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3797
Hanno Becker67bc7c32018-08-06 11:33:50 +01003798 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3799 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3800 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003801
3802 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3803
Hanno Becker3f7b9732018-08-28 09:53:25 +01003804 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003805 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3806 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003807 ssl->out_msgtype = cur->type;
3808
3809 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003810 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003811 }
3812
3813 /* If done with the current message move to the next one if any */
3814 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3815 {
3816 if( cur->next != NULL )
3817 {
3818 ssl->handshake->cur_msg = cur->next;
3819 ssl->handshake->cur_msg_p = cur->next->p + 12;
3820 }
3821 else
3822 {
3823 ssl->handshake->cur_msg = NULL;
3824 ssl->handshake->cur_msg_p = NULL;
3825 }
3826 }
3827
3828 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003829 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003832 return( ret );
3833 }
3834 }
3835
Hanno Becker67bc7c32018-08-06 11:33:50 +01003836 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3837 return( ret );
3838
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003839 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3841 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003842 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003845 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3846 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003847
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003849
3850 return( 0 );
3851}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003852
3853/*
3854 * To be called when the last message of an incoming flight is received.
3855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003857{
3858 /* We won't need to resend that one any more */
3859 ssl_flight_free( ssl->handshake->flight );
3860 ssl->handshake->flight = NULL;
3861 ssl->handshake->cur_msg = NULL;
3862
3863 /* The next incoming flight will start with this msg_seq */
3864 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3865
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003866 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003867 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003868
Hanno Becker0271f962018-08-16 13:23:47 +01003869 /* Clear future message buffering structure. */
3870 ssl_buffering_free( ssl );
3871
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003872 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003873 ssl_set_timer( ssl, 0 );
3874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3876 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003879 }
3880 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003882}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003883
3884/*
3885 * To be called when the last message of an outgoing flight is send.
3886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003888{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003889 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003890 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3893 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003896 }
3897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003901
Paul Bakker5121ce52009-01-03 21:22:43 +00003902/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003903 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003905
3906/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003907 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003908 *
3909 * - fill in handshake headers
3910 * - update handshake checksum
3911 * - DTLS: save message for resending
3912 * - then pass to the record layer
3913 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003914 * DTLS: except for HelloRequest, messages are only queued, and will only be
3915 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003916 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003917 * Inputs:
3918 * - ssl->out_msglen: 4 + actual handshake message len
3919 * (4 is the size of handshake headers for TLS)
3920 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3921 * - ssl->out_msg + 4: the handshake message body
3922 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003923 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003924 * - ssl->out_msglen: the length of the record contents
3925 * (including handshake headers but excluding record headers)
3926 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003927 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003928int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003929{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003930 int ret;
3931 const size_t hs_len = ssl->out_msglen - 4;
3932 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003933
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3935
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003936 /*
3937 * Sanity checks
3938 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003939 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003940 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3941 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003942 /* In SSLv3, the client might send a NoCertificate alert. */
3943#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3944 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3945 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3946 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3947#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3948 {
3949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3951 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003953
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003954 /* Whenever we send anything different from a
3955 * HelloRequest we should be in a handshake - double check. */
3956 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3957 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003958 ssl->handshake == NULL )
3959 {
3960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3961 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003965 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003966 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003971 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003972#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003973
Hanno Beckerb50a2532018-08-06 11:52:54 +01003974 /* Double-check that we did not exceed the bounds
3975 * of the outgoing record buffer.
3976 * This should never fail as the various message
3977 * writing functions must obey the bounds of the
3978 * outgoing record buffer, but better be safe.
3979 *
3980 * Note: We deliberately do not check for the MTU or MFL here.
3981 */
3982 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3983 {
3984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3985 "size %u, maximum %u",
3986 (unsigned) ssl->out_msglen,
3987 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3989 }
3990
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003991 /*
3992 * Fill handshake headers
3993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003995 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003996 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3997 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3998 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003999
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004000 /*
4001 * DTLS has additional fields in the Handshake layer,
4002 * between the length field and the actual payload:
4003 * uint16 message_seq;
4004 * uint24 fragment_offset;
4005 * uint24 fragment_length;
4006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004008 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004009 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004010 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004011 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004012 {
4013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4014 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004015 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004016 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4018 }
4019
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004020 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004021 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004022
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004023 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004024 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004025 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004026 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4027 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4028 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004029 }
4030 else
4031 {
4032 ssl->out_msg[4] = 0;
4033 ssl->out_msg[5] = 0;
4034 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004035
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004036 /* Handshake hashes are computed without fragmentation,
4037 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004038 memset( ssl->out_msg + 6, 0x00, 3 );
4039 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004042
Hanno Becker0207e532018-08-28 10:28:28 +01004043 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004044 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4045 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 }
4047
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004048 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004050 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004051 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4052 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053 {
4054 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004057 return( ret );
4058 }
4059 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004060 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004061#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004062 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004063 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004064 {
4065 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4066 return( ret );
4067 }
4068 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004069
4070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4071
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004072 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004073}
4074
4075/*
4076 * Record layer functions
4077 */
4078
4079/*
4080 * Write current record.
4081 *
4082 * Uses:
4083 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4084 * - ssl->out_msglen: length of the record content (excl headers)
4085 * - ssl->out_msg: record content
4086 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004087int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004088{
4089 int ret, done = 0;
4090 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004091 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004092
4093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004096 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004098 {
4099 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004102 return( ret );
4103 }
4104
4105 len = ssl->out_msglen;
4106 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4110 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 ret = mbedtls_ssl_hw_record_write( ssl );
4115 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4118 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004119 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004120
4121 if( ret == 0 )
4122 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004125 if( !done )
4126 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004127 unsigned i;
4128 size_t protected_record_size;
4129
Hanno Becker6430faf2019-05-08 11:57:13 +01004130 /* Skip writing the record content type to after the encryption,
4131 * as it may change when using the CID extension. */
4132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004134 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004135
Hanno Becker19859472018-08-06 09:40:20 +01004136 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004137 ssl->out_len[0] = (unsigned char)( len >> 8 );
4138 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004139
Paul Bakker48916f92012-09-16 19:57:18 +00004140 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004141 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004142 mbedtls_record rec;
4143
4144 rec.buf = ssl->out_iv;
4145 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4146 ( ssl->out_iv - ssl->out_buf );
4147 rec.data_len = ssl->out_msglen;
4148 rec.data_offset = ssl->out_msg - rec.buf;
4149
4150 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4151 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4152 ssl->conf->transport, rec.ver );
4153 rec.type = ssl->out_msgtype;
4154
Hanno Becker43c24b82019-05-01 09:45:57 +01004155#if defined(MBEDTLS_SSL_CID)
4156 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004157 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004158#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004159
Hanno Beckera18d1322018-01-03 14:27:32 +00004160 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004161 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004164 return( ret );
4165 }
4166
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004167 if( rec.data_offset != 0 )
4168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4170 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4171 }
4172
Hanno Becker6430faf2019-05-08 11:57:13 +01004173 /* Update the record content type and CID. */
4174 ssl->out_msgtype = rec.type;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004175#if defined(MBEDTLS_SSL_CID )
4176 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
4177#endif /* MBEDTLS_SSL_CID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004178 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004179 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4180 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004181 }
4182
Hanno Becker5903de42019-05-03 14:46:38 +01004183 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004184
4185#if defined(MBEDTLS_SSL_PROTO_DTLS)
4186 /* In case of DTLS, double-check that we don't exceed
4187 * the remaining space in the datagram. */
4188 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4189 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004190 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004191 if( ret < 0 )
4192 return( ret );
4193
4194 if( protected_record_size > (size_t) ret )
4195 {
4196 /* Should never happen */
4197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4198 }
4199 }
4200#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004201
Hanno Becker6430faf2019-05-08 11:57:13 +01004202 /* Now write the potentially updated record content type. */
4203 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004206 "version = [%d:%d], msglen = %d",
4207 ssl->out_hdr[0], ssl->out_hdr[1],
4208 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004211 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004212
4213 ssl->out_left += protected_record_size;
4214 ssl->out_hdr += protected_record_size;
4215 ssl_update_out_pointers( ssl, ssl->transform_out );
4216
Hanno Becker04484622018-08-06 09:49:38 +01004217 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4218 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4219 break;
4220
4221 /* The loop goes to its end iff the counter is wrapping */
4222 if( i == ssl_ep_len( ssl ) )
4223 {
4224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4225 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 }
4228
Hanno Becker67bc7c32018-08-06 11:33:50 +01004229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4231 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004232 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004233 size_t remaining;
4234 ret = ssl_get_remaining_payload_in_datagram( ssl );
4235 if( ret < 0 )
4236 {
4237 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4238 ret );
4239 return( ret );
4240 }
4241
4242 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004243 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004244 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004245 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004246 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004247 else
4248 {
Hanno Becker513815a2018-08-20 11:56:09 +01004249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004250 }
4251 }
4252#endif /* MBEDTLS_SSL_PROTO_DTLS */
4253
4254 if( ( flush == SSL_FORCE_FLUSH ) &&
4255 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 return( ret );
4259 }
4260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004262
4263 return( 0 );
4264}
4265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004267
4268static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4269{
4270 if( ssl->in_msglen < ssl->in_hslen ||
4271 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4272 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4273 {
4274 return( 1 );
4275 }
4276 return( 0 );
4277}
Hanno Becker44650b72018-08-16 12:51:11 +01004278
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004279static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004280{
4281 return( ( ssl->in_msg[9] << 16 ) |
4282 ( ssl->in_msg[10] << 8 ) |
4283 ssl->in_msg[11] );
4284}
4285
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004286static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004287{
4288 return( ( ssl->in_msg[6] << 16 ) |
4289 ( ssl->in_msg[7] << 8 ) |
4290 ssl->in_msg[8] );
4291}
4292
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004293static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004294{
4295 uint32_t msg_len, frag_off, frag_len;
4296
4297 msg_len = ssl_get_hs_total_len( ssl );
4298 frag_off = ssl_get_hs_frag_off( ssl );
4299 frag_len = ssl_get_hs_frag_len( ssl );
4300
4301 if( frag_off > msg_len )
4302 return( -1 );
4303
4304 if( frag_len > msg_len - frag_off )
4305 return( -1 );
4306
4307 if( frag_len + 12 > ssl->in_msglen )
4308 return( -1 );
4309
4310 return( 0 );
4311}
4312
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004313/*
4314 * Mark bits in bitmask (used for DTLS HS reassembly)
4315 */
4316static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4317{
4318 unsigned int start_bits, end_bits;
4319
4320 start_bits = 8 - ( offset % 8 );
4321 if( start_bits != 8 )
4322 {
4323 size_t first_byte_idx = offset / 8;
4324
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004325 /* Special case */
4326 if( len <= start_bits )
4327 {
4328 for( ; len != 0; len-- )
4329 mask[first_byte_idx] |= 1 << ( start_bits - len );
4330
4331 /* Avoid potential issues with offset or len becoming invalid */
4332 return;
4333 }
4334
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004335 offset += start_bits; /* Now offset % 8 == 0 */
4336 len -= start_bits;
4337
4338 for( ; start_bits != 0; start_bits-- )
4339 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4340 }
4341
4342 end_bits = len % 8;
4343 if( end_bits != 0 )
4344 {
4345 size_t last_byte_idx = ( offset + len ) / 8;
4346
4347 len -= end_bits; /* Now len % 8 == 0 */
4348
4349 for( ; end_bits != 0; end_bits-- )
4350 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4351 }
4352
4353 memset( mask + offset / 8, 0xFF, len / 8 );
4354}
4355
4356/*
4357 * Check that bitmask is full
4358 */
4359static int ssl_bitmask_check( unsigned char *mask, size_t len )
4360{
4361 size_t i;
4362
4363 for( i = 0; i < len / 8; i++ )
4364 if( mask[i] != 0xFF )
4365 return( -1 );
4366
4367 for( i = 0; i < len % 8; i++ )
4368 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4369 return( -1 );
4370
4371 return( 0 );
4372}
4373
Hanno Becker56e205e2018-08-16 09:06:12 +01004374/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004375static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004376 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004377{
Hanno Becker56e205e2018-08-16 09:06:12 +01004378 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004379
Hanno Becker56e205e2018-08-16 09:06:12 +01004380 alloc_len = 12; /* Handshake header */
4381 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004382
Hanno Beckerd07df862018-08-16 09:14:58 +01004383 if( add_bitmap )
4384 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004385
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004386 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004387}
Hanno Becker56e205e2018-08-16 09:06:12 +01004388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004390
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004391static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004392{
4393 return( ( ssl->in_msg[1] << 16 ) |
4394 ( ssl->in_msg[2] << 8 ) |
4395 ssl->in_msg[3] );
4396}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004397
Simon Butcher99000142016-10-13 17:21:01 +01004398int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004403 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004405 }
4406
Hanno Becker12555c62018-08-16 12:47:53 +01004407 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004410 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004411 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004415 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004416 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004417 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004418
Hanno Becker44650b72018-08-16 12:51:11 +01004419 if( ssl_check_hs_header( ssl ) != 0 )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4422 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4423 }
4424
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004425 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004426 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4427 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4428 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4429 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004430 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004431 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4432 {
4433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4434 recv_msg_seq,
4435 ssl->handshake->in_msg_seq ) );
4436 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4437 }
4438
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004439 /* Retransmit only on last message from previous flight, to avoid
4440 * too many retransmissions.
4441 * Besides, No sane server ever retransmits HelloVerifyRequest */
4442 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004446 "message_seq = %d, start_of_flight = %d",
4447 recv_msg_seq,
4448 ssl->handshake->in_flight_start_seq ) );
4449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004453 return( ret );
4454 }
4455 }
4456 else
4457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004459 "message_seq = %d, expected = %d",
4460 recv_msg_seq,
4461 ssl->handshake->in_msg_seq ) );
4462 }
4463
Hanno Becker90333da2017-10-10 11:27:13 +01004464 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004465 }
4466 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004467
Hanno Becker6d97ef52018-08-16 13:09:04 +01004468 /* Message reassembly is handled alongside buffering of future
4469 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004470 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004471 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004472 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004475 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004476 }
4477 }
4478 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004480 /* With TLS we don't handle fragmentation (for now) */
4481 if( ssl->in_msglen < ssl->in_hslen )
4482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4484 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004485 }
4486
Simon Butcher99000142016-10-13 17:21:01 +01004487 return( 0 );
4488}
4489
4490void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4491{
Hanno Becker0271f962018-08-16 13:23:47 +01004492 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004493
Hanno Becker0271f962018-08-16 13:23:47 +01004494 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004495 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004496 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004497 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004498
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004499 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004502 ssl->handshake != NULL )
4503 {
Hanno Becker0271f962018-08-16 13:23:47 +01004504 unsigned offset;
4505 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004506
Hanno Becker0271f962018-08-16 13:23:47 +01004507 /* Increment handshake sequence number */
4508 hs->in_msg_seq++;
4509
4510 /*
4511 * Clear up handshake buffering and reassembly structure.
4512 */
4513
4514 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004515 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004516
4517 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004518 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4519 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004520 offset++, hs_buf++ )
4521 {
4522 *hs_buf = *(hs_buf + 1);
4523 }
4524
4525 /* Create a fresh last entry */
4526 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004527 }
4528#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004529}
4530
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004531/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004532 * DTLS anti-replay: RFC 6347 4.1.2.6
4533 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004534 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4535 * Bit n is set iff record number in_window_top - n has been seen.
4536 *
4537 * Usually, in_window_top is the last record number seen and the lsb of
4538 * in_window is set. The only exception is the initial state (record number 0
4539 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004541#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4542static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004543{
4544 ssl->in_window_top = 0;
4545 ssl->in_window = 0;
4546}
4547
4548static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4549{
4550 return( ( (uint64_t) buf[0] << 40 ) |
4551 ( (uint64_t) buf[1] << 32 ) |
4552 ( (uint64_t) buf[2] << 24 ) |
4553 ( (uint64_t) buf[3] << 16 ) |
4554 ( (uint64_t) buf[4] << 8 ) |
4555 ( (uint64_t) buf[5] ) );
4556}
4557
4558/*
4559 * Return 0 if sequence number is acceptable, -1 otherwise
4560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004562{
4563 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4564 uint64_t bit;
4565
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004566 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004567 return( 0 );
4568
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004569 if( rec_seqnum > ssl->in_window_top )
4570 return( 0 );
4571
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004572 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004573
4574 if( bit >= 64 )
4575 return( -1 );
4576
4577 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4578 return( -1 );
4579
4580 return( 0 );
4581}
4582
4583/*
4584 * Update replay window on new validated record
4585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004587{
4588 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4589
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004590 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004591 return;
4592
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004593 if( rec_seqnum > ssl->in_window_top )
4594 {
4595 /* Update window_top and the contents of the window */
4596 uint64_t shift = rec_seqnum - ssl->in_window_top;
4597
4598 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004599 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004600 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004601 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004602 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004603 ssl->in_window |= 1;
4604 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004605
4606 ssl->in_window_top = rec_seqnum;
4607 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004608 else
4609 {
4610 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004611 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004612
4613 if( bit < 64 ) /* Always true, but be extra sure */
4614 ssl->in_window |= (uint64_t) 1 << bit;
4615 }
4616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004618
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004619#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004620/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004621static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4622
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004623/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004624 * Without any SSL context, check if a datagram looks like a ClientHello with
4625 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004626 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004627 *
4628 * - if cookie is valid, return 0
4629 * - if ClientHello looks superficially valid but cookie is not,
4630 * fill obuf and set olen, then
4631 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4632 * - otherwise return a specific error code
4633 */
4634static int ssl_check_dtls_clihlo_cookie(
4635 mbedtls_ssl_cookie_write_t *f_cookie_write,
4636 mbedtls_ssl_cookie_check_t *f_cookie_check,
4637 void *p_cookie,
4638 const unsigned char *cli_id, size_t cli_id_len,
4639 const unsigned char *in, size_t in_len,
4640 unsigned char *obuf, size_t buf_len, size_t *olen )
4641{
4642 size_t sid_len, cookie_len;
4643 unsigned char *p;
4644
4645 if( f_cookie_write == NULL || f_cookie_check == NULL )
4646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4647
4648 /*
4649 * Structure of ClientHello with record and handshake headers,
4650 * and expected values. We don't need to check a lot, more checks will be
4651 * done when actually parsing the ClientHello - skipping those checks
4652 * avoids code duplication and does not make cookie forging any easier.
4653 *
4654 * 0-0 ContentType type; copied, must be handshake
4655 * 1-2 ProtocolVersion version; copied
4656 * 3-4 uint16 epoch; copied, must be 0
4657 * 5-10 uint48 sequence_number; copied
4658 * 11-12 uint16 length; (ignored)
4659 *
4660 * 13-13 HandshakeType msg_type; (ignored)
4661 * 14-16 uint24 length; (ignored)
4662 * 17-18 uint16 message_seq; copied
4663 * 19-21 uint24 fragment_offset; copied, must be 0
4664 * 22-24 uint24 fragment_length; (ignored)
4665 *
4666 * 25-26 ProtocolVersion client_version; (ignored)
4667 * 27-58 Random random; (ignored)
4668 * 59-xx SessionID session_id; 1 byte len + sid_len content
4669 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4670 * ...
4671 *
4672 * Minimum length is 61 bytes.
4673 */
4674 if( in_len < 61 ||
4675 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4676 in[3] != 0 || in[4] != 0 ||
4677 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4678 {
4679 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4680 }
4681
4682 sid_len = in[59];
4683 if( sid_len > in_len - 61 )
4684 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4685
4686 cookie_len = in[60 + sid_len];
4687 if( cookie_len > in_len - 60 )
4688 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4689
4690 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4691 cli_id, cli_id_len ) == 0 )
4692 {
4693 /* Valid cookie */
4694 return( 0 );
4695 }
4696
4697 /*
4698 * If we get here, we've got an invalid cookie, let's prepare HVR.
4699 *
4700 * 0-0 ContentType type; copied
4701 * 1-2 ProtocolVersion version; copied
4702 * 3-4 uint16 epoch; copied
4703 * 5-10 uint48 sequence_number; copied
4704 * 11-12 uint16 length; olen - 13
4705 *
4706 * 13-13 HandshakeType msg_type; hello_verify_request
4707 * 14-16 uint24 length; olen - 25
4708 * 17-18 uint16 message_seq; copied
4709 * 19-21 uint24 fragment_offset; copied
4710 * 22-24 uint24 fragment_length; olen - 25
4711 *
4712 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4713 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4714 *
4715 * Minimum length is 28.
4716 */
4717 if( buf_len < 28 )
4718 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4719
4720 /* Copy most fields and adapt others */
4721 memcpy( obuf, in, 25 );
4722 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4723 obuf[25] = 0xfe;
4724 obuf[26] = 0xff;
4725
4726 /* Generate and write actual cookie */
4727 p = obuf + 28;
4728 if( f_cookie_write( p_cookie,
4729 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4730 {
4731 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4732 }
4733
4734 *olen = p - obuf;
4735
4736 /* Go back and fill length fields */
4737 obuf[27] = (unsigned char)( *olen - 28 );
4738
4739 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4740 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4741 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4742
4743 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4744 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4745
4746 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4747}
4748
4749/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004750 * Handle possible client reconnect with the same UDP quadruplet
4751 * (RFC 6347 Section 4.2.8).
4752 *
4753 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4754 * that looks like a ClientHello.
4755 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004756 * - if the input looks like a ClientHello without cookies,
4757 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004758 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004759 * - if the input looks like a ClientHello with a valid cookie,
4760 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004761 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004762 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004763 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004764 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004765 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4766 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004767 */
4768static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4769{
4770 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004771 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004772
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 ret = ssl_check_dtls_clihlo_cookie(
4774 ssl->conf->f_cookie_write,
4775 ssl->conf->f_cookie_check,
4776 ssl->conf->p_cookie,
4777 ssl->cli_id, ssl->cli_id_len,
4778 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004779 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004780
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004781 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4782
4783 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004784 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004785 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004786 * If the error is permanent we'll catch it later,
4787 * if it's not, then hopefully it'll work next time. */
4788 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4789
4790 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004791 }
4792
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004793 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004794 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004795 /* Got a valid cookie, partially reset context */
4796 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4797 {
4798 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4799 return( ret );
4800 }
4801
4802 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004803 }
4804
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004805 return( ret );
4806}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004807#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004808
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004809static int ssl_check_record_type( uint8_t record_type )
4810{
4811 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4812 record_type != MBEDTLS_SSL_MSG_ALERT &&
4813 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4814 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4815 {
4816 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4817 }
4818
4819 return( 0 );
4820}
4821
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004822/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004823 * ContentType type;
4824 * ProtocolVersion version;
4825 * uint16 epoch; // DTLS only
4826 * uint48 sequence_number; // DTLS only
4827 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004828 *
4829 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004830 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004831 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4832 *
4833 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004834 * 1. proceed with the record if this function returns 0
4835 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4836 * 3. return CLIENT_RECONNECT if this function return that value
4837 * 4. drop the whole datagram if this function returns anything else.
4838 * Point 2 is needed when the peer is resending, and we have already received
4839 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004842{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004843 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004844 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004845
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004846 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004847
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004849 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004850
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004851 /* Check record type */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004852#if defined(MBEDTLS_SSL_CID)
4853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4854 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4855 ssl->conf->cid_len != 0 )
4856 {
4857 /* Shift pointers to account for record header including CID
4858 * struct {
4859 * ContentType special_type = tls12_cid;
4860 * ProtocolVersion version;
4861 * uint16 epoch;
4862 * uint48 sequence_number;
4863 * opaque cid[cid_length]; // New field
4864 * uint16 length;
4865 * opaque enc_content[DTLSCiphertext.length];
4866 * } DTLSCiphertext;
4867 */
4868
4869 /* So far, we only support static CID lengths
4870 * fixed in the configuration. */
4871 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4872 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4873 }
4874 else
4875#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004876 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004879
4880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004881 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4882 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004883 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4884#endif /* MBEDTLS_SSL_PROTO_DTLS */
4885 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4886 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004889 }
4890
4891 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004892 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4895 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 }
4897
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004898 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4901 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 }
4903
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004904 /* Now that the total length of the record header is known, ensure
4905 * that the current datagram is large enough to hold it.
4906 * This would fail, for example, if we received a datagram of
4907 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4908 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4909 if( ret != 0 )
4910 {
4911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4912 return( ret );
4913 }
4914 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4915
4916 /* Parse and validate record length
4917 * This must happen after the CID parsing because
4918 * its position in the record header depends on
4919 * the presence of a CID. */
4920
4921 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004922 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004923 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4926 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004927 }
4928
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4930 "version = [%d:%d], msglen = %d",
4931 ssl->in_msgtype,
4932 major_ver, minor_ver, ssl->in_msglen ) );
4933
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004934 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004935 * DTLS-related tests.
4936 * Check epoch before checking length constraint because
4937 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4938 * message gets duplicated before the corresponding Finished message,
4939 * the second ChangeCipherSpec should be discarded because it belongs
4940 * to an old epoch, but not because its length is shorter than
4941 * the minimum record length for packets using the new record transform.
4942 * Note that these two kinds of failures are handled differently,
4943 * as an unexpected record is silently skipped but an invalid
4944 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004945 */
4946#if defined(MBEDTLS_SSL_PROTO_DTLS)
4947 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4948 {
4949 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4950
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004951 /* Check epoch (and sequence number) with DTLS */
4952 if( rec_epoch != ssl->in_epoch )
4953 {
4954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4955 "expected %d, received %d",
4956 ssl->in_epoch, rec_epoch ) );
4957
4958#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4959 /*
4960 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4961 * access the first byte of record content (handshake type), as we
4962 * have an active transform (possibly iv_len != 0), so use the
4963 * fact that the record header len is 13 instead.
4964 */
4965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4966 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4967 rec_epoch == 0 &&
4968 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4969 ssl->in_left > 13 &&
4970 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4971 {
4972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4973 "from the same port" ) );
4974 return( ssl_handle_possible_reconnect( ssl ) );
4975 }
4976 else
4977#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004978 {
4979 /* Consider buffering the record. */
4980 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4981 {
4982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4983 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4984 }
4985
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004986 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004987 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004988 }
4989
4990#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4991 /* Replay detection only works for the current epoch */
4992 if( rec_epoch == ssl->in_epoch &&
4993 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4994 {
4995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4996 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4997 }
4998#endif
4999 }
5000#endif /* MBEDTLS_SSL_PROTO_DTLS */
5001
Hanno Becker52c6dc62017-05-26 16:07:36 +01005002
5003 /* Check length against bounds of the current transform and version */
5004 if( ssl->transform_in == NULL )
5005 {
5006 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005007 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005008 {
5009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5010 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5011 }
5012 }
5013 else
5014 {
5015 if( ssl->in_msglen < ssl->transform_in->minlen )
5016 {
5017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5018 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5019 }
5020
5021#if defined(MBEDTLS_SSL_PROTO_SSL3)
5022 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005023 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005024 {
5025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5026 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5027 }
5028#endif
5029#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5030 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5031 /*
5032 * TLS encrypted messages can have up to 256 bytes of padding
5033 */
5034 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5035 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005036 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005037 {
5038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5039 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5040 }
5041#endif
5042 }
5043
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005044 return( 0 );
5045}
Paul Bakker5121ce52009-01-03 21:22:43 +00005046
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005047/*
5048 * If applicable, decrypt (and decompress) record content
5049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005051{
5052 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005054 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005055 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5058 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 ret = mbedtls_ssl_hw_record_read( ssl );
5063 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5066 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005067 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005068
5069 if( ret == 0 )
5070 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005073 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005074 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005075 mbedtls_record rec;
5076
5077 rec.buf = ssl->in_iv;
5078 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5079 - ( ssl->in_iv - ssl->in_buf );
5080 rec.data_len = ssl->in_msglen;
5081 rec.data_offset = 0;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005082#if defined(MBEDTLS_SSL_CID )
5083 rec.cid_len = ssl->in_len - ssl->in_cid;
5084 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
5085#endif /* MBEDTLS_SSL_CID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005086
5087 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5088 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5089 ssl->conf->transport, rec.ver );
5090 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005091 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5092 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker16ded982019-05-08 13:02:55 +01005095 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5096 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5097
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 return( ret );
5099 }
5100
Hanno Becker6430faf2019-05-08 11:57:13 +01005101 if( ssl->in_msgtype != rec.type )
5102 {
5103 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5104 ssl->in_msgtype, rec.type ) );
5105 }
5106
5107 /* The record content type may change during decryption,
5108 * so re-read it. */
5109 ssl->in_msgtype = rec.type;
5110 /* Also update the input buffer, because unfortunately
5111 * the server-side ssl_parse_client_hello() reparses the
5112 * record header when receiving a ClientHello initiating
5113 * a renegotiation. */
5114 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005115 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005116 ssl->in_msglen = rec.data_len;
5117 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5118 ssl->in_len[1] = (unsigned char)( rec.data_len );
5119
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005120 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5121 ssl->in_msg, ssl->in_msglen );
5122
Hanno Becker6430faf2019-05-08 11:57:13 +01005123#if defined(MBEDTLS_SSL_CID)
5124 /* We have already checked the record content type
5125 * in ssl_parse_record_header(), failing or silently
5126 * dropping the record in the case of an unknown type.
5127 *
5128 * Since with the use of CIDs, the record content type
5129 * might change during decryption, re-check the record
5130 * content type, but treat a failure as fatal this time. */
5131 if( ssl_check_record_type( ssl->in_msgtype ) )
5132 {
5133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5134 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5135 }
5136#endif /* MBEDTLS_SSL_CID */
5137
Angus Grattond8213d02016-05-25 20:56:48 +10005138 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5141 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005142 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005143 else if( ssl->in_msglen == 0 )
5144 {
5145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5146 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5147 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5148 {
5149 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5151 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5152 }
5153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5154
5155 ssl->nb_zero++;
5156
5157 /*
5158 * Three or more empty messages may be a DoS attack
5159 * (excessive CPU consumption).
5160 */
5161 if( ssl->nb_zero > 3 )
5162 {
5163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005164 "messages, possible DoS attack" ) );
5165 /* Treat the records as if they were not properly authenticated,
5166 * thereby failing the connection if we see more than allowed
5167 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005168 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5169 }
5170 }
5171 else
5172 ssl->nb_zero = 0;
5173
5174#if defined(MBEDTLS_SSL_PROTO_DTLS)
5175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5176 {
5177 ; /* in_ctr read from peer, not maintained internally */
5178 }
5179 else
5180#endif
5181 {
5182 unsigned i;
5183 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5184 if( ++ssl->in_ctr[i - 1] != 0 )
5185 break;
5186
5187 /* The loop goes to its end iff the counter is wrapping */
5188 if( i == ssl_ep_len( ssl ) )
5189 {
5190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5191 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5192 }
5193 }
5194
Paul Bakker5121ce52009-01-03 21:22:43 +00005195 }
5196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005198 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005200 {
5201 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005204 return( ret );
5205 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005206 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005213 }
5214#endif
5215
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005216 return( 0 );
5217}
5218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005220
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005221/*
5222 * Read a record.
5223 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005224 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5225 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5226 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005227 */
Hanno Becker1097b342018-08-15 14:09:41 +01005228
5229/* Helper functions for mbedtls_ssl_read_record(). */
5230static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005231static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5232static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005233
Hanno Becker327c93b2018-08-15 13:56:18 +01005234int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005235 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005236{
5237 int ret;
5238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005240
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005241 if( ssl->keep_current_message == 0 )
5242 {
5243 do {
Simon Butcher99000142016-10-13 17:21:01 +01005244
Hanno Becker26994592018-08-15 14:14:59 +01005245 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005246 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005247 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005248
Hanno Beckere74d5562018-08-15 14:26:08 +01005249 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005250 {
Hanno Becker40f50842018-08-15 14:48:01 +01005251#if defined(MBEDTLS_SSL_PROTO_DTLS)
5252 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005253
Hanno Becker40f50842018-08-15 14:48:01 +01005254 /* We only check for buffered messages if the
5255 * current datagram is fully consumed. */
5256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005257 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005258 {
Hanno Becker40f50842018-08-15 14:48:01 +01005259 if( ssl_load_buffered_message( ssl ) == 0 )
5260 have_buffered = 1;
5261 }
5262
5263 if( have_buffered == 0 )
5264#endif /* MBEDTLS_SSL_PROTO_DTLS */
5265 {
5266 ret = ssl_get_next_record( ssl );
5267 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5268 continue;
5269
5270 if( ret != 0 )
5271 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005272 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005273 return( ret );
5274 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005275 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005276 }
5277
5278 ret = mbedtls_ssl_handle_message_type( ssl );
5279
Hanno Becker40f50842018-08-15 14:48:01 +01005280#if defined(MBEDTLS_SSL_PROTO_DTLS)
5281 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5282 {
5283 /* Buffer future message */
5284 ret = ssl_buffer_message( ssl );
5285 if( ret != 0 )
5286 return( ret );
5287
5288 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5289 }
5290#endif /* MBEDTLS_SSL_PROTO_DTLS */
5291
Hanno Becker90333da2017-10-10 11:27:13 +01005292 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5293 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005294
5295 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005296 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005297 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005298 return( ret );
5299 }
5300
Hanno Becker327c93b2018-08-15 13:56:18 +01005301 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005302 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005303 {
5304 mbedtls_ssl_update_handshake_status( ssl );
5305 }
Simon Butcher99000142016-10-13 17:21:01 +01005306 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005307 else
Simon Butcher99000142016-10-13 17:21:01 +01005308 {
Hanno Becker02f59072018-08-15 14:00:24 +01005309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005310 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005311 }
5312
5313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5314
5315 return( 0 );
5316}
5317
Hanno Becker40f50842018-08-15 14:48:01 +01005318#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005319static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005320{
Hanno Becker40f50842018-08-15 14:48:01 +01005321 if( ssl->in_left > ssl->next_record_offset )
5322 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005323
Hanno Becker40f50842018-08-15 14:48:01 +01005324 return( 0 );
5325}
5326
5327static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5328{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005329 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005330 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005331 int ret = 0;
5332
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005333 if( hs == NULL )
5334 return( -1 );
5335
Hanno Beckere00ae372018-08-20 09:39:42 +01005336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5337
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005338 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5339 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5340 {
5341 /* Check if we have seen a ChangeCipherSpec before.
5342 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005343 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005344 {
5345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5346 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005347 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005348 }
5349
Hanno Becker39b8bc92018-08-28 17:17:13 +01005350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005351 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5352 ssl->in_msglen = 1;
5353 ssl->in_msg[0] = 1;
5354
5355 /* As long as they are equal, the exact value doesn't matter. */
5356 ssl->in_left = 0;
5357 ssl->next_record_offset = 0;
5358
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005359 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005360 goto exit;
5361 }
Hanno Becker37f95322018-08-16 13:55:32 +01005362
Hanno Beckerb8f50142018-08-28 10:01:34 +01005363#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005364 /* Debug only */
5365 {
5366 unsigned offset;
5367 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5368 {
5369 hs_buf = &hs->buffering.hs[offset];
5370 if( hs_buf->is_valid == 1 )
5371 {
5372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5373 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005374 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005375 }
5376 }
5377 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005378#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005379
5380 /* Check if we have buffered and/or fully reassembled the
5381 * next handshake message. */
5382 hs_buf = &hs->buffering.hs[0];
5383 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5384 {
5385 /* Synthesize a record containing the buffered HS message. */
5386 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5387 ( hs_buf->data[2] << 8 ) |
5388 hs_buf->data[3];
5389
5390 /* Double-check that we haven't accidentally buffered
5391 * a message that doesn't fit into the input buffer. */
5392 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5393 {
5394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5396 }
5397
5398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5399 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5400 hs_buf->data, msg_len + 12 );
5401
5402 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5403 ssl->in_hslen = msg_len + 12;
5404 ssl->in_msglen = msg_len + 12;
5405 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5406
5407 ret = 0;
5408 goto exit;
5409 }
5410 else
5411 {
5412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5413 hs->in_msg_seq ) );
5414 }
5415
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005416 ret = -1;
5417
5418exit:
5419
5420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5421 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005422}
5423
Hanno Beckera02b0b42018-08-21 17:20:27 +01005424static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5425 size_t desired )
5426{
5427 int offset;
5428 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5430 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005431
Hanno Becker01315ea2018-08-21 17:22:17 +01005432 /* Get rid of future records epoch first, if such exist. */
5433 ssl_free_buffered_record( ssl );
5434
5435 /* Check if we have enough space available now. */
5436 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5437 hs->buffering.total_bytes_buffered ) )
5438 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005440 return( 0 );
5441 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005442
Hanno Becker4f432ad2018-08-28 10:02:32 +01005443 /* We don't have enough space to buffer the next expected handshake
5444 * message. Remove buffers used for future messages to gain space,
5445 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005446 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5447 offset >= 0; offset-- )
5448 {
5449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5450 offset ) );
5451
Hanno Beckerb309b922018-08-23 13:18:05 +01005452 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005453
5454 /* Check if we have enough space available now. */
5455 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5456 hs->buffering.total_bytes_buffered ) )
5457 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005459 return( 0 );
5460 }
5461 }
5462
5463 return( -1 );
5464}
5465
Hanno Becker40f50842018-08-15 14:48:01 +01005466static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5467{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005468 int ret = 0;
5469 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5470
5471 if( hs == NULL )
5472 return( 0 );
5473
5474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5475
5476 switch( ssl->in_msgtype )
5477 {
5478 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005480
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005481 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005482 break;
5483
5484 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005485 {
5486 unsigned recv_msg_seq_offset;
5487 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5488 mbedtls_ssl_hs_buffer *hs_buf;
5489 size_t msg_len = ssl->in_hslen - 12;
5490
5491 /* We should never receive an old handshake
5492 * message - double-check nonetheless. */
5493 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5494 {
5495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5496 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5497 }
5498
5499 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5500 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5501 {
5502 /* Silently ignore -- message too far in the future */
5503 MBEDTLS_SSL_DEBUG_MSG( 2,
5504 ( "Ignore future HS message with sequence number %u, "
5505 "buffering window %u - %u",
5506 recv_msg_seq, ssl->handshake->in_msg_seq,
5507 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5508
5509 goto exit;
5510 }
5511
5512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5513 recv_msg_seq, recv_msg_seq_offset ) );
5514
5515 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5516
5517 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005518 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005519 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005520 size_t reassembly_buf_sz;
5521
Hanno Becker37f95322018-08-16 13:55:32 +01005522 hs_buf->is_fragmented =
5523 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5524
5525 /* We copy the message back into the input buffer
5526 * after reassembly, so check that it's not too large.
5527 * This is an implementation-specific limitation
5528 * and not one from the standard, hence it is not
5529 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005530 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005531 {
5532 /* Ignore message */
5533 goto exit;
5534 }
5535
Hanno Beckere0b150f2018-08-21 15:51:03 +01005536 /* Check if we have enough space to buffer the message. */
5537 if( hs->buffering.total_bytes_buffered >
5538 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5539 {
5540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5542 }
5543
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005544 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5545 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005546
5547 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5548 hs->buffering.total_bytes_buffered ) )
5549 {
5550 if( recv_msg_seq_offset > 0 )
5551 {
5552 /* If we can't buffer a future message because
5553 * of space limitations -- ignore. */
5554 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",
5555 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5556 (unsigned) hs->buffering.total_bytes_buffered ) );
5557 goto exit;
5558 }
Hanno Beckere1801392018-08-21 16:51:05 +01005559 else
5560 {
5561 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",
5562 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5563 (unsigned) hs->buffering.total_bytes_buffered ) );
5564 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005565
Hanno Beckera02b0b42018-08-21 17:20:27 +01005566 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005567 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005568 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",
5569 (unsigned) msg_len,
5570 (unsigned) reassembly_buf_sz,
5571 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005572 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005573 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5574 goto exit;
5575 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005576 }
5577
5578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5579 msg_len ) );
5580
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005581 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5582 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005583 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005584 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005585 goto exit;
5586 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005587 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005588
5589 /* Prepare final header: copy msg_type, length and message_seq,
5590 * then add standardised fragment_offset and fragment_length */
5591 memcpy( hs_buf->data, ssl->in_msg, 6 );
5592 memset( hs_buf->data + 6, 0, 3 );
5593 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5594
5595 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005596
5597 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005598 }
5599 else
5600 {
5601 /* Make sure msg_type and length are consistent */
5602 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5603 {
5604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5605 /* Ignore */
5606 goto exit;
5607 }
5608 }
5609
Hanno Becker4422bbb2018-08-20 09:40:19 +01005610 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005611 {
5612 size_t frag_len, frag_off;
5613 unsigned char * const msg = hs_buf->data + 12;
5614
5615 /*
5616 * Check and copy current fragment
5617 */
5618
5619 /* Validation of header fields already done in
5620 * mbedtls_ssl_prepare_handshake_record(). */
5621 frag_off = ssl_get_hs_frag_off( ssl );
5622 frag_len = ssl_get_hs_frag_len( ssl );
5623
5624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5625 frag_off, frag_len ) );
5626 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5627
5628 if( hs_buf->is_fragmented )
5629 {
5630 unsigned char * const bitmask = msg + msg_len;
5631 ssl_bitmask_set( bitmask, frag_off, frag_len );
5632 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5633 msg_len ) == 0 );
5634 }
5635 else
5636 {
5637 hs_buf->is_complete = 1;
5638 }
5639
5640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5641 hs_buf->is_complete ? "" : "not yet " ) );
5642 }
5643
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005644 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005645 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005646
5647 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005648 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005649 break;
5650 }
5651
5652exit:
5653
5654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5655 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005656}
5657#endif /* MBEDTLS_SSL_PROTO_DTLS */
5658
Hanno Becker1097b342018-08-15 14:09:41 +01005659static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005660{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005661 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005662 * Consume last content-layer message and potentially
5663 * update in_msglen which keeps track of the contents'
5664 * consumption state.
5665 *
5666 * (1) Handshake messages:
5667 * Remove last handshake message, move content
5668 * and adapt in_msglen.
5669 *
5670 * (2) Alert messages:
5671 * Consume whole record content, in_msglen = 0.
5672 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005673 * (3) Change cipher spec:
5674 * Consume whole record content, in_msglen = 0.
5675 *
5676 * (4) Application data:
5677 * Don't do anything - the record layer provides
5678 * the application data as a stream transport
5679 * and consumes through mbedtls_ssl_read only.
5680 *
5681 */
5682
5683 /* Case (1): Handshake messages */
5684 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005685 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005686 /* Hard assertion to be sure that no application data
5687 * is in flight, as corrupting ssl->in_msglen during
5688 * ssl->in_offt != NULL is fatal. */
5689 if( ssl->in_offt != NULL )
5690 {
5691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5693 }
5694
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005695 /*
5696 * Get next Handshake message in the current record
5697 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005698
Hanno Becker4a810fb2017-05-24 16:27:30 +01005699 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005700 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005701 * current handshake content: If DTLS handshake
5702 * fragmentation is used, that's the fragment
5703 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005704 * size here is faulty and should be changed at
5705 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005706 * (2) While it doesn't seem to cause problems, one
5707 * has to be very careful not to assume that in_hslen
5708 * is always <= in_msglen in a sensible communication.
5709 * Again, it's wrong for DTLS handshake fragmentation.
5710 * The following check is therefore mandatory, and
5711 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005712 * Additionally, ssl->in_hslen might be arbitrarily out of
5713 * bounds after handling a DTLS message with an unexpected
5714 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005715 */
5716 if( ssl->in_hslen < ssl->in_msglen )
5717 {
5718 ssl->in_msglen -= ssl->in_hslen;
5719 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5720 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721
Hanno Becker4a810fb2017-05-24 16:27:30 +01005722 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5723 ssl->in_msg, ssl->in_msglen );
5724 }
5725 else
5726 {
5727 ssl->in_msglen = 0;
5728 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005729
Hanno Becker4a810fb2017-05-24 16:27:30 +01005730 ssl->in_hslen = 0;
5731 }
5732 /* Case (4): Application data */
5733 else if( ssl->in_offt != NULL )
5734 {
5735 return( 0 );
5736 }
5737 /* Everything else (CCS & Alerts) */
5738 else
5739 {
5740 ssl->in_msglen = 0;
5741 }
5742
Hanno Becker1097b342018-08-15 14:09:41 +01005743 return( 0 );
5744}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005745
Hanno Beckere74d5562018-08-15 14:26:08 +01005746static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5747{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005748 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005749 return( 1 );
5750
5751 return( 0 );
5752}
5753
Hanno Becker5f066e72018-08-16 14:56:31 +01005754#if defined(MBEDTLS_SSL_PROTO_DTLS)
5755
5756static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5757{
5758 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5759 if( hs == NULL )
5760 return;
5761
Hanno Becker01315ea2018-08-21 17:22:17 +01005762 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005763 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005764 hs->buffering.total_bytes_buffered -=
5765 hs->buffering.future_record.len;
5766
5767 mbedtls_free( hs->buffering.future_record.data );
5768 hs->buffering.future_record.data = NULL;
5769 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005770}
5771
5772static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5773{
5774 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5775 unsigned char * rec;
5776 size_t rec_len;
5777 unsigned rec_epoch;
5778
5779 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5780 return( 0 );
5781
5782 if( hs == NULL )
5783 return( 0 );
5784
Hanno Becker5f066e72018-08-16 14:56:31 +01005785 rec = hs->buffering.future_record.data;
5786 rec_len = hs->buffering.future_record.len;
5787 rec_epoch = hs->buffering.future_record.epoch;
5788
5789 if( rec == NULL )
5790 return( 0 );
5791
Hanno Becker4cb782d2018-08-20 11:19:05 +01005792 /* Only consider loading future records if the
5793 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005794 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005795 return( 0 );
5796
Hanno Becker5f066e72018-08-16 14:56:31 +01005797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5798
5799 if( rec_epoch != ssl->in_epoch )
5800 {
5801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5802 goto exit;
5803 }
5804
5805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5806
5807 /* Double-check that the record is not too large */
5808 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5809 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5810 {
5811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5813 }
5814
5815 memcpy( ssl->in_hdr, rec, rec_len );
5816 ssl->in_left = rec_len;
5817 ssl->next_record_offset = 0;
5818
5819 ssl_free_buffered_record( ssl );
5820
5821exit:
5822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5823 return( 0 );
5824}
5825
5826static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5827{
5828 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5829 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005830 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005831
5832 /* Don't buffer future records outside handshakes. */
5833 if( hs == NULL )
5834 return( 0 );
5835
5836 /* Only buffer handshake records (we are only interested
5837 * in Finished messages). */
5838 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5839 return( 0 );
5840
5841 /* Don't buffer more than one future epoch record. */
5842 if( hs->buffering.future_record.data != NULL )
5843 return( 0 );
5844
Hanno Becker01315ea2018-08-21 17:22:17 +01005845 /* Don't buffer record if there's not enough buffering space remaining. */
5846 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5847 hs->buffering.total_bytes_buffered ) )
5848 {
5849 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",
5850 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5851 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005852 return( 0 );
5853 }
5854
Hanno Becker5f066e72018-08-16 14:56:31 +01005855 /* Buffer record */
5856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5857 ssl->in_epoch + 1 ) );
5858 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5859 rec_hdr_len + ssl->in_msglen );
5860
5861 /* ssl_parse_record_header() only considers records
5862 * of the next epoch as candidates for buffering. */
5863 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005864 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005865
5866 hs->buffering.future_record.data =
5867 mbedtls_calloc( 1, hs->buffering.future_record.len );
5868 if( hs->buffering.future_record.data == NULL )
5869 {
5870 /* If we run out of RAM trying to buffer a
5871 * record from the next epoch, just ignore. */
5872 return( 0 );
5873 }
5874
Hanno Becker01315ea2018-08-21 17:22:17 +01005875 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005876
Hanno Becker01315ea2018-08-21 17:22:17 +01005877 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005878 return( 0 );
5879}
5880
5881#endif /* MBEDTLS_SSL_PROTO_DTLS */
5882
Hanno Beckere74d5562018-08-15 14:26:08 +01005883static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005884{
5885 int ret;
5886
Hanno Becker5f066e72018-08-16 14:56:31 +01005887#if defined(MBEDTLS_SSL_PROTO_DTLS)
5888 /* We might have buffered a future record; if so,
5889 * and if the epoch matches now, load it.
5890 * On success, this call will set ssl->in_left to
5891 * the length of the buffered record, so that
5892 * the calls to ssl_fetch_input() below will
5893 * essentially be no-ops. */
5894 ret = ssl_load_buffered_record( ssl );
5895 if( ret != 0 )
5896 return( ret );
5897#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005898
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005899 /* Reset in pointers to default state for TLS/DTLS records,
5900 * assuming no CID and no offset between record content and
5901 * record plaintext. */
5902 ssl_update_in_pointers( ssl );
5903
5904 /* Ensure that we have enough space available for the default form
5905 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5906 * with no space for CIDs counted in). */
5907 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5908 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005911 return( ret );
5912 }
5913
5914 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005917 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5918 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005919 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005920 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5921 {
5922 ret = ssl_buffer_future_record( ssl );
5923 if( ret != 0 )
5924 return( ret );
5925
5926 /* Fall through to handling of unexpected records */
5927 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5928 }
5929
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005930 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5931 {
5932 /* Skip unexpected record (but not whole datagram) */
5933 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005934 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005935
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5937 "(header)" ) );
5938 }
5939 else
5940 {
5941 /* Skip invalid record and the rest of the datagram */
5942 ssl->next_record_offset = 0;
5943 ssl->in_left = 0;
5944
5945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5946 "(header)" ) );
5947 }
5948
5949 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005950 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005951 }
5952#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005953 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005954 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005955
5956 /*
5957 * Read and optionally decrypt the message contents
5958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005960 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005963 return( ret );
5964 }
5965
5966 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005968 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005969 {
Hanno Becker5903de42019-05-03 14:46:38 +01005970 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005971 if( ssl->next_record_offset < ssl->in_left )
5972 {
5973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5974 }
5975 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005976 else
5977#endif
5978 ssl->in_left = 0;
5979
5980 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005984 {
5985 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005986 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005987 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005988 /* Except when waiting for Finished as a bad mac here
5989 * probably means something went wrong in the handshake
5990 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5991 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5992 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5993 {
5994#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5995 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5996 {
5997 mbedtls_ssl_send_alert_message( ssl,
5998 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5999 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6000 }
6001#endif
6002 return( ret );
6003 }
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006006 if( ssl->conf->badmac_limit != 0 &&
6007 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6010 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006011 }
6012#endif
6013
Hanno Becker4a810fb2017-05-24 16:27:30 +01006014 /* As above, invalid records cause
6015 * dismissal of the whole datagram. */
6016
6017 ssl->next_record_offset = 0;
6018 ssl->in_left = 0;
6019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006021 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006022 }
6023
6024 return( ret );
6025 }
6026 else
6027#endif
6028 {
6029 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6031 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 mbedtls_ssl_send_alert_message( ssl,
6034 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6035 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006036 }
6037#endif
6038 return( ret );
6039 }
6040 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006041
Simon Butcher99000142016-10-13 17:21:01 +01006042 return( 0 );
6043}
6044
6045int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6046{
6047 int ret;
6048
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006049 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006050 * Handle particular types of records
6051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 {
Simon Butcher99000142016-10-13 17:21:01 +01006054 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6055 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006056 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 }
6059
Hanno Beckere678eaa2018-08-21 14:57:46 +01006060 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006061 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006062 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006063 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6065 ssl->in_msglen ) );
6066 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006067 }
6068
Hanno Beckere678eaa2018-08-21 14:57:46 +01006069 if( ssl->in_msg[0] != 1 )
6070 {
6071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6072 ssl->in_msg[0] ) );
6073 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6074 }
6075
6076#if defined(MBEDTLS_SSL_PROTO_DTLS)
6077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6078 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6079 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6080 {
6081 if( ssl->handshake == NULL )
6082 {
6083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6084 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6085 }
6086
6087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6088 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6089 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006090#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006091 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006094 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006095 if( ssl->in_msglen != 2 )
6096 {
6097 /* Note: Standard allows for more than one 2 byte alert
6098 to be packed in a single message, but Mbed TLS doesn't
6099 currently support this. */
6100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6101 ssl->in_msglen ) );
6102 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6103 }
6104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 ssl->in_msg[0], ssl->in_msg[1] ) );
6107
6108 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006109 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006114 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 }
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6119 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6122 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006124
6125#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6126 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6127 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6128 {
Hanno Becker90333da2017-10-10 11:27:13 +01006129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006130 /* Will be handled when trying to parse ServerHello */
6131 return( 0 );
6132 }
6133#endif
6134
6135#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6136 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6137 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6138 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6139 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6140 {
6141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6142 /* Will be handled in mbedtls_ssl_parse_certificate() */
6143 return( 0 );
6144 }
6145#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6146
6147 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006148 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006149 }
6150
Hanno Beckerc76c6192017-06-06 10:03:17 +01006151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006153 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006154 /* Drop unexpected ApplicationData records,
6155 * except at the beginning of renegotiations */
6156 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6157 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6158#if defined(MBEDTLS_SSL_RENEGOTIATION)
6159 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6160 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006161#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006162 )
6163 {
6164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6165 return( MBEDTLS_ERR_SSL_NON_FATAL );
6166 }
6167
6168 if( ssl->handshake != NULL &&
6169 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6170 {
6171 ssl_handshake_wrapup_free_hs_transform( ssl );
6172 }
6173 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006174#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006175
Paul Bakker5121ce52009-01-03 21:22:43 +00006176 return( 0 );
6177}
6178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006180{
6181 int ret;
6182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6184 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6185 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006186 {
6187 return( ret );
6188 }
6189
6190 return( 0 );
6191}
6192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006194 unsigned char level,
6195 unsigned char message )
6196{
6197 int ret;
6198
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006199 if( ssl == NULL || ssl->conf == NULL )
6200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006206 ssl->out_msglen = 2;
6207 ssl->out_msg[0] = level;
6208 ssl->out_msg[1] = message;
6209
Hanno Becker67bc7c32018-08-06 11:33:50 +01006210 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006213 return( ret );
6214 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006216
6217 return( 0 );
6218}
6219
Hanno Beckerb9d44792019-02-08 07:19:04 +00006220#if defined(MBEDTLS_X509_CRT_PARSE_C)
6221static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6222{
6223#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6224 if( session->peer_cert != NULL )
6225 {
6226 mbedtls_x509_crt_free( session->peer_cert );
6227 mbedtls_free( session->peer_cert );
6228 session->peer_cert = NULL;
6229 }
6230#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6231 if( session->peer_cert_digest != NULL )
6232 {
6233 /* Zeroization is not necessary. */
6234 mbedtls_free( session->peer_cert_digest );
6235 session->peer_cert_digest = NULL;
6236 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6237 session->peer_cert_digest_len = 0;
6238 }
6239#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6240}
6241#endif /* MBEDTLS_X509_CRT_PARSE_C */
6242
Paul Bakker5121ce52009-01-03 21:22:43 +00006243/*
6244 * Handshake functions
6245 */
Hanno Becker21489932019-02-05 13:20:55 +00006246#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006247/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006249{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006250 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6251 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006254
Hanno Becker7177a882019-02-05 13:36:46 +00006255 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006258 ssl->state++;
6259 return( 0 );
6260 }
6261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006264}
6265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006267{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006268 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6269 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006272
Hanno Becker7177a882019-02-05 13:36:46 +00006273 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006276 ssl->state++;
6277 return( 0 );
6278 }
6279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006282}
Gilles Peskinef9828522017-05-03 12:28:43 +02006283
Hanno Becker21489932019-02-05 13:20:55 +00006284#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006285/* Some certificate support -> implement write and parse */
6286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006290 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006292 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6293 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006296
Hanno Becker7177a882019-02-05 13:36:46 +00006297 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006300 ssl->state++;
6301 return( 0 );
6302 }
6303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006306 {
6307 if( ssl->client_auth == 0 )
6308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006310 ssl->state++;
6311 return( 0 );
6312 }
6313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006315 /*
6316 * If using SSLv3 and got no cert, send an Alert message
6317 * (otherwise an empty Certificate message will be sent).
6318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6320 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006321 {
6322 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6324 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6325 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 goto write_msg;
6329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332#endif /* MBEDTLS_SSL_CLI_C */
6333#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006334 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6339 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006340 }
6341 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006342#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006345
6346 /*
6347 * 0 . 0 handshake type
6348 * 1 . 3 handshake length
6349 * 4 . 6 length of all certs
6350 * 7 . 9 length of cert. 1
6351 * 10 . n-1 peer certificate
6352 * n . n+2 length of cert. 2
6353 * n+3 . ... upper level cert, etc.
6354 */
6355 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006357
Paul Bakker29087132010-03-21 21:03:34 +00006358 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006359 {
6360 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006361 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006364 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006366 }
6367
6368 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6369 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6370 ssl->out_msg[i + 2] = (unsigned char)( n );
6371
6372 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6373 i += n; crt = crt->next;
6374 }
6375
6376 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6377 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6378 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6379
6380 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6382 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006383
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006384#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006385write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006386#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006387
6388 ssl->state++;
6389
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006390 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 return( ret );
6394 }
6395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006397
Paul Bakkered27a042013-04-18 22:46:23 +02006398 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006399}
6400
Hanno Becker84879e32019-01-31 07:44:03 +00006401#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006402
6403#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006404static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6405 unsigned char *crt_buf,
6406 size_t crt_buf_len )
6407{
6408 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6409
6410 if( peer_crt == NULL )
6411 return( -1 );
6412
6413 if( peer_crt->raw.len != crt_buf_len )
6414 return( -1 );
6415
Hanno Becker46f34d02019-02-08 14:00:04 +00006416 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006417}
Hanno Becker177475a2019-02-05 17:02:46 +00006418#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6419static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6420 unsigned char *crt_buf,
6421 size_t crt_buf_len )
6422{
6423 int ret;
6424 unsigned char const * const peer_cert_digest =
6425 ssl->session->peer_cert_digest;
6426 mbedtls_md_type_t const peer_cert_digest_type =
6427 ssl->session->peer_cert_digest_type;
6428 mbedtls_md_info_t const * const digest_info =
6429 mbedtls_md_info_from_type( peer_cert_digest_type );
6430 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6431 size_t digest_len;
6432
6433 if( peer_cert_digest == NULL || digest_info == NULL )
6434 return( -1 );
6435
6436 digest_len = mbedtls_md_get_size( digest_info );
6437 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6438 return( -1 );
6439
6440 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6441 if( ret != 0 )
6442 return( -1 );
6443
6444 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6445}
6446#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006447#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006448
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006449/*
6450 * Once the certificate message is read, parse it into a cert chain and
6451 * perform basic checks, but leave actual verification to the caller
6452 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006453static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6454 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006455{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006456 int ret;
6457#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6458 int crt_cnt=0;
6459#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006460 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006461 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006466 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6467 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006469 }
6470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6472 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006475 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6476 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006478 }
6479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006481
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006484 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006485 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006486
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006487 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006491 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6492 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 }
6495
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006496 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6497 i += 3;
6498
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006499 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 while( i < ssl->in_hslen )
6501 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006502 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006503 if ( i + 3 > ssl->in_hslen ) {
6504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006505 mbedtls_ssl_send_alert_message( ssl,
6506 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6507 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006508 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6509 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006510 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6511 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 if( ssl->in_msg[i] != 0 )
6513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006515 mbedtls_ssl_send_alert_message( ssl,
6516 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6517 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006519 }
6520
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006521 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006522 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6523 | (unsigned int) ssl->in_msg[i + 2];
6524 i += 3;
6525
6526 if( n < 128 || i + n > ssl->in_hslen )
6527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006529 mbedtls_ssl_send_alert_message( ssl,
6530 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6531 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006533 }
6534
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006535 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006536#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6537 if( crt_cnt++ == 0 &&
6538 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6539 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006540 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006541 /* During client-side renegotiation, check that the server's
6542 * end-CRTs hasn't changed compared to the initial handshake,
6543 * mitigating the triple handshake attack. On success, reuse
6544 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6546 if( ssl_check_peer_crt_unchanged( ssl,
6547 &ssl->in_msg[i],
6548 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006549 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6551 mbedtls_ssl_send_alert_message( ssl,
6552 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6553 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6554 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006555 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006556
6557 /* Now we can safely free the original chain. */
6558 ssl_clear_peer_cert( ssl->session );
6559 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006560#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6561
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006562 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006563#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006564 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006565#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006566 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006567 * it in-place from the input buffer instead of making a copy. */
6568 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6569#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006570 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006572 case 0: /*ok*/
6573 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6574 /* Ignore certificate with an unknown algorithm: maybe a
6575 prior certificate was already trusted. */
6576 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006577
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006578 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6579 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6580 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006581
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006582 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6583 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6584 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006585
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006586 default:
6587 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6588 crt_parse_der_failed:
6589 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6590 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6591 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006592 }
6593
6594 i += n;
6595 }
6596
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006597 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006598 return( 0 );
6599}
6600
Hanno Becker4a55f632019-02-05 12:49:06 +00006601#if defined(MBEDTLS_SSL_SRV_C)
6602static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6603{
6604 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6605 return( -1 );
6606
6607#if defined(MBEDTLS_SSL_PROTO_SSL3)
6608 /*
6609 * Check if the client sent an empty certificate
6610 */
6611 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6612 {
6613 if( ssl->in_msglen == 2 &&
6614 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6615 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6616 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6617 {
6618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6619 return( 0 );
6620 }
6621
6622 return( -1 );
6623 }
6624#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6625
6626#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6627 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6628 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6629 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6630 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6631 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6632 {
6633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6634 return( 0 );
6635 }
6636
6637 return( -1 );
6638#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6639 MBEDTLS_SSL_PROTO_TLS1_2 */
6640}
6641#endif /* MBEDTLS_SSL_SRV_C */
6642
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006643/* Check if a certificate message is expected.
6644 * Return either
6645 * - SSL_CERTIFICATE_EXPECTED, or
6646 * - SSL_CERTIFICATE_SKIP
6647 * indicating whether a Certificate message is expected or not.
6648 */
6649#define SSL_CERTIFICATE_EXPECTED 0
6650#define SSL_CERTIFICATE_SKIP 1
6651static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6652 int authmode )
6653{
6654 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006655 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006656
6657 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6658 return( SSL_CERTIFICATE_SKIP );
6659
6660#if defined(MBEDTLS_SSL_SRV_C)
6661 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6662 {
6663 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6664 return( SSL_CERTIFICATE_SKIP );
6665
6666 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6667 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006668 ssl->session_negotiate->verify_result =
6669 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6670 return( SSL_CERTIFICATE_SKIP );
6671 }
6672 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006673#else
6674 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006675#endif /* MBEDTLS_SSL_SRV_C */
6676
6677 return( SSL_CERTIFICATE_EXPECTED );
6678}
6679
Hanno Becker68636192019-02-05 14:36:34 +00006680static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6681 int authmode,
6682 mbedtls_x509_crt *chain,
6683 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006684{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006685 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006686 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006687 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006688 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006689
Hanno Becker8927c832019-04-03 12:52:50 +01006690 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6691 void *p_vrfy;
6692
Hanno Becker68636192019-02-05 14:36:34 +00006693 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6694 return( 0 );
6695
Hanno Becker8927c832019-04-03 12:52:50 +01006696 if( ssl->f_vrfy != NULL )
6697 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006699 f_vrfy = ssl->f_vrfy;
6700 p_vrfy = ssl->p_vrfy;
6701 }
6702 else
6703 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006705 f_vrfy = ssl->conf->f_vrfy;
6706 p_vrfy = ssl->conf->p_vrfy;
6707 }
6708
Hanno Becker68636192019-02-05 14:36:34 +00006709 /*
6710 * Main check: verify certificate
6711 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006712#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6713 if( ssl->conf->f_ca_cb != NULL )
6714 {
6715 ((void) rs_ctx);
6716 have_ca_chain = 1;
6717
6718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006719 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006720 chain,
6721 ssl->conf->f_ca_cb,
6722 ssl->conf->p_ca_cb,
6723 ssl->conf->cert_profile,
6724 ssl->hostname,
6725 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006726 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006727 }
6728 else
6729#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6730 {
6731 mbedtls_x509_crt *ca_chain;
6732 mbedtls_x509_crl *ca_crl;
6733
6734#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6735 if( ssl->handshake->sni_ca_chain != NULL )
6736 {
6737 ca_chain = ssl->handshake->sni_ca_chain;
6738 ca_crl = ssl->handshake->sni_ca_crl;
6739 }
6740 else
6741#endif
6742 {
6743 ca_chain = ssl->conf->ca_chain;
6744 ca_crl = ssl->conf->ca_crl;
6745 }
6746
6747 if( ca_chain != NULL )
6748 have_ca_chain = 1;
6749
6750 ret = mbedtls_x509_crt_verify_restartable(
6751 chain,
6752 ca_chain, ca_crl,
6753 ssl->conf->cert_profile,
6754 ssl->hostname,
6755 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006756 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006757 }
Hanno Becker68636192019-02-05 14:36:34 +00006758
6759 if( ret != 0 )
6760 {
6761 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6762 }
6763
6764#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6765 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6766 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6767#endif
6768
6769 /*
6770 * Secondary checks: always done, but change 'ret' only if it was 0
6771 */
6772
6773#if defined(MBEDTLS_ECP_C)
6774 {
6775 const mbedtls_pk_context *pk = &chain->pk;
6776
6777 /* If certificate uses an EC key, make sure the curve is OK */
6778 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6779 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6780 {
6781 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6782
6783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6784 if( ret == 0 )
6785 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6786 }
6787 }
6788#endif /* MBEDTLS_ECP_C */
6789
6790 if( mbedtls_ssl_check_cert_usage( chain,
6791 ciphersuite_info,
6792 ! ssl->conf->endpoint,
6793 &ssl->session_negotiate->verify_result ) != 0 )
6794 {
6795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6796 if( ret == 0 )
6797 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6798 }
6799
6800 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6801 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6802 * with details encoded in the verification flags. All other kinds
6803 * of error codes, including those from the user provided f_vrfy
6804 * functions, are treated as fatal and lead to a failure of
6805 * ssl_parse_certificate even if verification was optional. */
6806 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6807 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6808 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6809 {
6810 ret = 0;
6811 }
6812
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006813 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006814 {
6815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6816 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6817 }
6818
6819 if( ret != 0 )
6820 {
6821 uint8_t alert;
6822
6823 /* The certificate may have been rejected for several reasons.
6824 Pick one and send the corresponding alert. Which alert to send
6825 may be a subject of debate in some cases. */
6826 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6827 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6828 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6829 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6830 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6831 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6832 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6833 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6834 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6835 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6836 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6837 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6838 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6839 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6840 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6841 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6842 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6843 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6844 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6845 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6846 else
6847 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6849 alert );
6850 }
6851
6852#if defined(MBEDTLS_DEBUG_C)
6853 if( ssl->session_negotiate->verify_result != 0 )
6854 {
6855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6856 ssl->session_negotiate->verify_result ) );
6857 }
6858 else
6859 {
6860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6861 }
6862#endif /* MBEDTLS_DEBUG_C */
6863
6864 return( ret );
6865}
6866
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006867#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6868static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6869 unsigned char *start, size_t len )
6870{
6871 int ret;
6872 /* Remember digest of the peer's end-CRT. */
6873 ssl->session_negotiate->peer_cert_digest =
6874 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6875 if( ssl->session_negotiate->peer_cert_digest == NULL )
6876 {
6877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6878 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6879 mbedtls_ssl_send_alert_message( ssl,
6880 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6881 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6882
6883 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6884 }
6885
6886 ret = mbedtls_md( mbedtls_md_info_from_type(
6887 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6888 start, len,
6889 ssl->session_negotiate->peer_cert_digest );
6890
6891 ssl->session_negotiate->peer_cert_digest_type =
6892 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6893 ssl->session_negotiate->peer_cert_digest_len =
6894 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6895
6896 return( ret );
6897}
6898
6899static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6900 unsigned char *start, size_t len )
6901{
6902 unsigned char *end = start + len;
6903 int ret;
6904
6905 /* Make a copy of the peer's raw public key. */
6906 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6907 ret = mbedtls_pk_parse_subpubkey( &start, end,
6908 &ssl->handshake->peer_pubkey );
6909 if( ret != 0 )
6910 {
6911 /* We should have parsed the public key before. */
6912 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6913 }
6914
6915 return( 0 );
6916}
6917#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6918
Hanno Becker68636192019-02-05 14:36:34 +00006919int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6920{
6921 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006922 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006923#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6924 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6925 ? ssl->handshake->sni_authmode
6926 : ssl->conf->authmode;
6927#else
6928 const int authmode = ssl->conf->authmode;
6929#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006930 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006931 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006932
6933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6934
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006935 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6936 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006937 {
6938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006939 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006940 }
6941
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006942#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6943 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006944 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006945 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006946 chain = ssl->handshake->ecrs_peer_cert;
6947 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006948 goto crt_verify;
6949 }
6950#endif
6951
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006952 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006953 {
6954 /* mbedtls_ssl_read_record may have sent an alert already. We
6955 let it decide whether to alert. */
6956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006957 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006958 }
6959
Hanno Becker4a55f632019-02-05 12:49:06 +00006960#if defined(MBEDTLS_SSL_SRV_C)
6961 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6962 {
6963 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006964
6965 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006966 ret = 0;
6967 else
6968 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006969
Hanno Becker6bdfab22019-02-05 13:11:17 +00006970 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006971 }
6972#endif /* MBEDTLS_SSL_SRV_C */
6973
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006974 /* Clear existing peer CRT structure in case we tried to
6975 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006976 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006977
6978 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6979 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006980 {
6981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6982 sizeof( mbedtls_x509_crt ) ) );
6983 mbedtls_ssl_send_alert_message( ssl,
6984 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6985 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006986
Hanno Becker3dad3112019-02-05 17:19:52 +00006987 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6988 goto exit;
6989 }
6990 mbedtls_x509_crt_init( chain );
6991
6992 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006993 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006994 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006995
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006996#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6997 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006998 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006999
7000crt_verify:
7001 if( ssl->handshake->ecrs_enabled)
7002 rs_ctx = &ssl->handshake->ecrs_ctx;
7003#endif
7004
Hanno Becker68636192019-02-05 14:36:34 +00007005 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007006 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007007 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007008 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007010#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007011 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007012 unsigned char *crt_start, *pk_start;
7013 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007014
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007015 /* We parse the CRT chain without copying, so
7016 * these pointers point into the input buffer,
7017 * and are hence still valid after freeing the
7018 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007019
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007020 crt_start = chain->raw.p;
7021 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007022
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007023 pk_start = chain->pk_raw.p;
7024 pk_len = chain->pk_raw.len;
7025
7026 /* Free the CRT structures before computing
7027 * digest and copying the peer's public key. */
7028 mbedtls_x509_crt_free( chain );
7029 mbedtls_free( chain );
7030 chain = NULL;
7031
7032 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007033 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007034 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007035
7036 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7037 if( ret != 0 )
7038 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007039 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007040#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7041 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007042 ssl->session_negotiate->peer_cert = chain;
7043 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007044#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007047
Hanno Becker6bdfab22019-02-05 13:11:17 +00007048exit:
7049
Hanno Becker3dad3112019-02-05 17:19:52 +00007050 if( ret == 0 )
7051 ssl->state++;
7052
7053#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7054 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7055 {
7056 ssl->handshake->ecrs_peer_cert = chain;
7057 chain = NULL;
7058 }
7059#endif
7060
7061 if( chain != NULL )
7062 {
7063 mbedtls_x509_crt_free( chain );
7064 mbedtls_free( chain );
7065 }
7066
Paul Bakker5121ce52009-01-03 21:22:43 +00007067 return( ret );
7068}
Hanno Becker21489932019-02-05 13:20:55 +00007069#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007072{
7073 int ret;
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007078 ssl->out_msglen = 1;
7079 ssl->out_msg[0] = 1;
7080
Paul Bakker5121ce52009-01-03 21:22:43 +00007081 ssl->state++;
7082
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007083 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007084 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007086 return( ret );
7087 }
7088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007090
7091 return( 0 );
7092}
7093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007095{
7096 int ret;
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007099
Hanno Becker327c93b2018-08-15 13:56:18 +01007100 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 return( ret );
7104 }
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007109 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7110 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007112 }
7113
Hanno Beckere678eaa2018-08-21 14:57:46 +01007114 /* CCS records are only accepted if they have length 1 and content '1',
7115 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007116
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007117 /*
7118 * Switch to our negotiated transform and session parameters for inbound
7119 * data.
7120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007122 ssl->transform_in = ssl->transform_negotiate;
7123 ssl->session_in = ssl->session_negotiate;
7124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007126 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007129 ssl_dtls_replay_reset( ssl );
7130#endif
7131
7132 /* Increment epoch */
7133 if( ++ssl->in_epoch == 0 )
7134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007136 /* This is highly unlikely to happen for legitimate reasons, so
7137 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007139 }
7140 }
7141 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007143 memset( ssl->in_ctr, 0, 8 );
7144
Hanno Becker79594fd2019-05-08 09:38:41 +01007145 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7148 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007153 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7154 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007156 }
7157 }
7158#endif
7159
Paul Bakker5121ce52009-01-03 21:22:43 +00007160 ssl->state++;
7161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007163
7164 return( 0 );
7165}
7166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7168 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007169{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007170 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7173 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7174 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007175 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007176 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7179#if defined(MBEDTLS_SHA512_C)
7180 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007181 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7182 else
7183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SHA256_C)
7185 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007186 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007187 else
7188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007192 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007193 }
Paul Bakker380da532012-04-18 16:10:25 +00007194}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7199 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007200 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7201 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7204#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007206 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007207 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7208#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007209 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007210#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007214 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007215 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007216#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007217 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007218#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007221}
7222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007224 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7227 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007228 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7229 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7232#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007233#if defined(MBEDTLS_USE_PSA_CRYPTO)
7234 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7235#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007236 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007237#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007240#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007241 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007242#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007243 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007244#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007247}
7248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7250 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7251static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007252 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007253{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007254 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7255 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007257#endif
Paul Bakker380da532012-04-18 16:10:25 +00007258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7260#if defined(MBEDTLS_SHA256_C)
7261static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007262 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007263{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007264#if defined(MBEDTLS_USE_PSA_CRYPTO)
7265 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7266#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007267 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007268#endif
Paul Bakker380da532012-04-18 16:10:25 +00007269}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007270#endif
Paul Bakker380da532012-04-18 16:10:25 +00007271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#if defined(MBEDTLS_SHA512_C)
7273static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007274 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007275{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007276#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007277 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007278#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007279 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007280#endif
Paul Bakker380da532012-04-18 16:10:25 +00007281}
Paul Bakker769075d2012-11-24 11:26:46 +01007282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007286static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007288{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007289 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007290 mbedtls_md5_context md5;
7291 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007292
Paul Bakker5121ce52009-01-03 21:22:43 +00007293 unsigned char padbuf[48];
7294 unsigned char md5sum[16];
7295 unsigned char sha1sum[20];
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007298 if( !session )
7299 session = ssl->session;
7300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007302
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007303 mbedtls_md5_init( &md5 );
7304 mbedtls_sha1_init( &sha1 );
7305
7306 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7307 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007308
7309 /*
7310 * SSLv3:
7311 * hash =
7312 * MD5( master + pad2 +
7313 * MD5( handshake + sender + master + pad1 ) )
7314 * + SHA1( master + pad2 +
7315 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007316 */
7317
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 Bakker5121ce52009-01-03 21:22:43 +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 Bakker5121ce52009-01-03 21:22:43 +00007327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007329 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007330
Paul Bakker1ef83d62012-04-11 12:09:53 +00007331 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007332
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007333 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7334 mbedtls_md5_update_ret( &md5, session->master, 48 );
7335 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7336 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007337
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007338 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7339 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7340 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7341 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007342
Paul Bakker1ef83d62012-04-11 12:09:53 +00007343 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007344
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007345 mbedtls_md5_starts_ret( &md5 );
7346 mbedtls_md5_update_ret( &md5, session->master, 48 );
7347 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7348 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7349 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007351 mbedtls_sha1_starts_ret( &sha1 );
7352 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7353 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7354 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7355 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007359 mbedtls_md5_free( &md5 );
7360 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007362 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7363 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7364 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007371static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007373{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007374 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007375 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007376 mbedtls_md5_context md5;
7377 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007381 if( !session )
7382 session = ssl->session;
7383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007385
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007386 mbedtls_md5_init( &md5 );
7387 mbedtls_sha1_init( &sha1 );
7388
7389 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7390 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007391
Paul Bakker1ef83d62012-04-11 12:09:53 +00007392 /*
7393 * TLSv1:
7394 * hash = PRF( master, finished_label,
7395 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7396 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007399 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7400 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007401#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007404 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7405 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007406#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007409 ? "client finished"
7410 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007411
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007412 mbedtls_md5_finish_ret( &md5, padbuf );
7413 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
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, 36, 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
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007420 mbedtls_md5_free( &md5 );
7421 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007422
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007423 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7430#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007431static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007433{
7434 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007435 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007436 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007437#if defined(MBEDTLS_USE_PSA_CRYPTO)
7438 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007439 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007440 psa_status_t status;
7441#else
7442 mbedtls_sha256_context sha256;
7443#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007446 if( !session )
7447 session = ssl->session;
7448
Andrzej Kurekeb342242019-01-29 09:14:33 -05007449 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7450 ? "client finished"
7451 : "server finished";
7452
7453#if defined(MBEDTLS_USE_PSA_CRYPTO)
7454 sha256_psa = psa_hash_operation_init();
7455
7456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7457
7458 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7459 if( status != PSA_SUCCESS )
7460 {
7461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7462 return;
7463 }
7464
7465 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7466 if( status != PSA_SUCCESS )
7467 {
7468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7469 return;
7470 }
7471 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7472#else
7473
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007474 mbedtls_sha256_init( &sha256 );
7475
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007477
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007478 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007479
7480 /*
7481 * TLSv1.2:
7482 * hash = PRF( master, finished_label,
7483 * Hash( handshake ) )[0.11]
7484 */
7485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486#if !defined(MBEDTLS_SHA256_ALT)
7487 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007488 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007489#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007490
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007491 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007492 mbedtls_sha256_free( &sha256 );
7493#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007494
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007495 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007496 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007500 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007507static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007509{
7510 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007511 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007512 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007513#if defined(MBEDTLS_USE_PSA_CRYPTO)
7514 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007515 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007516 psa_status_t status;
7517#else
7518 mbedtls_sha512_context sha512;
7519#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007522 if( !session )
7523 session = ssl->session;
7524
Andrzej Kurekeb342242019-01-29 09:14:33 -05007525 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7526 ? "client finished"
7527 : "server finished";
7528
7529#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007530 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007531
7532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7533
Andrzej Kurek972fba52019-01-30 03:29:12 -05007534 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007535 if( status != PSA_SUCCESS )
7536 {
7537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7538 return;
7539 }
7540
Andrzej Kurek972fba52019-01-30 03:29:12 -05007541 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007542 if( status != PSA_SUCCESS )
7543 {
7544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7545 return;
7546 }
7547 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7548#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007549 mbedtls_sha512_init( &sha512 );
7550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007552
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007553 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007554
7555 /*
7556 * TLSv1.2:
7557 * hash = PRF( master, finished_label,
7558 * Hash( handshake ) )[0.11]
7559 */
7560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007562 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7563 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007564#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007565
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007566 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007567 mbedtls_sha512_free( &sha512 );
7568#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007569
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007570 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007571 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007574
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007575 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#endif /* MBEDTLS_SHA512_C */
7580#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007583{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007585
7586 /*
7587 * Free our handshake params
7588 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007589 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007591 ssl->handshake = NULL;
7592
7593 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007594 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007595 */
7596 if( ssl->transform )
7597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 mbedtls_ssl_transform_free( ssl->transform );
7599 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007600 }
7601 ssl->transform = ssl->transform_negotiate;
7602 ssl->transform_negotiate = NULL;
7603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007605}
7606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007608{
7609 int resume = ssl->handshake->resume;
7610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613#if defined(MBEDTLS_SSL_RENEGOTIATION)
7614 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007617 ssl->renego_records_seen = 0;
7618 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007619#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007620
7621 /*
7622 * Free the previous session and switch in the current one
7623 */
Paul Bakker0a597072012-09-25 21:55:46 +00007624 if( ssl->session )
7625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007627 /* RFC 7366 3.1: keep the EtM state */
7628 ssl->session_negotiate->encrypt_then_mac =
7629 ssl->session->encrypt_then_mac;
7630#endif
7631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 mbedtls_ssl_session_free( ssl->session );
7633 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007634 }
7635 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007636 ssl->session_negotiate = NULL;
7637
Paul Bakker0a597072012-09-25 21:55:46 +00007638 /*
7639 * Add cache entry
7640 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007641 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007642 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007643 resume == 0 )
7644 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007645 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007647 }
Paul Bakker0a597072012-09-25 21:55:46 +00007648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007651 ssl->handshake->flight != NULL )
7652 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007653 /* Cancel handshake timer */
7654 ssl_set_timer( ssl, 0 );
7655
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007656 /* Keep last flight around in case we need to resend it:
7657 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007659 }
7660 else
7661#endif
7662 ssl_handshake_wrapup_free_hs_transform( ssl );
7663
Paul Bakker48916f92012-09-16 19:57:18 +00007664 ssl->state++;
7665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007667}
7668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007670{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007671 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007674
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007675 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007676
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007677 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007679 /*
7680 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7681 * may define some other value. Currently (early 2016), no defined
7682 * ciphersuite does this (and this is unlikely to change as activity has
7683 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007688 ssl->verify_data_len = hash_len;
7689 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007690#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007691
Paul Bakker5121ce52009-01-03 21:22:43 +00007692 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7694 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007695
7696 /*
7697 * In case of session resuming, invert the client and server
7698 * ChangeCipherSpec messages order.
7699 */
Paul Bakker0a597072012-09-25 21:55:46 +00007700 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007703 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007709#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007710 }
7711 else
7712 ssl->state++;
7713
Paul Bakker48916f92012-09-16 19:57:18 +00007714 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007715 * Switch to our negotiated transform and session parameters for outbound
7716 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007722 {
7723 unsigned char i;
7724
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007725 /* Remember current epoch settings for resending */
7726 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007727 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007728
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007729 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007730 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007731
7732 /* Increment epoch */
7733 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007734 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007735 break;
7736
7737 /* The loop goes to its end iff the counter is wrapping */
7738 if( i == 0 )
7739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7741 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007742 }
7743 }
7744 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007746 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007747
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007748 ssl->transform_out = ssl->transform_negotiate;
7749 ssl->session_out = ssl->session_negotiate;
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7752 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7757 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007758 }
7759 }
7760#endif
7761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007765#endif
7766
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007767 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007768 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007770 return( ret );
7771 }
7772
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007773#if defined(MBEDTLS_SSL_PROTO_DTLS)
7774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7775 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7776 {
7777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7778 return( ret );
7779 }
7780#endif
7781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007783
7784 return( 0 );
7785}
7786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007788#define SSL_MAX_HASH_LEN 36
7789#else
7790#define SSL_MAX_HASH_LEN 12
7791#endif
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007794{
Paul Bakker23986e52011-04-24 08:57:21 +00007795 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007796 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007797 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007800
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007801 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007802
Hanno Becker327c93b2018-08-15 13:56:18 +01007803 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007806 return( ret );
7807 }
7808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007812 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7813 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007815 }
7816
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007817 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818#if defined(MBEDTLS_SSL_PROTO_SSL3)
7819 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007820 hash_len = 36;
7821 else
7822#endif
7823 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7826 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007829 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7830 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007832 }
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007835 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7839 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007841 }
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007844 ssl->verify_data_len = hash_len;
7845 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007846#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007847
Paul Bakker0a597072012-09-25 21:55:46 +00007848 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007851 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007855 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007857#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007858 }
7859 else
7860 ssl->state++;
7861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007865#endif
7866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007868
7869 return( 0 );
7870}
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007873{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7877 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7878 mbedtls_md5_init( &handshake->fin_md5 );
7879 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007880 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7881 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7884#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007885#if defined(MBEDTLS_USE_PSA_CRYPTO)
7886 handshake->fin_sha256_psa = psa_hash_operation_init();
7887 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7888#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007890 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007891#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007892#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007894#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007895 handshake->fin_sha384_psa = psa_hash_operation_init();
7896 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007897#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007899 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007900#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007903
7904 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007905
7906#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7907 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7908 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7909#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_DHM_C)
7912 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914#if defined(MBEDTLS_ECDH_C)
7915 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007916#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007917#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007918 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007919#if defined(MBEDTLS_SSL_CLI_C)
7920 handshake->ecjpake_cache = NULL;
7921 handshake->ecjpake_cache_len = 0;
7922#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007923#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007924
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007925#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007926 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007927#endif
7928
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007929#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7930 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7931#endif
Hanno Becker75173122019-02-06 16:18:31 +00007932
7933#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7934 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7935 mbedtls_pk_init( &handshake->peer_pubkey );
7936#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007937}
7938
Hanno Beckera18d1322018-01-03 14:27:32 +00007939void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7944 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007945
Hanno Beckerd56ed242018-01-03 15:32:51 +00007946#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 mbedtls_md_init( &transform->md_ctx_enc );
7948 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007949#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007950}
7951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007955}
7956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007958{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007960 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007962 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007964 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007965 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966
7967 /*
7968 * Either the pointers are now NULL or cleared properly and can be freed.
7969 * Now allocate missing structures.
7970 */
7971 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007972 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007973 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007974 }
Paul Bakker48916f92012-09-16 19:57:18 +00007975
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007976 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007977 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007978 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007979 }
Paul Bakker48916f92012-09-16 19:57:18 +00007980
Paul Bakker82788fb2014-10-20 13:59:19 +02007981 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007982 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007983 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007984 }
Paul Bakker48916f92012-09-16 19:57:18 +00007985
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007986 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007987 if( ssl->handshake == NULL ||
7988 ssl->transform_negotiate == NULL ||
7989 ssl->session_negotiate == NULL )
7990 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 mbedtls_free( ssl->handshake );
7994 mbedtls_free( ssl->transform_negotiate );
7995 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996
7997 ssl->handshake = NULL;
7998 ssl->transform_negotiate = NULL;
7999 ssl->session_negotiate = NULL;
8000
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008001 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008002 }
8003
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008004 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008006 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008007 ssl_handshake_params_init( ssl->handshake );
8008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008010 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8011 {
8012 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008013
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008014 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8015 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8016 else
8017 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008018
8019 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008020 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008021#endif
8022
Paul Bakker48916f92012-09-16 19:57:18 +00008023 return( 0 );
8024}
8025
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008026#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008027/* Dummy cookie callbacks for defaults */
8028static int ssl_cookie_write_dummy( void *ctx,
8029 unsigned char **p, unsigned char *end,
8030 const unsigned char *cli_id, size_t cli_id_len )
8031{
8032 ((void) ctx);
8033 ((void) p);
8034 ((void) end);
8035 ((void) cli_id);
8036 ((void) cli_id_len);
8037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008039}
8040
8041static int ssl_cookie_check_dummy( void *ctx,
8042 const unsigned char *cookie, size_t cookie_len,
8043 const unsigned char *cli_id, size_t cli_id_len )
8044{
8045 ((void) ctx);
8046 ((void) cookie);
8047 ((void) cookie_len);
8048 ((void) cli_id);
8049 ((void) cli_id_len);
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008052}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008053#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008054
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008055/* Once ssl->out_hdr as the address of the beginning of the
8056 * next outgoing record is set, deduce the other pointers.
8057 *
8058 * Note: For TLS, we save the implicit record sequence number
8059 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8060 * and the caller has to make sure there's space for this.
8061 */
8062
8063static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8064 mbedtls_ssl_transform *transform )
8065{
8066#if defined(MBEDTLS_SSL_PROTO_DTLS)
8067 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8068 {
8069 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008070#if defined(MBEDTLS_SSL_CID)
8071 ssl->out_cid = ssl->out_ctr + 8;
8072 ssl->out_len = ssl->out_cid;
8073 if( transform != NULL )
8074 ssl->out_len += transform->out_cid_len;
8075#else /* MBEDTLS_SSL_CID */
8076 ssl->out_len = ssl->out_ctr + 8;
8077#endif /* MBEDTLS_SSL_CID */
8078 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008079 }
8080 else
8081#endif
8082 {
8083 ssl->out_ctr = ssl->out_hdr - 8;
8084 ssl->out_len = ssl->out_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008085#if defined(MBEDTLS_SSL_CID)
8086 ssl->out_cid = ssl->out_len;
8087#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008088 ssl->out_iv = ssl->out_hdr + 5;
8089 }
8090
8091 /* Adjust out_msg to make space for explicit IV, if used. */
8092 if( transform != NULL &&
8093 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8094 {
8095 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8096 }
8097 else
8098 ssl->out_msg = ssl->out_iv;
8099}
8100
8101/* Once ssl->in_hdr as the address of the beginning of the
8102 * next incoming record is set, deduce the other pointers.
8103 *
8104 * Note: For TLS, we save the implicit record sequence number
8105 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8106 * and the caller has to make sure there's space for this.
8107 */
8108
Hanno Becker79594fd2019-05-08 09:38:41 +01008109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008110{
Hanno Becker79594fd2019-05-08 09:38:41 +01008111 /* This function sets the pointers to match the case
8112 * of unprotected TLS/DTLS records, with both ssl->in_iv
8113 * and ssl->in_msg pointing to the beginning of the record
8114 * content.
8115 *
8116 * When decrypting a protected record, ssl->in_msg
8117 * will be shifted to point to the beginning of the
8118 * record plaintext.
8119 */
8120
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008121#if defined(MBEDTLS_SSL_PROTO_DTLS)
8122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8123 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008124 /* This sets the header pointers to match records
8125 * without CID. When we receive a record containing
8126 * a CID, the fields are shifted accordingly in
8127 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008128 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008129#if defined(MBEDTLS_SSL_CID)
8130 ssl->in_cid = ssl->in_ctr + 8;
8131 ssl->in_len = ssl->in_cid; /* Default: no CID */
8132#else /* MBEDTLS_SSL_CID */
8133 ssl->in_len = ssl->in_ctr + 8;
8134#endif /* MBEDTLS_SSL_CID */
8135 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008136 }
8137 else
8138#endif
8139 {
8140 ssl->in_ctr = ssl->in_hdr - 8;
8141 ssl->in_len = ssl->in_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008142#if defined(MBEDTLS_SSL_CID)
8143 ssl->in_cid = ssl->in_len;
8144#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008145 ssl->in_iv = ssl->in_hdr + 5;
8146 }
8147
Hanno Becker79594fd2019-05-08 09:38:41 +01008148 /* This will be adjusted at record decryption time. */
8149 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008150}
8151
Paul Bakker5121ce52009-01-03 21:22:43 +00008152/*
8153 * Initialize an SSL context
8154 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008155void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8156{
8157 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8158}
8159
8160/*
8161 * Setup an SSL context
8162 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008163
8164static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8165{
8166 /* Set the incoming and outgoing record pointers. */
8167#if defined(MBEDTLS_SSL_PROTO_DTLS)
8168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8169 {
8170 ssl->out_hdr = ssl->out_buf;
8171 ssl->in_hdr = ssl->in_buf;
8172 }
8173 else
8174#endif /* MBEDTLS_SSL_PROTO_DTLS */
8175 {
8176 ssl->out_hdr = ssl->out_buf + 8;
8177 ssl->in_hdr = ssl->in_buf + 8;
8178 }
8179
8180 /* Derive other internal pointers. */
8181 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008182 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008183}
8184
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008185int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008186 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008187{
Paul Bakker48916f92012-09-16 19:57:18 +00008188 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008189
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008190 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008191
8192 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008193 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008194 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008195
8196 /* Set to NULL in case of an error condition */
8197 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008198
Angus Grattond8213d02016-05-25 20:56:48 +10008199 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8200 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008201 {
Angus Grattond8213d02016-05-25 20:56:48 +10008202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008203 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008204 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008205 }
8206
8207 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8208 if( ssl->out_buf == NULL )
8209 {
8210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008211 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008212 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008213 }
8214
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008215 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008216
Paul Bakker48916f92012-09-16 19:57:18 +00008217 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008218 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008219
8220 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008221
8222error:
8223 mbedtls_free( ssl->in_buf );
8224 mbedtls_free( ssl->out_buf );
8225
8226 ssl->conf = NULL;
8227
8228 ssl->in_buf = NULL;
8229 ssl->out_buf = NULL;
8230
8231 ssl->in_hdr = NULL;
8232 ssl->in_ctr = NULL;
8233 ssl->in_len = NULL;
8234 ssl->in_iv = NULL;
8235 ssl->in_msg = NULL;
8236
8237 ssl->out_hdr = NULL;
8238 ssl->out_ctr = NULL;
8239 ssl->out_len = NULL;
8240 ssl->out_iv = NULL;
8241 ssl->out_msg = NULL;
8242
k-stachowiak9f7798e2018-07-31 16:52:32 +02008243 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008244}
8245
8246/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008247 * Reset an initialized and used SSL context for re-use while retaining
8248 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008249 *
8250 * If partial is non-zero, keep data in the input buffer and client ID.
8251 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008252 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008253static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008254{
Paul Bakker48916f92012-09-16 19:57:18 +00008255 int ret;
8256
Hanno Becker7e772132018-08-10 12:38:21 +01008257#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8258 !defined(MBEDTLS_SSL_SRV_C)
8259 ((void) partial);
8260#endif
8261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008263
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008264 /* Cancel any possibly running timer */
8265 ssl_set_timer( ssl, 0 );
8266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267#if defined(MBEDTLS_SSL_RENEGOTIATION)
8268 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008269 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008270
8271 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8273 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008276
Paul Bakker7eb013f2011-10-06 12:37:39 +00008277 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008278 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008279
8280 ssl->in_msgtype = 0;
8281 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008283 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008284 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008285#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008287 ssl_dtls_replay_reset( ssl );
8288#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008289
8290 ssl->in_hslen = 0;
8291 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008292
8293 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008294
8295 ssl->out_msgtype = 0;
8296 ssl->out_msglen = 0;
8297 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8299 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008300 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008301#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008302
Hanno Becker19859472018-08-06 09:40:20 +01008303 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8304
Paul Bakker48916f92012-09-16 19:57:18 +00008305 ssl->transform_in = NULL;
8306 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008307
Hanno Becker78640902018-08-13 16:35:15 +01008308 ssl->session_in = NULL;
8309 ssl->session_out = NULL;
8310
Angus Grattond8213d02016-05-25 20:56:48 +10008311 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008312
8313#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008314 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008315#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8316 {
8317 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008318 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008319 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8322 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8325 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8328 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008329 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008330 }
8331#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008332
Paul Bakker48916f92012-09-16 19:57:18 +00008333 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335 mbedtls_ssl_transform_free( ssl->transform );
8336 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008337 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008338 }
Paul Bakker48916f92012-09-16 19:57:18 +00008339
Paul Bakkerc0463502013-02-14 11:19:38 +01008340 if( ssl->session )
8341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 mbedtls_ssl_session_free( ssl->session );
8343 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008344 ssl->session = NULL;
8345 }
8346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008348 ssl->alpn_chosen = NULL;
8349#endif
8350
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008351#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008352#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008353 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008354#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008355 {
8356 mbedtls_free( ssl->cli_id );
8357 ssl->cli_id = NULL;
8358 ssl->cli_id_len = 0;
8359 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008360#endif
8361
Paul Bakker48916f92012-09-16 19:57:18 +00008362 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8363 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008364
8365 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008366}
8367
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008368/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008369 * Reset an initialized and used SSL context for re-use while retaining
8370 * all application-set variables, function pointers and data.
8371 */
8372int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8373{
8374 return( ssl_session_reset_int( ssl, 0 ) );
8375}
8376
8377/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008378 * SSL set accessors
8379 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008380void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008381{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008382 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008383}
8384
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008385void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008386{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008387 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008388}
8389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008391void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008392{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008393 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008394}
8395#endif
8396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008401}
8402#endif
8403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008405
Hanno Becker1841b0a2018-08-24 11:13:57 +01008406void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8407 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008408{
8409 ssl->disable_datagram_packing = !allow_packing;
8410}
8411
8412void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8413 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008415 conf->hs_timeout_min = min;
8416 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008417}
8418#endif
8419
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008420void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008422 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008423}
8424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008426void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008427 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008428 void *p_vrfy )
8429{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008430 conf->f_vrfy = f_vrfy;
8431 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008434
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008435void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008436 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008437 void *p_rng )
8438{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008439 conf->f_rng = f_rng;
8440 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008441}
8442
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008443void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008444 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008445 void *p_dbg )
8446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008447 conf->f_dbg = f_dbg;
8448 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008449}
8450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008452 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008453 mbedtls_ssl_send_t *f_send,
8454 mbedtls_ssl_recv_t *f_recv,
8455 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008456{
8457 ssl->p_bio = p_bio;
8458 ssl->f_send = f_send;
8459 ssl->f_recv = f_recv;
8460 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008461}
8462
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008463#if defined(MBEDTLS_SSL_PROTO_DTLS)
8464void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8465{
8466 ssl->mtu = mtu;
8467}
8468#endif
8469
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008470void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008471{
8472 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008473}
8474
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008475void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8476 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008477 mbedtls_ssl_set_timer_t *f_set_timer,
8478 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008479{
8480 ssl->p_timer = p_timer;
8481 ssl->f_set_timer = f_set_timer;
8482 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008483
8484 /* Make sure we start with no timer running */
8485 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008486}
8487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008489void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008490 void *p_cache,
8491 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8492 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008493{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008494 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008495 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008496 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#if defined(MBEDTLS_SSL_CLI_C)
8501int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008502{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008503 int ret;
8504
8505 if( ssl == NULL ||
8506 session == NULL ||
8507 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008508 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008511 }
8512
Hanno Becker52055ae2019-02-06 14:30:46 +00008513 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8514 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008515 return( ret );
8516
Paul Bakker0a597072012-09-25 21:55:46 +00008517 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008518
8519 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008522
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008523void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008524 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008525{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008526 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8527 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8528 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8529 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008530}
8531
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008532void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008533 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008534 int major, int minor )
8535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008537 return;
8538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008540 return;
8541
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008542 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008543}
8544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008546void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008547 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008548{
8549 conf->cert_profile = profile;
8550}
8551
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008552/* Append a new keycert entry to a (possibly empty) list */
8553static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8554 mbedtls_x509_crt *cert,
8555 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008556{
niisato8ee24222018-06-25 19:05:48 +09008557 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008558
niisato8ee24222018-06-25 19:05:48 +09008559 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8560 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008561 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008562
niisato8ee24222018-06-25 19:05:48 +09008563 new_cert->cert = cert;
8564 new_cert->key = key;
8565 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008566
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008567 /* Update head is the list was null, else add to the end */
8568 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008569 {
niisato8ee24222018-06-25 19:05:48 +09008570 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008571 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008572 else
8573 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008574 mbedtls_ssl_key_cert *cur = *head;
8575 while( cur->next != NULL )
8576 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008577 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008578 }
8579
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008580 return( 0 );
8581}
8582
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008583int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008584 mbedtls_x509_crt *own_cert,
8585 mbedtls_pk_context *pk_key )
8586{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008587 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008588}
8589
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008591 mbedtls_x509_crt *ca_chain,
8592 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008593{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008594 conf->ca_chain = ca_chain;
8595 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008596
8597#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8598 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8599 * cannot be used together. */
8600 conf->f_ca_cb = NULL;
8601 conf->p_ca_cb = NULL;
8602#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008603}
Hanno Becker5adaad92019-03-27 16:54:37 +00008604
8605#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8606void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008607 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008608 void *p_ca_cb )
8609{
8610 conf->f_ca_cb = f_ca_cb;
8611 conf->p_ca_cb = p_ca_cb;
8612
8613 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8614 * cannot be used together. */
8615 conf->ca_chain = NULL;
8616 conf->ca_crl = NULL;
8617}
8618#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008620
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008621#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8622int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8623 mbedtls_x509_crt *own_cert,
8624 mbedtls_pk_context *pk_key )
8625{
8626 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8627 own_cert, pk_key ) );
8628}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008629
8630void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8631 mbedtls_x509_crt *ca_chain,
8632 mbedtls_x509_crl *ca_crl )
8633{
8634 ssl->handshake->sni_ca_chain = ca_chain;
8635 ssl->handshake->sni_ca_crl = ca_crl;
8636}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008637
8638void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8639 int authmode )
8640{
8641 ssl->handshake->sni_authmode = authmode;
8642}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008643#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8644
Hanno Becker8927c832019-04-03 12:52:50 +01008645#if defined(MBEDTLS_X509_CRT_PARSE_C)
8646void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8647 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8648 void *p_vrfy )
8649{
8650 ssl->f_vrfy = f_vrfy;
8651 ssl->p_vrfy = p_vrfy;
8652}
8653#endif
8654
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008655#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008656/*
8657 * Set EC J-PAKE password for current handshake
8658 */
8659int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8660 const unsigned char *pw,
8661 size_t pw_len )
8662{
8663 mbedtls_ecjpake_role role;
8664
Janos Follath8eb64132016-06-03 15:40:57 +01008665 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8667
8668 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8669 role = MBEDTLS_ECJPAKE_SERVER;
8670 else
8671 role = MBEDTLS_ECJPAKE_CLIENT;
8672
8673 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8674 role,
8675 MBEDTLS_MD_SHA256,
8676 MBEDTLS_ECP_DP_SECP256R1,
8677 pw, pw_len ) );
8678}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008679#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008682
8683static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8684{
8685 /* Remove reference to existing PSK, if any. */
8686#if defined(MBEDTLS_USE_PSA_CRYPTO)
8687 if( conf->psk_opaque != 0 )
8688 {
8689 /* The maintenance of the PSK key slot is the
8690 * user's responsibility. */
8691 conf->psk_opaque = 0;
8692 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008693 /* This and the following branch should never
8694 * be taken simultaenously as we maintain the
8695 * invariant that raw and opaque PSKs are never
8696 * configured simultaneously. As a safeguard,
8697 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008698#endif /* MBEDTLS_USE_PSA_CRYPTO */
8699 if( conf->psk != NULL )
8700 {
8701 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8702
8703 mbedtls_free( conf->psk );
8704 conf->psk = NULL;
8705 conf->psk_len = 0;
8706 }
8707
8708 /* Remove reference to PSK identity, if any. */
8709 if( conf->psk_identity != NULL )
8710 {
8711 mbedtls_free( conf->psk_identity );
8712 conf->psk_identity = NULL;
8713 conf->psk_identity_len = 0;
8714 }
8715}
8716
Hanno Becker7390c712018-11-15 13:33:04 +00008717/* This function assumes that PSK identity in the SSL config is unset.
8718 * It checks that the provided identity is well-formed and attempts
8719 * to make a copy of it in the SSL config.
8720 * On failure, the PSK identity in the config remains unset. */
8721static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8722 unsigned char const *psk_identity,
8723 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008724{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008725 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008726 if( psk_identity == NULL ||
8727 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008728 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008729 {
8730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8731 }
8732
Hanno Becker7390c712018-11-15 13:33:04 +00008733 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8734 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008735 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008736
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008737 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008738 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008739
8740 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008741}
8742
Hanno Becker7390c712018-11-15 13:33:04 +00008743int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8744 const unsigned char *psk, size_t psk_len,
8745 const unsigned char *psk_identity, size_t psk_identity_len )
8746{
8747 int ret;
8748 /* Remove opaque/raw PSK + PSK Identity */
8749 ssl_conf_remove_psk( conf );
8750
8751 /* Check and set raw PSK */
8752 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8754 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8755 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8756 conf->psk_len = psk_len;
8757 memcpy( conf->psk, psk, conf->psk_len );
8758
8759 /* Check and set PSK Identity */
8760 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8761 if( ret != 0 )
8762 ssl_conf_remove_psk( conf );
8763
8764 return( ret );
8765}
8766
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008767static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8768{
8769#if defined(MBEDTLS_USE_PSA_CRYPTO)
8770 if( ssl->handshake->psk_opaque != 0 )
8771 {
8772 ssl->handshake->psk_opaque = 0;
8773 }
8774 else
8775#endif /* MBEDTLS_USE_PSA_CRYPTO */
8776 if( ssl->handshake->psk != NULL )
8777 {
8778 mbedtls_platform_zeroize( ssl->handshake->psk,
8779 ssl->handshake->psk_len );
8780 mbedtls_free( ssl->handshake->psk );
8781 ssl->handshake->psk_len = 0;
8782 }
8783}
8784
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008785int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8786 const unsigned char *psk, size_t psk_len )
8787{
8788 if( psk == NULL || ssl->handshake == NULL )
8789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8790
8791 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8793
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008794 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008795
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008796 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008797 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008798
8799 ssl->handshake->psk_len = psk_len;
8800 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8801
8802 return( 0 );
8803}
8804
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008805#if defined(MBEDTLS_USE_PSA_CRYPTO)
8806int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008807 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008808 const unsigned char *psk_identity,
8809 size_t psk_identity_len )
8810{
Hanno Becker7390c712018-11-15 13:33:04 +00008811 int ret;
8812 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008813 ssl_conf_remove_psk( conf );
8814
Hanno Becker7390c712018-11-15 13:33:04 +00008815 /* Check and set opaque PSK */
8816 if( psk_slot == 0 )
8817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008818 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008819
8820 /* Check and set PSK Identity */
8821 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8822 psk_identity_len );
8823 if( ret != 0 )
8824 ssl_conf_remove_psk( conf );
8825
8826 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008827}
8828
8829int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008830 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008831{
8832 if( psk_slot == 0 || ssl->handshake == NULL )
8833 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8834
8835 ssl_remove_psk( ssl );
8836 ssl->handshake->psk_opaque = psk_slot;
8837 return( 0 );
8838}
8839#endif /* MBEDTLS_USE_PSA_CRYPTO */
8840
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008841void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008843 size_t),
8844 void *p_psk )
8845{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008846 conf->f_psk = f_psk;
8847 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008850
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008851#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008852
8853#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008854int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008855{
8856 int ret;
8857
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008858 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8859 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8860 {
8861 mbedtls_mpi_free( &conf->dhm_P );
8862 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008863 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008865
8866 return( 0 );
8867}
Hanno Becker470a8c42017-10-04 15:28:46 +01008868#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008869
Hanno Beckera90658f2017-10-04 15:29:08 +01008870int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8871 const unsigned char *dhm_P, size_t P_len,
8872 const unsigned char *dhm_G, size_t G_len )
8873{
8874 int ret;
8875
8876 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8877 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8878 {
8879 mbedtls_mpi_free( &conf->dhm_P );
8880 mbedtls_mpi_free( &conf->dhm_G );
8881 return( ret );
8882 }
8883
8884 return( 0 );
8885}
Paul Bakker5121ce52009-01-03 21:22:43 +00008886
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008887int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008888{
8889 int ret;
8890
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008891 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8892 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8893 {
8894 mbedtls_mpi_free( &conf->dhm_P );
8895 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008896 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008897 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008898
8899 return( 0 );
8900}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008901#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008902
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008903#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8904/*
8905 * Set the minimum length for Diffie-Hellman parameters
8906 */
8907void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8908 unsigned int bitlen )
8909{
8910 conf->dhm_min_bitlen = bitlen;
8911}
8912#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8913
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008914#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008915/*
8916 * Set allowed/preferred hashes for handshake signatures
8917 */
8918void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8919 const int *hashes )
8920{
8921 conf->sig_hashes = hashes;
8922}
Hanno Becker947194e2017-04-07 13:25:49 +01008923#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008924
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008925#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008926/*
8927 * Set the allowed elliptic curves
8928 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008929void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008930 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008932 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008933}
Hanno Becker947194e2017-04-07 13:25:49 +01008934#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008935
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008936#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008938{
Hanno Becker947194e2017-04-07 13:25:49 +01008939 /* Initialize to suppress unnecessary compiler warning */
8940 size_t hostname_len = 0;
8941
8942 /* Check if new hostname is valid before
8943 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008944 if( hostname != NULL )
8945 {
8946 hostname_len = strlen( hostname );
8947
8948 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8950 }
8951
8952 /* Now it's clear that we will overwrite the old hostname,
8953 * so we can free it safely */
8954
8955 if( ssl->hostname != NULL )
8956 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008957 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008958 mbedtls_free( ssl->hostname );
8959 }
8960
8961 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008962
Paul Bakker5121ce52009-01-03 21:22:43 +00008963 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008964 {
8965 ssl->hostname = NULL;
8966 }
8967 else
8968 {
8969 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008970 if( ssl->hostname == NULL )
8971 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008972
Hanno Becker947194e2017-04-07 13:25:49 +01008973 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008974
Hanno Becker947194e2017-04-07 13:25:49 +01008975 ssl->hostname[hostname_len] = '\0';
8976 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008977
8978 return( 0 );
8979}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008980#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008981
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008982#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008983void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008985 const unsigned char *, size_t),
8986 void *p_sni )
8987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008988 conf->f_sni = f_sni;
8989 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008994int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008995{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008996 size_t cur_len, tot_len;
8997 const char **p;
8998
8999 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009000 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9001 * MUST NOT be truncated."
9002 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009003 */
9004 tot_len = 0;
9005 for( p = protos; *p != NULL; p++ )
9006 {
9007 cur_len = strlen( *p );
9008 tot_len += cur_len;
9009
9010 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009012 }
9013
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009014 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009015
9016 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009017}
9018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009020{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009021 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009024
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009025void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009026{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009027 conf->max_major_ver = major;
9028 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009029}
9030
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009031void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009032{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009033 conf->min_major_ver = major;
9034 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009035}
9036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009038void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009039{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009040 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009041}
9042#endif
9043
Janos Follath088ce432017-04-10 12:42:31 +01009044#if defined(MBEDTLS_SSL_SRV_C)
9045void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9046 char cert_req_ca_list )
9047{
9048 conf->cert_req_ca_list = cert_req_ca_list;
9049}
9050#endif
9051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009053void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009055 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009056}
9057#endif
9058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009060void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009061{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009062 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009063}
9064#endif
9065
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009066#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009067void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009069 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009070}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009071#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009074int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009077 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009080 }
9081
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009082 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009083
9084 return( 0 );
9085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009089void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009090{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009091 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009096void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009097{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009098 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009099}
9100#endif
9101
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009102void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009103{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009104 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009105}
9106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009108void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009109{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009110 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009111}
9112
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009113void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009114{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009115 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009116}
9117
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009118void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009119 const unsigned char period[8] )
9120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009121 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009126#if defined(MBEDTLS_SSL_CLI_C)
9127void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009128{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009129 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009130}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009131#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009132
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009133#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009134void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9135 mbedtls_ssl_ticket_write_t *f_ticket_write,
9136 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9137 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009138{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009139 conf->f_ticket_write = f_ticket_write;
9140 conf->f_ticket_parse = f_ticket_parse;
9141 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009142}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009145
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009146#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9147void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9148 mbedtls_ssl_export_keys_t *f_export_keys,
9149 void *p_export_keys )
9150{
9151 conf->f_export_keys = f_export_keys;
9152 conf->p_export_keys = p_export_keys;
9153}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009154
9155void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9156 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9157 void *p_export_keys )
9158{
9159 conf->f_export_keys_ext = f_export_keys_ext;
9160 conf->p_export_keys = p_export_keys;
9161}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009162#endif
9163
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009164#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009165void mbedtls_ssl_conf_async_private_cb(
9166 mbedtls_ssl_config *conf,
9167 mbedtls_ssl_async_sign_t *f_async_sign,
9168 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9169 mbedtls_ssl_async_resume_t *f_async_resume,
9170 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009171 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009172{
9173 conf->f_async_sign_start = f_async_sign;
9174 conf->f_async_decrypt_start = f_async_decrypt;
9175 conf->f_async_resume = f_async_resume;
9176 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009177 conf->p_async_config_data = async_config_data;
9178}
9179
Gilles Peskine8f97af72018-04-26 11:46:10 +02009180void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9181{
9182 return( conf->p_async_config_data );
9183}
9184
Gilles Peskine1febfef2018-04-30 11:54:39 +02009185void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009186{
9187 if( ssl->handshake == NULL )
9188 return( NULL );
9189 else
9190 return( ssl->handshake->user_async_ctx );
9191}
9192
Gilles Peskine1febfef2018-04-30 11:54:39 +02009193void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009194 void *ctx )
9195{
9196 if( ssl->handshake != NULL )
9197 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009198}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009199#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009200
Paul Bakker5121ce52009-01-03 21:22:43 +00009201/*
9202 * SSL get accessors
9203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009205{
9206 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9207}
9208
Hanno Becker8b170a02017-10-10 11:51:19 +01009209int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9210{
9211 /*
9212 * Case A: We're currently holding back
9213 * a message for further processing.
9214 */
9215
9216 if( ssl->keep_current_message == 1 )
9217 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009219 return( 1 );
9220 }
9221
9222 /*
9223 * Case B: Further records are pending in the current datagram.
9224 */
9225
9226#if defined(MBEDTLS_SSL_PROTO_DTLS)
9227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9228 ssl->in_left > ssl->next_record_offset )
9229 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009231 return( 1 );
9232 }
9233#endif /* MBEDTLS_SSL_PROTO_DTLS */
9234
9235 /*
9236 * Case C: A handshake message is being processed.
9237 */
9238
Hanno Becker8b170a02017-10-10 11:51:19 +01009239 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9240 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009242 return( 1 );
9243 }
9244
9245 /*
9246 * Case D: An application data message is being processed
9247 */
9248 if( ssl->in_offt != NULL )
9249 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009251 return( 1 );
9252 }
9253
9254 /*
9255 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009256 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009257 * we implement support for multiple alerts in single records.
9258 */
9259
9260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9261 return( 0 );
9262}
9263
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009264uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009265{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009266 if( ssl->session != NULL )
9267 return( ssl->session->verify_result );
9268
9269 if( ssl->session_negotiate != NULL )
9270 return( ssl->session_negotiate->verify_result );
9271
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009272 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009273}
9274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009276{
Paul Bakker926c8e42013-03-06 10:23:34 +01009277 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009278 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009281}
9282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009286 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009287 {
9288 switch( ssl->minor_ver )
9289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009291 return( "DTLSv1.0" );
9292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009294 return( "DTLSv1.2" );
9295
9296 default:
9297 return( "unknown (DTLS)" );
9298 }
9299 }
9300#endif
9301
Paul Bakker43ca69c2011-01-15 17:35:19 +00009302 switch( ssl->minor_ver )
9303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009305 return( "SSLv3.0" );
9306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009308 return( "TLSv1.0" );
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009311 return( "TLSv1.1" );
9312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009314 return( "TLSv1.2" );
9315
Paul Bakker43ca69c2011-01-15 17:35:19 +00009316 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009317 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009318 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009319}
9320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009322{
Hanno Becker3136ede2018-08-17 15:28:19 +01009323 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009325 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009326
Hanno Becker5903de42019-05-03 14:46:38 +01009327 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9328
Hanno Becker78640902018-08-13 16:35:15 +01009329 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009330 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332#if defined(MBEDTLS_ZLIB_SUPPORT)
9333 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9334 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009335#endif
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 case MBEDTLS_MODE_GCM:
9340 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009341 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009343 transform_expansion = transform->minlen;
9344 break;
9345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009347
9348 block_size = mbedtls_cipher_get_block_size(
9349 &transform->cipher_ctx_enc );
9350
Hanno Becker3136ede2018-08-17 15:28:19 +01009351 /* Expansion due to the addition of the MAC. */
9352 transform_expansion += transform->maclen;
9353
9354 /* Expansion due to the addition of CBC padding;
9355 * Theoretically up to 256 bytes, but we never use
9356 * more than the block size of the underlying cipher. */
9357 transform_expansion += block_size;
9358
9359 /* For TLS 1.1 or higher, an explicit IV is added
9360 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009361#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9362 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009363 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009364#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009365
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009366 break;
9367
9368 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009371 }
9372
Hanno Becker6cbad552019-05-08 15:40:11 +01009373#if defined(MBEDTLS_SSL_CID)
9374 if( transform->out_cid_len != 0 )
9375 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
9376#endif /* MBEDTLS_SSL_CID */
9377
Hanno Becker5903de42019-05-03 14:46:38 +01009378 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009379}
9380
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009381#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9382size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9383{
9384 size_t max_len;
9385
9386 /*
9387 * Assume mfl_code is correct since it was checked when set
9388 */
Angus Grattond8213d02016-05-25 20:56:48 +10009389 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009390
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009391 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009392 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009393 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009394 {
Angus Grattond8213d02016-05-25 20:56:48 +10009395 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009396 }
9397
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009398 /* During a handshake, use the value being negotiated */
9399 if( ssl->session_negotiate != NULL &&
9400 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9401 {
9402 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9403 }
9404
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009405 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009406}
9407#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9408
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009409#if defined(MBEDTLS_SSL_PROTO_DTLS)
9410static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9411{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009412 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9413 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9414 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9415 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9416 return ( 0 );
9417
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009418 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9419 return( ssl->mtu );
9420
9421 if( ssl->mtu == 0 )
9422 return( ssl->handshake->mtu );
9423
9424 return( ssl->mtu < ssl->handshake->mtu ?
9425 ssl->mtu : ssl->handshake->mtu );
9426}
9427#endif /* MBEDTLS_SSL_PROTO_DTLS */
9428
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009429int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9430{
9431 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9432
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009433#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9434 !defined(MBEDTLS_SSL_PROTO_DTLS)
9435 (void) ssl;
9436#endif
9437
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009438#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9439 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9440
9441 if( max_len > mfl )
9442 max_len = mfl;
9443#endif
9444
9445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009446 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009447 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009448 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009449 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9450 const size_t overhead = (size_t) ret;
9451
9452 if( ret < 0 )
9453 return( ret );
9454
9455 if( mtu <= overhead )
9456 {
9457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9458 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9459 }
9460
9461 if( max_len > mtu - overhead )
9462 max_len = mtu - overhead;
9463 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009464#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009465
Hanno Becker0defedb2018-08-10 12:35:02 +01009466#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9467 !defined(MBEDTLS_SSL_PROTO_DTLS)
9468 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009469#endif
9470
9471 return( (int) max_len );
9472}
9473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474#if defined(MBEDTLS_X509_CRT_PARSE_C)
9475const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009476{
9477 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009478 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009479
Hanno Beckere6824572019-02-07 13:18:46 +00009480#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009481 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009482#else
9483 return( NULL );
9484#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009489int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9490 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009491{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009492 if( ssl == NULL ||
9493 dst == NULL ||
9494 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009495 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009498 }
9499
Hanno Becker52055ae2019-02-06 14:30:46 +00009500 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009503
Paul Bakker5121ce52009-01-03 21:22:43 +00009504/*
Paul Bakker1961b702013-01-25 14:49:24 +01009505 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009510
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009511 if( ssl == NULL || ssl->conf == NULL )
9512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009515 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009519 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009521#endif
9522
Paul Bakker1961b702013-01-25 14:49:24 +01009523 return( ret );
9524}
9525
9526/*
9527 * Perform the SSL handshake
9528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009530{
9531 int ret = 0;
9532
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009533 if( ssl == NULL || ssl->conf == NULL )
9534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009541
9542 if( ret != 0 )
9543 break;
9544 }
9545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009547
9548 return( ret );
9549}
9550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551#if defined(MBEDTLS_SSL_RENEGOTIATION)
9552#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009553/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009554 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009557{
9558 int ret;
9559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009561
9562 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9564 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009565
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009566 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009567 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009569 return( ret );
9570 }
9571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009573
9574 return( 0 );
9575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009577
9578/*
9579 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 * - any side: calling mbedtls_ssl_renegotiate(),
9581 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9582 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009583 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009584 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009588{
9589 int ret;
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009592
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009593 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9594 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009595
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009596 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9597 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009599 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009601 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009602 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009603 ssl->handshake->out_msg_seq = 1;
9604 else
9605 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009606 }
9607#endif
9608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9610 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009615 return( ret );
9616 }
9617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009619
9620 return( 0 );
9621}
9622
9623/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009624 * Renegotiate current connection on client,
9625 * or request renegotiation on server
9626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009628{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009630
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009631 if( ssl == NULL || ssl->conf == NULL )
9632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009635 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009636 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009642
9643 /* Did we already try/start sending HelloRequest? */
9644 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009646
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009647 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009652 /*
9653 * On client, either start the renegotiation process or,
9654 * if already in progress, continue the handshake
9655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009660
9661 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009664 return( ret );
9665 }
9666 }
9667 else
9668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009672 return( ret );
9673 }
9674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009676
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009677 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009678}
9679
9680/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009681 * Check record counters and renegotiate if they're above the limit.
9682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009684{
Andres AG2196c7f2016-12-15 17:01:16 +00009685 size_t ep_len = ssl_ep_len( ssl );
9686 int in_ctr_cmp;
9687 int out_ctr_cmp;
9688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9690 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009691 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009692 {
9693 return( 0 );
9694 }
9695
Andres AG2196c7f2016-12-15 17:01:16 +00009696 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9697 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009698 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009699 ssl->conf->renego_period + ep_len, 8 - ep_len );
9700
9701 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009702 {
9703 return( 0 );
9704 }
9705
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009710
9711/*
9712 * Receive application data decrypted from the SSL layer
9713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009715{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009716 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009717 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009718
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009719 if( ssl == NULL || ssl->conf == NULL )
9720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009725 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009728 return( ret );
9729
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009730 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009732 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009733 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009734 return( ret );
9735 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009736 }
9737#endif
9738
Hanno Becker4a810fb2017-05-24 16:27:30 +01009739 /*
9740 * Check if renegotiation is necessary and/or handshake is
9741 * in process. If yes, perform/continue, and fall through
9742 * if an unexpected packet is received while the client
9743 * is waiting for the ServerHello.
9744 *
9745 * (There is no equivalent to the last condition on
9746 * the server-side as it is not treated as within
9747 * a handshake while waiting for the ClientHello
9748 * after a renegotiation request.)
9749 */
9750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009752 ret = ssl_check_ctr_renegotiate( ssl );
9753 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9754 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009757 return( ret );
9758 }
9759#endif
9760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009764 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9765 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009768 return( ret );
9769 }
9770 }
9771
Hanno Beckere41158b2017-10-23 13:30:32 +01009772 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009773 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009774 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009775 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009776 if( ssl->f_get_timer != NULL &&
9777 ssl->f_get_timer( ssl->p_timer ) == -1 )
9778 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009779 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009780 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009781
Hanno Becker327c93b2018-08-15 13:56:18 +01009782 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009783 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009784 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9785 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009786
Hanno Becker4a810fb2017-05-24 16:27:30 +01009787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9788 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009789 }
9790
9791 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009793 {
9794 /*
9795 * OpenSSL sends empty messages to randomize the IV
9796 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009797 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009800 return( 0 );
9801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009803 return( ret );
9804 }
9805 }
9806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009810
Hanno Becker4a810fb2017-05-24 16:27:30 +01009811 /*
9812 * - For client-side, expect SERVER_HELLO_REQUEST.
9813 * - For server-side, expect CLIENT_HELLO.
9814 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9815 */
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009818 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009820 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009823
9824 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009827 {
9828 continue;
9829 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009830#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009832 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009833#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009834
Hanno Becker4a810fb2017-05-24 16:27:30 +01009835#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009836 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009840
9841 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009843 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009844 {
9845 continue;
9846 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009849 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009850#endif /* MBEDTLS_SSL_SRV_C */
9851
Hanno Becker21df7f92017-10-17 11:03:26 +01009852#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009853 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009854 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9855 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9856 ssl->conf->allow_legacy_renegotiation ==
9857 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9858 {
9859 /*
9860 * Accept renegotiation request
9861 */
Paul Bakker48916f92012-09-16 19:57:18 +00009862
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009863 /* DTLS clients need to know renego is server-initiated */
9864#if defined(MBEDTLS_SSL_PROTO_DTLS)
9865 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9866 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9867 {
9868 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9869 }
9870#endif
9871 ret = ssl_start_renegotiation( ssl );
9872 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9873 ret != 0 )
9874 {
9875 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9876 return( ret );
9877 }
9878 }
9879 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009880#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009881 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009882 /*
9883 * Refuse renegotiation
9884 */
9885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888#if defined(MBEDTLS_SSL_PROTO_SSL3)
9889 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009890 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009891 /* SSLv3 does not have a "no_renegotiation" warning, so
9892 we send a fatal alert and abort the connection. */
9893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9894 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9895 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009896 }
9897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9899#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9900 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9901 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9904 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9905 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009906 {
9907 return( ret );
9908 }
Paul Bakker48916f92012-09-16 19:57:18 +00009909 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9912 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9915 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009916 }
Paul Bakker48916f92012-09-16 19:57:18 +00009917 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009918
Hanno Becker90333da2017-10-10 11:27:13 +01009919 /* At this point, we don't know whether the renegotiation has been
9920 * completed or not. The cases to consider are the following:
9921 * 1) The renegotiation is complete. In this case, no new record
9922 * has been read yet.
9923 * 2) The renegotiation is incomplete because the client received
9924 * an application data record while awaiting the ServerHello.
9925 * 3) The renegotiation is incomplete because the client received
9926 * a non-handshake, non-application data message while awaiting
9927 * the ServerHello.
9928 * In each of these case, looping will be the proper action:
9929 * - For 1), the next iteration will read a new record and check
9930 * if it's application data.
9931 * - For 2), the loop condition isn't satisfied as application data
9932 * is present, hence continue is the same as break
9933 * - For 3), the loop condition is satisfied and read_record
9934 * will re-deliver the message that was held back by the client
9935 * when expecting the ServerHello.
9936 */
9937 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009938 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009939#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009941 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009942 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009943 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009944 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009947 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009949 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009950 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9955 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009958 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009959 }
9960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9964 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009965 }
9966
9967 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009968
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009969 /* We're going to return something now, cancel timer,
9970 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009972 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009973
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009975 /* If we requested renego but received AppData, resend HelloRequest.
9976 * Do it now, after setting in_offt, to avoid taking this branch
9977 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009981 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009982 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009985 return( ret );
9986 }
9987 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009989#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009990 }
9991
9992 n = ( len < ssl->in_msglen )
9993 ? len : ssl->in_msglen;
9994
9995 memcpy( buf, ssl->in_offt, n );
9996 ssl->in_msglen -= n;
9997
9998 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009999 {
10000 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010001 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010002 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010003 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010004 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010005 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010006 /* more data available */
10007 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010008 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010011
Paul Bakker23986e52011-04-24 08:57:21 +000010012 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010013}
10014
10015/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010016 * Send application data to be encrypted by the SSL layer, taking care of max
10017 * fragment length and buffer size.
10018 *
10019 * According to RFC 5246 Section 6.2.1:
10020 *
10021 * Zero-length fragments of Application data MAY be sent as they are
10022 * potentially useful as a traffic analysis countermeasure.
10023 *
10024 * Therefore, it is possible that the input message length is 0 and the
10025 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010026 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010027static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010028 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010029{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010030 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10031 const size_t max_len = (size_t) ret;
10032
10033 if( ret < 0 )
10034 {
10035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10036 return( ret );
10037 }
10038
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010039 if( len > max_len )
10040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010042 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010045 "maximum fragment length: %d > %d",
10046 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010048 }
10049 else
10050#endif
10051 len = max_len;
10052 }
Paul Bakker887bd502011-06-08 13:10:54 +000010053
Paul Bakker5121ce52009-01-03 21:22:43 +000010054 if( ssl->out_left != 0 )
10055 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010056 /*
10057 * The user has previously tried to send the data and
10058 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10059 * written. In this case, we expect the high-level write function
10060 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010065 return( ret );
10066 }
10067 }
Paul Bakker887bd502011-06-08 13:10:54 +000010068 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010069 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010070 /*
10071 * The user is trying to send a message the first time, so we need to
10072 * copy the data into the internal buffers and setup the data structure
10073 * to keep track of partial writes
10074 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010075 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010076 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010077 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010078
Hanno Becker67bc7c32018-08-06 11:33:50 +010010079 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010082 return( ret );
10083 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010084 }
10085
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010086 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010087}
10088
10089/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010090 * Write application data, doing 1/n-1 splitting if necessary.
10091 *
10092 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010093 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010094 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010097static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010098 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010099{
10100 int ret;
10101
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010102 if( ssl->conf->cbc_record_splitting ==
10103 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010104 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10106 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10107 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010108 {
10109 return( ssl_write_real( ssl, buf, len ) );
10110 }
10111
10112 if( ssl->split_done == 0 )
10113 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010114 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010115 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010116 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010117 }
10118
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010119 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10120 return( ret );
10121 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010122
10123 return( ret + 1 );
10124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010126
10127/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010128 * Write application data (public-facing wrapper)
10129 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010130int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010131{
10132 int ret;
10133
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010135
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010136 if( ssl == NULL || ssl->conf == NULL )
10137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10138
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010139#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010140 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10141 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010142 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010143 return( ret );
10144 }
10145#endif
10146
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010147 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010148 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010149 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010150 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010152 return( ret );
10153 }
10154 }
10155
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010156#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010157 ret = ssl_write_split( ssl, buf, len );
10158#else
10159 ret = ssl_write_real( ssl, buf, len );
10160#endif
10161
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010163
10164 return( ret );
10165}
10166
10167/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010168 * Notify the peer that the connection is being closed
10169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010171{
10172 int ret;
10173
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010174 if( ssl == NULL || ssl->conf == NULL )
10175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010178
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010179 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10185 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10186 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010189 return( ret );
10190 }
10191 }
10192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010194
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010195 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196}
10197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010199{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010200 if( transform == NULL )
10201 return;
10202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010204 deflateEnd( &transform->ctx_deflate );
10205 inflateEnd( &transform->ctx_inflate );
10206#endif
10207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10209 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010210
Hanno Beckerd56ed242018-01-03 15:32:51 +000010211#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 mbedtls_md_free( &transform->md_ctx_enc );
10213 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010214#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010215
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010216 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010217}
10218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219#if defined(MBEDTLS_X509_CRT_PARSE_C)
10220static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010223
10224 while( cur != NULL )
10225 {
10226 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010228 cur = next;
10229 }
10230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010232
Hanno Becker0271f962018-08-16 13:23:47 +010010233#if defined(MBEDTLS_SSL_PROTO_DTLS)
10234
10235static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10236{
10237 unsigned offset;
10238 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10239
10240 if( hs == NULL )
10241 return;
10242
Hanno Becker283f5ef2018-08-24 09:34:47 +010010243 ssl_free_buffered_record( ssl );
10244
Hanno Becker0271f962018-08-16 13:23:47 +010010245 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010246 ssl_buffering_free_slot( ssl, offset );
10247}
10248
10249static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10250 uint8_t slot )
10251{
10252 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10253 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010254
10255 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10256 return;
10257
Hanno Beckere605b192018-08-21 15:59:07 +010010258 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010259 {
Hanno Beckere605b192018-08-21 15:59:07 +010010260 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010261 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010262 mbedtls_free( hs_buf->data );
10263 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010264 }
10265}
10266
10267#endif /* MBEDTLS_SSL_PROTO_DTLS */
10268
Gilles Peskine9b562d52018-04-25 20:32:43 +020010269void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010270{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010271 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10272
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010273 if( handshake == NULL )
10274 return;
10275
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010276#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10277 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10278 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010279 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010280 handshake->async_in_progress = 0;
10281 }
10282#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10283
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010284#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10285 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10286 mbedtls_md5_free( &handshake->fin_md5 );
10287 mbedtls_sha1_free( &handshake->fin_sha1 );
10288#endif
10289#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10290#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010291#if defined(MBEDTLS_USE_PSA_CRYPTO)
10292 psa_hash_abort( &handshake->fin_sha256_psa );
10293#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010294 mbedtls_sha256_free( &handshake->fin_sha256 );
10295#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010296#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010297#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010298#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010299 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010300#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010301 mbedtls_sha512_free( &handshake->fin_sha512 );
10302#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010303#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010304#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306#if defined(MBEDTLS_DHM_C)
10307 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309#if defined(MBEDTLS_ECDH_C)
10310 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010311#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010312#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010313 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010314#if defined(MBEDTLS_SSL_CLI_C)
10315 mbedtls_free( handshake->ecjpake_cache );
10316 handshake->ecjpake_cache = NULL;
10317 handshake->ecjpake_cache_len = 0;
10318#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010319#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010320
Janos Follath4ae5c292016-02-10 11:27:43 +000010321#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10322 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010323 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010325#endif
10326
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010327#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10328 if( handshake->psk != NULL )
10329 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010330 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010331 mbedtls_free( handshake->psk );
10332 }
10333#endif
10334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10336 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010337 /*
10338 * Free only the linked list wrapper, not the keys themselves
10339 * since the belong to the SNI callback
10340 */
10341 if( handshake->sni_key_cert != NULL )
10342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010344
10345 while( cur != NULL )
10346 {
10347 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010349 cur = next;
10350 }
10351 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010353
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010354#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010355 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010356 if( handshake->ecrs_peer_cert != NULL )
10357 {
10358 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10359 mbedtls_free( handshake->ecrs_peer_cert );
10360 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010361#endif
10362
Hanno Becker75173122019-02-06 16:18:31 +000010363#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10364 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10365 mbedtls_pk_free( &handshake->peer_pubkey );
10366#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368#if defined(MBEDTLS_SSL_PROTO_DTLS)
10369 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010370 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010371 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010372#endif
10373
Hanno Becker4a63ed42019-01-08 11:39:35 +000010374#if defined(MBEDTLS_ECDH_C) && \
10375 defined(MBEDTLS_USE_PSA_CRYPTO)
10376 psa_destroy_key( handshake->ecdh_psa_privkey );
10377#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10378
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010379 mbedtls_platform_zeroize( handshake,
10380 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010381}
10382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010384{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010385 if( session == NULL )
10386 return;
10387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010389 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010390#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010391
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010392#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010394#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010395
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010396 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010397}
10398
Paul Bakker5121ce52009-01-03 21:22:43 +000010399/*
10400 * Free an SSL context
10401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010402void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010403{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010404 if( ssl == NULL )
10405 return;
10406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010408
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010409 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010410 {
Angus Grattond8213d02016-05-25 20:56:48 +100010411 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010413 }
10414
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010415 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010416 {
Angus Grattond8213d02016-05-25 20:56:48 +100010417 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010419 }
10420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010422 if( ssl->compress_buf != NULL )
10423 {
Angus Grattond8213d02016-05-25 20:56:48 +100010424 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010426 }
10427#endif
10428
Paul Bakker48916f92012-09-16 19:57:18 +000010429 if( ssl->transform )
10430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 mbedtls_ssl_transform_free( ssl->transform );
10432 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010433 }
10434
10435 if( ssl->handshake )
10436 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010437 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10439 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 mbedtls_free( ssl->handshake );
10442 mbedtls_free( ssl->transform_negotiate );
10443 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010444 }
10445
Paul Bakkerc0463502013-02-14 11:19:38 +010010446 if( ssl->session )
10447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 mbedtls_ssl_session_free( ssl->session );
10449 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010450 }
10451
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010452#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010453 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010454 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010455 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010457 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010458#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10461 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10464 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010465 }
10466#endif
10467
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010468#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010470#endif
10471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010473
Paul Bakker86f04f42013-02-14 11:20:09 +010010474 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010475 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010476}
10477
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010478/*
10479 * Initialze mbedtls_ssl_config
10480 */
10481void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10482{
10483 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10484}
10485
Simon Butcherc97b6972015-12-27 23:48:17 +000010486#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010487static int ssl_preset_default_hashes[] = {
10488#if defined(MBEDTLS_SHA512_C)
10489 MBEDTLS_MD_SHA512,
10490 MBEDTLS_MD_SHA384,
10491#endif
10492#if defined(MBEDTLS_SHA256_C)
10493 MBEDTLS_MD_SHA256,
10494 MBEDTLS_MD_SHA224,
10495#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010496#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010497 MBEDTLS_MD_SHA1,
10498#endif
10499 MBEDTLS_MD_NONE
10500};
Simon Butcherc97b6972015-12-27 23:48:17 +000010501#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010502
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010503static int ssl_preset_suiteb_ciphersuites[] = {
10504 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10505 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10506 0
10507};
10508
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010509#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010510static int ssl_preset_suiteb_hashes[] = {
10511 MBEDTLS_MD_SHA256,
10512 MBEDTLS_MD_SHA384,
10513 MBEDTLS_MD_NONE
10514};
10515#endif
10516
10517#if defined(MBEDTLS_ECP_C)
10518static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10519 MBEDTLS_ECP_DP_SECP256R1,
10520 MBEDTLS_ECP_DP_SECP384R1,
10521 MBEDTLS_ECP_DP_NONE
10522};
10523#endif
10524
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010525/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010526 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010527 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010528int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010529 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010530{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010531#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010532 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010533#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010534
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010535 /* Use the functions here so that they are covered in tests,
10536 * but otherwise access member directly for efficiency */
10537 mbedtls_ssl_conf_endpoint( conf, endpoint );
10538 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010539
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010540 /*
10541 * Things that are common to all presets
10542 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010543#if defined(MBEDTLS_SSL_CLI_C)
10544 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10545 {
10546 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10547#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10548 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10549#endif
10550 }
10551#endif
10552
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010553#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010555#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556
10557#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10558 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10559#endif
10560
10561#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10562 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10563#endif
10564
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010565#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10566 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10567#endif
10568
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010569#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010570 conf->f_cookie_write = ssl_cookie_write_dummy;
10571 conf->f_cookie_check = ssl_cookie_check_dummy;
10572#endif
10573
10574#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10575 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10576#endif
10577
Janos Follath088ce432017-04-10 12:42:31 +010010578#if defined(MBEDTLS_SSL_SRV_C)
10579 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10580#endif
10581
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010582#if defined(MBEDTLS_SSL_PROTO_DTLS)
10583 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10584 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10585#endif
10586
10587#if defined(MBEDTLS_SSL_RENEGOTIATION)
10588 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010589 memset( conf->renego_period, 0x00, 2 );
10590 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010591#endif
10592
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010593#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10594 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10595 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010596 const unsigned char dhm_p[] =
10597 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10598 const unsigned char dhm_g[] =
10599 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10600
Hanno Beckera90658f2017-10-04 15:29:08 +010010601 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10602 dhm_p, sizeof( dhm_p ),
10603 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010604 {
10605 return( ret );
10606 }
10607 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010608#endif
10609
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010610 /*
10611 * Preset-specific defaults
10612 */
10613 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010614 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010615 /*
10616 * NSA Suite B
10617 */
10618 case MBEDTLS_SSL_PRESET_SUITEB:
10619 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10620 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10621 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10622 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10623
10624 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10625 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10626 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10627 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10628 ssl_preset_suiteb_ciphersuites;
10629
10630#if defined(MBEDTLS_X509_CRT_PARSE_C)
10631 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010632#endif
10633
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010634#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010635 conf->sig_hashes = ssl_preset_suiteb_hashes;
10636#endif
10637
10638#if defined(MBEDTLS_ECP_C)
10639 conf->curve_list = ssl_preset_suiteb_curves;
10640#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010641 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010642
10643 /*
10644 * Default
10645 */
10646 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010647 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10648 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10649 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10650 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10651 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10652 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10653 MBEDTLS_SSL_MIN_MINOR_VERSION :
10654 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010655 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10656 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10657
10658#if defined(MBEDTLS_SSL_PROTO_DTLS)
10659 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10660 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10661#endif
10662
10663 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10664 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10665 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10666 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10667 mbedtls_ssl_list_ciphersuites();
10668
10669#if defined(MBEDTLS_X509_CRT_PARSE_C)
10670 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10671#endif
10672
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010673#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010674 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010675#endif
10676
10677#if defined(MBEDTLS_ECP_C)
10678 conf->curve_list = mbedtls_ecp_grp_id_list();
10679#endif
10680
10681#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10682 conf->dhm_min_bitlen = 1024;
10683#endif
10684 }
10685
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010686 return( 0 );
10687}
10688
10689/*
10690 * Free mbedtls_ssl_config
10691 */
10692void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10693{
10694#if defined(MBEDTLS_DHM_C)
10695 mbedtls_mpi_free( &conf->dhm_P );
10696 mbedtls_mpi_free( &conf->dhm_G );
10697#endif
10698
10699#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10700 if( conf->psk != NULL )
10701 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010702 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010703 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010704 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010705 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010706 }
10707
10708 if( conf->psk_identity != NULL )
10709 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010710 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010711 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010712 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010713 conf->psk_identity_len = 0;
10714 }
10715#endif
10716
10717#if defined(MBEDTLS_X509_CRT_PARSE_C)
10718 ssl_key_cert_free( conf->key_cert );
10719#endif
10720
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010721 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722}
10723
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010724#if defined(MBEDTLS_PK_C) && \
10725 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010726/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731#if defined(MBEDTLS_RSA_C)
10732 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10733 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010734#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735#if defined(MBEDTLS_ECDSA_C)
10736 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10737 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010740}
10741
Hanno Becker7e5437a2017-04-28 17:15:26 +010010742unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10743{
10744 switch( type ) {
10745 case MBEDTLS_PK_RSA:
10746 return( MBEDTLS_SSL_SIG_RSA );
10747 case MBEDTLS_PK_ECDSA:
10748 case MBEDTLS_PK_ECKEY:
10749 return( MBEDTLS_SSL_SIG_ECDSA );
10750 default:
10751 return( MBEDTLS_SSL_SIG_ANON );
10752 }
10753}
10754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010756{
10757 switch( sig )
10758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759#if defined(MBEDTLS_RSA_C)
10760 case MBEDTLS_SSL_SIG_RSA:
10761 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763#if defined(MBEDTLS_ECDSA_C)
10764 case MBEDTLS_SSL_SIG_ECDSA:
10765 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010766#endif
10767 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010768 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010769 }
10770}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010771#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010772
Hanno Becker7e5437a2017-04-28 17:15:26 +010010773#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10774 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10775
10776/* Find an entry in a signature-hash set matching a given hash algorithm. */
10777mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10778 mbedtls_pk_type_t sig_alg )
10779{
10780 switch( sig_alg )
10781 {
10782 case MBEDTLS_PK_RSA:
10783 return( set->rsa );
10784 case MBEDTLS_PK_ECDSA:
10785 return( set->ecdsa );
10786 default:
10787 return( MBEDTLS_MD_NONE );
10788 }
10789}
10790
10791/* Add a signature-hash-pair to a signature-hash set */
10792void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10793 mbedtls_pk_type_t sig_alg,
10794 mbedtls_md_type_t md_alg )
10795{
10796 switch( sig_alg )
10797 {
10798 case MBEDTLS_PK_RSA:
10799 if( set->rsa == MBEDTLS_MD_NONE )
10800 set->rsa = md_alg;
10801 break;
10802
10803 case MBEDTLS_PK_ECDSA:
10804 if( set->ecdsa == MBEDTLS_MD_NONE )
10805 set->ecdsa = md_alg;
10806 break;
10807
10808 default:
10809 break;
10810 }
10811}
10812
10813/* Allow exactly one hash algorithm for each signature. */
10814void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10815 mbedtls_md_type_t md_alg )
10816{
10817 set->rsa = md_alg;
10818 set->ecdsa = md_alg;
10819}
10820
10821#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10822 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10823
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010824/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010825 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010828{
10829 switch( hash )
10830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831#if defined(MBEDTLS_MD5_C)
10832 case MBEDTLS_SSL_HASH_MD5:
10833 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010834#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010835#if defined(MBEDTLS_SHA1_C)
10836 case MBEDTLS_SSL_HASH_SHA1:
10837 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010838#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010839#if defined(MBEDTLS_SHA256_C)
10840 case MBEDTLS_SSL_HASH_SHA224:
10841 return( MBEDTLS_MD_SHA224 );
10842 case MBEDTLS_SSL_HASH_SHA256:
10843 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845#if defined(MBEDTLS_SHA512_C)
10846 case MBEDTLS_SSL_HASH_SHA384:
10847 return( MBEDTLS_MD_SHA384 );
10848 case MBEDTLS_SSL_HASH_SHA512:
10849 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010850#endif
10851 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010853 }
10854}
10855
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010856/*
10857 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10858 */
10859unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10860{
10861 switch( md )
10862 {
10863#if defined(MBEDTLS_MD5_C)
10864 case MBEDTLS_MD_MD5:
10865 return( MBEDTLS_SSL_HASH_MD5 );
10866#endif
10867#if defined(MBEDTLS_SHA1_C)
10868 case MBEDTLS_MD_SHA1:
10869 return( MBEDTLS_SSL_HASH_SHA1 );
10870#endif
10871#if defined(MBEDTLS_SHA256_C)
10872 case MBEDTLS_MD_SHA224:
10873 return( MBEDTLS_SSL_HASH_SHA224 );
10874 case MBEDTLS_MD_SHA256:
10875 return( MBEDTLS_SSL_HASH_SHA256 );
10876#endif
10877#if defined(MBEDTLS_SHA512_C)
10878 case MBEDTLS_MD_SHA384:
10879 return( MBEDTLS_SSL_HASH_SHA384 );
10880 case MBEDTLS_MD_SHA512:
10881 return( MBEDTLS_SSL_HASH_SHA512 );
10882#endif
10883 default:
10884 return( MBEDTLS_SSL_HASH_NONE );
10885 }
10886}
10887
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010888#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010889/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010890 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010891 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010892 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010893int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010895 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010896
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010897 if( ssl->conf->curve_list == NULL )
10898 return( -1 );
10899
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010900 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010901 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010902 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010903
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010904 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010905}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010906#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010907
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010908#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010909/*
10910 * Check if a hash proposed by the peer is in our list.
10911 * Return 0 if we're willing to use it, -1 otherwise.
10912 */
10913int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10914 mbedtls_md_type_t md )
10915{
10916 const int *cur;
10917
10918 if( ssl->conf->sig_hashes == NULL )
10919 return( -1 );
10920
10921 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10922 if( *cur == (int) md )
10923 return( 0 );
10924
10925 return( -1 );
10926}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010927#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929#if defined(MBEDTLS_X509_CRT_PARSE_C)
10930int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10931 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010932 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010933 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010934{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010935 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010937 int usage = 0;
10938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010940 const char *ext_oid;
10941 size_t ext_len;
10942#endif
10943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10945 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010946 ((void) cert);
10947 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010948 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010949#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10952 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010953 {
10954 /* Server part of the key exchange */
10955 switch( ciphersuite->key_exchange )
10956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957 case MBEDTLS_KEY_EXCHANGE_RSA:
10958 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010959 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010960 break;
10961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10963 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10964 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10965 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010966 break;
10967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10969 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010970 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010971 break;
10972
10973 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974 case MBEDTLS_KEY_EXCHANGE_NONE:
10975 case MBEDTLS_KEY_EXCHANGE_PSK:
10976 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10977 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010978 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010979 usage = 0;
10980 }
10981 }
10982 else
10983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10985 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010986 }
10987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010988 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010989 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010990 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010991 ret = -1;
10992 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010993#else
10994 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010997#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10998 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011000 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11001 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011002 }
11003 else
11004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11006 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011007 }
11008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011009 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011010 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011011 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011012 ret = -1;
11013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011015
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011016 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011017}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011018#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011019
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011020/*
11021 * Convert version numbers to/from wire format
11022 * and, for DTLS, to/from TLS equivalent.
11023 *
11024 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011025 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011026 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11027 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011030 unsigned char ver[2] )
11031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032#if defined(MBEDTLS_SSL_PROTO_DTLS)
11033 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011036 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11037
11038 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11039 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11040 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011041 else
11042#else
11043 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011044#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011045 {
11046 ver[0] = (unsigned char) major;
11047 ver[1] = (unsigned char) minor;
11048 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011049}
11050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011052 const unsigned char ver[2] )
11053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054#if defined(MBEDTLS_SSL_PROTO_DTLS)
11055 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011056 {
11057 *major = 255 - ver[0] + 2;
11058 *minor = 255 - ver[1] + 1;
11059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011061 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11062 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011063 else
11064#else
11065 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011066#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011067 {
11068 *major = ver[0];
11069 *minor = ver[1];
11070 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011071}
11072
Simon Butcher99000142016-10-13 17:21:01 +010011073int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11074{
11075#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11076 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11077 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11078
11079 switch( md )
11080 {
11081#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11082#if defined(MBEDTLS_MD5_C)
11083 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011084 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011085#endif
11086#if defined(MBEDTLS_SHA1_C)
11087 case MBEDTLS_SSL_HASH_SHA1:
11088 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11089 break;
11090#endif
11091#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11092#if defined(MBEDTLS_SHA512_C)
11093 case MBEDTLS_SSL_HASH_SHA384:
11094 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11095 break;
11096#endif
11097#if defined(MBEDTLS_SHA256_C)
11098 case MBEDTLS_SSL_HASH_SHA256:
11099 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11100 break;
11101#endif
11102 default:
11103 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11104 }
11105
11106 return 0;
11107#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11108 (void) ssl;
11109 (void) md;
11110
11111 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11112#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11113}
11114
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011115#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11116 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11117int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11118 unsigned char *output,
11119 unsigned char *data, size_t data_len )
11120{
11121 int ret = 0;
11122 mbedtls_md5_context mbedtls_md5;
11123 mbedtls_sha1_context mbedtls_sha1;
11124
11125 mbedtls_md5_init( &mbedtls_md5 );
11126 mbedtls_sha1_init( &mbedtls_sha1 );
11127
11128 /*
11129 * digitally-signed struct {
11130 * opaque md5_hash[16];
11131 * opaque sha_hash[20];
11132 * };
11133 *
11134 * md5_hash
11135 * MD5(ClientHello.random + ServerHello.random
11136 * + ServerParams);
11137 * sha_hash
11138 * SHA(ClientHello.random + ServerHello.random
11139 * + ServerParams);
11140 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011141 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011142 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011144 goto exit;
11145 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011146 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011147 ssl->handshake->randbytes, 64 ) ) != 0 )
11148 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011150 goto exit;
11151 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011152 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011153 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011155 goto exit;
11156 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011157 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011158 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011160 goto exit;
11161 }
11162
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011163 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011164 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011166 goto exit;
11167 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011168 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011169 ssl->handshake->randbytes, 64 ) ) != 0 )
11170 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 goto exit;
11173 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011174 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 data_len ) ) != 0 )
11176 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011178 goto exit;
11179 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011180 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011181 output + 16 ) ) != 0 )
11182 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011184 goto exit;
11185 }
11186
11187exit:
11188 mbedtls_md5_free( &mbedtls_md5 );
11189 mbedtls_sha1_free( &mbedtls_sha1 );
11190
11191 if( ret != 0 )
11192 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11193 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11194
11195 return( ret );
11196
11197}
11198#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11199 MBEDTLS_SSL_PROTO_TLS1_1 */
11200
11201#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11202 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011203
11204#if defined(MBEDTLS_USE_PSA_CRYPTO)
11205int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11206 unsigned char *hash, size_t *hashlen,
11207 unsigned char *data, size_t data_len,
11208 mbedtls_md_type_t md_alg )
11209{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011210 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011211 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011212 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11213
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011215
11216 if( ( status = psa_hash_setup( &hash_operation,
11217 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011218 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011219 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011220 goto exit;
11221 }
11222
Andrzej Kurek814feff2019-01-14 04:35:19 -050011223 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11224 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011225 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011226 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011227 goto exit;
11228 }
11229
Andrzej Kurek814feff2019-01-14 04:35:19 -050011230 if( ( status = psa_hash_update( &hash_operation,
11231 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011232 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011233 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011234 goto exit;
11235 }
11236
Andrzej Kurek814feff2019-01-14 04:35:19 -050011237 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11238 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011239 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011240 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011241 goto exit;
11242 }
11243
11244exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011245 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011246 {
11247 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11248 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011249 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011250 {
11251 case PSA_ERROR_NOT_SUPPORTED:
11252 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011253 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 case PSA_ERROR_BUFFER_TOO_SMALL:
11255 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11256 case PSA_ERROR_INSUFFICIENT_MEMORY:
11257 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11258 default:
11259 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11260 }
11261 }
11262 return( 0 );
11263}
11264
11265#else
11266
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011267int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011268 unsigned char *hash, size_t *hashlen,
11269 unsigned char *data, size_t data_len,
11270 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011271{
11272 int ret = 0;
11273 mbedtls_md_context_t ctx;
11274 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011275 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011276
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011278
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011279 mbedtls_md_init( &ctx );
11280
11281 /*
11282 * digitally-signed struct {
11283 * opaque client_random[32];
11284 * opaque server_random[32];
11285 * ServerDHParams params;
11286 * };
11287 */
11288 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11289 {
11290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11291 goto exit;
11292 }
11293 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11294 {
11295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11296 goto exit;
11297 }
11298 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11299 {
11300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11301 goto exit;
11302 }
11303 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11304 {
11305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11306 goto exit;
11307 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011308 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011309 {
11310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11311 goto exit;
11312 }
11313
11314exit:
11315 mbedtls_md_free( &ctx );
11316
11317 if( ret != 0 )
11318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11319 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11320
11321 return( ret );
11322}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011323#endif /* MBEDTLS_USE_PSA_CRYPTO */
11324
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011325#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11326 MBEDTLS_SSL_PROTO_TLS1_2 */
11327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011328#endif /* MBEDTLS_SSL_TLS_C */