blob: d16ce01d1ca17695a8f8880cca85db5e97326fe6 [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;
Hanno Beckerb1aa1b32019-05-08 17:37:58 +01002016
2017 /* MBEDTLS_SSL_CID_PADDING_GRANULARITY must be a power of 2. */
2018 size_t pad = ~len & ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - 1 );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002019
2020 /* Write real content type */
2021 if( remaining == 0 )
2022 return( -1 );
2023 content[ len ] = rec_type;
2024 len++;
2025 remaining--;
2026
2027 if( remaining < pad )
2028 return( -1 );
2029 memset( content + len, 0, pad );
2030 len += pad;
2031 remaining -= pad;
2032
2033 *content_size = len;
2034 return( 0 );
2035}
2036
Hanno Becker07dc97d2019-05-20 15:08:01 +01002037/* This function parses a DTLSInnerPlaintext structure.
2038 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002039static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2040 size_t *content_size,
2041 uint8_t *rec_type )
2042{
2043 size_t remaining = *content_size;
2044
2045 /* Determine length of padding by skipping zeroes from the back. */
2046 do
2047 {
2048 if( remaining == 0 )
2049 return( -1 );
2050 remaining--;
2051 } while( content[ remaining ] == 0 );
2052
2053 *content_size = remaining;
2054 *rec_type = content[ remaining ];
2055
2056 return( 0 );
2057}
2058#endif /* MBEDTLS_SSL_CID */
2059
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002060/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002061 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002062static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002063 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002064 mbedtls_record *rec )
2065{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002066 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002067 *
2068 * additional_data = seq_num + TLSCompressed.type +
2069 * TLSCompressed.version + TLSCompressed.length;
2070 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002071 * For the CID extension, this is extended as follows
2072 * (quoting draft-ietf-tls-dtls-connection-id-05,
2073 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002074 *
2075 * additional_data = seq_num + DTLSPlaintext.type +
2076 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002077 * cid +
2078 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002079 * length_of_DTLSInnerPlaintext;
2080 */
2081
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002082 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2083 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002084 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002085
2086#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002087 if( rec->cid_len != 0 )
2088 {
2089 memcpy( add_data + 11, rec->cid, rec->cid_len );
2090 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2091 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2092 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2093 *add_data_len = 13 + 1 + rec->cid_len;
2094 }
2095 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002096#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002097 {
2098 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2099 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2100 *add_data_len = 13;
2101 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102}
2103
Hanno Beckera18d1322018-01-03 14:27:32 +00002104int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2105 mbedtls_ssl_transform *transform,
2106 mbedtls_record *rec,
2107 int (*f_rng)(void *, unsigned char *, size_t),
2108 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002111 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002112 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002113 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002114 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002115 size_t post_avail;
2116
2117 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002118#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002119 ((void) ssl);
2120#endif
2121
2122 /* The PRNG is used for dynamic IV generation that's used
2123 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2124#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2125 ( defined(MBEDTLS_AES_C) || \
2126 defined(MBEDTLS_ARIA_C) || \
2127 defined(MBEDTLS_CAMELLIA_C) ) && \
2128 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2129 ((void) f_rng);
2130 ((void) p_rng);
2131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002136 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2139 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002140 if( rec == NULL
2141 || rec->buf == NULL
2142 || rec->buf_len < rec->data_offset
2143 || rec->buf_len - rec->data_offset < rec->data_len
2144#if defined(MBEDTLS_SSL_CID)
2145 || rec->cid_len != 0
2146#endif
2147 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002148 {
2149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002151 }
2152
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002153 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002154 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002156 data, rec->data_len );
2157
2158 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2159
2160 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2161 {
2162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2163 (unsigned) rec->data_len,
2164 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2166 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002167
Hanno Beckercab87e62019-04-29 13:52:53 +01002168#if defined(MBEDTLS_SSL_CID)
2169 /*
2170 * Add CID information
2171 */
2172 rec->cid_len = transform->out_cid_len;
2173 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2174 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002175
2176 if( rec->cid_len != 0 )
2177 {
2178 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002179 * Wrap plaintext into DTLSInnerPlaintext structure.
2180 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002181 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002182 * Note that this changes `rec->data_len`, and hence
2183 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002184 */
2185 if( ssl_cid_build_inner_plaintext( data,
2186 &rec->data_len,
2187 post_avail,
2188 rec->type ) != 0 )
2189 {
2190 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2191 }
2192
2193 rec->type = MBEDTLS_SSL_MSG_CID;
2194 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002195#endif /* MBEDTLS_SSL_CID */
2196
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002197 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2198
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002200 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 */
Hanno Becker52344c22018-01-03 15:24:20 +00002202#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 if( mode == MBEDTLS_MODE_STREAM ||
2204 ( mode == MBEDTLS_MODE_CBC
2205#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002206 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002207#endif
2208 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002210 if( post_avail < transform->maclen )
2211 {
2212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2213 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2214 }
2215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002217 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002218 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002219 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002220 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2221 data, rec->data_len, rec->ctr, rec->type, mac );
2222 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002223 }
2224 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2227 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002228 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002229 {
Hanno Becker992b6872017-11-09 18:57:39 +00002230 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2231
Hanno Beckercab87e62019-04-29 13:52:53 +01002232 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002233
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002235 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002236 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2237 data, rec->data_len );
2238 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2239 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2240
2241 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002242 }
2243 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002244#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002248 }
2249
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002250 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2251 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002253 rec->data_len += transform->maclen;
2254 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002255 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002256 }
Hanno Becker52344c22018-01-03 15:24:20 +00002257#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002259 /*
2260 * Encrypt
2261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2263 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002265 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002266 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002268 "including %d bytes of padding",
2269 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002271 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2272 transform->iv_enc, transform->ivlen,
2273 data, rec->data_len,
2274 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002277 return( ret );
2278 }
2279
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002280 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 }
Paul Bakker68884e32013-01-07 18:20:04 +01002286 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002288
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002289#if defined(MBEDTLS_GCM_C) || \
2290 defined(MBEDTLS_CCM_C) || \
2291 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002293 mode == MBEDTLS_MODE_CCM ||
2294 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002295 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002296 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002297 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002298 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002299
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002300 /* Check that there's space for both the authentication tag
2301 * and the explicit IV before and after the record content. */
2302 if( post_avail < transform->taglen ||
2303 rec->data_offset < explicit_iv_len )
2304 {
2305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2306 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2307 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002308
Paul Bakker68884e32013-01-07 18:20:04 +01002309 /*
2310 * Generate IV
2311 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002312 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2313 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002314 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002315 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002316 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2317 explicit_iv_len );
2318 /* Prefix record content with explicit IV. */
2319 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002320 }
2321 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2322 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002323 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002324 unsigned char i;
2325
2326 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2327
2328 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002329 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330 }
2331 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002332 {
2333 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002336 }
2337
Hanno Beckercab87e62019-04-29 13:52:53 +01002338 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002339
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002340 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2341 iv, transform->ivlen );
2342 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002343 data - explicit_iv_len, explicit_iv_len );
2344 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002345 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002349
Paul Bakker68884e32013-01-07 18:20:04 +01002350 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002351 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002352 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002353
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002356 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 data, rec->data_len, /* source */
2358 data, &rec->data_len, /* destination */
2359 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002362 return( ret );
2363 }
2364
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2366 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002367
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 rec->data_len += transform->taglen + explicit_iv_len;
2369 rec->data_offset -= explicit_iv_len;
2370 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002371 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2375#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002376 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002379 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 size_t padlen, i;
2381 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002382
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002383 /* Currently we're always using minimal padding
2384 * (up to 255 bytes would be allowed). */
2385 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2386 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 padlen = 0;
2388
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002389 /* Check there's enough space in the buffer for the padding. */
2390 if( post_avail < padlen + 1 )
2391 {
2392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2393 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2394 }
2395
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002397 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 rec->data_len += padlen + 1;
2400 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002403 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002404 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2405 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002407 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002408 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002409 if( f_rng == NULL )
2410 {
2411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2413 }
2414
2415 if( rec->data_offset < transform->ivlen )
2416 {
2417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2418 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2419 }
2420
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002421 /*
2422 * Generate IV
2423 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002424 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002425 if( ret != 0 )
2426 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002427
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002428 memcpy( data - transform->ivlen, transform->iv_enc,
2429 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002430
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002435 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002436 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002437 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002439 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2440 transform->iv_enc,
2441 transform->ivlen,
2442 data, rec->data_len,
2443 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002446 return( ret );
2447 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002448
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002453 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002456 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002457 {
2458 /*
2459 * Save IV in SSL3 and TLS1
2460 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002461 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2462 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002464 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002465#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002466 {
2467 data -= transform->ivlen;
2468 rec->data_offset -= transform->ivlen;
2469 rec->data_len += transform->ivlen;
2470 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002473 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002474 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002475 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2476
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002477 /*
2478 * MAC(MAC_write_key, seq_num +
2479 * TLSCipherText.type +
2480 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002481 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002482 * IV + // except for TLS 1.0
2483 * ENC(content + padding + padding_length));
2484 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002485
2486 if( post_avail < transform->maclen)
2487 {
2488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2489 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2490 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491
Hanno Beckercab87e62019-04-29 13:52:53 +01002492 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002495 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002496 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002497
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002498 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002499 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002500 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2501 data, rec->data_len );
2502 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2503 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002506
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002507 rec->data_len += transform->maclen;
2508 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002509 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002510 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002513 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002515 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 /* Make extra sure authentication was performed, exactly once */
2522 if( auth_done != 1 )
2523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002526 }
2527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
2530 return( 0 );
2531}
2532
Hanno Beckera18d1322018-01-03 14:27:32 +00002533int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2534 mbedtls_ssl_transform *transform,
2535 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002536{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002537 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002539 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002540#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002541 size_t padlen = 0, correct = 1;
2542#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002543 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002544 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002545 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546
Hanno Beckera18d1322018-01-03 14:27:32 +00002547#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002548 ((void) ssl);
2549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002552 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002553 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2556 }
2557 if( rec == NULL ||
2558 rec->buf == NULL ||
2559 rec->buf_len < rec->data_offset ||
2560 rec->buf_len - rec->data_offset < rec->data_len )
2561 {
2562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002564 }
2565
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002566 data = rec->buf + rec->data_offset;
2567 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Hanno Beckercab87e62019-04-29 13:52:53 +01002569#if defined(MBEDTLS_SSL_CID)
2570 /*
2571 * Match record's CID with incoming CID.
2572 */
2573
Hanno Becker938489a2019-05-08 13:02:22 +01002574 if( rec->cid_len != transform->in_cid_len ||
2575 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2576 {
Hanno Becker16ded982019-05-08 13:02:55 +01002577 /* Silently skip over record with mismatching CID. */
2578 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker938489a2019-05-08 13:02:22 +01002579 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002580#endif /* MBEDTLS_SSL_CID */
2581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2583 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002584 {
2585 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002586 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2587 transform->iv_dec,
2588 transform->ivlen,
2589 data, rec->data_len,
2590 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002593 return( ret );
2594 }
2595
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002596 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2599 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
Paul Bakker68884e32013-01-07 18:20:04 +01002602 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604#if defined(MBEDTLS_GCM_C) || \
2605 defined(MBEDTLS_CCM_C) || \
2606 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002608 mode == MBEDTLS_MODE_CCM ||
2609 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002610 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002611 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002612 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002613
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002614 /*
2615 * Compute and update sizes
2616 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002620 "+ taglen (%d)", rec->data_len,
2621 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002623 }
Paul Bakker68884e32013-01-07 18:20:04 +01002624
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002625 /*
2626 * Prepare IV
2627 */
2628 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2629 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002630 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002631 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002632 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002633
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002634 }
2635 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2636 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002637 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002638 unsigned char i;
2639
2640 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2641
2642 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002643 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002644 }
2645 else
2646 {
2647 /* Reminder if we ever add an AEAD mode with a different size */
2648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2650 }
2651
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 data += explicit_iv_len;
2653 rec->data_offset += explicit_iv_len;
2654 rec->data_len -= explicit_iv_len + transform->taglen;
2655
Hanno Beckercab87e62019-04-29 13:52:53 +01002656 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002658 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659
2660 memcpy( transform->iv_dec + transform->fixed_ivlen,
2661 data - explicit_iv_len, explicit_iv_len );
2662
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002663 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002665 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002666
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002667
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002668 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002669 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002670 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2672 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002673 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002674 data, rec->data_len,
2675 data, &olen,
2676 data + rec->data_len,
2677 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2682 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002683
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002684 return( ret );
2685 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002686 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002687
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002692 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2696#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002697 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002700 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002701
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 /*
Paul Bakker45829992013-01-03 14:52:21 +01002703 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002706 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2707 {
2708 /* The ciphertext is prefixed with the CBC IV. */
2709 minlen += transform->ivlen;
2710 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002711#endif
Paul Bakker45829992013-01-03 14:52:21 +01002712
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002713 /* Size considerations:
2714 *
2715 * - The CBC cipher text must not be empty and hence
2716 * at least of size transform->ivlen.
2717 *
2718 * Together with the potential IV-prefix, this explains
2719 * the first of the two checks below.
2720 *
2721 * - The record must contain a MAC, either in plain or
2722 * encrypted, depending on whether Encrypt-then-MAC
2723 * is used or not.
2724 * - If it is, the message contains the IV-prefix,
2725 * the CBC ciphertext, and the MAC.
2726 * - If it is not, the padded plaintext, and hence
2727 * the CBC ciphertext, has at least length maclen + 1
2728 * because there is at least the padding length byte.
2729 *
2730 * As the CBC ciphertext is not empty, both cases give the
2731 * lower bound minlen + maclen + 1 on the record size, which
2732 * we test for in the second check below.
2733 */
2734 if( rec->data_len < minlen + transform->ivlen ||
2735 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002738 "+ 1 ) ( + expl IV )", rec->data_len,
2739 transform->ivlen,
2740 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002742 }
2743
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002744 /*
2745 * Authenticate before decrypt if enabled
2746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002748 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002749 {
Hanno Becker992b6872017-11-09 18:57:39 +00002750 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002753
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002754 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2755 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002756
Hanno Beckercab87e62019-04-29 13:52:53 +01002757 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002758
Hanno Beckercab87e62019-04-29 13:52:53 +01002759 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2760 add_data_len );
2761 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2762 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002763 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2764 data, rec->data_len );
2765 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2766 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002767
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2769 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002770 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002772
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002773 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2774 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002779 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782
2783 /*
2784 * Check length sanity
2785 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002786 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002791 }
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002794 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002795 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002796 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002797 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002798 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002799 /* This is safe because data_len >= minlen + maclen + 1 initially,
2800 * and at this point we have at most subtracted maclen (note that
2801 * minlen == transform->ivlen here). */
2802 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002804 data += transform->ivlen;
2805 rec->data_offset += transform->ivlen;
2806 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002807 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002809
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2811 transform->iv_dec, transform->ivlen,
2812 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002815 return( ret );
2816 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002817
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002822 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002826 {
2827 /*
2828 * Save IV in SSL3 and TLS1
2829 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2831 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002835 /* Safe since data_len >= minlen + maclen + 1, so after having
2836 * subtracted at most minlen and maclen up to this point,
2837 * data_len > 0. */
2838 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002839
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002840 if( auth_done == 1 )
2841 {
2842 correct *= ( rec->data_len >= padlen + 1 );
2843 padlen *= ( rec->data_len >= padlen + 1 );
2844 }
2845 else
Paul Bakker45829992013-01-03 14:52:21 +01002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002848 if( rec->data_len < transform->maclen + padlen + 1 )
2849 {
2850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2851 rec->data_len,
2852 transform->maclen,
2853 padlen + 1 ) );
2854 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002855#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856
2857 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2858 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002861 padlen++;
2862
2863 /* Regardless of the validity of the padding,
2864 * we have data_len >= padlen here. */
2865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002867 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_SSL_DEBUG_ALL)
2872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002873 "should be no more than %d",
2874 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002875#endif
Paul Bakker45829992013-01-03 14:52:21 +01002876 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 }
2878 }
2879 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2881#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2882 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002885 /* The padding check involves a series of up to 256
2886 * consecutive memory reads at the end of the record
2887 * plaintext buffer. In order to hide the length and
2888 * validity of the padding, always perform exactly
2889 * `min(256,plaintext_len)` reads (but take into account
2890 * only the last `padlen` bytes for the padding check). */
2891 size_t pad_count = 0;
2892 size_t real_count = 0;
2893 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002894
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002895 /* Index of first padding byte; it has been ensured above
2896 * that the subtraction is safe. */
2897 size_t const padding_idx = rec->data_len - padlen;
2898 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2899 size_t const start_idx = rec->data_len - num_checks;
2900 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002901
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002902 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002903 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904 real_count |= ( idx >= padding_idx );
2905 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002906 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002907 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002910 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002912#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002913 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2917 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002921 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002922
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002923 /* If the padding was found to be invalid, padlen == 0
2924 * and the subtraction is safe. If the padding was found valid,
2925 * padlen hasn't been changed and the previous assertion
2926 * data_len >= padlen still holds. */
2927 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002931 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002937#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002939 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
2942 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002943 * Authenticate if not done yet.
2944 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 */
Hanno Becker52344c22018-01-03 15:24:20 +00002946#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002947 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002948 {
Hanno Becker992b6872017-11-09 18:57:39 +00002949 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002950
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 /* If the initial value of padlen was such that
2952 * data_len < maclen + padlen + 1, then padlen
2953 * got reset to 1, and the initial check
2954 * data_len >= minlen + maclen + 1
2955 * guarantees that at this point we still
2956 * have at least data_len >= maclen.
2957 *
2958 * If the initial value of padlen was such that
2959 * data_len >= maclen + padlen + 1, then we have
2960 * subtracted either padlen + 1 (if the padding was correct)
2961 * or 0 (if the padding was incorrect) since then,
2962 * hence data_len >= maclen in any case.
2963 */
2964 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Hanno Beckercab87e62019-04-29 13:52:53 +01002966 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002969 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002970 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 ssl_mac( &transform->md_ctx_dec,
2972 transform->mac_dec,
2973 data, rec->data_len,
2974 rec->ctr, rec->type,
2975 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002976 }
2977 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2979#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2980 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002981 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002982 {
2983 /*
2984 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002985 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002986 *
2987 * Known timing attacks:
2988 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2989 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002990 * To compensate for different timings for the MAC calculation
2991 * depending on how much padding was removed (which is determined
2992 * by padlen), process extra_run more blocks through the hash
2993 * function.
2994 *
2995 * The formula in the paper is
2996 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2997 * where L1 is the size of the header plus the decrypted message
2998 * plus CBC padding and L2 is the size of the header plus the
2999 * decrypted message. This is for an underlying hash function
3000 * with 64-byte blocks.
3001 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3002 * correctly. We round down instead of up, so -56 is the correct
3003 * value for our calculations instead of -55.
3004 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003005 * Repeat the formula rather than defining a block_size variable.
3006 * This avoids requiring division by a variable at runtime
3007 * (which would be marginally less efficient and would require
3008 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003009 */
3010 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003011 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003012
3013 /*
3014 * The next two sizes are the minimum and maximum values of
3015 * in_msglen over all padlen values.
3016 *
3017 * They're independent of padlen, since we previously did
3018 * in_msglen -= padlen.
3019 *
3020 * Note that max_len + maclen is never more than the buffer
3021 * length, as we previously did in_msglen -= maclen too.
3022 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003023 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003024 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3025
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003026 memset( tmp, 0, sizeof( tmp ) );
3027
3028 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003029 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003030#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3031 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003032 case MBEDTLS_MD_MD5:
3033 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003034 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003035 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003036 extra_run =
3037 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3038 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003039 break;
3040#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003041#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003042 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003044 extra_run =
3045 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3046 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003047 break;
3048#endif
3049 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3052 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003053
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003054 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003055
Hanno Beckercab87e62019-04-29 13:52:53 +01003056 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3057 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003058 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3059 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003060 /* Make sure we access everything even when padlen > 0. This
3061 * makes the synchronisation requirements for just-in-time
3062 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003063 ssl_read_memory( data + rec->data_len, padlen );
3064 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003065
3066 /* Call mbedtls_md_process at least once due to cache attacks
3067 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003068 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003069 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003070
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003071 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003072
3073 /* Make sure we access all the memory that could contain the MAC,
3074 * before we check it in the next code block. This makes the
3075 * synchronisation requirements for just-in-time Prime+Probe
3076 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003077 ssl_read_memory( data + min_len,
3078 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003079 }
3080 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3082 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003088#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003089 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3090 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003093 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3094 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_DEBUG_ALL)
3097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003098#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003099 correct = 0;
3100 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003101 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003102 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003103
3104 /*
3105 * Finally check the correct flag
3106 */
3107 if( correct == 0 )
3108 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003109#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003110
3111 /* Make extra sure authentication was performed, exactly once */
3112 if( auth_done != 1 )
3113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3115 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003118#if defined(MBEDTLS_SSL_CID)
3119 if( rec->cid_len != 0 )
3120 {
3121 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3122 &rec->type );
3123 if( ret != 0 )
3124 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3125 }
3126#endif /* MBEDTLS_SSL_CID */
3127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003129
3130 return( 0 );
3131}
3132
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003133#undef MAC_NONE
3134#undef MAC_PLAINTEXT
3135#undef MAC_CIPHERTEXT
3136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003138/*
3139 * Compression/decompression functions
3140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003142{
3143 int ret;
3144 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003145 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003146 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003147 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003150
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003151 if( len_pre == 0 )
3152 return( 0 );
3153
Paul Bakker2770fbd2012-07-03 13:30:23 +00003154 memcpy( msg_pre, ssl->out_msg, len_pre );
3155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003157 ssl->out_msglen ) );
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003160 ssl->out_msg, ssl->out_msglen );
3161
Paul Bakker48916f92012-09-16 19:57:18 +00003162 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3163 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3164 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003165 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003166
Paul Bakker48916f92012-09-16 19:57:18 +00003167 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168 if( ret != Z_OK )
3169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3171 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003172 }
3173
Angus Grattond8213d02016-05-25 20:56:48 +10003174 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003175 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003178 ssl->out_msglen ) );
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181 ssl->out_msg, ssl->out_msglen );
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003184
3185 return( 0 );
3186}
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189{
3190 int ret;
3191 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003192 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003194 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003198 if( len_pre == 0 )
3199 return( 0 );
3200
Paul Bakker2770fbd2012-07-03 13:30:23 +00003201 memcpy( msg_pre, ssl->in_msg, len_pre );
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003204 ssl->in_msglen ) );
3205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003207 ssl->in_msg, ssl->in_msglen );
3208
Paul Bakker48916f92012-09-16 19:57:18 +00003209 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3210 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3211 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003212 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003213 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214
Paul Bakker48916f92012-09-16 19:57:18 +00003215 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216 if( ret != Z_OK )
3217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3219 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003220 }
3221
Angus Grattond8213d02016-05-25 20:56:48 +10003222 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003223 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003226 ssl->in_msglen ) );
3227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003229 ssl->in_msg, ssl->in_msglen );
3230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003232
3233 return( 0 );
3234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3238static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#if defined(MBEDTLS_SSL_PROTO_DTLS)
3241static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003242{
3243 /* If renegotiation is not enforced, retransmit until we would reach max
3244 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003245 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003246 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003247 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003248 unsigned char doublings = 1;
3249
3250 while( ratio != 0 )
3251 {
3252 ++doublings;
3253 ratio >>= 1;
3254 }
3255
3256 if( ++ssl->renego_records_seen > doublings )
3257 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003259 return( 0 );
3260 }
3261 }
3262
3263 return( ssl_write_hello_request( ssl ) );
3264}
3265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003267
Paul Bakker5121ce52009-01-03 21:22:43 +00003268/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003269 * Fill the input message buffer by appending data to it.
3270 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003271 *
3272 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3273 * available (from this read and/or a previous one). Otherwise, an error code
3274 * is returned (possibly EOF or WANT_READ).
3275 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003276 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3277 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3278 * since we always read a whole datagram at once.
3279 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003280 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003281 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003284{
Paul Bakker23986e52011-04-24 08:57:21 +00003285 int ret;
3286 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003289
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003290 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003293 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003295 }
3296
Angus Grattond8213d02016-05-25 20:56:48 +10003297 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003301 }
3302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003306 uint32_t timeout;
3307
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003308 /* Just to be sure */
3309 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3310 {
3311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3312 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3314 }
3315
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003316 /*
3317 * The point is, we need to always read a full datagram at once, so we
3318 * sometimes read more then requested, and handle the additional data.
3319 * It could be the rest of the current record (while fetching the
3320 * header) and/or some other records in the same datagram.
3321 */
3322
3323 /*
3324 * Move to the next record in the already read datagram if applicable
3325 */
3326 if( ssl->next_record_offset != 0 )
3327 {
3328 if( ssl->in_left < ssl->next_record_offset )
3329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003332 }
3333
3334 ssl->in_left -= ssl->next_record_offset;
3335
3336 if( ssl->in_left != 0 )
3337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003339 ssl->next_record_offset ) );
3340 memmove( ssl->in_hdr,
3341 ssl->in_hdr + ssl->next_record_offset,
3342 ssl->in_left );
3343 }
3344
3345 ssl->next_record_offset = 0;
3346 }
3347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003350
3351 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003352 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003353 */
3354 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003357 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003358 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003359
3360 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003361 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003362 * are not at the beginning of a new record, the caller did something
3363 * wrong.
3364 */
3365 if( ssl->in_left != 0 )
3366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003369 }
3370
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003371 /*
3372 * Don't even try to read if time's out already.
3373 * This avoids by-passing the timer when repeatedly receiving messages
3374 * that will end up being dropped.
3375 */
3376 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003377 {
3378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003379 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003380 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003381 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003382 {
Angus Grattond8213d02016-05-25 20:56:48 +10003383 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003386 timeout = ssl->handshake->retransmit_timeout;
3387 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003388 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003391
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003392 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003393 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3394 timeout );
3395 else
3396 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399
3400 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003402 }
3403
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003404 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003410 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003411 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003414 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003415 }
3416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003420 return( ret );
3421 }
3422
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003423 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003424 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003426 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003428 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003429 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003432 return( ret );
3433 }
3434
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003435 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003436 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003438 }
3439
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 if( ret < 0 )
3441 return( ret );
3442
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003443 ssl->in_left = ret;
3444 }
3445 else
3446#endif
3447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003449 ssl->in_left, nb_want ) );
3450
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003451 while( ssl->in_left < nb_want )
3452 {
3453 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003454
3455 if( ssl_check_timer( ssl ) != 0 )
3456 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3457 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003458 {
3459 if( ssl->f_recv_timeout != NULL )
3460 {
3461 ret = ssl->f_recv_timeout( ssl->p_bio,
3462 ssl->in_hdr + ssl->in_left, len,
3463 ssl->conf->read_timeout );
3464 }
3465 else
3466 {
3467 ret = ssl->f_recv( ssl->p_bio,
3468 ssl->in_hdr + ssl->in_left, len );
3469 }
3470 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003473 ssl->in_left, nb_want ) );
3474 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003475
3476 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003478
3479 if( ret < 0 )
3480 return( ret );
3481
mohammad160352aecb92018-03-28 23:41:40 -07003482 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003483 {
Darryl Green11999bb2018-03-13 15:22:58 +00003484 MBEDTLS_SSL_DEBUG_MSG( 1,
3485 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003486 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3488 }
3489
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003490 ssl->in_left += ret;
3491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003492 }
3493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003495
3496 return( 0 );
3497}
3498
3499/*
3500 * Flush any data not yet written
3501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003503{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003504 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003505 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003509 if( ssl->f_send == NULL )
3510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003512 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003514 }
3515
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003516 /* Avoid incrementing counter if data is flushed */
3517 if( ssl->out_left == 0 )
3518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003520 return( 0 );
3521 }
3522
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 while( ssl->out_left > 0 )
3524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003526 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Hanno Becker2b1e3542018-08-06 11:19:13 +01003528 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003529 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
3533 if( ret <= 0 )
3534 return( ret );
3535
mohammad160352aecb92018-03-28 23:41:40 -07003536 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003537 {
Darryl Green11999bb2018-03-13 15:22:58 +00003538 MBEDTLS_SSL_DEBUG_MSG( 1,
3539 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003540 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3542 }
3543
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 ssl->out_left -= ret;
3545 }
3546
Hanno Becker2b1e3542018-08-06 11:19:13 +01003547#if defined(MBEDTLS_SSL_PROTO_DTLS)
3548 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003549 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003550 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003551 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003552 else
3553#endif
3554 {
3555 ssl->out_hdr = ssl->out_buf + 8;
3556 }
3557 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
3561 return( 0 );
3562}
3563
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003564/*
3565 * Functions to handle the DTLS retransmission state machine
3566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003568/*
3569 * Append current handshake message to current outgoing flight
3570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3575 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3576 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577
3578 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003579 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003583 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003584 }
3585
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003586 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003590 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003591 }
3592
3593 /* Copy current handshake message with headers */
3594 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3595 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003596 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597 msg->next = NULL;
3598
3599 /* Append to the current flight */
3600 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003601 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003602 else
3603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605 while( cur->next != NULL )
3606 cur = cur->next;
3607 cur->next = msg;
3608 }
3609
Hanno Becker3b235902018-08-06 09:54:53 +01003610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611 return( 0 );
3612}
3613
3614/*
3615 * Free the current flight of handshake messages
3616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 mbedtls_ssl_flight_item *cur = flight;
3620 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003621
3622 while( cur != NULL )
3623 {
3624 next = cur->next;
3625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 mbedtls_free( cur->p );
3627 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003628
3629 cur = next;
3630 }
3631}
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3634static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003635#endif
3636
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003637/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003638 * Swap transform_out and out_ctr with the alternative ones
3639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003643 unsigned char tmp_out_ctr[8];
3644
3645 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003648 return;
3649 }
3650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003652
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003653 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003654 tmp_transform = ssl->transform_out;
3655 ssl->transform_out = ssl->handshake->alt_transform_out;
3656 ssl->handshake->alt_transform_out = tmp_transform;
3657
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003658 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003659 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3660 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003661 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003662
3663 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003664 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3667 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003673 }
3674 }
3675#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003676}
3677
3678/*
3679 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003680 */
3681int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3682{
3683 int ret = 0;
3684
3685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3686
3687 ret = mbedtls_ssl_flight_transmit( ssl );
3688
3689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3690
3691 return( ret );
3692}
3693
3694/*
3695 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696 *
3697 * Need to remember the current message in case flush_output returns
3698 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003699 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003700 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003701int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003702{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003703 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003707 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003709
3710 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003711 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003712 ssl_swap_epochs( ssl );
3713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003715 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716
3717 while( ssl->handshake->cur_msg != NULL )
3718 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003719 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003720 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003721
Hanno Beckere1dcb032018-08-17 16:47:58 +01003722 int const is_finished =
3723 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3724 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3725
Hanno Becker04da1892018-08-14 13:22:10 +01003726 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3727 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3728
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003729 /* Swap epochs before sending Finished: we can't do it after
3730 * sending ChangeCipherSpec, in case write returns WANT_READ.
3731 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003732 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003733 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003735 ssl_swap_epochs( ssl );
3736 }
3737
Hanno Becker67bc7c32018-08-06 11:33:50 +01003738 ret = ssl_get_remaining_payload_in_datagram( ssl );
3739 if( ret < 0 )
3740 return( ret );
3741 max_frag_len = (size_t) ret;
3742
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003743 /* CCS is copied as is, while HS messages may need fragmentation */
3744 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3745 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003746 if( max_frag_len == 0 )
3747 {
3748 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3749 return( ret );
3750
3751 continue;
3752 }
3753
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003754 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003755 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003756 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003757
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003758 /* Update position inside current message */
3759 ssl->handshake->cur_msg_p += cur->len;
3760 }
3761 else
3762 {
3763 const unsigned char * const p = ssl->handshake->cur_msg_p;
3764 const size_t hs_len = cur->len - 12;
3765 const size_t frag_off = p - ( cur->p + 12 );
3766 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003767 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003768
Hanno Beckere1dcb032018-08-17 16:47:58 +01003769 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003770 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003771 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003772 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003773
Hanno Becker67bc7c32018-08-06 11:33:50 +01003774 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3775 return( ret );
3776
3777 continue;
3778 }
3779 max_hs_frag_len = max_frag_len - 12;
3780
3781 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3782 max_hs_frag_len : rem_len;
3783
3784 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003785 {
3786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003787 (unsigned) cur_hs_frag_len,
3788 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003789 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003790
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003791 /* Messages are stored with handshake headers as if not fragmented,
3792 * copy beginning of headers then fill fragmentation fields.
3793 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3794 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003795
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003796 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3797 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3798 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3799
Hanno Becker67bc7c32018-08-06 11:33:50 +01003800 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3801 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3802 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003803
3804 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3805
Hanno Becker3f7b9732018-08-28 09:53:25 +01003806 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003807 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3808 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003809 ssl->out_msgtype = cur->type;
3810
3811 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003813 }
3814
3815 /* If done with the current message move to the next one if any */
3816 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3817 {
3818 if( cur->next != NULL )
3819 {
3820 ssl->handshake->cur_msg = cur->next;
3821 ssl->handshake->cur_msg_p = cur->next->p + 12;
3822 }
3823 else
3824 {
3825 ssl->handshake->cur_msg = NULL;
3826 ssl->handshake->cur_msg_p = NULL;
3827 }
3828 }
3829
3830 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003831 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003834 return( ret );
3835 }
3836 }
3837
Hanno Becker67bc7c32018-08-06 11:33:50 +01003838 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3839 return( ret );
3840
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003841 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3843 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003844 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003847 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3848 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003849
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003851
3852 return( 0 );
3853}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003854
3855/*
3856 * To be called when the last message of an incoming flight is received.
3857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003859{
3860 /* We won't need to resend that one any more */
3861 ssl_flight_free( ssl->handshake->flight );
3862 ssl->handshake->flight = NULL;
3863 ssl->handshake->cur_msg = NULL;
3864
3865 /* The next incoming flight will start with this msg_seq */
3866 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3867
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003868 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003869 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003870
Hanno Becker0271f962018-08-16 13:23:47 +01003871 /* Clear future message buffering structure. */
3872 ssl_buffering_free( ssl );
3873
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003874 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003875 ssl_set_timer( ssl, 0 );
3876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3878 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003881 }
3882 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003884}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003885
3886/*
3887 * To be called when the last message of an outgoing flight is send.
3888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003890{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003891 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003892 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3895 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003898 }
3899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003903
Paul Bakker5121ce52009-01-03 21:22:43 +00003904/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003905 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003906 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003907
3908/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003909 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003910 *
3911 * - fill in handshake headers
3912 * - update handshake checksum
3913 * - DTLS: save message for resending
3914 * - then pass to the record layer
3915 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003916 * DTLS: except for HelloRequest, messages are only queued, and will only be
3917 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003918 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003919 * Inputs:
3920 * - ssl->out_msglen: 4 + actual handshake message len
3921 * (4 is the size of handshake headers for TLS)
3922 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3923 * - ssl->out_msg + 4: the handshake message body
3924 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003925 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926 * - ssl->out_msglen: the length of the record contents
3927 * (including handshake headers but excluding record headers)
3928 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003929 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003930int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003931{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003932 int ret;
3933 const size_t hs_len = ssl->out_msglen - 4;
3934 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003935
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3937
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003938 /*
3939 * Sanity checks
3940 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003941 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003942 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3943 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003944 /* In SSLv3, the client might send a NoCertificate alert. */
3945#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3946 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3947 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3948 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3949#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3950 {
3951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3953 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003956 /* Whenever we send anything different from a
3957 * HelloRequest we should be in a handshake - double check. */
3958 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3959 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003960 ssl->handshake == NULL )
3961 {
3962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3963 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3964 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003967 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003973 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003974#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003975
Hanno Beckerb50a2532018-08-06 11:52:54 +01003976 /* Double-check that we did not exceed the bounds
3977 * of the outgoing record buffer.
3978 * This should never fail as the various message
3979 * writing functions must obey the bounds of the
3980 * outgoing record buffer, but better be safe.
3981 *
3982 * Note: We deliberately do not check for the MTU or MFL here.
3983 */
3984 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3985 {
3986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3987 "size %u, maximum %u",
3988 (unsigned) ssl->out_msglen,
3989 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3991 }
3992
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003993 /*
3994 * Fill handshake headers
3995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003997 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003998 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3999 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4000 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004001
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004002 /*
4003 * DTLS has additional fields in the Handshake layer,
4004 * between the length field and the actual payload:
4005 * uint16 message_seq;
4006 * uint24 fragment_offset;
4007 * uint24 fragment_length;
4008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004010 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004011 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004012 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004013 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004014 {
4015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4016 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004017 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004018 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4020 }
4021
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004022 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004023 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004024
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004025 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004026 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004027 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004028 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4029 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4030 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004031 }
4032 else
4033 {
4034 ssl->out_msg[4] = 0;
4035 ssl->out_msg[5] = 0;
4036 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004037
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004038 /* Handshake hashes are computed without fragmentation,
4039 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004040 memset( ssl->out_msg + 6, 0x00, 3 );
4041 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004044
Hanno Becker0207e532018-08-28 10:28:28 +01004045 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004046 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4047 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 }
4049
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004050 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004052 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004053 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4054 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004055 {
4056 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059 return( ret );
4060 }
4061 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004062 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004063#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004064 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004065 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004066 {
4067 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4068 return( ret );
4069 }
4070 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004071
4072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4073
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004074 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004075}
4076
4077/*
4078 * Record layer functions
4079 */
4080
4081/*
4082 * Write current record.
4083 *
4084 * Uses:
4085 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4086 * - ssl->out_msglen: length of the record content (excl headers)
4087 * - ssl->out_msg: record content
4088 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004089int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004090{
4091 int ret, done = 0;
4092 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004093 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004094
4095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004098 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004100 {
4101 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004104 return( ret );
4105 }
4106
4107 len = ssl->out_msglen;
4108 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4112 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 ret = mbedtls_ssl_hw_record_write( ssl );
4117 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4120 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004121 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004122
4123 if( ret == 0 )
4124 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004125 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004127 if( !done )
4128 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004129 unsigned i;
4130 size_t protected_record_size;
4131
Hanno Becker6430faf2019-05-08 11:57:13 +01004132 /* Skip writing the record content type to after the encryption,
4133 * as it may change when using the CID extension. */
4134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004136 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004137
Hanno Becker19859472018-08-06 09:40:20 +01004138 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004139 ssl->out_len[0] = (unsigned char)( len >> 8 );
4140 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004141
Paul Bakker48916f92012-09-16 19:57:18 +00004142 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004143 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004144 mbedtls_record rec;
4145
4146 rec.buf = ssl->out_iv;
4147 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4148 ( ssl->out_iv - ssl->out_buf );
4149 rec.data_len = ssl->out_msglen;
4150 rec.data_offset = ssl->out_msg - rec.buf;
4151
4152 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4153 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4154 ssl->conf->transport, rec.ver );
4155 rec.type = ssl->out_msgtype;
4156
Hanno Becker43c24b82019-05-01 09:45:57 +01004157#if defined(MBEDTLS_SSL_CID)
4158 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004159 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004160#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004161
Hanno Beckera18d1322018-01-03 14:27:32 +00004162 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004163 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004166 return( ret );
4167 }
4168
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004169 if( rec.data_offset != 0 )
4170 {
4171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4173 }
4174
Hanno Becker6430faf2019-05-08 11:57:13 +01004175 /* Update the record content type and CID. */
4176 ssl->out_msgtype = rec.type;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004177#if defined(MBEDTLS_SSL_CID )
4178 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
4179#endif /* MBEDTLS_SSL_CID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004180 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004181 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4182 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004183 }
4184
Hanno Becker5903de42019-05-03 14:46:38 +01004185 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004186
4187#if defined(MBEDTLS_SSL_PROTO_DTLS)
4188 /* In case of DTLS, double-check that we don't exceed
4189 * the remaining space in the datagram. */
4190 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4191 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004192 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004193 if( ret < 0 )
4194 return( ret );
4195
4196 if( protected_record_size > (size_t) ret )
4197 {
4198 /* Should never happen */
4199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4200 }
4201 }
4202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004203
Hanno Becker6430faf2019-05-08 11:57:13 +01004204 /* Now write the potentially updated record content type. */
4205 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004208 "version = [%d:%d], msglen = %d",
4209 ssl->out_hdr[0], ssl->out_hdr[1],
4210 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004213 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004214
4215 ssl->out_left += protected_record_size;
4216 ssl->out_hdr += protected_record_size;
4217 ssl_update_out_pointers( ssl, ssl->transform_out );
4218
Hanno Becker04484622018-08-06 09:49:38 +01004219 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4220 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4221 break;
4222
4223 /* The loop goes to its end iff the counter is wrapping */
4224 if( i == ssl_ep_len( ssl ) )
4225 {
4226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4227 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 }
4230
Hanno Becker67bc7c32018-08-06 11:33:50 +01004231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004232 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4233 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004234 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004235 size_t remaining;
4236 ret = ssl_get_remaining_payload_in_datagram( ssl );
4237 if( ret < 0 )
4238 {
4239 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4240 ret );
4241 return( ret );
4242 }
4243
4244 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004245 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004246 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004247 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004248 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004249 else
4250 {
Hanno Becker513815a2018-08-20 11:56:09 +01004251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004252 }
4253 }
4254#endif /* MBEDTLS_SSL_PROTO_DTLS */
4255
4256 if( ( flush == SSL_FORCE_FLUSH ) &&
4257 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 return( ret );
4261 }
4262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004264
4265 return( 0 );
4266}
4267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004269
4270static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4271{
4272 if( ssl->in_msglen < ssl->in_hslen ||
4273 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4274 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4275 {
4276 return( 1 );
4277 }
4278 return( 0 );
4279}
Hanno Becker44650b72018-08-16 12:51:11 +01004280
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004281static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004282{
4283 return( ( ssl->in_msg[9] << 16 ) |
4284 ( ssl->in_msg[10] << 8 ) |
4285 ssl->in_msg[11] );
4286}
4287
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004288static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004289{
4290 return( ( ssl->in_msg[6] << 16 ) |
4291 ( ssl->in_msg[7] << 8 ) |
4292 ssl->in_msg[8] );
4293}
4294
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004295static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004296{
4297 uint32_t msg_len, frag_off, frag_len;
4298
4299 msg_len = ssl_get_hs_total_len( ssl );
4300 frag_off = ssl_get_hs_frag_off( ssl );
4301 frag_len = ssl_get_hs_frag_len( ssl );
4302
4303 if( frag_off > msg_len )
4304 return( -1 );
4305
4306 if( frag_len > msg_len - frag_off )
4307 return( -1 );
4308
4309 if( frag_len + 12 > ssl->in_msglen )
4310 return( -1 );
4311
4312 return( 0 );
4313}
4314
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004315/*
4316 * Mark bits in bitmask (used for DTLS HS reassembly)
4317 */
4318static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4319{
4320 unsigned int start_bits, end_bits;
4321
4322 start_bits = 8 - ( offset % 8 );
4323 if( start_bits != 8 )
4324 {
4325 size_t first_byte_idx = offset / 8;
4326
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004327 /* Special case */
4328 if( len <= start_bits )
4329 {
4330 for( ; len != 0; len-- )
4331 mask[first_byte_idx] |= 1 << ( start_bits - len );
4332
4333 /* Avoid potential issues with offset or len becoming invalid */
4334 return;
4335 }
4336
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004337 offset += start_bits; /* Now offset % 8 == 0 */
4338 len -= start_bits;
4339
4340 for( ; start_bits != 0; start_bits-- )
4341 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4342 }
4343
4344 end_bits = len % 8;
4345 if( end_bits != 0 )
4346 {
4347 size_t last_byte_idx = ( offset + len ) / 8;
4348
4349 len -= end_bits; /* Now len % 8 == 0 */
4350
4351 for( ; end_bits != 0; end_bits-- )
4352 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4353 }
4354
4355 memset( mask + offset / 8, 0xFF, len / 8 );
4356}
4357
4358/*
4359 * Check that bitmask is full
4360 */
4361static int ssl_bitmask_check( unsigned char *mask, size_t len )
4362{
4363 size_t i;
4364
4365 for( i = 0; i < len / 8; i++ )
4366 if( mask[i] != 0xFF )
4367 return( -1 );
4368
4369 for( i = 0; i < len % 8; i++ )
4370 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4371 return( -1 );
4372
4373 return( 0 );
4374}
4375
Hanno Becker56e205e2018-08-16 09:06:12 +01004376/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004377static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004378 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004379{
Hanno Becker56e205e2018-08-16 09:06:12 +01004380 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004381
Hanno Becker56e205e2018-08-16 09:06:12 +01004382 alloc_len = 12; /* Handshake header */
4383 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004384
Hanno Beckerd07df862018-08-16 09:14:58 +01004385 if( add_bitmap )
4386 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004387
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004388 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004389}
Hanno Becker56e205e2018-08-16 09:06:12 +01004390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004392
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004393static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004394{
4395 return( ( ssl->in_msg[1] << 16 ) |
4396 ( ssl->in_msg[2] << 8 ) |
4397 ssl->in_msg[3] );
4398}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004399
Simon Butcher99000142016-10-13 17:21:01 +01004400int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004405 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004407 }
4408
Hanno Becker12555c62018-08-16 12:47:53 +01004409 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004412 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004413 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004417 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004418 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004419 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004420
Hanno Becker44650b72018-08-16 12:51:11 +01004421 if( ssl_check_hs_header( ssl ) != 0 )
4422 {
4423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4424 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4425 }
4426
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004427 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004428 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4429 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4430 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4431 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004432 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004433 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4434 {
4435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4436 recv_msg_seq,
4437 ssl->handshake->in_msg_seq ) );
4438 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4439 }
4440
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004441 /* Retransmit only on last message from previous flight, to avoid
4442 * too many retransmissions.
4443 * Besides, No sane server ever retransmits HelloVerifyRequest */
4444 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004448 "message_seq = %d, start_of_flight = %d",
4449 recv_msg_seq,
4450 ssl->handshake->in_flight_start_seq ) );
4451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004455 return( ret );
4456 }
4457 }
4458 else
4459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004461 "message_seq = %d, expected = %d",
4462 recv_msg_seq,
4463 ssl->handshake->in_msg_seq ) );
4464 }
4465
Hanno Becker90333da2017-10-10 11:27:13 +01004466 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004467 }
4468 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004469
Hanno Becker6d97ef52018-08-16 13:09:04 +01004470 /* Message reassembly is handled alongside buffering of future
4471 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004472 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004473 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004474 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004477 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004478 }
4479 }
4480 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004482 /* With TLS we don't handle fragmentation (for now) */
4483 if( ssl->in_msglen < ssl->in_hslen )
4484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4486 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004487 }
4488
Simon Butcher99000142016-10-13 17:21:01 +01004489 return( 0 );
4490}
4491
4492void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4493{
Hanno Becker0271f962018-08-16 13:23:47 +01004494 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004495
Hanno Becker0271f962018-08-16 13:23:47 +01004496 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004497 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004498 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004499 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004500
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004501 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004503 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004504 ssl->handshake != NULL )
4505 {
Hanno Becker0271f962018-08-16 13:23:47 +01004506 unsigned offset;
4507 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004508
Hanno Becker0271f962018-08-16 13:23:47 +01004509 /* Increment handshake sequence number */
4510 hs->in_msg_seq++;
4511
4512 /*
4513 * Clear up handshake buffering and reassembly structure.
4514 */
4515
4516 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004517 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004518
4519 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004520 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4521 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004522 offset++, hs_buf++ )
4523 {
4524 *hs_buf = *(hs_buf + 1);
4525 }
4526
4527 /* Create a fresh last entry */
4528 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004529 }
4530#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004531}
4532
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004534 * DTLS anti-replay: RFC 6347 4.1.2.6
4535 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004536 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4537 * Bit n is set iff record number in_window_top - n has been seen.
4538 *
4539 * Usually, in_window_top is the last record number seen and the lsb of
4540 * in_window is set. The only exception is the initial state (record number 0
4541 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4544static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004545{
4546 ssl->in_window_top = 0;
4547 ssl->in_window = 0;
4548}
4549
4550static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4551{
4552 return( ( (uint64_t) buf[0] << 40 ) |
4553 ( (uint64_t) buf[1] << 32 ) |
4554 ( (uint64_t) buf[2] << 24 ) |
4555 ( (uint64_t) buf[3] << 16 ) |
4556 ( (uint64_t) buf[4] << 8 ) |
4557 ( (uint64_t) buf[5] ) );
4558}
4559
4560/*
4561 * Return 0 if sequence number is acceptable, -1 otherwise
4562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004564{
4565 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4566 uint64_t bit;
4567
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004568 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004569 return( 0 );
4570
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004571 if( rec_seqnum > ssl->in_window_top )
4572 return( 0 );
4573
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004574 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004575
4576 if( bit >= 64 )
4577 return( -1 );
4578
4579 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4580 return( -1 );
4581
4582 return( 0 );
4583}
4584
4585/*
4586 * Update replay window on new validated record
4587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004589{
4590 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4591
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004592 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004593 return;
4594
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004595 if( rec_seqnum > ssl->in_window_top )
4596 {
4597 /* Update window_top and the contents of the window */
4598 uint64_t shift = rec_seqnum - ssl->in_window_top;
4599
4600 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004601 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004602 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004603 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004604 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004605 ssl->in_window |= 1;
4606 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004607
4608 ssl->in_window_top = rec_seqnum;
4609 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004610 else
4611 {
4612 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004613 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004614
4615 if( bit < 64 ) /* Always true, but be extra sure */
4616 ssl->in_window |= (uint64_t) 1 << bit;
4617 }
4618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004620
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004621#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004622/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004623static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4624
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004625/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004626 * Without any SSL context, check if a datagram looks like a ClientHello with
4627 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004628 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004629 *
4630 * - if cookie is valid, return 0
4631 * - if ClientHello looks superficially valid but cookie is not,
4632 * fill obuf and set olen, then
4633 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4634 * - otherwise return a specific error code
4635 */
4636static int ssl_check_dtls_clihlo_cookie(
4637 mbedtls_ssl_cookie_write_t *f_cookie_write,
4638 mbedtls_ssl_cookie_check_t *f_cookie_check,
4639 void *p_cookie,
4640 const unsigned char *cli_id, size_t cli_id_len,
4641 const unsigned char *in, size_t in_len,
4642 unsigned char *obuf, size_t buf_len, size_t *olen )
4643{
4644 size_t sid_len, cookie_len;
4645 unsigned char *p;
4646
4647 if( f_cookie_write == NULL || f_cookie_check == NULL )
4648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4649
4650 /*
4651 * Structure of ClientHello with record and handshake headers,
4652 * and expected values. We don't need to check a lot, more checks will be
4653 * done when actually parsing the ClientHello - skipping those checks
4654 * avoids code duplication and does not make cookie forging any easier.
4655 *
4656 * 0-0 ContentType type; copied, must be handshake
4657 * 1-2 ProtocolVersion version; copied
4658 * 3-4 uint16 epoch; copied, must be 0
4659 * 5-10 uint48 sequence_number; copied
4660 * 11-12 uint16 length; (ignored)
4661 *
4662 * 13-13 HandshakeType msg_type; (ignored)
4663 * 14-16 uint24 length; (ignored)
4664 * 17-18 uint16 message_seq; copied
4665 * 19-21 uint24 fragment_offset; copied, must be 0
4666 * 22-24 uint24 fragment_length; (ignored)
4667 *
4668 * 25-26 ProtocolVersion client_version; (ignored)
4669 * 27-58 Random random; (ignored)
4670 * 59-xx SessionID session_id; 1 byte len + sid_len content
4671 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4672 * ...
4673 *
4674 * Minimum length is 61 bytes.
4675 */
4676 if( in_len < 61 ||
4677 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4678 in[3] != 0 || in[4] != 0 ||
4679 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4680 {
4681 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4682 }
4683
4684 sid_len = in[59];
4685 if( sid_len > in_len - 61 )
4686 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4687
4688 cookie_len = in[60 + sid_len];
4689 if( cookie_len > in_len - 60 )
4690 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4691
4692 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4693 cli_id, cli_id_len ) == 0 )
4694 {
4695 /* Valid cookie */
4696 return( 0 );
4697 }
4698
4699 /*
4700 * If we get here, we've got an invalid cookie, let's prepare HVR.
4701 *
4702 * 0-0 ContentType type; copied
4703 * 1-2 ProtocolVersion version; copied
4704 * 3-4 uint16 epoch; copied
4705 * 5-10 uint48 sequence_number; copied
4706 * 11-12 uint16 length; olen - 13
4707 *
4708 * 13-13 HandshakeType msg_type; hello_verify_request
4709 * 14-16 uint24 length; olen - 25
4710 * 17-18 uint16 message_seq; copied
4711 * 19-21 uint24 fragment_offset; copied
4712 * 22-24 uint24 fragment_length; olen - 25
4713 *
4714 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4715 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4716 *
4717 * Minimum length is 28.
4718 */
4719 if( buf_len < 28 )
4720 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4721
4722 /* Copy most fields and adapt others */
4723 memcpy( obuf, in, 25 );
4724 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4725 obuf[25] = 0xfe;
4726 obuf[26] = 0xff;
4727
4728 /* Generate and write actual cookie */
4729 p = obuf + 28;
4730 if( f_cookie_write( p_cookie,
4731 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4732 {
4733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4734 }
4735
4736 *olen = p - obuf;
4737
4738 /* Go back and fill length fields */
4739 obuf[27] = (unsigned char)( *olen - 28 );
4740
4741 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4742 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4743 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4744
4745 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4746 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4747
4748 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4749}
4750
4751/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004752 * Handle possible client reconnect with the same UDP quadruplet
4753 * (RFC 6347 Section 4.2.8).
4754 *
4755 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4756 * that looks like a ClientHello.
4757 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004758 * - if the input looks like a ClientHello without cookies,
4759 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004760 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004761 * - if the input looks like a ClientHello with a valid cookie,
4762 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004763 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004764 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004765 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004766 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004767 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4768 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004769 */
4770static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4771{
4772 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004774
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004775 ret = ssl_check_dtls_clihlo_cookie(
4776 ssl->conf->f_cookie_write,
4777 ssl->conf->f_cookie_check,
4778 ssl->conf->p_cookie,
4779 ssl->cli_id, ssl->cli_id_len,
4780 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004781 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004783 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4784
4785 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004786 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004787 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004788 * If the error is permanent we'll catch it later,
4789 * if it's not, then hopefully it'll work next time. */
4790 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4791
4792 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004793 }
4794
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004795 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004796 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004797 /* Got a valid cookie, partially reset context */
4798 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4799 {
4800 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4801 return( ret );
4802 }
4803
4804 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004805 }
4806
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004807 return( ret );
4808}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004809#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004810
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004811static int ssl_check_record_type( uint8_t record_type )
4812{
4813 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4814 record_type != MBEDTLS_SSL_MSG_ALERT &&
4815 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4816 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4817 {
4818 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4819 }
4820
4821 return( 0 );
4822}
4823
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004824/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004825 * ContentType type;
4826 * ProtocolVersion version;
4827 * uint16 epoch; // DTLS only
4828 * uint48 sequence_number; // DTLS only
4829 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004830 *
4831 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004832 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004833 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4834 *
4835 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004836 * 1. proceed with the record if this function returns 0
4837 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4838 * 3. return CLIENT_RECONNECT if this function return that value
4839 * 4. drop the whole datagram if this function returns anything else.
4840 * Point 2 is needed when the peer is resending, and we have already received
4841 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004844{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004845 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004846 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004847
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004848 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004849
Paul Bakker5121ce52009-01-03 21:22:43 +00004850 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004851 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004852
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004853 /* Check record type */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004854#if defined(MBEDTLS_SSL_CID)
4855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4856 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4857 ssl->conf->cid_len != 0 )
4858 {
4859 /* Shift pointers to account for record header including CID
4860 * struct {
4861 * ContentType special_type = tls12_cid;
4862 * ProtocolVersion version;
4863 * uint16 epoch;
4864 * uint48 sequence_number;
4865 * opaque cid[cid_length]; // New field
4866 * uint16 length;
4867 * opaque enc_content[DTLSCiphertext.length];
4868 * } DTLSCiphertext;
4869 */
4870
4871 /* So far, we only support static CID lengths
4872 * fixed in the configuration. */
4873 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4874 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4875 }
4876 else
4877#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004878 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004881
4882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004883 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4884 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004885 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4886#endif /* MBEDTLS_SSL_PROTO_DTLS */
4887 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4888 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004891 }
4892
4893 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004894 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4897 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004898 }
4899
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004900 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4903 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 }
4905
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004906 /* Now that the total length of the record header is known, ensure
4907 * that the current datagram is large enough to hold it.
4908 * This would fail, for example, if we received a datagram of
4909 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4910 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4911 if( ret != 0 )
4912 {
4913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4914 return( ret );
4915 }
4916 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4917
4918 /* Parse and validate record length
4919 * This must happen after the CID parsing because
4920 * its position in the record header depends on
4921 * the presence of a CID. */
4922
4923 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004924 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004925 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4928 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004929 }
4930
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4932 "version = [%d:%d], msglen = %d",
4933 ssl->in_msgtype,
4934 major_ver, minor_ver, ssl->in_msglen ) );
4935
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004936 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004937 * DTLS-related tests.
4938 * Check epoch before checking length constraint because
4939 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4940 * message gets duplicated before the corresponding Finished message,
4941 * the second ChangeCipherSpec should be discarded because it belongs
4942 * to an old epoch, but not because its length is shorter than
4943 * the minimum record length for packets using the new record transform.
4944 * Note that these two kinds of failures are handled differently,
4945 * as an unexpected record is silently skipped but an invalid
4946 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004947 */
4948#if defined(MBEDTLS_SSL_PROTO_DTLS)
4949 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4950 {
4951 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4952
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004953 /* Check epoch (and sequence number) with DTLS */
4954 if( rec_epoch != ssl->in_epoch )
4955 {
4956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4957 "expected %d, received %d",
4958 ssl->in_epoch, rec_epoch ) );
4959
4960#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4961 /*
4962 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4963 * access the first byte of record content (handshake type), as we
4964 * have an active transform (possibly iv_len != 0), so use the
4965 * fact that the record header len is 13 instead.
4966 */
4967 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4968 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4969 rec_epoch == 0 &&
4970 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4971 ssl->in_left > 13 &&
4972 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4973 {
4974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4975 "from the same port" ) );
4976 return( ssl_handle_possible_reconnect( ssl ) );
4977 }
4978 else
4979#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004980 {
4981 /* Consider buffering the record. */
4982 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4983 {
4984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4985 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4986 }
4987
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004988 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004989 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004990 }
4991
4992#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4993 /* Replay detection only works for the current epoch */
4994 if( rec_epoch == ssl->in_epoch &&
4995 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4996 {
4997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4998 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4999 }
5000#endif
5001 }
5002#endif /* MBEDTLS_SSL_PROTO_DTLS */
5003
Hanno Becker52c6dc62017-05-26 16:07:36 +01005004
5005 /* Check length against bounds of the current transform and version */
5006 if( ssl->transform_in == NULL )
5007 {
5008 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005009 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005010 {
5011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5012 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5013 }
5014 }
5015 else
5016 {
5017 if( ssl->in_msglen < ssl->transform_in->minlen )
5018 {
5019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5020 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5021 }
5022
5023#if defined(MBEDTLS_SSL_PROTO_SSL3)
5024 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005025 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005026 {
5027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5028 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5029 }
5030#endif
5031#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5032 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5033 /*
5034 * TLS encrypted messages can have up to 256 bytes of padding
5035 */
5036 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5037 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005038 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005039 {
5040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5041 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5042 }
5043#endif
5044 }
5045
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005046 return( 0 );
5047}
Paul Bakker5121ce52009-01-03 21:22:43 +00005048
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005049/*
5050 * If applicable, decrypt (and decompress) record content
5051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005053{
5054 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005057 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5060 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 ret = mbedtls_ssl_hw_record_read( ssl );
5065 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5068 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005069 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005070
5071 if( ret == 0 )
5072 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005073 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005075 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005076 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005077 mbedtls_record rec;
5078
5079 rec.buf = ssl->in_iv;
5080 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5081 - ( ssl->in_iv - ssl->in_buf );
5082 rec.data_len = ssl->in_msglen;
5083 rec.data_offset = 0;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005084#if defined(MBEDTLS_SSL_CID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005085 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005086 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
5087#endif /* MBEDTLS_SSL_CID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005088
5089 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5090 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5091 ssl->conf->transport, rec.ver );
5092 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005093 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5094 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker16ded982019-05-08 13:02:55 +01005097 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5098 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5099
Paul Bakker5121ce52009-01-03 21:22:43 +00005100 return( ret );
5101 }
5102
Hanno Becker6430faf2019-05-08 11:57:13 +01005103 if( ssl->in_msgtype != rec.type )
5104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5106 ssl->in_msgtype, rec.type ) );
5107 }
5108
5109 /* The record content type may change during decryption,
5110 * so re-read it. */
5111 ssl->in_msgtype = rec.type;
5112 /* Also update the input buffer, because unfortunately
5113 * the server-side ssl_parse_client_hello() reparses the
5114 * record header when receiving a ClientHello initiating
5115 * a renegotiation. */
5116 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005117 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005118 ssl->in_msglen = rec.data_len;
5119 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5120 ssl->in_len[1] = (unsigned char)( rec.data_len );
5121
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005122 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5123 ssl->in_msg, ssl->in_msglen );
5124
Hanno Becker6430faf2019-05-08 11:57:13 +01005125#if defined(MBEDTLS_SSL_CID)
5126 /* We have already checked the record content type
5127 * in ssl_parse_record_header(), failing or silently
5128 * dropping the record in the case of an unknown type.
5129 *
5130 * Since with the use of CIDs, the record content type
5131 * might change during decryption, re-check the record
5132 * content type, but treat a failure as fatal this time. */
5133 if( ssl_check_record_type( ssl->in_msgtype ) )
5134 {
5135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5136 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5137 }
5138#endif /* MBEDTLS_SSL_CID */
5139
Angus Grattond8213d02016-05-25 20:56:48 +10005140 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5143 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005144 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005145 else if( ssl->in_msglen == 0 )
5146 {
5147#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5148 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5149 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5150 {
5151 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5154 }
5155#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5156
5157 ssl->nb_zero++;
5158
5159 /*
5160 * Three or more empty messages may be a DoS attack
5161 * (excessive CPU consumption).
5162 */
5163 if( ssl->nb_zero > 3 )
5164 {
5165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005166 "messages, possible DoS attack" ) );
5167 /* Treat the records as if they were not properly authenticated,
5168 * thereby failing the connection if we see more than allowed
5169 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005170 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5171 }
5172 }
5173 else
5174 ssl->nb_zero = 0;
5175
5176#if defined(MBEDTLS_SSL_PROTO_DTLS)
5177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5178 {
5179 ; /* in_ctr read from peer, not maintained internally */
5180 }
5181 else
5182#endif
5183 {
5184 unsigned i;
5185 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5186 if( ++ssl->in_ctr[i - 1] != 0 )
5187 break;
5188
5189 /* The loop goes to its end iff the counter is wrapping */
5190 if( i == ssl_ep_len( ssl ) )
5191 {
5192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5193 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5194 }
5195 }
5196
Paul Bakker5121ce52009-01-03 21:22:43 +00005197 }
5198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005200 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005202 {
5203 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005206 return( ret );
5207 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005215 }
5216#endif
5217
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005218 return( 0 );
5219}
5220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005222
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005223/*
5224 * Read a record.
5225 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005226 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5227 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5228 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005229 */
Hanno Becker1097b342018-08-15 14:09:41 +01005230
5231/* Helper functions for mbedtls_ssl_read_record(). */
5232static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005233static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5234static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005235
Hanno Becker327c93b2018-08-15 13:56:18 +01005236int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005237 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005238{
5239 int ret;
5240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005242
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005243 if( ssl->keep_current_message == 0 )
5244 {
5245 do {
Simon Butcher99000142016-10-13 17:21:01 +01005246
Hanno Becker26994592018-08-15 14:14:59 +01005247 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005248 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005249 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005250
Hanno Beckere74d5562018-08-15 14:26:08 +01005251 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005252 {
Hanno Becker40f50842018-08-15 14:48:01 +01005253#if defined(MBEDTLS_SSL_PROTO_DTLS)
5254 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005255
Hanno Becker40f50842018-08-15 14:48:01 +01005256 /* We only check for buffered messages if the
5257 * current datagram is fully consumed. */
5258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005259 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005260 {
Hanno Becker40f50842018-08-15 14:48:01 +01005261 if( ssl_load_buffered_message( ssl ) == 0 )
5262 have_buffered = 1;
5263 }
5264
5265 if( have_buffered == 0 )
5266#endif /* MBEDTLS_SSL_PROTO_DTLS */
5267 {
5268 ret = ssl_get_next_record( ssl );
5269 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5270 continue;
5271
5272 if( ret != 0 )
5273 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005274 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005275 return( ret );
5276 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005277 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005278 }
5279
5280 ret = mbedtls_ssl_handle_message_type( ssl );
5281
Hanno Becker40f50842018-08-15 14:48:01 +01005282#if defined(MBEDTLS_SSL_PROTO_DTLS)
5283 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5284 {
5285 /* Buffer future message */
5286 ret = ssl_buffer_message( ssl );
5287 if( ret != 0 )
5288 return( ret );
5289
5290 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5291 }
5292#endif /* MBEDTLS_SSL_PROTO_DTLS */
5293
Hanno Becker90333da2017-10-10 11:27:13 +01005294 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5295 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005296
5297 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005298 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005299 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005300 return( ret );
5301 }
5302
Hanno Becker327c93b2018-08-15 13:56:18 +01005303 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005304 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005305 {
5306 mbedtls_ssl_update_handshake_status( ssl );
5307 }
Simon Butcher99000142016-10-13 17:21:01 +01005308 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005309 else
Simon Butcher99000142016-10-13 17:21:01 +01005310 {
Hanno Becker02f59072018-08-15 14:00:24 +01005311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005312 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005313 }
5314
5315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5316
5317 return( 0 );
5318}
5319
Hanno Becker40f50842018-08-15 14:48:01 +01005320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005321static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005322{
Hanno Becker40f50842018-08-15 14:48:01 +01005323 if( ssl->in_left > ssl->next_record_offset )
5324 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005325
Hanno Becker40f50842018-08-15 14:48:01 +01005326 return( 0 );
5327}
5328
5329static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5330{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005331 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005332 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005333 int ret = 0;
5334
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005335 if( hs == NULL )
5336 return( -1 );
5337
Hanno Beckere00ae372018-08-20 09:39:42 +01005338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5339
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005340 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5341 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5342 {
5343 /* Check if we have seen a ChangeCipherSpec before.
5344 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005345 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005346 {
5347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5348 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005349 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005350 }
5351
Hanno Becker39b8bc92018-08-28 17:17:13 +01005352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005353 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5354 ssl->in_msglen = 1;
5355 ssl->in_msg[0] = 1;
5356
5357 /* As long as they are equal, the exact value doesn't matter. */
5358 ssl->in_left = 0;
5359 ssl->next_record_offset = 0;
5360
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005361 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005362 goto exit;
5363 }
Hanno Becker37f95322018-08-16 13:55:32 +01005364
Hanno Beckerb8f50142018-08-28 10:01:34 +01005365#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005366 /* Debug only */
5367 {
5368 unsigned offset;
5369 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5370 {
5371 hs_buf = &hs->buffering.hs[offset];
5372 if( hs_buf->is_valid == 1 )
5373 {
5374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5375 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005376 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005377 }
5378 }
5379 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005380#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005381
5382 /* Check if we have buffered and/or fully reassembled the
5383 * next handshake message. */
5384 hs_buf = &hs->buffering.hs[0];
5385 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5386 {
5387 /* Synthesize a record containing the buffered HS message. */
5388 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5389 ( hs_buf->data[2] << 8 ) |
5390 hs_buf->data[3];
5391
5392 /* Double-check that we haven't accidentally buffered
5393 * a message that doesn't fit into the input buffer. */
5394 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5395 {
5396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5398 }
5399
5400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5401 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5402 hs_buf->data, msg_len + 12 );
5403
5404 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5405 ssl->in_hslen = msg_len + 12;
5406 ssl->in_msglen = msg_len + 12;
5407 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5408
5409 ret = 0;
5410 goto exit;
5411 }
5412 else
5413 {
5414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5415 hs->in_msg_seq ) );
5416 }
5417
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005418 ret = -1;
5419
5420exit:
5421
5422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5423 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005424}
5425
Hanno Beckera02b0b42018-08-21 17:20:27 +01005426static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5427 size_t desired )
5428{
5429 int offset;
5430 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5432 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005433
Hanno Becker01315ea2018-08-21 17:22:17 +01005434 /* Get rid of future records epoch first, if such exist. */
5435 ssl_free_buffered_record( ssl );
5436
5437 /* Check if we have enough space available now. */
5438 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5439 hs->buffering.total_bytes_buffered ) )
5440 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005442 return( 0 );
5443 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005444
Hanno Becker4f432ad2018-08-28 10:02:32 +01005445 /* We don't have enough space to buffer the next expected handshake
5446 * message. Remove buffers used for future messages to gain space,
5447 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005448 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5449 offset >= 0; offset-- )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5452 offset ) );
5453
Hanno Beckerb309b922018-08-23 13:18:05 +01005454 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005455
5456 /* Check if we have enough space available now. */
5457 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5458 hs->buffering.total_bytes_buffered ) )
5459 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005461 return( 0 );
5462 }
5463 }
5464
5465 return( -1 );
5466}
5467
Hanno Becker40f50842018-08-15 14:48:01 +01005468static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5469{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005470 int ret = 0;
5471 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5472
5473 if( hs == NULL )
5474 return( 0 );
5475
5476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5477
5478 switch( ssl->in_msgtype )
5479 {
5480 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005482
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005483 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005484 break;
5485
5486 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005487 {
5488 unsigned recv_msg_seq_offset;
5489 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5490 mbedtls_ssl_hs_buffer *hs_buf;
5491 size_t msg_len = ssl->in_hslen - 12;
5492
5493 /* We should never receive an old handshake
5494 * message - double-check nonetheless. */
5495 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5496 {
5497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5499 }
5500
5501 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5502 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5503 {
5504 /* Silently ignore -- message too far in the future */
5505 MBEDTLS_SSL_DEBUG_MSG( 2,
5506 ( "Ignore future HS message with sequence number %u, "
5507 "buffering window %u - %u",
5508 recv_msg_seq, ssl->handshake->in_msg_seq,
5509 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5510
5511 goto exit;
5512 }
5513
5514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5515 recv_msg_seq, recv_msg_seq_offset ) );
5516
5517 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5518
5519 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005520 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005521 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005522 size_t reassembly_buf_sz;
5523
Hanno Becker37f95322018-08-16 13:55:32 +01005524 hs_buf->is_fragmented =
5525 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5526
5527 /* We copy the message back into the input buffer
5528 * after reassembly, so check that it's not too large.
5529 * This is an implementation-specific limitation
5530 * and not one from the standard, hence it is not
5531 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005532 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005533 {
5534 /* Ignore message */
5535 goto exit;
5536 }
5537
Hanno Beckere0b150f2018-08-21 15:51:03 +01005538 /* Check if we have enough space to buffer the message. */
5539 if( hs->buffering.total_bytes_buffered >
5540 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5541 {
5542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5544 }
5545
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005546 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5547 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005548
5549 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5550 hs->buffering.total_bytes_buffered ) )
5551 {
5552 if( recv_msg_seq_offset > 0 )
5553 {
5554 /* If we can't buffer a future message because
5555 * of space limitations -- ignore. */
5556 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",
5557 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5558 (unsigned) hs->buffering.total_bytes_buffered ) );
5559 goto exit;
5560 }
Hanno Beckere1801392018-08-21 16:51:05 +01005561 else
5562 {
5563 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",
5564 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5565 (unsigned) hs->buffering.total_bytes_buffered ) );
5566 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005567
Hanno Beckera02b0b42018-08-21 17:20:27 +01005568 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005569 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005570 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",
5571 (unsigned) msg_len,
5572 (unsigned) reassembly_buf_sz,
5573 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005574 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005575 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5576 goto exit;
5577 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005578 }
5579
5580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5581 msg_len ) );
5582
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005583 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5584 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005585 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005586 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005587 goto exit;
5588 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005589 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005590
5591 /* Prepare final header: copy msg_type, length and message_seq,
5592 * then add standardised fragment_offset and fragment_length */
5593 memcpy( hs_buf->data, ssl->in_msg, 6 );
5594 memset( hs_buf->data + 6, 0, 3 );
5595 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5596
5597 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005598
5599 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005600 }
5601 else
5602 {
5603 /* Make sure msg_type and length are consistent */
5604 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5605 {
5606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5607 /* Ignore */
5608 goto exit;
5609 }
5610 }
5611
Hanno Becker4422bbb2018-08-20 09:40:19 +01005612 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005613 {
5614 size_t frag_len, frag_off;
5615 unsigned char * const msg = hs_buf->data + 12;
5616
5617 /*
5618 * Check and copy current fragment
5619 */
5620
5621 /* Validation of header fields already done in
5622 * mbedtls_ssl_prepare_handshake_record(). */
5623 frag_off = ssl_get_hs_frag_off( ssl );
5624 frag_len = ssl_get_hs_frag_len( ssl );
5625
5626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5627 frag_off, frag_len ) );
5628 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5629
5630 if( hs_buf->is_fragmented )
5631 {
5632 unsigned char * const bitmask = msg + msg_len;
5633 ssl_bitmask_set( bitmask, frag_off, frag_len );
5634 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5635 msg_len ) == 0 );
5636 }
5637 else
5638 {
5639 hs_buf->is_complete = 1;
5640 }
5641
5642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5643 hs_buf->is_complete ? "" : "not yet " ) );
5644 }
5645
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005646 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005647 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005648
5649 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005650 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005651 break;
5652 }
5653
5654exit:
5655
5656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5657 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005658}
5659#endif /* MBEDTLS_SSL_PROTO_DTLS */
5660
Hanno Becker1097b342018-08-15 14:09:41 +01005661static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005662{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005663 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005664 * Consume last content-layer message and potentially
5665 * update in_msglen which keeps track of the contents'
5666 * consumption state.
5667 *
5668 * (1) Handshake messages:
5669 * Remove last handshake message, move content
5670 * and adapt in_msglen.
5671 *
5672 * (2) Alert messages:
5673 * Consume whole record content, in_msglen = 0.
5674 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005675 * (3) Change cipher spec:
5676 * Consume whole record content, in_msglen = 0.
5677 *
5678 * (4) Application data:
5679 * Don't do anything - the record layer provides
5680 * the application data as a stream transport
5681 * and consumes through mbedtls_ssl_read only.
5682 *
5683 */
5684
5685 /* Case (1): Handshake messages */
5686 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005687 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005688 /* Hard assertion to be sure that no application data
5689 * is in flight, as corrupting ssl->in_msglen during
5690 * ssl->in_offt != NULL is fatal. */
5691 if( ssl->in_offt != NULL )
5692 {
5693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5695 }
5696
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005697 /*
5698 * Get next Handshake message in the current record
5699 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005700
Hanno Becker4a810fb2017-05-24 16:27:30 +01005701 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005702 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005703 * current handshake content: If DTLS handshake
5704 * fragmentation is used, that's the fragment
5705 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005706 * size here is faulty and should be changed at
5707 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005708 * (2) While it doesn't seem to cause problems, one
5709 * has to be very careful not to assume that in_hslen
5710 * is always <= in_msglen in a sensible communication.
5711 * Again, it's wrong for DTLS handshake fragmentation.
5712 * The following check is therefore mandatory, and
5713 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005714 * Additionally, ssl->in_hslen might be arbitrarily out of
5715 * bounds after handling a DTLS message with an unexpected
5716 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005717 */
5718 if( ssl->in_hslen < ssl->in_msglen )
5719 {
5720 ssl->in_msglen -= ssl->in_hslen;
5721 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5722 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005723
Hanno Becker4a810fb2017-05-24 16:27:30 +01005724 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5725 ssl->in_msg, ssl->in_msglen );
5726 }
5727 else
5728 {
5729 ssl->in_msglen = 0;
5730 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005731
Hanno Becker4a810fb2017-05-24 16:27:30 +01005732 ssl->in_hslen = 0;
5733 }
5734 /* Case (4): Application data */
5735 else if( ssl->in_offt != NULL )
5736 {
5737 return( 0 );
5738 }
5739 /* Everything else (CCS & Alerts) */
5740 else
5741 {
5742 ssl->in_msglen = 0;
5743 }
5744
Hanno Becker1097b342018-08-15 14:09:41 +01005745 return( 0 );
5746}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005747
Hanno Beckere74d5562018-08-15 14:26:08 +01005748static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5749{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005750 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005751 return( 1 );
5752
5753 return( 0 );
5754}
5755
Hanno Becker5f066e72018-08-16 14:56:31 +01005756#if defined(MBEDTLS_SSL_PROTO_DTLS)
5757
5758static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5759{
5760 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5761 if( hs == NULL )
5762 return;
5763
Hanno Becker01315ea2018-08-21 17:22:17 +01005764 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005765 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005766 hs->buffering.total_bytes_buffered -=
5767 hs->buffering.future_record.len;
5768
5769 mbedtls_free( hs->buffering.future_record.data );
5770 hs->buffering.future_record.data = NULL;
5771 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005772}
5773
5774static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5775{
5776 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5777 unsigned char * rec;
5778 size_t rec_len;
5779 unsigned rec_epoch;
5780
5781 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5782 return( 0 );
5783
5784 if( hs == NULL )
5785 return( 0 );
5786
Hanno Becker5f066e72018-08-16 14:56:31 +01005787 rec = hs->buffering.future_record.data;
5788 rec_len = hs->buffering.future_record.len;
5789 rec_epoch = hs->buffering.future_record.epoch;
5790
5791 if( rec == NULL )
5792 return( 0 );
5793
Hanno Becker4cb782d2018-08-20 11:19:05 +01005794 /* Only consider loading future records if the
5795 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005796 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005797 return( 0 );
5798
Hanno Becker5f066e72018-08-16 14:56:31 +01005799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5800
5801 if( rec_epoch != ssl->in_epoch )
5802 {
5803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5804 goto exit;
5805 }
5806
5807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5808
5809 /* Double-check that the record is not too large */
5810 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5811 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5812 {
5813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5815 }
5816
5817 memcpy( ssl->in_hdr, rec, rec_len );
5818 ssl->in_left = rec_len;
5819 ssl->next_record_offset = 0;
5820
5821 ssl_free_buffered_record( ssl );
5822
5823exit:
5824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5825 return( 0 );
5826}
5827
5828static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5829{
5830 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5831 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005832 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005833
5834 /* Don't buffer future records outside handshakes. */
5835 if( hs == NULL )
5836 return( 0 );
5837
5838 /* Only buffer handshake records (we are only interested
5839 * in Finished messages). */
5840 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5841 return( 0 );
5842
5843 /* Don't buffer more than one future epoch record. */
5844 if( hs->buffering.future_record.data != NULL )
5845 return( 0 );
5846
Hanno Becker01315ea2018-08-21 17:22:17 +01005847 /* Don't buffer record if there's not enough buffering space remaining. */
5848 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5849 hs->buffering.total_bytes_buffered ) )
5850 {
5851 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",
5852 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5853 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005854 return( 0 );
5855 }
5856
Hanno Becker5f066e72018-08-16 14:56:31 +01005857 /* Buffer record */
5858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5859 ssl->in_epoch + 1 ) );
5860 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5861 rec_hdr_len + ssl->in_msglen );
5862
5863 /* ssl_parse_record_header() only considers records
5864 * of the next epoch as candidates for buffering. */
5865 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005866 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005867
5868 hs->buffering.future_record.data =
5869 mbedtls_calloc( 1, hs->buffering.future_record.len );
5870 if( hs->buffering.future_record.data == NULL )
5871 {
5872 /* If we run out of RAM trying to buffer a
5873 * record from the next epoch, just ignore. */
5874 return( 0 );
5875 }
5876
Hanno Becker01315ea2018-08-21 17:22:17 +01005877 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005878
Hanno Becker01315ea2018-08-21 17:22:17 +01005879 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005880 return( 0 );
5881}
5882
5883#endif /* MBEDTLS_SSL_PROTO_DTLS */
5884
Hanno Beckere74d5562018-08-15 14:26:08 +01005885static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005886{
5887 int ret;
5888
Hanno Becker5f066e72018-08-16 14:56:31 +01005889#if defined(MBEDTLS_SSL_PROTO_DTLS)
5890 /* We might have buffered a future record; if so,
5891 * and if the epoch matches now, load it.
5892 * On success, this call will set ssl->in_left to
5893 * the length of the buffered record, so that
5894 * the calls to ssl_fetch_input() below will
5895 * essentially be no-ops. */
5896 ret = ssl_load_buffered_record( ssl );
5897 if( ret != 0 )
5898 return( ret );
5899#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005900
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005901 /* Reset in pointers to default state for TLS/DTLS records,
5902 * assuming no CID and no offset between record content and
5903 * record plaintext. */
5904 ssl_update_in_pointers( ssl );
5905
5906 /* Ensure that we have enough space available for the default form
5907 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5908 * with no space for CIDs counted in). */
5909 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5910 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005913 return( ret );
5914 }
5915
5916 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005919 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5920 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005921 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005922 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5923 {
5924 ret = ssl_buffer_future_record( ssl );
5925 if( ret != 0 )
5926 return( ret );
5927
5928 /* Fall through to handling of unexpected records */
5929 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5930 }
5931
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005932 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5933 {
5934 /* Skip unexpected record (but not whole datagram) */
5935 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005936 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005937
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5939 "(header)" ) );
5940 }
5941 else
5942 {
5943 /* Skip invalid record and the rest of the datagram */
5944 ssl->next_record_offset = 0;
5945 ssl->in_left = 0;
5946
5947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5948 "(header)" ) );
5949 }
5950
5951 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005952 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005953 }
5954#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005955 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005956 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005957
5958 /*
5959 * Read and optionally decrypt the message contents
5960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005962 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005965 return( ret );
5966 }
5967
5968 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005970 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005971 {
Hanno Becker5903de42019-05-03 14:46:38 +01005972 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005973 if( ssl->next_record_offset < ssl->in_left )
5974 {
5975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5976 }
5977 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005978 else
5979#endif
5980 ssl->in_left = 0;
5981
5982 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005986 {
5987 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005988 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005989 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005990 /* Except when waiting for Finished as a bad mac here
5991 * probably means something went wrong in the handshake
5992 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5993 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5994 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5995 {
5996#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5997 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5998 {
5999 mbedtls_ssl_send_alert_message( ssl,
6000 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6001 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6002 }
6003#endif
6004 return( ret );
6005 }
6006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006008 if( ssl->conf->badmac_limit != 0 &&
6009 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6012 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006013 }
6014#endif
6015
Hanno Becker4a810fb2017-05-24 16:27:30 +01006016 /* As above, invalid records cause
6017 * dismissal of the whole datagram. */
6018
6019 ssl->next_record_offset = 0;
6020 ssl->in_left = 0;
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006023 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006024 }
6025
6026 return( ret );
6027 }
6028 else
6029#endif
6030 {
6031 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6033 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 mbedtls_ssl_send_alert_message( ssl,
6036 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6037 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006038 }
6039#endif
6040 return( ret );
6041 }
6042 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006043
Simon Butcher99000142016-10-13 17:21:01 +01006044 return( 0 );
6045}
6046
6047int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6048{
6049 int ret;
6050
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006051 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006052 * Handle particular types of records
6053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 {
Simon Butcher99000142016-10-13 17:21:01 +01006056 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6057 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006058 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006059 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 }
6061
Hanno Beckere678eaa2018-08-21 14:57:46 +01006062 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006063 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006064 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006065 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6067 ssl->in_msglen ) );
6068 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006069 }
6070
Hanno Beckere678eaa2018-08-21 14:57:46 +01006071 if( ssl->in_msg[0] != 1 )
6072 {
6073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6074 ssl->in_msg[0] ) );
6075 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6076 }
6077
6078#if defined(MBEDTLS_SSL_PROTO_DTLS)
6079 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6080 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6081 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6082 {
6083 if( ssl->handshake == NULL )
6084 {
6085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6086 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6087 }
6088
6089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6090 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6091 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006092#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006093 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006096 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006097 if( ssl->in_msglen != 2 )
6098 {
6099 /* Note: Standard allows for more than one 2 byte alert
6100 to be packed in a single message, but Mbed TLS doesn't
6101 currently support this. */
6102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6103 ssl->in_msglen ) );
6104 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6105 }
6106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006108 ssl->in_msg[0], ssl->in_msg[1] ) );
6109
6110 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006111 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006116 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006118 }
6119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6121 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6124 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006125 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006126
6127#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6128 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6129 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6130 {
Hanno Becker90333da2017-10-10 11:27:13 +01006131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006132 /* Will be handled when trying to parse ServerHello */
6133 return( 0 );
6134 }
6135#endif
6136
6137#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6138 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6139 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6140 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6141 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6142 {
6143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6144 /* Will be handled in mbedtls_ssl_parse_certificate() */
6145 return( 0 );
6146 }
6147#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6148
6149 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006150 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006151 }
6152
Hanno Beckerc76c6192017-06-06 10:03:17 +01006153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006155 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006156 /* Drop unexpected ApplicationData records,
6157 * except at the beginning of renegotiations */
6158 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6159 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6160#if defined(MBEDTLS_SSL_RENEGOTIATION)
6161 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6162 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006163#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006164 )
6165 {
6166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6167 return( MBEDTLS_ERR_SSL_NON_FATAL );
6168 }
6169
6170 if( ssl->handshake != NULL &&
6171 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6172 {
6173 ssl_handshake_wrapup_free_hs_transform( ssl );
6174 }
6175 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006176#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006177
Paul Bakker5121ce52009-01-03 21:22:43 +00006178 return( 0 );
6179}
6180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006182{
6183 int ret;
6184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6186 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6187 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006188 {
6189 return( ret );
6190 }
6191
6192 return( 0 );
6193}
6194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006196 unsigned char level,
6197 unsigned char message )
6198{
6199 int ret;
6200
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006201 if( ssl == NULL || ssl->conf == NULL )
6202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006208 ssl->out_msglen = 2;
6209 ssl->out_msg[0] = level;
6210 ssl->out_msg[1] = message;
6211
Hanno Becker67bc7c32018-08-06 11:33:50 +01006212 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006215 return( ret );
6216 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006218
6219 return( 0 );
6220}
6221
Hanno Beckerb9d44792019-02-08 07:19:04 +00006222#if defined(MBEDTLS_X509_CRT_PARSE_C)
6223static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6224{
6225#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6226 if( session->peer_cert != NULL )
6227 {
6228 mbedtls_x509_crt_free( session->peer_cert );
6229 mbedtls_free( session->peer_cert );
6230 session->peer_cert = NULL;
6231 }
6232#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6233 if( session->peer_cert_digest != NULL )
6234 {
6235 /* Zeroization is not necessary. */
6236 mbedtls_free( session->peer_cert_digest );
6237 session->peer_cert_digest = NULL;
6238 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6239 session->peer_cert_digest_len = 0;
6240 }
6241#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6242}
6243#endif /* MBEDTLS_X509_CRT_PARSE_C */
6244
Paul Bakker5121ce52009-01-03 21:22:43 +00006245/*
6246 * Handshake functions
6247 */
Hanno Becker21489932019-02-05 13:20:55 +00006248#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006249/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006251{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006252 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6253 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006256
Hanno Becker7177a882019-02-05 13:36:46 +00006257 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006260 ssl->state++;
6261 return( 0 );
6262 }
6263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006266}
6267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006269{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006270 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6271 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006274
Hanno Becker7177a882019-02-05 13:36:46 +00006275 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006278 ssl->state++;
6279 return( 0 );
6280 }
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006284}
Gilles Peskinef9828522017-05-03 12:28:43 +02006285
Hanno Becker21489932019-02-05 13:20:55 +00006286#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006287/* Some certificate support -> implement write and parse */
6288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006294 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6295 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006298
Hanno Becker7177a882019-02-05 13:36:46 +00006299 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006302 ssl->state++;
6303 return( 0 );
6304 }
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006307 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006308 {
6309 if( ssl->client_auth == 0 )
6310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006312 ssl->state++;
6313 return( 0 );
6314 }
6315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006317 /*
6318 * If using SSLv3 and got no cert, send an Alert message
6319 * (otherwise an empty Certificate message will be sent).
6320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6322 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006323 {
6324 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6326 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6327 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006330 goto write_msg;
6331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#endif /* MBEDTLS_SSL_CLI_C */
6335#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006336 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6341 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006342 }
6343 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006344#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347
6348 /*
6349 * 0 . 0 handshake type
6350 * 1 . 3 handshake length
6351 * 4 . 6 length of all certs
6352 * 7 . 9 length of cert. 1
6353 * 10 . n-1 peer certificate
6354 * n . n+2 length of cert. 2
6355 * n+3 . ... upper level cert, etc.
6356 */
6357 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006359
Paul Bakker29087132010-03-21 21:03:34 +00006360 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006361 {
6362 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006363 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006366 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 }
6369
6370 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6371 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6372 ssl->out_msg[i + 2] = (unsigned char)( n );
6373
6374 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6375 i += n; crt = crt->next;
6376 }
6377
6378 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6379 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6380 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6381
6382 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6384 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006385
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006386#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006387write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006388#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006389
6390 ssl->state++;
6391
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006392 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006395 return( ret );
6396 }
6397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006399
Paul Bakkered27a042013-04-18 22:46:23 +02006400 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006401}
6402
Hanno Becker84879e32019-01-31 07:44:03 +00006403#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006404
6405#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006406static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6407 unsigned char *crt_buf,
6408 size_t crt_buf_len )
6409{
6410 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6411
6412 if( peer_crt == NULL )
6413 return( -1 );
6414
6415 if( peer_crt->raw.len != crt_buf_len )
6416 return( -1 );
6417
Hanno Becker46f34d02019-02-08 14:00:04 +00006418 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006419}
Hanno Becker177475a2019-02-05 17:02:46 +00006420#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6421static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6422 unsigned char *crt_buf,
6423 size_t crt_buf_len )
6424{
6425 int ret;
6426 unsigned char const * const peer_cert_digest =
6427 ssl->session->peer_cert_digest;
6428 mbedtls_md_type_t const peer_cert_digest_type =
6429 ssl->session->peer_cert_digest_type;
6430 mbedtls_md_info_t const * const digest_info =
6431 mbedtls_md_info_from_type( peer_cert_digest_type );
6432 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6433 size_t digest_len;
6434
6435 if( peer_cert_digest == NULL || digest_info == NULL )
6436 return( -1 );
6437
6438 digest_len = mbedtls_md_get_size( digest_info );
6439 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6440 return( -1 );
6441
6442 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6443 if( ret != 0 )
6444 return( -1 );
6445
6446 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6447}
6448#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006449#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006450
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006451/*
6452 * Once the certificate message is read, parse it into a cert chain and
6453 * perform basic checks, but leave actual verification to the caller
6454 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006455static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6456 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006457{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006458 int ret;
6459#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6460 int crt_cnt=0;
6461#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006462 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006463 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006468 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6469 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006471 }
6472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6474 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006477 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6478 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006480 }
6481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006483
Paul Bakker5121ce52009-01-03 21:22:43 +00006484 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006487 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006488
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006489 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6494 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 }
6497
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006498 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6499 i += 3;
6500
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006501 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 while( i < ssl->in_hslen )
6503 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006504 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006505 if ( i + 3 > ssl->in_hslen ) {
6506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006507 mbedtls_ssl_send_alert_message( ssl,
6508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6509 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006510 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6511 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006512 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6513 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 if( ssl->in_msg[i] != 0 )
6515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006517 mbedtls_ssl_send_alert_message( ssl,
6518 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6519 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 }
6522
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006523 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006524 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6525 | (unsigned int) ssl->in_msg[i + 2];
6526 i += 3;
6527
6528 if( n < 128 || i + n > ssl->in_hslen )
6529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006531 mbedtls_ssl_send_alert_message( ssl,
6532 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6533 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 }
6536
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006537 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006538#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6539 if( crt_cnt++ == 0 &&
6540 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6541 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006542 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006543 /* During client-side renegotiation, check that the server's
6544 * end-CRTs hasn't changed compared to the initial handshake,
6545 * mitigating the triple handshake attack. On success, reuse
6546 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6548 if( ssl_check_peer_crt_unchanged( ssl,
6549 &ssl->in_msg[i],
6550 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006551 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6553 mbedtls_ssl_send_alert_message( ssl,
6554 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6555 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6556 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006557 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006558
6559 /* Now we can safely free the original chain. */
6560 ssl_clear_peer_cert( ssl->session );
6561 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006562#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6563
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006564 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006565#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006566 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006567#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006568 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006569 * it in-place from the input buffer instead of making a copy. */
6570 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6571#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006572 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006573 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006574 case 0: /*ok*/
6575 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6576 /* Ignore certificate with an unknown algorithm: maybe a
6577 prior certificate was already trusted. */
6578 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006579
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006580 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6581 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6582 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006583
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006584 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6585 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6586 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006587
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006588 default:
6589 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6590 crt_parse_der_failed:
6591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6592 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6593 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 }
6595
6596 i += n;
6597 }
6598
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006599 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006600 return( 0 );
6601}
6602
Hanno Becker4a55f632019-02-05 12:49:06 +00006603#if defined(MBEDTLS_SSL_SRV_C)
6604static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6605{
6606 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6607 return( -1 );
6608
6609#if defined(MBEDTLS_SSL_PROTO_SSL3)
6610 /*
6611 * Check if the client sent an empty certificate
6612 */
6613 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6614 {
6615 if( ssl->in_msglen == 2 &&
6616 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6617 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6618 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6619 {
6620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6621 return( 0 );
6622 }
6623
6624 return( -1 );
6625 }
6626#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6627
6628#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6629 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6630 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6631 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6632 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6633 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6634 {
6635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6636 return( 0 );
6637 }
6638
6639 return( -1 );
6640#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6641 MBEDTLS_SSL_PROTO_TLS1_2 */
6642}
6643#endif /* MBEDTLS_SSL_SRV_C */
6644
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006645/* Check if a certificate message is expected.
6646 * Return either
6647 * - SSL_CERTIFICATE_EXPECTED, or
6648 * - SSL_CERTIFICATE_SKIP
6649 * indicating whether a Certificate message is expected or not.
6650 */
6651#define SSL_CERTIFICATE_EXPECTED 0
6652#define SSL_CERTIFICATE_SKIP 1
6653static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6654 int authmode )
6655{
6656 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006657 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006658
6659 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6660 return( SSL_CERTIFICATE_SKIP );
6661
6662#if defined(MBEDTLS_SSL_SRV_C)
6663 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6664 {
6665 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6666 return( SSL_CERTIFICATE_SKIP );
6667
6668 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6669 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006670 ssl->session_negotiate->verify_result =
6671 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6672 return( SSL_CERTIFICATE_SKIP );
6673 }
6674 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006675#else
6676 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006677#endif /* MBEDTLS_SSL_SRV_C */
6678
6679 return( SSL_CERTIFICATE_EXPECTED );
6680}
6681
Hanno Becker68636192019-02-05 14:36:34 +00006682static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6683 int authmode,
6684 mbedtls_x509_crt *chain,
6685 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006686{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006687 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006688 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006689 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006690 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006691
Hanno Becker8927c832019-04-03 12:52:50 +01006692 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6693 void *p_vrfy;
6694
Hanno Becker68636192019-02-05 14:36:34 +00006695 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6696 return( 0 );
6697
Hanno Becker8927c832019-04-03 12:52:50 +01006698 if( ssl->f_vrfy != NULL )
6699 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006701 f_vrfy = ssl->f_vrfy;
6702 p_vrfy = ssl->p_vrfy;
6703 }
6704 else
6705 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006707 f_vrfy = ssl->conf->f_vrfy;
6708 p_vrfy = ssl->conf->p_vrfy;
6709 }
6710
Hanno Becker68636192019-02-05 14:36:34 +00006711 /*
6712 * Main check: verify certificate
6713 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006714#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6715 if( ssl->conf->f_ca_cb != NULL )
6716 {
6717 ((void) rs_ctx);
6718 have_ca_chain = 1;
6719
6720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006721 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006722 chain,
6723 ssl->conf->f_ca_cb,
6724 ssl->conf->p_ca_cb,
6725 ssl->conf->cert_profile,
6726 ssl->hostname,
6727 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006728 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006729 }
6730 else
6731#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6732 {
6733 mbedtls_x509_crt *ca_chain;
6734 mbedtls_x509_crl *ca_crl;
6735
6736#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6737 if( ssl->handshake->sni_ca_chain != NULL )
6738 {
6739 ca_chain = ssl->handshake->sni_ca_chain;
6740 ca_crl = ssl->handshake->sni_ca_crl;
6741 }
6742 else
6743#endif
6744 {
6745 ca_chain = ssl->conf->ca_chain;
6746 ca_crl = ssl->conf->ca_crl;
6747 }
6748
6749 if( ca_chain != NULL )
6750 have_ca_chain = 1;
6751
6752 ret = mbedtls_x509_crt_verify_restartable(
6753 chain,
6754 ca_chain, ca_crl,
6755 ssl->conf->cert_profile,
6756 ssl->hostname,
6757 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006758 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006759 }
Hanno Becker68636192019-02-05 14:36:34 +00006760
6761 if( ret != 0 )
6762 {
6763 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6764 }
6765
6766#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6767 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6768 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6769#endif
6770
6771 /*
6772 * Secondary checks: always done, but change 'ret' only if it was 0
6773 */
6774
6775#if defined(MBEDTLS_ECP_C)
6776 {
6777 const mbedtls_pk_context *pk = &chain->pk;
6778
6779 /* If certificate uses an EC key, make sure the curve is OK */
6780 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6781 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6782 {
6783 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6784
6785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6786 if( ret == 0 )
6787 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6788 }
6789 }
6790#endif /* MBEDTLS_ECP_C */
6791
6792 if( mbedtls_ssl_check_cert_usage( chain,
6793 ciphersuite_info,
6794 ! ssl->conf->endpoint,
6795 &ssl->session_negotiate->verify_result ) != 0 )
6796 {
6797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6798 if( ret == 0 )
6799 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6800 }
6801
6802 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6803 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6804 * with details encoded in the verification flags. All other kinds
6805 * of error codes, including those from the user provided f_vrfy
6806 * functions, are treated as fatal and lead to a failure of
6807 * ssl_parse_certificate even if verification was optional. */
6808 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6809 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6810 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6811 {
6812 ret = 0;
6813 }
6814
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006815 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006816 {
6817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6818 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6819 }
6820
6821 if( ret != 0 )
6822 {
6823 uint8_t alert;
6824
6825 /* The certificate may have been rejected for several reasons.
6826 Pick one and send the corresponding alert. Which alert to send
6827 may be a subject of debate in some cases. */
6828 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6829 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6830 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6831 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6832 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6833 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6834 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6835 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6836 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6837 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6838 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6839 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6840 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6841 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6842 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6843 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6844 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6845 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6846 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6847 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6848 else
6849 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6850 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6851 alert );
6852 }
6853
6854#if defined(MBEDTLS_DEBUG_C)
6855 if( ssl->session_negotiate->verify_result != 0 )
6856 {
6857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6858 ssl->session_negotiate->verify_result ) );
6859 }
6860 else
6861 {
6862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6863 }
6864#endif /* MBEDTLS_DEBUG_C */
6865
6866 return( ret );
6867}
6868
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006869#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6870static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6871 unsigned char *start, size_t len )
6872{
6873 int ret;
6874 /* Remember digest of the peer's end-CRT. */
6875 ssl->session_negotiate->peer_cert_digest =
6876 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6877 if( ssl->session_negotiate->peer_cert_digest == NULL )
6878 {
6879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6880 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6881 mbedtls_ssl_send_alert_message( ssl,
6882 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6883 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6884
6885 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6886 }
6887
6888 ret = mbedtls_md( mbedtls_md_info_from_type(
6889 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6890 start, len,
6891 ssl->session_negotiate->peer_cert_digest );
6892
6893 ssl->session_negotiate->peer_cert_digest_type =
6894 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6895 ssl->session_negotiate->peer_cert_digest_len =
6896 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6897
6898 return( ret );
6899}
6900
6901static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6902 unsigned char *start, size_t len )
6903{
6904 unsigned char *end = start + len;
6905 int ret;
6906
6907 /* Make a copy of the peer's raw public key. */
6908 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6909 ret = mbedtls_pk_parse_subpubkey( &start, end,
6910 &ssl->handshake->peer_pubkey );
6911 if( ret != 0 )
6912 {
6913 /* We should have parsed the public key before. */
6914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6915 }
6916
6917 return( 0 );
6918}
6919#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6920
Hanno Becker68636192019-02-05 14:36:34 +00006921int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6922{
6923 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006924 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006925#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6926 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6927 ? ssl->handshake->sni_authmode
6928 : ssl->conf->authmode;
6929#else
6930 const int authmode = ssl->conf->authmode;
6931#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006932 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006933 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006934
6935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6936
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006937 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6938 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006939 {
6940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006941 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006942 }
6943
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006944#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6945 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006946 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006947 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006948 chain = ssl->handshake->ecrs_peer_cert;
6949 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006950 goto crt_verify;
6951 }
6952#endif
6953
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006954 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006955 {
6956 /* mbedtls_ssl_read_record may have sent an alert already. We
6957 let it decide whether to alert. */
6958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006959 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006960 }
6961
Hanno Becker4a55f632019-02-05 12:49:06 +00006962#if defined(MBEDTLS_SSL_SRV_C)
6963 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6964 {
6965 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006966
6967 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006968 ret = 0;
6969 else
6970 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006971
Hanno Becker6bdfab22019-02-05 13:11:17 +00006972 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006973 }
6974#endif /* MBEDTLS_SSL_SRV_C */
6975
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006976 /* Clear existing peer CRT structure in case we tried to
6977 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006978 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006979
6980 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6981 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006982 {
6983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6984 sizeof( mbedtls_x509_crt ) ) );
6985 mbedtls_ssl_send_alert_message( ssl,
6986 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6987 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006988
Hanno Becker3dad3112019-02-05 17:19:52 +00006989 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6990 goto exit;
6991 }
6992 mbedtls_x509_crt_init( chain );
6993
6994 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006995 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006996 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006997
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006998#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6999 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007000 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007001
7002crt_verify:
7003 if( ssl->handshake->ecrs_enabled)
7004 rs_ctx = &ssl->handshake->ecrs_ctx;
7005#endif
7006
Hanno Becker68636192019-02-05 14:36:34 +00007007 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007008 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007009 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007010 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007011
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007012#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007013 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007014 unsigned char *crt_start, *pk_start;
7015 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007016
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007017 /* We parse the CRT chain without copying, so
7018 * these pointers point into the input buffer,
7019 * and are hence still valid after freeing the
7020 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007021
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007022 crt_start = chain->raw.p;
7023 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007024
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007025 pk_start = chain->pk_raw.p;
7026 pk_len = chain->pk_raw.len;
7027
7028 /* Free the CRT structures before computing
7029 * digest and copying the peer's public key. */
7030 mbedtls_x509_crt_free( chain );
7031 mbedtls_free( chain );
7032 chain = NULL;
7033
7034 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007035 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007036 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007037
7038 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7039 if( ret != 0 )
7040 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007041 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007042#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7043 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007044 ssl->session_negotiate->peer_cert = chain;
7045 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007046#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007049
Hanno Becker6bdfab22019-02-05 13:11:17 +00007050exit:
7051
Hanno Becker3dad3112019-02-05 17:19:52 +00007052 if( ret == 0 )
7053 ssl->state++;
7054
7055#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7056 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7057 {
7058 ssl->handshake->ecrs_peer_cert = chain;
7059 chain = NULL;
7060 }
7061#endif
7062
7063 if( chain != NULL )
7064 {
7065 mbedtls_x509_crt_free( chain );
7066 mbedtls_free( chain );
7067 }
7068
Paul Bakker5121ce52009-01-03 21:22:43 +00007069 return( ret );
7070}
Hanno Becker21489932019-02-05 13:20:55 +00007071#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007074{
7075 int ret;
7076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007080 ssl->out_msglen = 1;
7081 ssl->out_msg[0] = 1;
7082
Paul Bakker5121ce52009-01-03 21:22:43 +00007083 ssl->state++;
7084
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007085 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007086 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 return( ret );
7089 }
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092
7093 return( 0 );
7094}
7095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097{
7098 int ret;
7099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007101
Hanno Becker327c93b2018-08-15 13:56:18 +01007102 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105 return( ret );
7106 }
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007111 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7112 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007114 }
7115
Hanno Beckere678eaa2018-08-21 14:57:46 +01007116 /* CCS records are only accepted if they have length 1 and content '1',
7117 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007118
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007119 /*
7120 * Switch to our negotiated transform and session parameters for inbound
7121 * data.
7122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007124 ssl->transform_in = ssl->transform_negotiate;
7125 ssl->session_in = ssl->session_negotiate;
7126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007128 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007131 ssl_dtls_replay_reset( ssl );
7132#endif
7133
7134 /* Increment epoch */
7135 if( ++ssl->in_epoch == 0 )
7136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007138 /* This is highly unlikely to happen for legitimate reasons, so
7139 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007141 }
7142 }
7143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007145 memset( ssl->in_ctr, 0, 8 );
7146
Hanno Becker79594fd2019-05-08 09:38:41 +01007147 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7150 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007155 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7156 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007158 }
7159 }
7160#endif
7161
Paul Bakker5121ce52009-01-03 21:22:43 +00007162 ssl->state++;
7163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007165
7166 return( 0 );
7167}
7168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7170 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007171{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007172 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7175 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7176 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007177 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007178 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7181#if defined(MBEDTLS_SHA512_C)
7182 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007183 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7184 else
7185#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186#if defined(MBEDTLS_SHA256_C)
7187 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007188 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007189 else
7190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007194 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007195 }
Paul Bakker380da532012-04-18 16:10:25 +00007196}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7201 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007202 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7203 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7206#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007207#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007208 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007209 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7210#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007211 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007212#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007215#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007216 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007217 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007218#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007219 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007220#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007223}
7224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007226 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7229 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007230 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7231 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7234#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007235#if defined(MBEDTLS_USE_PSA_CRYPTO)
7236 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7237#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007238 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007239#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007242#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007243 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007244#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007245 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007246#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007249}
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7252 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7253static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007254 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007255{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007256 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7257 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007258}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007259#endif
Paul Bakker380da532012-04-18 16:10:25 +00007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7262#if defined(MBEDTLS_SHA256_C)
7263static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007264 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007265{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007266#if defined(MBEDTLS_USE_PSA_CRYPTO)
7267 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7268#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007269 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007270#endif
Paul Bakker380da532012-04-18 16:10:25 +00007271}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007272#endif
Paul Bakker380da532012-04-18 16:10:25 +00007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SHA512_C)
7275static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007276 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007277{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007278#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007279 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007280#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007281 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007282#endif
Paul Bakker380da532012-04-18 16:10:25 +00007283}
Paul Bakker769075d2012-11-24 11:26:46 +01007284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007288static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007290{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007291 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007292 mbedtls_md5_context md5;
7293 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007294
Paul Bakker5121ce52009-01-03 21:22:43 +00007295 unsigned char padbuf[48];
7296 unsigned char md5sum[16];
7297 unsigned char sha1sum[20];
7298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007300 if( !session )
7301 session = ssl->session;
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007304
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007305 mbedtls_md5_init( &md5 );
7306 mbedtls_sha1_init( &sha1 );
7307
7308 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7309 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007310
7311 /*
7312 * SSLv3:
7313 * hash =
7314 * MD5( master + pad2 +
7315 * MD5( handshake + sender + master + pad1 ) )
7316 * + SHA1( master + pad2 +
7317 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007318 */
7319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007321 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7322 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007323#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007326 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7327 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007331 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007332
Paul Bakker1ef83d62012-04-11 12:09:53 +00007333 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007334
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007335 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7336 mbedtls_md5_update_ret( &md5, session->master, 48 );
7337 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7338 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007339
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007340 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7341 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7342 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7343 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007344
Paul Bakker1ef83d62012-04-11 12:09:53 +00007345 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007346
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007347 mbedtls_md5_starts_ret( &md5 );
7348 mbedtls_md5_update_ret( &md5, session->master, 48 );
7349 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7350 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7351 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007353 mbedtls_sha1_starts_ret( &sha1 );
7354 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7355 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7356 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7357 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007361 mbedtls_md5_free( &md5 );
7362 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007363
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007364 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7365 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7366 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007373static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007375{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007376 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007377 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007378 mbedtls_md5_context md5;
7379 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007380 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007383 if( !session )
7384 session = ssl->session;
7385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007388 mbedtls_md5_init( &md5 );
7389 mbedtls_sha1_init( &sha1 );
7390
7391 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7392 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007393
Paul Bakker1ef83d62012-04-11 12:09:53 +00007394 /*
7395 * TLSv1:
7396 * hash = PRF( master, finished_label,
7397 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7398 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007401 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7402 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007403#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007406 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7407 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007408#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007411 ? "client finished"
7412 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007413
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007414 mbedtls_md5_finish_ret( &md5, padbuf );
7415 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007416
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007417 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007418 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007421
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007422 mbedtls_md5_free( &md5 );
7423 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007424
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007425 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7432#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007433static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007435{
7436 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007437 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007438 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007439#if defined(MBEDTLS_USE_PSA_CRYPTO)
7440 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007441 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007442 psa_status_t status;
7443#else
7444 mbedtls_sha256_context sha256;
7445#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007448 if( !session )
7449 session = ssl->session;
7450
Andrzej Kurekeb342242019-01-29 09:14:33 -05007451 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7452 ? "client finished"
7453 : "server finished";
7454
7455#if defined(MBEDTLS_USE_PSA_CRYPTO)
7456 sha256_psa = psa_hash_operation_init();
7457
7458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7459
7460 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7461 if( status != PSA_SUCCESS )
7462 {
7463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7464 return;
7465 }
7466
7467 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7468 if( status != PSA_SUCCESS )
7469 {
7470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7471 return;
7472 }
7473 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7474#else
7475
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007476 mbedtls_sha256_init( &sha256 );
7477
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007479
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007480 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007481
7482 /*
7483 * TLSv1.2:
7484 * hash = PRF( master, finished_label,
7485 * Hash( handshake ) )[0.11]
7486 */
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if !defined(MBEDTLS_SHA256_ALT)
7489 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007490 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007491#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007492
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007493 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007494 mbedtls_sha256_free( &sha256 );
7495#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007496
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007497 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007498 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007501
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007502 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007509static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007511{
7512 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007513 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007514 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007515#if defined(MBEDTLS_USE_PSA_CRYPTO)
7516 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007517 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007518 psa_status_t status;
7519#else
7520 mbedtls_sha512_context sha512;
7521#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007524 if( !session )
7525 session = ssl->session;
7526
Andrzej Kurekeb342242019-01-29 09:14:33 -05007527 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7528 ? "client finished"
7529 : "server finished";
7530
7531#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007532 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007533
7534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7535
Andrzej Kurek972fba52019-01-30 03:29:12 -05007536 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007537 if( status != PSA_SUCCESS )
7538 {
7539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7540 return;
7541 }
7542
Andrzej Kurek972fba52019-01-30 03:29:12 -05007543 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007544 if( status != PSA_SUCCESS )
7545 {
7546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7547 return;
7548 }
7549 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7550#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007551 mbedtls_sha512_init( &sha512 );
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007554
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007555 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007556
7557 /*
7558 * TLSv1.2:
7559 * hash = PRF( master, finished_label,
7560 * Hash( handshake ) )[0.11]
7561 */
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007564 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7565 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007566#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007567
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007568 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007569 mbedtls_sha512_free( &sha512 );
7570#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007571
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007572 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007573 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007576
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007577 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#endif /* MBEDTLS_SHA512_C */
7582#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007587
7588 /*
7589 * Free our handshake params
7590 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007591 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007593 ssl->handshake = NULL;
7594
7595 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007596 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007597 */
7598 if( ssl->transform )
7599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 mbedtls_ssl_transform_free( ssl->transform );
7601 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007602 }
7603 ssl->transform = ssl->transform_negotiate;
7604 ssl->transform_negotiate = NULL;
7605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007607}
7608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007610{
7611 int resume = ssl->handshake->resume;
7612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if defined(MBEDTLS_SSL_RENEGOTIATION)
7616 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007619 ssl->renego_records_seen = 0;
7620 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007621#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007622
7623 /*
7624 * Free the previous session and switch in the current one
7625 */
Paul Bakker0a597072012-09-25 21:55:46 +00007626 if( ssl->session )
7627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007629 /* RFC 7366 3.1: keep the EtM state */
7630 ssl->session_negotiate->encrypt_then_mac =
7631 ssl->session->encrypt_then_mac;
7632#endif
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 mbedtls_ssl_session_free( ssl->session );
7635 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007636 }
7637 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007638 ssl->session_negotiate = NULL;
7639
Paul Bakker0a597072012-09-25 21:55:46 +00007640 /*
7641 * Add cache entry
7642 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007643 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007644 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007645 resume == 0 )
7646 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007647 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007649 }
Paul Bakker0a597072012-09-25 21:55:46 +00007650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007652 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007653 ssl->handshake->flight != NULL )
7654 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007655 /* Cancel handshake timer */
7656 ssl_set_timer( ssl, 0 );
7657
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007658 /* Keep last flight around in case we need to resend it:
7659 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007661 }
7662 else
7663#endif
7664 ssl_handshake_wrapup_free_hs_transform( ssl );
7665
Paul Bakker48916f92012-09-16 19:57:18 +00007666 ssl->state++;
7667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007669}
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007672{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007673 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007676
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007677 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007678
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007679 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007681 /*
7682 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7683 * may define some other value. Currently (early 2016), no defined
7684 * ciphersuite does this (and this is unlikely to change as activity has
7685 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007690 ssl->verify_data_len = hash_len;
7691 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007692#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007693
Paul Bakker5121ce52009-01-03 21:22:43 +00007694 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7696 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007697
7698 /*
7699 * In case of session resuming, invert the client and server
7700 * ChangeCipherSpec messages order.
7701 */
Paul Bakker0a597072012-09-25 21:55:46 +00007702 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007707#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007709 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007711#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007712 }
7713 else
7714 ssl->state++;
7715
Paul Bakker48916f92012-09-16 19:57:18 +00007716 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007717 * Switch to our negotiated transform and session parameters for outbound
7718 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007724 {
7725 unsigned char i;
7726
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007727 /* Remember current epoch settings for resending */
7728 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007729 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007730
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007731 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007732 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007733
7734 /* Increment epoch */
7735 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007736 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007737 break;
7738
7739 /* The loop goes to its end iff the counter is wrapping */
7740 if( i == 0 )
7741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7743 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007744 }
7745 }
7746 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007748 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007749
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007750 ssl->transform_out = ssl->transform_negotiate;
7751 ssl->session_out = ssl->session_negotiate;
7752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7754 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7759 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007760 }
7761 }
7762#endif
7763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007765 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007767#endif
7768
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007769 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007770 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007772 return( ret );
7773 }
7774
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007775#if defined(MBEDTLS_SSL_PROTO_DTLS)
7776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7777 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7778 {
7779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7780 return( ret );
7781 }
7782#endif
7783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007785
7786 return( 0 );
7787}
7788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007790#define SSL_MAX_HASH_LEN 36
7791#else
7792#define SSL_MAX_HASH_LEN 12
7793#endif
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007796{
Paul Bakker23986e52011-04-24 08:57:21 +00007797 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007798 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007799 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007802
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007803 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Hanno Becker327c93b2018-08-15 13:56:18 +01007805 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808 return( ret );
7809 }
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007814 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7815 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007817 }
7818
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007819 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820#if defined(MBEDTLS_SSL_PROTO_SSL3)
7821 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007822 hash_len = 36;
7823 else
7824#endif
7825 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7828 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7832 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007834 }
7835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007837 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007840 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7841 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 }
7844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007846 ssl->verify_data_len = hash_len;
7847 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007848#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007849
Paul Bakker0a597072012-09-25 21:55:46 +00007850 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007853 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007857 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007860 }
7861 else
7862 ssl->state++;
7863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007865 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007867#endif
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007870
7871 return( 0 );
7872}
7873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7879 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7880 mbedtls_md5_init( &handshake->fin_md5 );
7881 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007882 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7883 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007884#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7886#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007887#if defined(MBEDTLS_USE_PSA_CRYPTO)
7888 handshake->fin_sha256_psa = psa_hash_operation_init();
7889 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7890#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007892 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007893#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007896#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007897 handshake->fin_sha384_psa = psa_hash_operation_init();
7898 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007899#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007901 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007902#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007905
7906 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007907
7908#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7909 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7910 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7911#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_DHM_C)
7914 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916#if defined(MBEDTLS_ECDH_C)
7917 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007918#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007919#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007920 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007921#if defined(MBEDTLS_SSL_CLI_C)
7922 handshake->ecjpake_cache = NULL;
7923 handshake->ecjpake_cache_len = 0;
7924#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007925#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007926
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007927#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007928 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007929#endif
7930
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007931#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7932 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7933#endif
Hanno Becker75173122019-02-06 16:18:31 +00007934
7935#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7936 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7937 mbedtls_pk_init( &handshake->peer_pubkey );
7938#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007939}
7940
Hanno Beckera18d1322018-01-03 14:27:32 +00007941void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7946 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007947
Hanno Beckerd56ed242018-01-03 15:32:51 +00007948#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 mbedtls_md_init( &transform->md_ctx_enc );
7950 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007951#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007952}
7953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957}
7958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007960{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007961 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007962 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007964 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007967 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968
7969 /*
7970 * Either the pointers are now NULL or cleared properly and can be freed.
7971 * Now allocate missing structures.
7972 */
7973 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007974 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007975 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007976 }
Paul Bakker48916f92012-09-16 19:57:18 +00007977
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007979 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007980 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007981 }
Paul Bakker48916f92012-09-16 19:57:18 +00007982
Paul Bakker82788fb2014-10-20 13:59:19 +02007983 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007984 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007985 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007986 }
Paul Bakker48916f92012-09-16 19:57:18 +00007987
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007988 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007989 if( ssl->handshake == NULL ||
7990 ssl->transform_negotiate == NULL ||
7991 ssl->session_negotiate == NULL )
7992 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 mbedtls_free( ssl->handshake );
7996 mbedtls_free( ssl->transform_negotiate );
7997 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007998
7999 ssl->handshake = NULL;
8000 ssl->transform_negotiate = NULL;
8001 ssl->session_negotiate = NULL;
8002
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008003 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008004 }
8005
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008006 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008008 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008009 ssl_handshake_params_init( ssl->handshake );
8010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008012 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8013 {
8014 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008015
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008016 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8017 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8018 else
8019 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008020
8021 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008022 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008023#endif
8024
Paul Bakker48916f92012-09-16 19:57:18 +00008025 return( 0 );
8026}
8027
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008028#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008029/* Dummy cookie callbacks for defaults */
8030static int ssl_cookie_write_dummy( void *ctx,
8031 unsigned char **p, unsigned char *end,
8032 const unsigned char *cli_id, size_t cli_id_len )
8033{
8034 ((void) ctx);
8035 ((void) p);
8036 ((void) end);
8037 ((void) cli_id);
8038 ((void) cli_id_len);
8039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008041}
8042
8043static int ssl_cookie_check_dummy( void *ctx,
8044 const unsigned char *cookie, size_t cookie_len,
8045 const unsigned char *cli_id, size_t cli_id_len )
8046{
8047 ((void) ctx);
8048 ((void) cookie);
8049 ((void) cookie_len);
8050 ((void) cli_id);
8051 ((void) cli_id_len);
8052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008054}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008055#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008056
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008057/* Once ssl->out_hdr as the address of the beginning of the
8058 * next outgoing record is set, deduce the other pointers.
8059 *
8060 * Note: For TLS, we save the implicit record sequence number
8061 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8062 * and the caller has to make sure there's space for this.
8063 */
8064
8065static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8066 mbedtls_ssl_transform *transform )
8067{
8068#if defined(MBEDTLS_SSL_PROTO_DTLS)
8069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8070 {
8071 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008072#if defined(MBEDTLS_SSL_CID)
8073 ssl->out_cid = ssl->out_ctr + 8;
8074 ssl->out_len = ssl->out_cid;
8075 if( transform != NULL )
8076 ssl->out_len += transform->out_cid_len;
8077#else /* MBEDTLS_SSL_CID */
8078 ssl->out_len = ssl->out_ctr + 8;
8079#endif /* MBEDTLS_SSL_CID */
8080 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008081 }
8082 else
8083#endif
8084 {
8085 ssl->out_ctr = ssl->out_hdr - 8;
8086 ssl->out_len = ssl->out_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008087#if defined(MBEDTLS_SSL_CID)
8088 ssl->out_cid = ssl->out_len;
8089#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008090 ssl->out_iv = ssl->out_hdr + 5;
8091 }
8092
8093 /* Adjust out_msg to make space for explicit IV, if used. */
8094 if( transform != NULL &&
8095 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8096 {
8097 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8098 }
8099 else
8100 ssl->out_msg = ssl->out_iv;
8101}
8102
8103/* Once ssl->in_hdr as the address of the beginning of the
8104 * next incoming record is set, deduce the other pointers.
8105 *
8106 * Note: For TLS, we save the implicit record sequence number
8107 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8108 * and the caller has to make sure there's space for this.
8109 */
8110
Hanno Becker79594fd2019-05-08 09:38:41 +01008111static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008112{
Hanno Becker79594fd2019-05-08 09:38:41 +01008113 /* This function sets the pointers to match the case
8114 * of unprotected TLS/DTLS records, with both ssl->in_iv
8115 * and ssl->in_msg pointing to the beginning of the record
8116 * content.
8117 *
8118 * When decrypting a protected record, ssl->in_msg
8119 * will be shifted to point to the beginning of the
8120 * record plaintext.
8121 */
8122
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008123#if defined(MBEDTLS_SSL_PROTO_DTLS)
8124 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8125 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008126 /* This sets the header pointers to match records
8127 * without CID. When we receive a record containing
8128 * a CID, the fields are shifted accordingly in
8129 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008130 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008131#if defined(MBEDTLS_SSL_CID)
8132 ssl->in_cid = ssl->in_ctr + 8;
8133 ssl->in_len = ssl->in_cid; /* Default: no CID */
8134#else /* MBEDTLS_SSL_CID */
8135 ssl->in_len = ssl->in_ctr + 8;
8136#endif /* MBEDTLS_SSL_CID */
8137 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008138 }
8139 else
8140#endif
8141 {
8142 ssl->in_ctr = ssl->in_hdr - 8;
8143 ssl->in_len = ssl->in_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008144#if defined(MBEDTLS_SSL_CID)
8145 ssl->in_cid = ssl->in_len;
8146#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008147 ssl->in_iv = ssl->in_hdr + 5;
8148 }
8149
Hanno Becker79594fd2019-05-08 09:38:41 +01008150 /* This will be adjusted at record decryption time. */
8151 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008152}
8153
Paul Bakker5121ce52009-01-03 21:22:43 +00008154/*
8155 * Initialize an SSL context
8156 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008157void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8158{
8159 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8160}
8161
8162/*
8163 * Setup an SSL context
8164 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008165
8166static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8167{
8168 /* Set the incoming and outgoing record pointers. */
8169#if defined(MBEDTLS_SSL_PROTO_DTLS)
8170 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8171 {
8172 ssl->out_hdr = ssl->out_buf;
8173 ssl->in_hdr = ssl->in_buf;
8174 }
8175 else
8176#endif /* MBEDTLS_SSL_PROTO_DTLS */
8177 {
8178 ssl->out_hdr = ssl->out_buf + 8;
8179 ssl->in_hdr = ssl->in_buf + 8;
8180 }
8181
8182 /* Derive other internal pointers. */
8183 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008184 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008185}
8186
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008187int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008188 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008189{
Paul Bakker48916f92012-09-16 19:57:18 +00008190 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008191
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008192 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008193
8194 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008195 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008196 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008197
8198 /* Set to NULL in case of an error condition */
8199 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008200
Angus Grattond8213d02016-05-25 20:56:48 +10008201 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8202 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008203 {
Angus Grattond8213d02016-05-25 20:56:48 +10008204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008205 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008206 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008207 }
8208
8209 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8210 if( ssl->out_buf == NULL )
8211 {
8212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008213 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008214 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008215 }
8216
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008217 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008218
Paul Bakker48916f92012-09-16 19:57:18 +00008219 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008220 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008221
8222 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008223
8224error:
8225 mbedtls_free( ssl->in_buf );
8226 mbedtls_free( ssl->out_buf );
8227
8228 ssl->conf = NULL;
8229
8230 ssl->in_buf = NULL;
8231 ssl->out_buf = NULL;
8232
8233 ssl->in_hdr = NULL;
8234 ssl->in_ctr = NULL;
8235 ssl->in_len = NULL;
8236 ssl->in_iv = NULL;
8237 ssl->in_msg = NULL;
8238
8239 ssl->out_hdr = NULL;
8240 ssl->out_ctr = NULL;
8241 ssl->out_len = NULL;
8242 ssl->out_iv = NULL;
8243 ssl->out_msg = NULL;
8244
k-stachowiak9f7798e2018-07-31 16:52:32 +02008245 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008246}
8247
8248/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008249 * Reset an initialized and used SSL context for re-use while retaining
8250 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008251 *
8252 * If partial is non-zero, keep data in the input buffer and client ID.
8253 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008254 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008255static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008256{
Paul Bakker48916f92012-09-16 19:57:18 +00008257 int ret;
8258
Hanno Becker7e772132018-08-10 12:38:21 +01008259#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8260 !defined(MBEDTLS_SSL_SRV_C)
8261 ((void) partial);
8262#endif
8263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008265
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008266 /* Cancel any possibly running timer */
8267 ssl_set_timer( ssl, 0 );
8268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269#if defined(MBEDTLS_SSL_RENEGOTIATION)
8270 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008271 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008272
8273 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8275 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008278
Paul Bakker7eb013f2011-10-06 12:37:39 +00008279 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008280 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008281
8282 ssl->in_msgtype = 0;
8283 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008285 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008286 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008287#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008289 ssl_dtls_replay_reset( ssl );
8290#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008291
8292 ssl->in_hslen = 0;
8293 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008294
8295 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008296
8297 ssl->out_msgtype = 0;
8298 ssl->out_msglen = 0;
8299 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8301 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008302 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008303#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008304
Hanno Becker19859472018-08-06 09:40:20 +01008305 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8306
Paul Bakker48916f92012-09-16 19:57:18 +00008307 ssl->transform_in = NULL;
8308 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008309
Hanno Becker78640902018-08-13 16:35:15 +01008310 ssl->session_in = NULL;
8311 ssl->session_out = NULL;
8312
Angus Grattond8213d02016-05-25 20:56:48 +10008313 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008314
8315#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008316 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008317#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8318 {
8319 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008320 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008321 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8324 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8327 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8330 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008331 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008332 }
8333#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008334
Paul Bakker48916f92012-09-16 19:57:18 +00008335 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 mbedtls_ssl_transform_free( ssl->transform );
8338 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008339 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008340 }
Paul Bakker48916f92012-09-16 19:57:18 +00008341
Paul Bakkerc0463502013-02-14 11:19:38 +01008342 if( ssl->session )
8343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 mbedtls_ssl_session_free( ssl->session );
8345 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008346 ssl->session = NULL;
8347 }
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008350 ssl->alpn_chosen = NULL;
8351#endif
8352
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008353#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008354#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008355 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008356#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008357 {
8358 mbedtls_free( ssl->cli_id );
8359 ssl->cli_id = NULL;
8360 ssl->cli_id_len = 0;
8361 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008362#endif
8363
Paul Bakker48916f92012-09-16 19:57:18 +00008364 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8365 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008366
8367 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008368}
8369
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008370/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008371 * Reset an initialized and used SSL context for re-use while retaining
8372 * all application-set variables, function pointers and data.
8373 */
8374int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8375{
8376 return( ssl_session_reset_int( ssl, 0 ) );
8377}
8378
8379/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008380 * SSL set accessors
8381 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008382void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008383{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008384 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008385}
8386
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008387void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008388{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008389 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008390}
8391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008395 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008396}
8397#endif
8398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008400void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008402 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008403}
8404#endif
8405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008407
Hanno Becker1841b0a2018-08-24 11:13:57 +01008408void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8409 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008410{
8411 ssl->disable_datagram_packing = !allow_packing;
8412}
8413
8414void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8415 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008416{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008417 conf->hs_timeout_min = min;
8418 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008419}
8420#endif
8421
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008422void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008423{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008424 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008425}
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008428void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008429 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008430 void *p_vrfy )
8431{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008432 conf->f_vrfy = f_vrfy;
8433 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008436
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008437void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008438 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008439 void *p_rng )
8440{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008441 conf->f_rng = f_rng;
8442 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008443}
8444
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008445void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008446 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008447 void *p_dbg )
8448{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008449 conf->f_dbg = f_dbg;
8450 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008451}
8452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008454 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008455 mbedtls_ssl_send_t *f_send,
8456 mbedtls_ssl_recv_t *f_recv,
8457 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008458{
8459 ssl->p_bio = p_bio;
8460 ssl->f_send = f_send;
8461 ssl->f_recv = f_recv;
8462 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008463}
8464
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008465#if defined(MBEDTLS_SSL_PROTO_DTLS)
8466void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8467{
8468 ssl->mtu = mtu;
8469}
8470#endif
8471
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008472void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008473{
8474 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008475}
8476
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008477void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8478 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008479 mbedtls_ssl_set_timer_t *f_set_timer,
8480 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008481{
8482 ssl->p_timer = p_timer;
8483 ssl->f_set_timer = f_set_timer;
8484 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008485
8486 /* Make sure we start with no timer running */
8487 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008488}
8489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008491void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008492 void *p_cache,
8493 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8494 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008495{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008496 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008497 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008498 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#if defined(MBEDTLS_SSL_CLI_C)
8503int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008504{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008505 int ret;
8506
8507 if( ssl == NULL ||
8508 session == NULL ||
8509 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008510 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008513 }
8514
Hanno Becker52055ae2019-02-06 14:30:46 +00008515 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8516 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008517 return( ret );
8518
Paul Bakker0a597072012-09-25 21:55:46 +00008519 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008520
8521 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008524
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008525void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008526 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008527{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008528 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8529 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8530 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8531 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008532}
8533
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008534void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008535 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008536 int major, int minor )
8537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008539 return;
8540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008541 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008542 return;
8543
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008544 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008545}
8546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008548void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008549 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008550{
8551 conf->cert_profile = profile;
8552}
8553
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008554/* Append a new keycert entry to a (possibly empty) list */
8555static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8556 mbedtls_x509_crt *cert,
8557 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008558{
niisato8ee24222018-06-25 19:05:48 +09008559 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008560
niisato8ee24222018-06-25 19:05:48 +09008561 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8562 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008563 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008564
niisato8ee24222018-06-25 19:05:48 +09008565 new_cert->cert = cert;
8566 new_cert->key = key;
8567 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008568
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008569 /* Update head is the list was null, else add to the end */
8570 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008571 {
niisato8ee24222018-06-25 19:05:48 +09008572 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008573 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008574 else
8575 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008576 mbedtls_ssl_key_cert *cur = *head;
8577 while( cur->next != NULL )
8578 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008579 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008580 }
8581
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008582 return( 0 );
8583}
8584
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008585int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008586 mbedtls_x509_crt *own_cert,
8587 mbedtls_pk_context *pk_key )
8588{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008589 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008590}
8591
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008592void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008593 mbedtls_x509_crt *ca_chain,
8594 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008595{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008596 conf->ca_chain = ca_chain;
8597 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008598
8599#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8600 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8601 * cannot be used together. */
8602 conf->f_ca_cb = NULL;
8603 conf->p_ca_cb = NULL;
8604#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008605}
Hanno Becker5adaad92019-03-27 16:54:37 +00008606
8607#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8608void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008609 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008610 void *p_ca_cb )
8611{
8612 conf->f_ca_cb = f_ca_cb;
8613 conf->p_ca_cb = p_ca_cb;
8614
8615 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8616 * cannot be used together. */
8617 conf->ca_chain = NULL;
8618 conf->ca_crl = NULL;
8619}
8620#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008622
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008623#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8624int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8625 mbedtls_x509_crt *own_cert,
8626 mbedtls_pk_context *pk_key )
8627{
8628 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8629 own_cert, pk_key ) );
8630}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008631
8632void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8633 mbedtls_x509_crt *ca_chain,
8634 mbedtls_x509_crl *ca_crl )
8635{
8636 ssl->handshake->sni_ca_chain = ca_chain;
8637 ssl->handshake->sni_ca_crl = ca_crl;
8638}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008639
8640void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8641 int authmode )
8642{
8643 ssl->handshake->sni_authmode = authmode;
8644}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008645#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8646
Hanno Becker8927c832019-04-03 12:52:50 +01008647#if defined(MBEDTLS_X509_CRT_PARSE_C)
8648void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8649 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8650 void *p_vrfy )
8651{
8652 ssl->f_vrfy = f_vrfy;
8653 ssl->p_vrfy = p_vrfy;
8654}
8655#endif
8656
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008657#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008658/*
8659 * Set EC J-PAKE password for current handshake
8660 */
8661int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8662 const unsigned char *pw,
8663 size_t pw_len )
8664{
8665 mbedtls_ecjpake_role role;
8666
Janos Follath8eb64132016-06-03 15:40:57 +01008667 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8669
8670 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8671 role = MBEDTLS_ECJPAKE_SERVER;
8672 else
8673 role = MBEDTLS_ECJPAKE_CLIENT;
8674
8675 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8676 role,
8677 MBEDTLS_MD_SHA256,
8678 MBEDTLS_ECP_DP_SECP256R1,
8679 pw, pw_len ) );
8680}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008681#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008683#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008684
8685static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8686{
8687 /* Remove reference to existing PSK, if any. */
8688#if defined(MBEDTLS_USE_PSA_CRYPTO)
8689 if( conf->psk_opaque != 0 )
8690 {
8691 /* The maintenance of the PSK key slot is the
8692 * user's responsibility. */
8693 conf->psk_opaque = 0;
8694 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008695 /* This and the following branch should never
8696 * be taken simultaenously as we maintain the
8697 * invariant that raw and opaque PSKs are never
8698 * configured simultaneously. As a safeguard,
8699 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008700#endif /* MBEDTLS_USE_PSA_CRYPTO */
8701 if( conf->psk != NULL )
8702 {
8703 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8704
8705 mbedtls_free( conf->psk );
8706 conf->psk = NULL;
8707 conf->psk_len = 0;
8708 }
8709
8710 /* Remove reference to PSK identity, if any. */
8711 if( conf->psk_identity != NULL )
8712 {
8713 mbedtls_free( conf->psk_identity );
8714 conf->psk_identity = NULL;
8715 conf->psk_identity_len = 0;
8716 }
8717}
8718
Hanno Becker7390c712018-11-15 13:33:04 +00008719/* This function assumes that PSK identity in the SSL config is unset.
8720 * It checks that the provided identity is well-formed and attempts
8721 * to make a copy of it in the SSL config.
8722 * On failure, the PSK identity in the config remains unset. */
8723static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8724 unsigned char const *psk_identity,
8725 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008726{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008727 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008728 if( psk_identity == NULL ||
8729 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008730 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008731 {
8732 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8733 }
8734
Hanno Becker7390c712018-11-15 13:33:04 +00008735 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8736 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008737 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008738
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008739 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008740 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008741
8742 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008743}
8744
Hanno Becker7390c712018-11-15 13:33:04 +00008745int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8746 const unsigned char *psk, size_t psk_len,
8747 const unsigned char *psk_identity, size_t psk_identity_len )
8748{
8749 int ret;
8750 /* Remove opaque/raw PSK + PSK Identity */
8751 ssl_conf_remove_psk( conf );
8752
8753 /* Check and set raw PSK */
8754 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8756 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8757 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8758 conf->psk_len = psk_len;
8759 memcpy( conf->psk, psk, conf->psk_len );
8760
8761 /* Check and set PSK Identity */
8762 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8763 if( ret != 0 )
8764 ssl_conf_remove_psk( conf );
8765
8766 return( ret );
8767}
8768
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008769static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8770{
8771#if defined(MBEDTLS_USE_PSA_CRYPTO)
8772 if( ssl->handshake->psk_opaque != 0 )
8773 {
8774 ssl->handshake->psk_opaque = 0;
8775 }
8776 else
8777#endif /* MBEDTLS_USE_PSA_CRYPTO */
8778 if( ssl->handshake->psk != NULL )
8779 {
8780 mbedtls_platform_zeroize( ssl->handshake->psk,
8781 ssl->handshake->psk_len );
8782 mbedtls_free( ssl->handshake->psk );
8783 ssl->handshake->psk_len = 0;
8784 }
8785}
8786
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008787int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8788 const unsigned char *psk, size_t psk_len )
8789{
8790 if( psk == NULL || ssl->handshake == NULL )
8791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8792
8793 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8795
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008796 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008797
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008798 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008799 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008800
8801 ssl->handshake->psk_len = psk_len;
8802 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8803
8804 return( 0 );
8805}
8806
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008807#if defined(MBEDTLS_USE_PSA_CRYPTO)
8808int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008809 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008810 const unsigned char *psk_identity,
8811 size_t psk_identity_len )
8812{
Hanno Becker7390c712018-11-15 13:33:04 +00008813 int ret;
8814 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008815 ssl_conf_remove_psk( conf );
8816
Hanno Becker7390c712018-11-15 13:33:04 +00008817 /* Check and set opaque PSK */
8818 if( psk_slot == 0 )
8819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008820 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008821
8822 /* Check and set PSK Identity */
8823 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8824 psk_identity_len );
8825 if( ret != 0 )
8826 ssl_conf_remove_psk( conf );
8827
8828 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008829}
8830
8831int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008832 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008833{
8834 if( psk_slot == 0 || ssl->handshake == NULL )
8835 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8836
8837 ssl_remove_psk( ssl );
8838 ssl->handshake->psk_opaque = psk_slot;
8839 return( 0 );
8840}
8841#endif /* MBEDTLS_USE_PSA_CRYPTO */
8842
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008843void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008845 size_t),
8846 void *p_psk )
8847{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008848 conf->f_psk = f_psk;
8849 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008852
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008853#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008854
8855#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008856int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008857{
8858 int ret;
8859
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008860 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8861 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8862 {
8863 mbedtls_mpi_free( &conf->dhm_P );
8864 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008865 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008866 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008867
8868 return( 0 );
8869}
Hanno Becker470a8c42017-10-04 15:28:46 +01008870#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008871
Hanno Beckera90658f2017-10-04 15:29:08 +01008872int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8873 const unsigned char *dhm_P, size_t P_len,
8874 const unsigned char *dhm_G, size_t G_len )
8875{
8876 int ret;
8877
8878 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8879 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8880 {
8881 mbedtls_mpi_free( &conf->dhm_P );
8882 mbedtls_mpi_free( &conf->dhm_G );
8883 return( ret );
8884 }
8885
8886 return( 0 );
8887}
Paul Bakker5121ce52009-01-03 21:22:43 +00008888
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008889int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008890{
8891 int ret;
8892
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008893 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8894 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8895 {
8896 mbedtls_mpi_free( &conf->dhm_P );
8897 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008898 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008899 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008900
8901 return( 0 );
8902}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008903#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008904
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008905#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8906/*
8907 * Set the minimum length for Diffie-Hellman parameters
8908 */
8909void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8910 unsigned int bitlen )
8911{
8912 conf->dhm_min_bitlen = bitlen;
8913}
8914#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8915
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008916#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008917/*
8918 * Set allowed/preferred hashes for handshake signatures
8919 */
8920void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8921 const int *hashes )
8922{
8923 conf->sig_hashes = hashes;
8924}
Hanno Becker947194e2017-04-07 13:25:49 +01008925#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008926
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008927#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008928/*
8929 * Set the allowed elliptic curves
8930 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008931void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008932 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008934 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008935}
Hanno Becker947194e2017-04-07 13:25:49 +01008936#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008937
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008938#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008940{
Hanno Becker947194e2017-04-07 13:25:49 +01008941 /* Initialize to suppress unnecessary compiler warning */
8942 size_t hostname_len = 0;
8943
8944 /* Check if new hostname is valid before
8945 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008946 if( hostname != NULL )
8947 {
8948 hostname_len = strlen( hostname );
8949
8950 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8952 }
8953
8954 /* Now it's clear that we will overwrite the old hostname,
8955 * so we can free it safely */
8956
8957 if( ssl->hostname != NULL )
8958 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008959 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008960 mbedtls_free( ssl->hostname );
8961 }
8962
8963 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008964
Paul Bakker5121ce52009-01-03 21:22:43 +00008965 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008966 {
8967 ssl->hostname = NULL;
8968 }
8969 else
8970 {
8971 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008972 if( ssl->hostname == NULL )
8973 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008974
Hanno Becker947194e2017-04-07 13:25:49 +01008975 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008976
Hanno Becker947194e2017-04-07 13:25:49 +01008977 ssl->hostname[hostname_len] = '\0';
8978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008979
8980 return( 0 );
8981}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008982#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008983
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008984#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008985void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008987 const unsigned char *, size_t),
8988 void *p_sni )
8989{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008990 conf->f_sni = f_sni;
8991 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008996int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008997{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008998 size_t cur_len, tot_len;
8999 const char **p;
9000
9001 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009002 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9003 * MUST NOT be truncated."
9004 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009005 */
9006 tot_len = 0;
9007 for( p = protos; *p != NULL; p++ )
9008 {
9009 cur_len = strlen( *p );
9010 tot_len += cur_len;
9011
9012 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009014 }
9015
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009016 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009017
9018 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009019}
9020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009022{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009023 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009024}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009026
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009027void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009028{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009029 conf->max_major_ver = major;
9030 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009031}
9032
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009033void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009034{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009035 conf->min_major_ver = major;
9036 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009037}
9038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009040void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009041{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009042 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009043}
9044#endif
9045
Janos Follath088ce432017-04-10 12:42:31 +01009046#if defined(MBEDTLS_SSL_SRV_C)
9047void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9048 char cert_req_ca_list )
9049{
9050 conf->cert_req_ca_list = cert_req_ca_list;
9051}
9052#endif
9053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009055void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009056{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009057 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009058}
9059#endif
9060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009062void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009064 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009065}
9066#endif
9067
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009068#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009069void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009071 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009072}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009073#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009076int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009079 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009082 }
9083
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009084 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009085
9086 return( 0 );
9087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009091void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009092{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009093 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009098void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009099{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009100 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009101}
9102#endif
9103
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009104void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009106 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009107}
9108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009110void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009111{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009112 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009113}
9114
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009115void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009117 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009118}
9119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009120void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009121 const unsigned char period[8] )
9122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009123 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009128#if defined(MBEDTLS_SSL_CLI_C)
9129void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009130{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009131 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009132}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009133#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009134
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009135#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009136void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9137 mbedtls_ssl_ticket_write_t *f_ticket_write,
9138 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9139 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009140{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009141 conf->f_ticket_write = f_ticket_write;
9142 conf->f_ticket_parse = f_ticket_parse;
9143 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009144}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009147
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009148#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9149void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9150 mbedtls_ssl_export_keys_t *f_export_keys,
9151 void *p_export_keys )
9152{
9153 conf->f_export_keys = f_export_keys;
9154 conf->p_export_keys = p_export_keys;
9155}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009156
9157void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9158 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9159 void *p_export_keys )
9160{
9161 conf->f_export_keys_ext = f_export_keys_ext;
9162 conf->p_export_keys = p_export_keys;
9163}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009164#endif
9165
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009166#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009167void mbedtls_ssl_conf_async_private_cb(
9168 mbedtls_ssl_config *conf,
9169 mbedtls_ssl_async_sign_t *f_async_sign,
9170 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9171 mbedtls_ssl_async_resume_t *f_async_resume,
9172 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009173 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009174{
9175 conf->f_async_sign_start = f_async_sign;
9176 conf->f_async_decrypt_start = f_async_decrypt;
9177 conf->f_async_resume = f_async_resume;
9178 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009179 conf->p_async_config_data = async_config_data;
9180}
9181
Gilles Peskine8f97af72018-04-26 11:46:10 +02009182void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9183{
9184 return( conf->p_async_config_data );
9185}
9186
Gilles Peskine1febfef2018-04-30 11:54:39 +02009187void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009188{
9189 if( ssl->handshake == NULL )
9190 return( NULL );
9191 else
9192 return( ssl->handshake->user_async_ctx );
9193}
9194
Gilles Peskine1febfef2018-04-30 11:54:39 +02009195void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009196 void *ctx )
9197{
9198 if( ssl->handshake != NULL )
9199 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009200}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009201#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009202
Paul Bakker5121ce52009-01-03 21:22:43 +00009203/*
9204 * SSL get accessors
9205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009207{
9208 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9209}
9210
Hanno Becker8b170a02017-10-10 11:51:19 +01009211int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9212{
9213 /*
9214 * Case A: We're currently holding back
9215 * a message for further processing.
9216 */
9217
9218 if( ssl->keep_current_message == 1 )
9219 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009221 return( 1 );
9222 }
9223
9224 /*
9225 * Case B: Further records are pending in the current datagram.
9226 */
9227
9228#if defined(MBEDTLS_SSL_PROTO_DTLS)
9229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9230 ssl->in_left > ssl->next_record_offset )
9231 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009233 return( 1 );
9234 }
9235#endif /* MBEDTLS_SSL_PROTO_DTLS */
9236
9237 /*
9238 * Case C: A handshake message is being processed.
9239 */
9240
Hanno Becker8b170a02017-10-10 11:51:19 +01009241 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9242 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009244 return( 1 );
9245 }
9246
9247 /*
9248 * Case D: An application data message is being processed
9249 */
9250 if( ssl->in_offt != NULL )
9251 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009253 return( 1 );
9254 }
9255
9256 /*
9257 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009258 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009259 * we implement support for multiple alerts in single records.
9260 */
9261
9262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9263 return( 0 );
9264}
9265
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009266uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009267{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009268 if( ssl->session != NULL )
9269 return( ssl->session->verify_result );
9270
9271 if( ssl->session_negotiate != NULL )
9272 return( ssl->session_negotiate->verify_result );
9273
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009274 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009275}
9276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009278{
Paul Bakker926c8e42013-03-06 10:23:34 +01009279 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009280 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009283}
9284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009289 {
9290 switch( ssl->minor_ver )
9291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009293 return( "DTLSv1.0" );
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009296 return( "DTLSv1.2" );
9297
9298 default:
9299 return( "unknown (DTLS)" );
9300 }
9301 }
9302#endif
9303
Paul Bakker43ca69c2011-01-15 17:35:19 +00009304 switch( ssl->minor_ver )
9305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009307 return( "SSLv3.0" );
9308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009310 return( "TLSv1.0" );
9311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009313 return( "TLSv1.1" );
9314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009316 return( "TLSv1.2" );
9317
Paul Bakker43ca69c2011-01-15 17:35:19 +00009318 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009319 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321}
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009324{
Hanno Becker3136ede2018-08-17 15:28:19 +01009325 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009327 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009328
Hanno Becker5903de42019-05-03 14:46:38 +01009329 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9330
Hanno Becker78640902018-08-13 16:35:15 +01009331 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009332 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#if defined(MBEDTLS_ZLIB_SUPPORT)
9335 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9336 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009337#endif
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 case MBEDTLS_MODE_GCM:
9342 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009343 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009345 transform_expansion = transform->minlen;
9346 break;
9347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009349
9350 block_size = mbedtls_cipher_get_block_size(
9351 &transform->cipher_ctx_enc );
9352
Hanno Becker3136ede2018-08-17 15:28:19 +01009353 /* Expansion due to the addition of the MAC. */
9354 transform_expansion += transform->maclen;
9355
9356 /* Expansion due to the addition of CBC padding;
9357 * Theoretically up to 256 bytes, but we never use
9358 * more than the block size of the underlying cipher. */
9359 transform_expansion += block_size;
9360
9361 /* For TLS 1.1 or higher, an explicit IV is added
9362 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009363#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9364 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009365 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009366#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009367
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009368 break;
9369
9370 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009373 }
9374
Hanno Becker6cbad552019-05-08 15:40:11 +01009375#if defined(MBEDTLS_SSL_CID)
9376 if( transform->out_cid_len != 0 )
9377 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
9378#endif /* MBEDTLS_SSL_CID */
9379
Hanno Becker5903de42019-05-03 14:46:38 +01009380 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009381}
9382
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009383#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9384size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9385{
9386 size_t max_len;
9387
9388 /*
9389 * Assume mfl_code is correct since it was checked when set
9390 */
Angus Grattond8213d02016-05-25 20:56:48 +10009391 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009392
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009393 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009394 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009395 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009396 {
Angus Grattond8213d02016-05-25 20:56:48 +10009397 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009398 }
9399
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009400 /* During a handshake, use the value being negotiated */
9401 if( ssl->session_negotiate != NULL &&
9402 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9403 {
9404 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9405 }
9406
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009407 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009408}
9409#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9410
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009411#if defined(MBEDTLS_SSL_PROTO_DTLS)
9412static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9413{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009414 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9415 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9416 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9417 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9418 return ( 0 );
9419
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009420 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9421 return( ssl->mtu );
9422
9423 if( ssl->mtu == 0 )
9424 return( ssl->handshake->mtu );
9425
9426 return( ssl->mtu < ssl->handshake->mtu ?
9427 ssl->mtu : ssl->handshake->mtu );
9428}
9429#endif /* MBEDTLS_SSL_PROTO_DTLS */
9430
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009431int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9432{
9433 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9434
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009435#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9436 !defined(MBEDTLS_SSL_PROTO_DTLS)
9437 (void) ssl;
9438#endif
9439
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009440#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9441 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9442
9443 if( max_len > mfl )
9444 max_len = mfl;
9445#endif
9446
9447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009448 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009449 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009450 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009451 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9452 const size_t overhead = (size_t) ret;
9453
9454 if( ret < 0 )
9455 return( ret );
9456
9457 if( mtu <= overhead )
9458 {
9459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9460 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9461 }
9462
9463 if( max_len > mtu - overhead )
9464 max_len = mtu - overhead;
9465 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009466#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009467
Hanno Becker0defedb2018-08-10 12:35:02 +01009468#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9469 !defined(MBEDTLS_SSL_PROTO_DTLS)
9470 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009471#endif
9472
9473 return( (int) max_len );
9474}
9475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476#if defined(MBEDTLS_X509_CRT_PARSE_C)
9477const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009478{
9479 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009480 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009481
Hanno Beckere6824572019-02-07 13:18:46 +00009482#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009483 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009484#else
9485 return( NULL );
9486#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009491int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9492 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009493{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009494 if( ssl == NULL ||
9495 dst == NULL ||
9496 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009497 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009500 }
9501
Hanno Becker52055ae2019-02-06 14:30:46 +00009502 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009505
Paul Bakker5121ce52009-01-03 21:22:43 +00009506/*
Paul Bakker1961b702013-01-25 14:49:24 +01009507 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009512
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009513 if( ssl == NULL || ssl->conf == NULL )
9514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009517 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009521 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009523#endif
9524
Paul Bakker1961b702013-01-25 14:49:24 +01009525 return( ret );
9526}
9527
9528/*
9529 * Perform the SSL handshake
9530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009532{
9533 int ret = 0;
9534
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009535 if( ssl == NULL || ssl->conf == NULL )
9536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009543
9544 if( ret != 0 )
9545 break;
9546 }
9547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009549
9550 return( ret );
9551}
9552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553#if defined(MBEDTLS_SSL_RENEGOTIATION)
9554#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009555/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009556 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009559{
9560 int ret;
9561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009563
9564 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9566 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009567
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009568 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009569 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009571 return( ret );
9572 }
9573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009575
9576 return( 0 );
9577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009579
9580/*
9581 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 * - any side: calling mbedtls_ssl_renegotiate(),
9583 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9584 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009585 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009586 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009590{
9591 int ret;
9592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009594
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009595 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9596 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009597
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009598 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9599 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009601 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009603 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009604 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009605 ssl->handshake->out_msg_seq = 1;
9606 else
9607 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009608 }
9609#endif
9610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9612 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009617 return( ret );
9618 }
9619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009621
9622 return( 0 );
9623}
9624
9625/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009626 * Renegotiate current connection on client,
9627 * or request renegotiation on server
9628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009632
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009633 if( ssl == NULL || ssl->conf == NULL )
9634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009637 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009638 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009644
9645 /* Did we already try/start sending HelloRequest? */
9646 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009648
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009649 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009654 /*
9655 * On client, either start the renegotiation process or,
9656 * if already in progress, continue the handshake
9657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009662
9663 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009666 return( ret );
9667 }
9668 }
9669 else
9670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009674 return( ret );
9675 }
9676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009678
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009679 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009680}
9681
9682/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009683 * Check record counters and renegotiate if they're above the limit.
9684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009686{
Andres AG2196c7f2016-12-15 17:01:16 +00009687 size_t ep_len = ssl_ep_len( ssl );
9688 int in_ctr_cmp;
9689 int out_ctr_cmp;
9690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9692 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009693 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009694 {
9695 return( 0 );
9696 }
9697
Andres AG2196c7f2016-12-15 17:01:16 +00009698 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9699 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009700 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009701 ssl->conf->renego_period + ep_len, 8 - ep_len );
9702
9703 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009704 {
9705 return( 0 );
9706 }
9707
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009712
9713/*
9714 * Receive application data decrypted from the SSL layer
9715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009717{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009718 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009719 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009720
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009721 if( ssl == NULL || ssl->conf == NULL )
9722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009727 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009730 return( ret );
9731
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009732 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009734 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009735 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009736 return( ret );
9737 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009738 }
9739#endif
9740
Hanno Becker4a810fb2017-05-24 16:27:30 +01009741 /*
9742 * Check if renegotiation is necessary and/or handshake is
9743 * in process. If yes, perform/continue, and fall through
9744 * if an unexpected packet is received while the client
9745 * is waiting for the ServerHello.
9746 *
9747 * (There is no equivalent to the last condition on
9748 * the server-side as it is not treated as within
9749 * a handshake while waiting for the ClientHello
9750 * after a renegotiation request.)
9751 */
9752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009753#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009754 ret = ssl_check_ctr_renegotiate( ssl );
9755 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9756 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009759 return( ret );
9760 }
9761#endif
9762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009766 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9767 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009770 return( ret );
9771 }
9772 }
9773
Hanno Beckere41158b2017-10-23 13:30:32 +01009774 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009775 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009776 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009777 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009778 if( ssl->f_get_timer != NULL &&
9779 ssl->f_get_timer( ssl->p_timer ) == -1 )
9780 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009781 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009782 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009783
Hanno Becker327c93b2018-08-15 13:56:18 +01009784 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009785 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009786 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9787 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009788
Hanno Becker4a810fb2017-05-24 16:27:30 +01009789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9790 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009791 }
9792
9793 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009795 {
9796 /*
9797 * OpenSSL sends empty messages to randomize the IV
9798 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009799 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009802 return( 0 );
9803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009805 return( ret );
9806 }
9807 }
9808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009812
Hanno Becker4a810fb2017-05-24 16:27:30 +01009813 /*
9814 * - For client-side, expect SERVER_HELLO_REQUEST.
9815 * - For server-side, expect CLIENT_HELLO.
9816 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9817 */
9818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009820 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009822 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009825
9826 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009828 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009829 {
9830 continue;
9831 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009832#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009834 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009835#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009836
Hanno Becker4a810fb2017-05-24 16:27:30 +01009837#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009838 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009842
9843 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009846 {
9847 continue;
9848 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009851 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852#endif /* MBEDTLS_SSL_SRV_C */
9853
Hanno Becker21df7f92017-10-17 11:03:26 +01009854#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009855 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009856 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9857 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9858 ssl->conf->allow_legacy_renegotiation ==
9859 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9860 {
9861 /*
9862 * Accept renegotiation request
9863 */
Paul Bakker48916f92012-09-16 19:57:18 +00009864
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009865 /* DTLS clients need to know renego is server-initiated */
9866#if defined(MBEDTLS_SSL_PROTO_DTLS)
9867 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9868 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9869 {
9870 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9871 }
9872#endif
9873 ret = ssl_start_renegotiation( ssl );
9874 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9875 ret != 0 )
9876 {
9877 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9878 return( ret );
9879 }
9880 }
9881 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009882#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009883 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009884 /*
9885 * Refuse renegotiation
9886 */
9887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890#if defined(MBEDTLS_SSL_PROTO_SSL3)
9891 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009892 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009893 /* SSLv3 does not have a "no_renegotiation" warning, so
9894 we send a fatal alert and abort the connection. */
9895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9896 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9897 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009898 }
9899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9901#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9902 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9903 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9906 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9907 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009908 {
9909 return( ret );
9910 }
Paul Bakker48916f92012-09-16 19:57:18 +00009911 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009912 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9914 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009918 }
Paul Bakker48916f92012-09-16 19:57:18 +00009919 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009920
Hanno Becker90333da2017-10-10 11:27:13 +01009921 /* At this point, we don't know whether the renegotiation has been
9922 * completed or not. The cases to consider are the following:
9923 * 1) The renegotiation is complete. In this case, no new record
9924 * has been read yet.
9925 * 2) The renegotiation is incomplete because the client received
9926 * an application data record while awaiting the ServerHello.
9927 * 3) The renegotiation is incomplete because the client received
9928 * a non-handshake, non-application data message while awaiting
9929 * the ServerHello.
9930 * In each of these case, looping will be the proper action:
9931 * - For 1), the next iteration will read a new record and check
9932 * if it's application data.
9933 * - For 2), the loop condition isn't satisfied as application data
9934 * is present, hence continue is the same as break
9935 * - For 3), the loop condition is satisfied and read_record
9936 * will re-deliver the message that was held back by the client
9937 * when expecting the ServerHello.
9938 */
9939 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009940 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009941#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009943 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009944 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009945 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009946 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009949 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009951 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009952 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009953 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9957 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009960 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009961 }
9962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9966 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009967 }
9968
9969 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009970
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009971 /* We're going to return something now, cancel timer,
9972 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009974 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009975
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009976#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009977 /* If we requested renego but received AppData, resend HelloRequest.
9978 * Do it now, after setting in_offt, to avoid taking this branch
9979 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009981 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009983 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009984 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009987 return( ret );
9988 }
9989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009991#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009992 }
9993
9994 n = ( len < ssl->in_msglen )
9995 ? len : ssl->in_msglen;
9996
9997 memcpy( buf, ssl->in_offt, n );
9998 ssl->in_msglen -= n;
9999
10000 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010001 {
10002 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010003 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010004 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010005 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010006 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010007 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010008 /* more data available */
10009 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010010 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010013
Paul Bakker23986e52011-04-24 08:57:21 +000010014 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010015}
10016
10017/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010018 * Send application data to be encrypted by the SSL layer, taking care of max
10019 * fragment length and buffer size.
10020 *
10021 * According to RFC 5246 Section 6.2.1:
10022 *
10023 * Zero-length fragments of Application data MAY be sent as they are
10024 * potentially useful as a traffic analysis countermeasure.
10025 *
10026 * Therefore, it is possible that the input message length is 0 and the
10027 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010028 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010029static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010030 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010031{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010032 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10033 const size_t max_len = (size_t) ret;
10034
10035 if( ret < 0 )
10036 {
10037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10038 return( ret );
10039 }
10040
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010041 if( len > max_len )
10042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010044 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010047 "maximum fragment length: %d > %d",
10048 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010050 }
10051 else
10052#endif
10053 len = max_len;
10054 }
Paul Bakker887bd502011-06-08 13:10:54 +000010055
Paul Bakker5121ce52009-01-03 21:22:43 +000010056 if( ssl->out_left != 0 )
10057 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010058 /*
10059 * The user has previously tried to send the data and
10060 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10061 * written. In this case, we expect the high-level write function
10062 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010067 return( ret );
10068 }
10069 }
Paul Bakker887bd502011-06-08 13:10:54 +000010070 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010071 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010072 /*
10073 * The user is trying to send a message the first time, so we need to
10074 * copy the data into the internal buffers and setup the data structure
10075 * to keep track of partial writes
10076 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010077 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010079 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010080
Hanno Becker67bc7c32018-08-06 11:33:50 +010010081 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010084 return( ret );
10085 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010086 }
10087
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010088 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010089}
10090
10091/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010092 * Write application data, doing 1/n-1 splitting if necessary.
10093 *
10094 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010095 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010096 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010099static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010100 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010101{
10102 int ret;
10103
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010104 if( ssl->conf->cbc_record_splitting ==
10105 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010106 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10108 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10109 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010110 {
10111 return( ssl_write_real( ssl, buf, len ) );
10112 }
10113
10114 if( ssl->split_done == 0 )
10115 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010116 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010117 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010118 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010119 }
10120
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010121 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10122 return( ret );
10123 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010124
10125 return( ret + 1 );
10126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010127#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010128
10129/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010130 * Write application data (public-facing wrapper)
10131 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010132int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010133{
10134 int ret;
10135
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010137
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010138 if( ssl == NULL || ssl->conf == NULL )
10139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10140
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010141#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010142 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10143 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010144 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010145 return( ret );
10146 }
10147#endif
10148
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010149 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010150 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010151 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010152 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010154 return( ret );
10155 }
10156 }
10157
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010158#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010159 ret = ssl_write_split( ssl, buf, len );
10160#else
10161 ret = ssl_write_real( ssl, buf, len );
10162#endif
10163
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010165
10166 return( ret );
10167}
10168
10169/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010170 * Notify the peer that the connection is being closed
10171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010173{
10174 int ret;
10175
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010176 if( ssl == NULL || ssl->conf == NULL )
10177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010180
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010181 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10187 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10188 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010191 return( ret );
10192 }
10193 }
10194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010197 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010198}
10199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010201{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010202 if( transform == NULL )
10203 return;
10204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010206 deflateEnd( &transform->ctx_deflate );
10207 inflateEnd( &transform->ctx_inflate );
10208#endif
10209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10211 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010212
Hanno Beckerd56ed242018-01-03 15:32:51 +000010213#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 mbedtls_md_free( &transform->md_ctx_enc );
10215 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010216#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010217
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010218 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010219}
10220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221#if defined(MBEDTLS_X509_CRT_PARSE_C)
10222static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010225
10226 while( cur != NULL )
10227 {
10228 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010230 cur = next;
10231 }
10232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010234
Hanno Becker0271f962018-08-16 13:23:47 +010010235#if defined(MBEDTLS_SSL_PROTO_DTLS)
10236
10237static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10238{
10239 unsigned offset;
10240 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10241
10242 if( hs == NULL )
10243 return;
10244
Hanno Becker283f5ef2018-08-24 09:34:47 +010010245 ssl_free_buffered_record( ssl );
10246
Hanno Becker0271f962018-08-16 13:23:47 +010010247 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010248 ssl_buffering_free_slot( ssl, offset );
10249}
10250
10251static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10252 uint8_t slot )
10253{
10254 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10255 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010256
10257 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10258 return;
10259
Hanno Beckere605b192018-08-21 15:59:07 +010010260 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010261 {
Hanno Beckere605b192018-08-21 15:59:07 +010010262 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010263 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010264 mbedtls_free( hs_buf->data );
10265 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010266 }
10267}
10268
10269#endif /* MBEDTLS_SSL_PROTO_DTLS */
10270
Gilles Peskine9b562d52018-04-25 20:32:43 +020010271void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010272{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010273 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10274
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010275 if( handshake == NULL )
10276 return;
10277
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010278#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10279 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10280 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010281 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010282 handshake->async_in_progress = 0;
10283 }
10284#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10285
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010286#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10287 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10288 mbedtls_md5_free( &handshake->fin_md5 );
10289 mbedtls_sha1_free( &handshake->fin_sha1 );
10290#endif
10291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10292#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010293#if defined(MBEDTLS_USE_PSA_CRYPTO)
10294 psa_hash_abort( &handshake->fin_sha256_psa );
10295#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010296 mbedtls_sha256_free( &handshake->fin_sha256 );
10297#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010298#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010299#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010300#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010301 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010302#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010303 mbedtls_sha512_free( &handshake->fin_sha512 );
10304#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010305#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010306#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010308#if defined(MBEDTLS_DHM_C)
10309 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311#if defined(MBEDTLS_ECDH_C)
10312 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010313#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010314#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010315 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010316#if defined(MBEDTLS_SSL_CLI_C)
10317 mbedtls_free( handshake->ecjpake_cache );
10318 handshake->ecjpake_cache = NULL;
10319 handshake->ecjpake_cache_len = 0;
10320#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010321#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010322
Janos Follath4ae5c292016-02-10 11:27:43 +000010323#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10324 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010325 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010327#endif
10328
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010329#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10330 if( handshake->psk != NULL )
10331 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010332 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010333 mbedtls_free( handshake->psk );
10334 }
10335#endif
10336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10338 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010339 /*
10340 * Free only the linked list wrapper, not the keys themselves
10341 * since the belong to the SNI callback
10342 */
10343 if( handshake->sni_key_cert != NULL )
10344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010346
10347 while( cur != NULL )
10348 {
10349 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010350 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010351 cur = next;
10352 }
10353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010355
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010356#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010357 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010358 if( handshake->ecrs_peer_cert != NULL )
10359 {
10360 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10361 mbedtls_free( handshake->ecrs_peer_cert );
10362 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010363#endif
10364
Hanno Becker75173122019-02-06 16:18:31 +000010365#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10366 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10367 mbedtls_pk_free( &handshake->peer_pubkey );
10368#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370#if defined(MBEDTLS_SSL_PROTO_DTLS)
10371 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010372 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010373 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010374#endif
10375
Hanno Becker4a63ed42019-01-08 11:39:35 +000010376#if defined(MBEDTLS_ECDH_C) && \
10377 defined(MBEDTLS_USE_PSA_CRYPTO)
10378 psa_destroy_key( handshake->ecdh_psa_privkey );
10379#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10380
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010381 mbedtls_platform_zeroize( handshake,
10382 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010383}
10384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010386{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010387 if( session == NULL )
10388 return;
10389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010391 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010392#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010393
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010394#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010396#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010397
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010398 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010399}
10400
Paul Bakker5121ce52009-01-03 21:22:43 +000010401/*
10402 * Free an SSL context
10403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010405{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010406 if( ssl == NULL )
10407 return;
10408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010410
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010411 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010412 {
Angus Grattond8213d02016-05-25 20:56:48 +100010413 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010415 }
10416
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010417 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010418 {
Angus Grattond8213d02016-05-25 20:56:48 +100010419 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010421 }
10422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010424 if( ssl->compress_buf != NULL )
10425 {
Angus Grattond8213d02016-05-25 20:56:48 +100010426 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010428 }
10429#endif
10430
Paul Bakker48916f92012-09-16 19:57:18 +000010431 if( ssl->transform )
10432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010433 mbedtls_ssl_transform_free( ssl->transform );
10434 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010435 }
10436
10437 if( ssl->handshake )
10438 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010439 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10441 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 mbedtls_free( ssl->handshake );
10444 mbedtls_free( ssl->transform_negotiate );
10445 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010446 }
10447
Paul Bakkerc0463502013-02-14 11:19:38 +010010448 if( ssl->session )
10449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 mbedtls_ssl_session_free( ssl->session );
10451 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010452 }
10453
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010454#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010455 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010456 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010457 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010459 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010460#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10463 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10466 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010467 }
10468#endif
10469
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010470#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010472#endif
10473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010475
Paul Bakker86f04f42013-02-14 11:20:09 +010010476 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010477 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010478}
10479
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010480/*
10481 * Initialze mbedtls_ssl_config
10482 */
10483void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10484{
10485 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10486}
10487
Simon Butcherc97b6972015-12-27 23:48:17 +000010488#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010489static int ssl_preset_default_hashes[] = {
10490#if defined(MBEDTLS_SHA512_C)
10491 MBEDTLS_MD_SHA512,
10492 MBEDTLS_MD_SHA384,
10493#endif
10494#if defined(MBEDTLS_SHA256_C)
10495 MBEDTLS_MD_SHA256,
10496 MBEDTLS_MD_SHA224,
10497#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010498#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010499 MBEDTLS_MD_SHA1,
10500#endif
10501 MBEDTLS_MD_NONE
10502};
Simon Butcherc97b6972015-12-27 23:48:17 +000010503#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010504
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010505static int ssl_preset_suiteb_ciphersuites[] = {
10506 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10507 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10508 0
10509};
10510
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010511#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010512static int ssl_preset_suiteb_hashes[] = {
10513 MBEDTLS_MD_SHA256,
10514 MBEDTLS_MD_SHA384,
10515 MBEDTLS_MD_NONE
10516};
10517#endif
10518
10519#if defined(MBEDTLS_ECP_C)
10520static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10521 MBEDTLS_ECP_DP_SECP256R1,
10522 MBEDTLS_ECP_DP_SECP384R1,
10523 MBEDTLS_ECP_DP_NONE
10524};
10525#endif
10526
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010527/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010528 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010529 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010530int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010531 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010532{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010533#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010534 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010535#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010536
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010537 /* Use the functions here so that they are covered in tests,
10538 * but otherwise access member directly for efficiency */
10539 mbedtls_ssl_conf_endpoint( conf, endpoint );
10540 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010541
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010542 /*
10543 * Things that are common to all presets
10544 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010545#if defined(MBEDTLS_SSL_CLI_C)
10546 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10547 {
10548 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10549#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10550 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10551#endif
10552 }
10553#endif
10554
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010555#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010557#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010558
10559#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10560 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10561#endif
10562
10563#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10564 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10565#endif
10566
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010567#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10568 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10569#endif
10570
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010571#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010572 conf->f_cookie_write = ssl_cookie_write_dummy;
10573 conf->f_cookie_check = ssl_cookie_check_dummy;
10574#endif
10575
10576#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10577 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10578#endif
10579
Janos Follath088ce432017-04-10 12:42:31 +010010580#if defined(MBEDTLS_SSL_SRV_C)
10581 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10582#endif
10583
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010584#if defined(MBEDTLS_SSL_PROTO_DTLS)
10585 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10586 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10587#endif
10588
10589#if defined(MBEDTLS_SSL_RENEGOTIATION)
10590 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010591 memset( conf->renego_period, 0x00, 2 );
10592 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010593#endif
10594
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010595#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10596 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10597 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010598 const unsigned char dhm_p[] =
10599 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10600 const unsigned char dhm_g[] =
10601 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10602
Hanno Beckera90658f2017-10-04 15:29:08 +010010603 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10604 dhm_p, sizeof( dhm_p ),
10605 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010606 {
10607 return( ret );
10608 }
10609 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010610#endif
10611
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010612 /*
10613 * Preset-specific defaults
10614 */
10615 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010616 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010617 /*
10618 * NSA Suite B
10619 */
10620 case MBEDTLS_SSL_PRESET_SUITEB:
10621 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10622 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10623 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10624 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10625
10626 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10627 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10628 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10629 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10630 ssl_preset_suiteb_ciphersuites;
10631
10632#if defined(MBEDTLS_X509_CRT_PARSE_C)
10633 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010634#endif
10635
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010636#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010637 conf->sig_hashes = ssl_preset_suiteb_hashes;
10638#endif
10639
10640#if defined(MBEDTLS_ECP_C)
10641 conf->curve_list = ssl_preset_suiteb_curves;
10642#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010643 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010644
10645 /*
10646 * Default
10647 */
10648 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010649 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10650 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10651 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10652 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10653 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10654 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10655 MBEDTLS_SSL_MIN_MINOR_VERSION :
10656 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010657 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10658 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10659
10660#if defined(MBEDTLS_SSL_PROTO_DTLS)
10661 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10662 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10663#endif
10664
10665 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10666 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10667 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10668 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10669 mbedtls_ssl_list_ciphersuites();
10670
10671#if defined(MBEDTLS_X509_CRT_PARSE_C)
10672 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10673#endif
10674
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010675#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010676 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010677#endif
10678
10679#if defined(MBEDTLS_ECP_C)
10680 conf->curve_list = mbedtls_ecp_grp_id_list();
10681#endif
10682
10683#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10684 conf->dhm_min_bitlen = 1024;
10685#endif
10686 }
10687
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010688 return( 0 );
10689}
10690
10691/*
10692 * Free mbedtls_ssl_config
10693 */
10694void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10695{
10696#if defined(MBEDTLS_DHM_C)
10697 mbedtls_mpi_free( &conf->dhm_P );
10698 mbedtls_mpi_free( &conf->dhm_G );
10699#endif
10700
10701#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10702 if( conf->psk != NULL )
10703 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010704 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010705 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010706 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010707 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010708 }
10709
10710 if( conf->psk_identity != NULL )
10711 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010712 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010713 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010714 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010715 conf->psk_identity_len = 0;
10716 }
10717#endif
10718
10719#if defined(MBEDTLS_X509_CRT_PARSE_C)
10720 ssl_key_cert_free( conf->key_cert );
10721#endif
10722
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010723 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010724}
10725
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010726#if defined(MBEDTLS_PK_C) && \
10727 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010728/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733#if defined(MBEDTLS_RSA_C)
10734 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10735 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010736#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737#if defined(MBEDTLS_ECDSA_C)
10738 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10739 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010742}
10743
Hanno Becker7e5437a2017-04-28 17:15:26 +010010744unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10745{
10746 switch( type ) {
10747 case MBEDTLS_PK_RSA:
10748 return( MBEDTLS_SSL_SIG_RSA );
10749 case MBEDTLS_PK_ECDSA:
10750 case MBEDTLS_PK_ECKEY:
10751 return( MBEDTLS_SSL_SIG_ECDSA );
10752 default:
10753 return( MBEDTLS_SSL_SIG_ANON );
10754 }
10755}
10756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010758{
10759 switch( sig )
10760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761#if defined(MBEDTLS_RSA_C)
10762 case MBEDTLS_SSL_SIG_RSA:
10763 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010764#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765#if defined(MBEDTLS_ECDSA_C)
10766 case MBEDTLS_SSL_SIG_ECDSA:
10767 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010768#endif
10769 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010771 }
10772}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010773#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010774
Hanno Becker7e5437a2017-04-28 17:15:26 +010010775#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10776 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10777
10778/* Find an entry in a signature-hash set matching a given hash algorithm. */
10779mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10780 mbedtls_pk_type_t sig_alg )
10781{
10782 switch( sig_alg )
10783 {
10784 case MBEDTLS_PK_RSA:
10785 return( set->rsa );
10786 case MBEDTLS_PK_ECDSA:
10787 return( set->ecdsa );
10788 default:
10789 return( MBEDTLS_MD_NONE );
10790 }
10791}
10792
10793/* Add a signature-hash-pair to a signature-hash set */
10794void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10795 mbedtls_pk_type_t sig_alg,
10796 mbedtls_md_type_t md_alg )
10797{
10798 switch( sig_alg )
10799 {
10800 case MBEDTLS_PK_RSA:
10801 if( set->rsa == MBEDTLS_MD_NONE )
10802 set->rsa = md_alg;
10803 break;
10804
10805 case MBEDTLS_PK_ECDSA:
10806 if( set->ecdsa == MBEDTLS_MD_NONE )
10807 set->ecdsa = md_alg;
10808 break;
10809
10810 default:
10811 break;
10812 }
10813}
10814
10815/* Allow exactly one hash algorithm for each signature. */
10816void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10817 mbedtls_md_type_t md_alg )
10818{
10819 set->rsa = md_alg;
10820 set->ecdsa = md_alg;
10821}
10822
10823#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10824 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10825
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010826/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010827 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010829mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010830{
10831 switch( hash )
10832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833#if defined(MBEDTLS_MD5_C)
10834 case MBEDTLS_SSL_HASH_MD5:
10835 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010836#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837#if defined(MBEDTLS_SHA1_C)
10838 case MBEDTLS_SSL_HASH_SHA1:
10839 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841#if defined(MBEDTLS_SHA256_C)
10842 case MBEDTLS_SSL_HASH_SHA224:
10843 return( MBEDTLS_MD_SHA224 );
10844 case MBEDTLS_SSL_HASH_SHA256:
10845 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847#if defined(MBEDTLS_SHA512_C)
10848 case MBEDTLS_SSL_HASH_SHA384:
10849 return( MBEDTLS_MD_SHA384 );
10850 case MBEDTLS_SSL_HASH_SHA512:
10851 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010852#endif
10853 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010855 }
10856}
10857
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010858/*
10859 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10860 */
10861unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10862{
10863 switch( md )
10864 {
10865#if defined(MBEDTLS_MD5_C)
10866 case MBEDTLS_MD_MD5:
10867 return( MBEDTLS_SSL_HASH_MD5 );
10868#endif
10869#if defined(MBEDTLS_SHA1_C)
10870 case MBEDTLS_MD_SHA1:
10871 return( MBEDTLS_SSL_HASH_SHA1 );
10872#endif
10873#if defined(MBEDTLS_SHA256_C)
10874 case MBEDTLS_MD_SHA224:
10875 return( MBEDTLS_SSL_HASH_SHA224 );
10876 case MBEDTLS_MD_SHA256:
10877 return( MBEDTLS_SSL_HASH_SHA256 );
10878#endif
10879#if defined(MBEDTLS_SHA512_C)
10880 case MBEDTLS_MD_SHA384:
10881 return( MBEDTLS_SSL_HASH_SHA384 );
10882 case MBEDTLS_MD_SHA512:
10883 return( MBEDTLS_SSL_HASH_SHA512 );
10884#endif
10885 default:
10886 return( MBEDTLS_SSL_HASH_NONE );
10887 }
10888}
10889
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010890#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010891/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010892 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010893 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010894 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010895int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010898
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010899 if( ssl->conf->curve_list == NULL )
10900 return( -1 );
10901
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010902 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010903 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010904 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010905
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010906 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010907}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010908#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010909
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010910#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010911/*
10912 * Check if a hash proposed by the peer is in our list.
10913 * Return 0 if we're willing to use it, -1 otherwise.
10914 */
10915int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10916 mbedtls_md_type_t md )
10917{
10918 const int *cur;
10919
10920 if( ssl->conf->sig_hashes == NULL )
10921 return( -1 );
10922
10923 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10924 if( *cur == (int) md )
10925 return( 0 );
10926
10927 return( -1 );
10928}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010929#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931#if defined(MBEDTLS_X509_CRT_PARSE_C)
10932int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10933 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010934 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010935 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010936{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010937 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010938#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010939 int usage = 0;
10940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010942 const char *ext_oid;
10943 size_t ext_len;
10944#endif
10945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10947 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010948 ((void) cert);
10949 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010950 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010951#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10954 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010955 {
10956 /* Server part of the key exchange */
10957 switch( ciphersuite->key_exchange )
10958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959 case MBEDTLS_KEY_EXCHANGE_RSA:
10960 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010961 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010962 break;
10963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10965 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10966 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10967 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010968 break;
10969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10971 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010972 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010973 break;
10974
10975 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010976 case MBEDTLS_KEY_EXCHANGE_NONE:
10977 case MBEDTLS_KEY_EXCHANGE_PSK:
10978 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10979 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010980 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010981 usage = 0;
10982 }
10983 }
10984 else
10985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10987 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010988 }
10989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010991 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010992 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010993 ret = -1;
10994 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010995#else
10996 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010997#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11000 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011002 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11003 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011004 }
11005 else
11006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011007 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11008 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011009 }
11010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011011 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011012 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011013 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011014 ret = -1;
11015 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011017
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011018 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011021
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011022/*
11023 * Convert version numbers to/from wire format
11024 * and, for DTLS, to/from TLS equivalent.
11025 *
11026 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011027 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011028 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11029 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011032 unsigned char ver[2] )
11033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034#if defined(MBEDTLS_SSL_PROTO_DTLS)
11035 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011038 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11039
11040 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11041 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11042 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011043 else
11044#else
11045 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011046#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011047 {
11048 ver[0] = (unsigned char) major;
11049 ver[1] = (unsigned char) minor;
11050 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011051}
11052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011054 const unsigned char ver[2] )
11055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056#if defined(MBEDTLS_SSL_PROTO_DTLS)
11057 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011058 {
11059 *major = 255 - ver[0] + 2;
11060 *minor = 255 - ver[1] + 1;
11061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011063 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11064 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011065 else
11066#else
11067 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011068#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011069 {
11070 *major = ver[0];
11071 *minor = ver[1];
11072 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011073}
11074
Simon Butcher99000142016-10-13 17:21:01 +010011075int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11076{
11077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11078 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11079 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11080
11081 switch( md )
11082 {
11083#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11084#if defined(MBEDTLS_MD5_C)
11085 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011086 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011087#endif
11088#if defined(MBEDTLS_SHA1_C)
11089 case MBEDTLS_SSL_HASH_SHA1:
11090 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11091 break;
11092#endif
11093#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11094#if defined(MBEDTLS_SHA512_C)
11095 case MBEDTLS_SSL_HASH_SHA384:
11096 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11097 break;
11098#endif
11099#if defined(MBEDTLS_SHA256_C)
11100 case MBEDTLS_SSL_HASH_SHA256:
11101 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11102 break;
11103#endif
11104 default:
11105 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11106 }
11107
11108 return 0;
11109#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11110 (void) ssl;
11111 (void) md;
11112
11113 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11114#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11115}
11116
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011117#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11118 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11119int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11120 unsigned char *output,
11121 unsigned char *data, size_t data_len )
11122{
11123 int ret = 0;
11124 mbedtls_md5_context mbedtls_md5;
11125 mbedtls_sha1_context mbedtls_sha1;
11126
11127 mbedtls_md5_init( &mbedtls_md5 );
11128 mbedtls_sha1_init( &mbedtls_sha1 );
11129
11130 /*
11131 * digitally-signed struct {
11132 * opaque md5_hash[16];
11133 * opaque sha_hash[20];
11134 * };
11135 *
11136 * md5_hash
11137 * MD5(ClientHello.random + ServerHello.random
11138 * + ServerParams);
11139 * sha_hash
11140 * SHA(ClientHello.random + ServerHello.random
11141 * + ServerParams);
11142 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011143 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011144 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011145 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011146 goto exit;
11147 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011148 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011149 ssl->handshake->randbytes, 64 ) ) != 0 )
11150 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011152 goto exit;
11153 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011154 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011155 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011157 goto exit;
11158 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011159 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011160 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011162 goto exit;
11163 }
11164
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011165 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011166 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011168 goto exit;
11169 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011170 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011171 ssl->handshake->randbytes, 64 ) ) != 0 )
11172 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011174 goto exit;
11175 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 data_len ) ) != 0 )
11178 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011180 goto exit;
11181 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011182 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011183 output + 16 ) ) != 0 )
11184 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 goto exit;
11187 }
11188
11189exit:
11190 mbedtls_md5_free( &mbedtls_md5 );
11191 mbedtls_sha1_free( &mbedtls_sha1 );
11192
11193 if( ret != 0 )
11194 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11195 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11196
11197 return( ret );
11198
11199}
11200#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11201 MBEDTLS_SSL_PROTO_TLS1_1 */
11202
11203#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11204 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011205
11206#if defined(MBEDTLS_USE_PSA_CRYPTO)
11207int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11208 unsigned char *hash, size_t *hashlen,
11209 unsigned char *data, size_t data_len,
11210 mbedtls_md_type_t md_alg )
11211{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011212 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011213 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011214 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11215
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011217
11218 if( ( status = psa_hash_setup( &hash_operation,
11219 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011220 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011221 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011222 goto exit;
11223 }
11224
Andrzej Kurek814feff2019-01-14 04:35:19 -050011225 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11226 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011227 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011228 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011229 goto exit;
11230 }
11231
Andrzej Kurek814feff2019-01-14 04:35:19 -050011232 if( ( status = psa_hash_update( &hash_operation,
11233 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011234 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011235 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011236 goto exit;
11237 }
11238
Andrzej Kurek814feff2019-01-14 04:35:19 -050011239 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11240 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011241 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011242 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011243 goto exit;
11244 }
11245
11246exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011247 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011248 {
11249 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11250 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011251 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011252 {
11253 case PSA_ERROR_NOT_SUPPORTED:
11254 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011255 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 case PSA_ERROR_BUFFER_TOO_SMALL:
11257 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11258 case PSA_ERROR_INSUFFICIENT_MEMORY:
11259 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11260 default:
11261 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11262 }
11263 }
11264 return( 0 );
11265}
11266
11267#else
11268
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011269int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011270 unsigned char *hash, size_t *hashlen,
11271 unsigned char *data, size_t data_len,
11272 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011273{
11274 int ret = 0;
11275 mbedtls_md_context_t ctx;
11276 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011277 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011278
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011280
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011281 mbedtls_md_init( &ctx );
11282
11283 /*
11284 * digitally-signed struct {
11285 * opaque client_random[32];
11286 * opaque server_random[32];
11287 * ServerDHParams params;
11288 * };
11289 */
11290 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11291 {
11292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11293 goto exit;
11294 }
11295 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11296 {
11297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11298 goto exit;
11299 }
11300 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11301 {
11302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11303 goto exit;
11304 }
11305 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11306 {
11307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11308 goto exit;
11309 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011310 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011311 {
11312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11313 goto exit;
11314 }
11315
11316exit:
11317 mbedtls_md_free( &ctx );
11318
11319 if( ret != 0 )
11320 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11321 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11322
11323 return( ret );
11324}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011325#endif /* MBEDTLS_USE_PSA_CRYPTO */
11326
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011327#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11328 MBEDTLS_SSL_PROTO_TLS1_2 */
11329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011330#endif /* MBEDTLS_SSL_TLS_C */