blob: 4cdad6c90e52078e744251bdd1d884e790150098 [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 Becker8367ccc2019-05-14 11:30:10 +0100123int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
124 size_t len,
125 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100126{
127 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
129
Hanno Becker8367ccc2019-05-14 11:30:10 +0100130 conf->ignore_unexpected_cid =
131 ( ignore_other_cid == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100132 conf->cid_len = len;
133 return( 0 );
134}
135
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100136int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
137 int enable,
138 unsigned char const *own_cid,
139 size_t own_cid_len )
140{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100141 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
143
Hanno Beckerca092242019-04-25 16:01:49 +0100144 ssl->negotiate_cid = enable;
145 if( enable == MBEDTLS_SSL_CID_DISABLED )
146 {
147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
148 return( 0 );
149 }
150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100151 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100152
Hanno Beckerad4a1372019-05-03 13:06:44 +0100153 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100154 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
156 (unsigned) own_cid_len,
157 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
159 }
160
161 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100162 /* Truncation is not an issue here because
163 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
164 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100165
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100166 return( 0 );
167}
168
169int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
170 int *enabled,
171 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
172 size_t *peer_cid_len )
173{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100174 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Becker76a79ab2019-05-03 14:38:32 +0100176 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
177 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
178 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181
Hanno Beckerc5f24222019-05-03 12:54:52 +0100182 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
183 * were used, but client and server requested the empty CID.
184 * This is indistinguishable from not using the CID extension
185 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100186 if( ssl->transform_in->in_cid_len == 0 &&
187 ssl->transform_in->out_cid_len == 0 )
188 {
189 return( 0 );
190 }
191
192 *peer_cid_len = ssl->transform_in->out_cid_len;
193 memcpy( peer_cid, ssl->transform_in->out_cid,
194 ssl->transform_in->out_cid_len );
195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Becker35c36a62019-04-23 12:31:42 +0100200#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Hanno Beckerd5847772018-08-28 10:09:23 +0100202/* Forward declarations for functions related to message buffering. */
203static void ssl_buffering_free( mbedtls_ssl_context *ssl );
204static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
205 uint8_t slot );
206static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
207static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
208static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
209static int ssl_buffer_message( mbedtls_ssl_context *ssl );
210static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100211static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100212
Hanno Beckera67dee22018-08-22 10:05:20 +0100213static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100214static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100215{
Hanno Becker11682cc2018-08-22 14:41:02 +0100216 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100217
218 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100219 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100220
221 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
222}
223
Hanno Becker67bc7c32018-08-06 11:33:50 +0100224static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
225{
Hanno Becker11682cc2018-08-22 14:41:02 +0100226 size_t const bytes_written = ssl->out_left;
227 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100228
229 /* Double-check that the write-index hasn't gone
230 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100231 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232 {
233 /* Should never happen... */
234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
235 }
236
237 return( (int) ( mtu - bytes_written ) );
238}
239
240static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
241{
242 int ret;
243 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400244 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100245
246#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
247 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
248
249 if( max_len > mfl )
250 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100251
252 /* By the standard (RFC 6066 Sect. 4), the MFL extension
253 * only limits the maximum record payload size, so in theory
254 * we would be allowed to pack multiple records of payload size
255 * MFL into a single datagram. However, this would mean that there's
256 * no way to explicitly communicate MTU restrictions to the peer.
257 *
258 * The following reduction of max_len makes sure that we never
259 * write datagrams larger than MFL + Record Expansion Overhead.
260 */
261 if( max_len <= ssl->out_left )
262 return( 0 );
263
264 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100265#endif
266
267 ret = ssl_get_remaining_space_in_datagram( ssl );
268 if( ret < 0 )
269 return( ret );
270 remaining = (size_t) ret;
271
272 ret = mbedtls_ssl_get_record_expansion( ssl );
273 if( ret < 0 )
274 return( ret );
275 expansion = (size_t) ret;
276
277 if( remaining <= expansion )
278 return( 0 );
279
280 remaining -= expansion;
281 if( remaining >= max_len )
282 remaining = max_len;
283
284 return( (int) remaining );
285}
286
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200287/*
288 * Double the retransmit timeout value, within the allowed range,
289 * returning -1 if the maximum value has already been reached.
290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200292{
293 uint32_t new_timeout;
294
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200295 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296 return( -1 );
297
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200298 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
299 * in the following way: after the initial transmission and a first
300 * retransmission, back off to a temporary estimated MTU of 508 bytes.
301 * This value is guaranteed to be deliverable (if not guaranteed to be
302 * delivered) of any compliant IPv4 (and IPv6) network, and should work
303 * on most non-IP stacks too. */
304 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400305 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
308 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200309
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200310 new_timeout = 2 * ssl->handshake->retransmit_timeout;
311
312 /* Avoid arithmetic overflow and range overflow */
313 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200314 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200316 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200317 }
318
319 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200321 ssl->handshake->retransmit_timeout ) );
322
323 return( 0 );
324}
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200327{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200328 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200330 ssl->handshake->retransmit_timeout ) );
331}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200335/*
336 * Convert max_fragment_length codes to length.
337 * RFC 6066 says:
338 * enum{
339 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
340 * } MaxFragmentLength;
341 * and we add 0 -> extension unused
342 */
Angus Grattond8213d02016-05-25 20:56:48 +1000343static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200344{
Angus Grattond8213d02016-05-25 20:56:48 +1000345 switch( mfl )
346 {
347 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
348 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
349 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
350 return 512;
351 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
352 return 1024;
353 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
354 return 2048;
355 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
356 return 4096;
357 default:
358 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
359 }
360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200362
Hanno Becker52055ae2019-02-06 14:30:46 +0000363int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
364 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_ssl_session_free( dst );
367 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000370
371#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372 if( src->peer_cert != NULL )
373 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200374 int ret;
375
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200376 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200377 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200378 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200383 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200386 dst->peer_cert = NULL;
387 return( ret );
388 }
389 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000390#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000391 if( src->peer_cert_digest != NULL )
392 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000393 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000394 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000395 if( dst->peer_cert_digest == NULL )
396 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
397
398 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
399 src->peer_cert_digest_len );
400 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000401 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000402 }
403#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200406
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200407#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200408 if( src->ticket != NULL )
409 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200410 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200411 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200412 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200413
414 memcpy( dst->ticket, src->ticket, src->ticket_len );
415 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200416#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200417
418 return( 0 );
419}
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
422int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200423 const unsigned char *key_enc, const unsigned char *key_dec,
424 size_t keylen,
425 const unsigned char *iv_enc, const unsigned char *iv_dec,
426 size_t ivlen,
427 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200428 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
430int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
431int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
432int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
433int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
434#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000435
Paul Bakker5121ce52009-01-03 21:22:43 +0000436/*
437 * Key material generation
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200440static int ssl3_prf( const unsigned char *secret, size_t slen,
441 const char *label,
442 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000443 unsigned char *dstbuf, size_t dlen )
444{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100445 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000446 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200447 mbedtls_md5_context md5;
448 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000449 unsigned char padding[16];
450 unsigned char sha1sum[20];
451 ((void)label);
452
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200453 mbedtls_md5_init( &md5 );
454 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200455
Paul Bakker5f70b252012-09-13 14:23:06 +0000456 /*
457 * SSLv3:
458 * block =
459 * MD5( secret + SHA1( 'A' + secret + random ) ) +
460 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
461 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
462 * ...
463 */
464 for( i = 0; i < dlen / 16; i++ )
465 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200466 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000467
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100474 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100485 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100486 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000487 }
488
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100489exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200490 mbedtls_md5_free( &md5 );
491 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500493 mbedtls_platform_zeroize( padding, sizeof( padding ) );
494 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000495
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200501static int tls1_prf( const unsigned char *secret, size_t slen,
502 const char *label,
503 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000504 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000505{
Paul Bakker23986e52011-04-24 08:57:21 +0000506 size_t nb, hs;
507 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200508 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300509 unsigned char *tmp;
510 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 const mbedtls_md_info_t *md_info;
513 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100514 int ret;
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
Ron Eldor3b350852019-05-07 18:31:49 +0300518 tmp_len = 20 + strlen( label ) + rlen;
519 tmp = mbedtls_calloc( 1, tmp_len );
520 if( tmp == NULL )
521 {
522 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
523 goto exit;
524 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 hs = ( slen + 1 ) / 2;
527 S1 = secret;
528 S2 = secret + slen - hs;
529
530 nb = strlen( label );
531 memcpy( tmp + 20, label, nb );
532 memcpy( tmp + 20 + nb, random, rlen );
533 nb += rlen;
534
535 /*
536 * First compute P_md5(secret,label+random)[0..dlen]
537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300539 {
540 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
541 goto exit;
542 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300545 {
546 goto exit;
547 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
550 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
551 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553 for( i = 0; i < dlen; i += 16 )
554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_reset ( &md_ctx );
556 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
557 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_md_hmac_reset ( &md_ctx );
560 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
561 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
564
565 for( j = 0; j < k; j++ )
566 dstbuf[i + j] = h_i[j];
567 }
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 /*
572 * XOR out with P_sha1(secret,label+random)[0..dlen]
573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300575 {
576 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
577 goto exit;
578 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300581 {
582 goto exit;
583 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
586 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
587 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
589 for( i = 0; i < dlen; i += 20 )
590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_md_hmac_reset ( &md_ctx );
592 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
593 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_md_hmac_reset ( &md_ctx );
596 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
597 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
600
601 for( j = 0; j < k; j++ )
602 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
603 }
604
Ron Eldor3b350852019-05-07 18:31:49 +0300605exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100607
Ron Eldor3b350852019-05-07 18:31:49 +0300608 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500609 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Ron Eldor3b350852019-05-07 18:31:49 +0300611 mbedtls_free( tmp );
612 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500617#if defined(MBEDTLS_USE_PSA_CRYPTO)
618static int tls_prf_generic( mbedtls_md_type_t md_type,
619 const unsigned char *secret, size_t slen,
620 const char *label,
621 const unsigned char *random, size_t rlen,
622 unsigned char *dstbuf, size_t dlen )
623{
624 psa_status_t status;
625 psa_algorithm_t alg;
626 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500627 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500628 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
629
Andrzej Kurek2f760752019-01-28 08:08:15 -0500630 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500631 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500632
Andrzej Kurekc929a822019-01-14 03:51:11 -0500633 if( md_type == MBEDTLS_MD_SHA384 )
634 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
635 else
636 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
637
Andrzej Kurek2f760752019-01-28 08:08:15 -0500638 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500639 psa_key_policy_set_usage( &policy,
640 PSA_KEY_USAGE_DERIVE,
641 alg );
642 status = psa_set_key_policy( master_slot, &policy );
643 if( status != PSA_SUCCESS )
644 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
645
646 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
647 if( status != PSA_SUCCESS )
648 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
649
650 status = psa_key_derivation( &generator,
651 master_slot, alg,
652 random, rlen,
653 (unsigned char const *) label,
654 (size_t) strlen( label ),
655 dlen );
656 if( status != PSA_SUCCESS )
657 {
658 psa_generator_abort( &generator );
659 psa_destroy_key( master_slot );
660 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
661 }
662
663 status = psa_generator_read( &generator, dstbuf, dlen );
664 if( status != PSA_SUCCESS )
665 {
666 psa_generator_abort( &generator );
667 psa_destroy_key( master_slot );
668 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
669 }
670
671 status = psa_generator_abort( &generator );
672 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500673 {
674 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500675 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500676 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500677
678 status = psa_destroy_key( master_slot );
679 if( status != PSA_SUCCESS )
680 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
681
Andrzej Kurek33171262019-01-15 03:25:18 -0500682 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500683}
684
685#else /* MBEDTLS_USE_PSA_CRYPTO */
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100688 const unsigned char *secret, size_t slen,
689 const char *label,
690 const unsigned char *random, size_t rlen,
691 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000692{
693 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100694 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300695 unsigned char *tmp;
696 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
698 const mbedtls_md_info_t *md_info;
699 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100700 int ret;
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100708
Ron Eldor3b350852019-05-07 18:31:49 +0300709 tmp_len = md_len + strlen( label ) + rlen;
710 tmp = mbedtls_calloc( 1, tmp_len );
711 if( tmp == NULL )
712 {
713 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
714 goto exit;
715 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000716
717 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100718 memcpy( tmp + md_len, label, nb );
719 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000720 nb += rlen;
721
722 /*
723 * Compute P_<hash>(secret, label + random)[0..dlen]
724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300726 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
729 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
730 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100731
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100732 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_md_hmac_reset ( &md_ctx );
735 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
736 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_md_hmac_reset ( &md_ctx );
739 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
740 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000741
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100742 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743
744 for( j = 0; j < k; j++ )
745 dstbuf[i + j] = h_i[j];
746 }
747
Ron Eldor3b350852019-05-07 18:31:49 +0300748exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100750
Ron Eldor3b350852019-05-07 18:31:49 +0300751 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500752 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000753
Ron Eldor3b350852019-05-07 18:31:49 +0300754 mbedtls_free( tmp );
755
756 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000757}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500758#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100760static int tls_prf_sha256( const unsigned char *secret, size_t slen,
761 const char *label,
762 const unsigned char *random, size_t rlen,
763 unsigned char *dstbuf, size_t dlen )
764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100766 label, random, rlen, dstbuf, dlen ) );
767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200771static int tls_prf_sha384( const unsigned char *secret, size_t slen,
772 const char *label,
773 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000774 unsigned char *dstbuf, size_t dlen )
775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100777 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_SHA512_C */
780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
785 defined(MBEDTLS_SSL_PROTO_TLS1_1)
786static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200787#endif
Paul Bakker380da532012-04-18 16:10:25 +0000788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_SSL_PROTO_SSL3)
790static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
791static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792#endif
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
795static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
796static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797#endif
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
800#if defined(MBEDTLS_SHA256_C)
801static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
802static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
803static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_SHA512_C)
807static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
808static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
809static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100810#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100813#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100814 defined(MBEDTLS_USE_PSA_CRYPTO)
815static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
816{
817 if( ssl->conf->f_psk != NULL )
818 {
819 /* If we've used a callback to select the PSK,
820 * the static configuration is irrelevant. */
821 if( ssl->handshake->psk_opaque != 0 )
822 return( 1 );
823
824 return( 0 );
825 }
826
827 if( ssl->conf->psk_opaque != 0 )
828 return( 1 );
829
830 return( 0 );
831}
832#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100833 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100834
Ron Eldorcf280092019-05-14 20:19:13 +0300835#if defined(MBEDTLS_SSL_EXPORT_KEYS)
836static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
837{
838#if defined(MBEDTLS_SSL_PROTO_SSL3)
839 if( tls_prf == ssl3_prf )
840 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300841 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300842 }
843 else
844#endif
845#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
846 if( tls_prf == tls1_prf )
847 {
848 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
849 }
850 else
851#endif
852#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
853#if defined(MBEDTLS_SHA512_C)
854 if( tls_prf == tls_prf_sha384 )
855 {
856 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
857 }
858 else
859#endif
860#if defined(MBEDTLS_SHA256_C)
861 if( tls_prf == tls_prf_sha256 )
862 {
863 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
864 }
865 else
866#endif
867#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
868 return( MBEDTLS_SSL_TLS_PRF_NONE );
869}
870#endif /* MBEDTLS_SSL_EXPORT_KEYS */
871
Ron Eldor51d3ab52019-05-12 14:54:30 +0300872int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
873 const unsigned char *secret, size_t slen,
874 const char *label,
875 const unsigned char *random, size_t rlen,
876 unsigned char *dstbuf, size_t dlen )
877{
878 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
879
880 switch( prf )
881 {
882#if defined(MBEDTLS_SSL_PROTO_SSL3)
883 case MBEDTLS_SSL_TLS_PRF_SSL3:
884 tls_prf = ssl3_prf;
885 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300886#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300887#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
888 case MBEDTLS_SSL_TLS_PRF_TLS1:
889 tls_prf = tls1_prf;
890 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300891#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
892
893#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300894#if defined(MBEDTLS_SHA512_C)
895 case MBEDTLS_SSL_TLS_PRF_SHA384:
896 tls_prf = tls_prf_sha384;
897 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300898#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300899#if defined(MBEDTLS_SHA256_C)
900 case MBEDTLS_SSL_TLS_PRF_SHA256:
901 tls_prf = tls_prf_sha256;
902 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300903#endif /* MBEDTLS_SHA256_C */
904#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300905 default:
906 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
907 }
908
909 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
910}
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000913{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200914 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000915#if defined(MBEDTLS_USE_PSA_CRYPTO)
916 int psa_fallthrough;
917#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 unsigned char keyblk[256];
920 unsigned char *key1;
921 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100922 unsigned char *mac_enc;
923 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000924 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200925 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000926 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000927 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 const mbedtls_cipher_info_t *cipher_info;
929 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100930
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000931 /* cf. RFC 5246, Section 8.1:
932 * "The master secret is always exactly 48 bytes in length." */
933 size_t const master_secret_len = 48;
934
Hanno Becker35b23c72018-10-23 12:10:41 +0100935#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
936 unsigned char session_hash[48];
937#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_ssl_session *session = ssl->session_negotiate;
940 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
941 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000945#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
946 transform->encrypt_then_mac = session->encrypt_then_mac;
947#endif
948 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000949
950 ciphersuite_info = handshake->ciphersuite_info;
951 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100952 if( cipher_info == NULL )
953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000955 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100957 }
958
Hanno Beckere694c3e2017-12-27 21:34:08 +0000959 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100960 if( md_info == NULL )
961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000963 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100965 }
966
Hanno Becker4bf74652019-04-26 16:22:27 +0100967#if defined(MBEDTLS_SSL_CID)
968 /* Copy own and peer's CID if the use of the CID
969 * extension has been negotiated. */
970 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
971 {
972 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100973
Hanno Becker05154c32019-05-03 15:23:51 +0100974 transform->in_cid_len = ssl->own_cid_len;
975 transform->out_cid_len = ssl->handshake->peer_cid_len;
976 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
977 memcpy( transform->out_cid, ssl->handshake->peer_cid,
978 ssl->handshake->peer_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100979
980 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
981 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100982 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100983 transform->in_cid_len );
984 }
985#endif /* MBEDTLS_SSL_CID */
986
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000988 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#if defined(MBEDTLS_SSL_PROTO_SSL3)
991 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000992 {
Paul Bakker48916f92012-09-16 19:57:18 +0000993 handshake->tls_prf = ssl3_prf;
994 handshake->calc_verify = ssl_calc_verify_ssl;
995 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000996 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200997 else
998#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1000 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001001 {
Paul Bakker48916f92012-09-16 19:57:18 +00001002 handshake->tls_prf = tls1_prf;
1003 handshake->calc_verify = ssl_calc_verify_tls;
1004 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001005 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001006 else
1007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1009#if defined(MBEDTLS_SHA512_C)
1010 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001011 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 {
Paul Bakker48916f92012-09-16 19:57:18 +00001013 handshake->tls_prf = tls_prf_sha384;
1014 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1015 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001017 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_SHA256_C)
1020 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001021 {
Paul Bakker48916f92012-09-16 19:57:18 +00001022 handshake->tls_prf = tls_prf_sha256;
1023 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1024 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001025 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001026 else
1027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001032 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001033
1034 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 * SSLv3:
1036 * master =
1037 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1038 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1039 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001040 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001041 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 * master = PRF( premaster, "master secret", randbytes )[0..47]
1043 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001044 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1047 }
1048 else
1049 {
1050 /* The label for the KDF used for key expansion.
1051 * This is either "master secret" or "extended master secret"
1052 * depending on whether the Extended Master Secret extension
1053 * is used. */
1054 char const *lbl = "master secret";
1055
1056 /* The salt for the KDF used for key expansion.
1057 * - If the Extended Master Secret extension is not used,
1058 * this is ClientHello.Random + ServerHello.Random
1059 * (see Sect. 8.1 in RFC 5246).
1060 * - If the Extended Master Secret extension is used,
1061 * this is the transcript of the handshake so far.
1062 * (see Sect. 4 in RFC 7627). */
1063 unsigned char const *salt = handshake->randbytes;
1064 size_t salt_len = 64;
1065
1066#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001070
Hanno Becker35b23c72018-10-23 12:10:41 +01001071 lbl = "extended master secret";
1072 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001073 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1075 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001078 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1079 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001080 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001081 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001082 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001083#endif /* MBEDTLS_SHA512_C */
1084 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001085 }
1086 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001088 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001089
Hanno Becker35b23c72018-10-23 12:10:41 +01001090 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001091 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001092#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1093
Hanno Becker7d0a5692018-10-23 15:26:22 +01001094#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1095 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1096 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1097 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1098 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001099 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001100 /* Perform PSK-to-MS expansion in a single step. */
1101 psa_status_t status;
1102 psa_algorithm_t alg;
1103 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001104 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001105
1106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1107
1108 psk = ssl->conf->psk_opaque;
1109 if( ssl->handshake->psk_opaque != 0 )
1110 psk = ssl->handshake->psk_opaque;
1111
Hanno Becker22bf1452019-04-05 11:21:08 +01001112 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001113 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1114 else
1115 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1116
1117 status = psa_key_derivation( &generator, psk, alg,
1118 salt, salt_len,
1119 (unsigned char const *) lbl,
1120 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001121 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001122 if( status != PSA_SUCCESS )
1123 {
1124 psa_generator_abort( &generator );
1125 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1126 }
1127
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001128 status = psa_generator_read( &generator, session->master,
1129 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001130 if( status != PSA_SUCCESS )
1131 {
1132 psa_generator_abort( &generator );
1133 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1134 }
1135
1136 status = psa_generator_abort( &generator );
1137 if( status != PSA_SUCCESS )
1138 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001139 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001140 else
1141#endif
1142 {
1143 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1144 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001145 session->master,
1146 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001147 if( ret != 0 )
1148 {
1149 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1150 return( ret );
1151 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001152
Hanno Becker7d0a5692018-10-23 15:26:22 +01001153 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1154 handshake->premaster,
1155 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001156
Hanno Becker7d0a5692018-10-23 15:26:22 +01001157 mbedtls_platform_zeroize( handshake->premaster,
1158 sizeof(handshake->premaster) );
1159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
1162 /*
1163 * Swap the client and server random values.
1164 */
Paul Bakker48916f92012-09-16 19:57:18 +00001165 memcpy( tmp, handshake->randbytes, 64 );
1166 memcpy( handshake->randbytes, tmp + 32, 32 );
1167 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001168 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
1170 /*
1171 * SSLv3:
1172 * key block =
1173 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1174 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1175 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1176 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1177 * ...
1178 *
1179 * TLSv1:
1180 * key block = PRF( master, "key expansion", randbytes )
1181 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001182 ret = handshake->tls_prf( session->master, 48, "key expansion",
1183 handshake->randbytes, 64, keyblk, 256 );
1184 if( ret != 0 )
1185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001187 return( ret );
1188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1191 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1192 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1193 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1194 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001195
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 /*
1197 * Determine the appropriate key, IV and MAC length.
1198 */
Paul Bakker68884e32013-01-07 18:20:04 +01001199
Hanno Becker88aaf652017-12-27 08:17:40 +00001200 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001201
Hanno Becker8031d062018-01-03 15:32:31 +00001202#if defined(MBEDTLS_GCM_C) || \
1203 defined(MBEDTLS_CCM_C) || \
1204 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001206 cipher_info->mode == MBEDTLS_MODE_CCM ||
1207 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001209 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001210
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001211 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001212 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001213 transform->taglen =
1214 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001215
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001216 /* All modes haves 96-bit IVs;
1217 * GCM and CCM has 4 implicit and 8 explicit bytes
1218 * ChachaPoly has all 12 bytes implicit
1219 */
Paul Bakker68884e32013-01-07 18:20:04 +01001220 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001221 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1222 transform->fixed_ivlen = 12;
1223 else
1224 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001225
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001226 /* Minimum length of encrypted record */
1227 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001228 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001229 }
1230 else
Hanno Becker8031d062018-01-03 15:32:31 +00001231#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1232#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1233 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1234 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001235 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001236 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1238 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001241 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001244 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001245 mac_key_len = mbedtls_md_get_size( md_info );
1246 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001249 /*
1250 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1251 * (rfc 6066 page 13 or rfc 2104 section 4),
1252 * so we only need to adjust the length here.
1253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001257
1258#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1259 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001260 * HMAC implementation which also truncates the key
1261 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001262 mac_key_len = transform->maclen;
1263#endif
1264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001266
1267 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001268 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001270 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001272 transform->minlen = transform->maclen;
1273 else
Paul Bakker68884e32013-01-07 18:20:04 +01001274 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001275 /*
1276 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001277 * 1. if EtM is in use: one block plus MAC
1278 * otherwise: * first multiple of blocklen greater than maclen
1279 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1282 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001283 {
1284 transform->minlen = transform->maclen
1285 + cipher_info->block_size;
1286 }
1287 else
1288#endif
1289 {
1290 transform->minlen = transform->maclen
1291 + cipher_info->block_size
1292 - transform->maclen % cipher_info->block_size;
1293 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1296 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1297 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001298 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001299 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1302 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1303 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001304 {
1305 transform->minlen += transform->ivlen;
1306 }
1307 else
1308#endif
1309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001311 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1312 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001313 }
Paul Bakker68884e32013-01-07 18:20:04 +01001314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
Hanno Becker8031d062018-01-03 15:32:31 +00001316 else
1317#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1318 {
1319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Hanno Becker88aaf652017-12-27 08:17:40 +00001323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1324 (unsigned) keylen,
1325 (unsigned) transform->minlen,
1326 (unsigned) transform->ivlen,
1327 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
1329 /*
1330 * Finally setup the cipher contexts, IVs and MAC secrets.
1331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001333 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001335 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001336 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Paul Bakker68884e32013-01-07 18:20:04 +01001338 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001339 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001341 /*
1342 * This is not used in TLS v1.1.
1343 */
Paul Bakker48916f92012-09-16 19:57:18 +00001344 iv_copy_len = ( transform->fixed_ivlen ) ?
1345 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001346 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1347 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001348 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 }
1350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_SSL_CLI_C */
1352#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001353 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001355 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Hanno Becker81c7b182017-11-09 18:39:33 +00001358 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001359 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001361 /*
1362 * This is not used in TLS v1.1.
1363 */
Paul Bakker48916f92012-09-16 19:57:18 +00001364 iv_copy_len = ( transform->fixed_ivlen ) ?
1365 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001366 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1367 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001368 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001370 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001374 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1375 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Hanno Beckerd56ed242018-01-03 15:32:51 +00001378#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_SSL_PROTO_SSL3)
1380 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001381 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001382 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001385 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1386 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001387 }
1388
Hanno Becker81c7b182017-11-09 18:39:33 +00001389 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1390 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001391 }
1392 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1394#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1395 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1396 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001397 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001398 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1399 For AEAD-based ciphersuites, there is nothing to do here. */
1400 if( mac_key_len != 0 )
1401 {
1402 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1403 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1404 }
Paul Bakker68884e32013-01-07 18:20:04 +01001405 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001406 else
1407#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001410 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1411 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001412 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001413#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1416 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001417 {
1418 int ret = 0;
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001421
Hanno Becker88aaf652017-12-27 08:17:40 +00001422 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001423 transform->iv_enc, transform->iv_dec,
1424 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001425 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001426 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001429 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1430 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001431 }
1432 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001433#else
1434 ((void) mac_dec);
1435 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001437
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001438#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1439 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001440 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001441 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1442 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001443 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001444 iv_copy_len );
1445 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001446
1447 if( ssl->conf->f_export_keys_ext != NULL )
1448 {
1449 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1450 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001451 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001452 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001453 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001454 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001455 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001456 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001457#endif
1458
Hanno Beckerf704bef2018-11-16 15:21:18 +00001459#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001460
1461 /* Only use PSA-based ciphers for TLS-1.2.
1462 * That's relevant at least for TLS-1.0, where
1463 * we assume that mbedtls_cipher_crypt() updates
1464 * the structure field for the IV, which the PSA-based
1465 * implementation currently doesn't. */
1466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1467 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001468 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001469 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001470 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001471 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1472 {
1473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001474 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001475 }
1476
1477 if( ret == 0 )
1478 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 psa_fallthrough = 0;
1481 }
1482 else
1483 {
1484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1485 psa_fallthrough = 1;
1486 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001487 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001488 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001489 psa_fallthrough = 1;
1490#else
1491 psa_fallthrough = 1;
1492#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001493
Hanno Beckercb1cc802018-11-17 22:27:38 +00001494 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001495#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001496 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001497 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001500 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001501 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001502
Hanno Beckerf704bef2018-11-16 15:21:18 +00001503#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001504 /* Only use PSA-based ciphers for TLS-1.2.
1505 * That's relevant at least for TLS-1.0, where
1506 * we assume that mbedtls_cipher_crypt() updates
1507 * the structure field for the IV, which the PSA-based
1508 * implementation currently doesn't. */
1509#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1510 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001511 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001512 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001513 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001514 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1515 {
1516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001517 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001518 }
1519
1520 if( ret == 0 )
1521 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001523 psa_fallthrough = 0;
1524 }
1525 else
1526 {
1527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1528 psa_fallthrough = 1;
1529 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001530 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001531 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001532 psa_fallthrough = 1;
1533#else
1534 psa_fallthrough = 1;
1535#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001536
Hanno Beckercb1cc802018-11-17 22:27:38 +00001537 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001538#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001539 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001540 cipher_info ) ) != 0 )
1541 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001543 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001544 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001547 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001551 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001552 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001555 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001559 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_CIPHER_MODE_CBC)
1563 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1566 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001569 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001570 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1573 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001576 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001583 // Initialize compression
1584 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001586 {
Paul Bakker16770332013-10-11 09:59:44 +02001587 if( ssl->compress_buf == NULL )
1588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001590 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001591 if( ssl->compress_buf == NULL )
1592 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001594 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1596 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001597 }
1598 }
1599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001601
Paul Bakker48916f92012-09-16 19:57:18 +00001602 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1603 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001604
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001605 if( deflateInit( &transform->ctx_deflate,
1606 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001607 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001610 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1611 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001612 }
1613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001617end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001618 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1619 mbedtls_platform_zeroize( handshake->randbytes,
1620 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001621 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622}
1623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#if defined(MBEDTLS_SSL_PROTO_SSL3)
1625void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001626{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001627 mbedtls_md5_context md5;
1628 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 unsigned char pad_1[48];
1630 unsigned char pad_2[48];
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001633
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001634 mbedtls_md5_init( &md5 );
1635 mbedtls_sha1_init( &sha1 );
1636
1637 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1638 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Paul Bakker380da532012-04-18 16:10:25 +00001640 memset( pad_1, 0x36, 48 );
1641 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001643 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1644 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1645 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001647 mbedtls_md5_starts_ret( &md5 );
1648 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1649 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1650 mbedtls_md5_update_ret( &md5, hash, 16 );
1651 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001653 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1654 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1655 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001656
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001657 mbedtls_sha1_starts_ret( &sha1 );
1658 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1659 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1660 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1661 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001665
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001666 mbedtls_md5_free( &md5 );
1667 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001668
Paul Bakker380da532012-04-18 16:10:25 +00001669 return;
1670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1674void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001675{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001676 mbedtls_md5_context md5;
1677 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001681 mbedtls_md5_init( &md5 );
1682 mbedtls_sha1_init( &sha1 );
1683
1684 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1685 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001686
Andrzej Kurekeb342242019-01-29 09:14:33 -05001687 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001688 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001692
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001693 mbedtls_md5_free( &md5 );
1694 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001695
Paul Bakker380da532012-04-18 16:10:25 +00001696 return;
1697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1701#if defined(MBEDTLS_SHA256_C)
1702void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001703{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001704#if defined(MBEDTLS_USE_PSA_CRYPTO)
1705 size_t hash_size;
1706 psa_status_t status;
1707 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1708
1709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1710 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1711 if( status != PSA_SUCCESS )
1712 {
1713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1714 return;
1715 }
1716
1717 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1718 if( status != PSA_SUCCESS )
1719 {
1720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1721 return;
1722 }
1723 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1725#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001726 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001727
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001728 mbedtls_sha256_init( &sha256 );
1729
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001731
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001732 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001733 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001737
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001738 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001739#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001740 return;
1741}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_SHA512_C)
1745void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001746{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001747#if defined(MBEDTLS_USE_PSA_CRYPTO)
1748 size_t hash_size;
1749 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001750 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001751
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001753 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001754 if( status != PSA_SUCCESS )
1755 {
1756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1757 return;
1758 }
1759
Andrzej Kurek972fba52019-01-30 03:29:12 -05001760 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001761 if( status != PSA_SUCCESS )
1762 {
1763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1764 return;
1765 }
1766 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1768#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001769 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001770
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001771 mbedtls_sha512_init( &sha512 );
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001774
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001775 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001776 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001781 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001782#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 return;
1784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_SHA512_C */
1786#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1789int 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 +02001790{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001791 unsigned char *p = ssl->handshake->premaster;
1792 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001793 const unsigned char *psk = ssl->conf->psk;
1794 size_t psk_len = ssl->conf->psk_len;
1795
1796 /* If the psk callback was called, use its result */
1797 if( ssl->handshake->psk != NULL )
1798 {
1799 psk = ssl->handshake->psk;
1800 psk_len = ssl->handshake->psk_len;
1801 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001802
1803 /*
1804 * PMS = struct {
1805 * opaque other_secret<0..2^16-1>;
1806 * opaque psk<0..2^16-1>;
1807 * };
1808 * with "other_secret" depending on the particular key exchange
1809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1811 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001812 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001813 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001815
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001816 *(p++) = (unsigned char)( psk_len >> 8 );
1817 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001818
1819 if( end < p || (size_t)( end - p ) < psk_len )
1820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1821
1822 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001823 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001824 }
1825 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1827#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1828 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001829 {
1830 /*
1831 * other_secret already set by the ClientKeyExchange message,
1832 * and is 48 bytes long
1833 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001834 if( end - p < 2 )
1835 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1836
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001837 *p++ = 0;
1838 *p++ = 48;
1839 p += 48;
1840 }
1841 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1843#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1844 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001845 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001846 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001847 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001848
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001849 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001851 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001852 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001855 return( ret );
1856 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001857 *(p++) = (unsigned char)( len >> 8 );
1858 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001859 p += len;
1860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001862 }
1863 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1865#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1866 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001867 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001868 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 size_t zlen;
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001872 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001873 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001876 return( ret );
1877 }
1878
1879 *(p++) = (unsigned char)( zlen >> 8 );
1880 *(p++) = (unsigned char)( zlen );
1881 p += zlen;
1882
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001883 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1884 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001885 }
1886 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001891 }
1892
1893 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001894 if( end - p < 2 )
1895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001896
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001897 *(p++) = (unsigned char)( psk_len >> 8 );
1898 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001899
1900 if( end < p || (size_t)( end - p ) < psk_len )
1901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1902
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001903 memcpy( p, psk, psk_len );
1904 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001905
1906 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1907
1908 return( 0 );
1909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001913/*
1914 * SSLv3.0 MAC functions
1915 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001916#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001917static void ssl_mac( mbedtls_md_context_t *md_ctx,
1918 const unsigned char *secret,
1919 const unsigned char *buf, size_t len,
1920 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001921 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001922{
1923 unsigned char header[11];
1924 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001925 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1927 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001928
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001929 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001931 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001932 else
Paul Bakker68884e32013-01-07 18:20:04 +01001933 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
1935 memcpy( header, ctr, 8 );
1936 header[ 8] = (unsigned char) type;
1937 header[ 9] = (unsigned char)( len >> 8 );
1938 header[10] = (unsigned char)( len );
1939
Paul Bakker68884e32013-01-07 18:20:04 +01001940 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 mbedtls_md_starts( md_ctx );
1942 mbedtls_md_update( md_ctx, secret, md_size );
1943 mbedtls_md_update( md_ctx, padding, padlen );
1944 mbedtls_md_update( md_ctx, header, 11 );
1945 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001946 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
Paul Bakker68884e32013-01-07 18:20:04 +01001948 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 mbedtls_md_starts( md_ctx );
1950 mbedtls_md_update( md_ctx, secret, md_size );
1951 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001952 mbedtls_md_update( md_ctx, out, md_size );
1953 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001956
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001957/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001958 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001959#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001960 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1961 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1962 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1963/* This function makes sure every byte in the memory region is accessed
1964 * (in ascending addresses order) */
1965static void ssl_read_memory( unsigned char *p, size_t len )
1966{
1967 unsigned char acc = 0;
1968 volatile unsigned char force;
1969
1970 for( ; len != 0; p++, len-- )
1971 acc ^= *p;
1972
1973 force = acc;
1974 (void) force;
1975}
1976#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1977
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001978/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001980 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001981
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001982#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001983/* This functions transforms a DTLS plaintext fragment and a record content
1984 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001985 *
1986 * struct {
1987 * opaque content[DTLSPlaintext.length];
1988 * ContentType real_type;
1989 * uint8 zeros[length_of_padding];
1990 * } DTLSInnerPlaintext;
1991 *
1992 * Input:
1993 * - `content`: The beginning of the buffer holding the
1994 * plaintext to be wrapped.
1995 * - `*content_size`: The length of the plaintext in Bytes.
1996 * - `max_len`: The number of Bytes available starting from
1997 * `content`. This must be `>= *content_size`.
1998 * - `rec_type`: The desired record content type.
1999 *
2000 * Output:
2001 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2002 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2003 *
2004 * Returns:
2005 * - `0` on success.
2006 * - A negative error code if `max_len` didn't offer enough space
2007 * for the expansion.
2008 */
2009static int ssl_cid_build_inner_plaintext( unsigned char *content,
2010 size_t *content_size,
2011 size_t remaining,
2012 uint8_t rec_type )
2013{
2014 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002015 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2016 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2017 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002018
2019 /* Write real content type */
2020 if( remaining == 0 )
2021 return( -1 );
2022 content[ len ] = rec_type;
2023 len++;
2024 remaining--;
2025
2026 if( remaining < pad )
2027 return( -1 );
2028 memset( content + len, 0, pad );
2029 len += pad;
2030 remaining -= pad;
2031
2032 *content_size = len;
2033 return( 0 );
2034}
2035
Hanno Becker07dc97d2019-05-20 15:08:01 +01002036/* This function parses a DTLSInnerPlaintext structure.
2037 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002038static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2039 size_t *content_size,
2040 uint8_t *rec_type )
2041{
2042 size_t remaining = *content_size;
2043
2044 /* Determine length of padding by skipping zeroes from the back. */
2045 do
2046 {
2047 if( remaining == 0 )
2048 return( -1 );
2049 remaining--;
2050 } while( content[ remaining ] == 0 );
2051
2052 *content_size = remaining;
2053 *rec_type = content[ remaining ];
2054
2055 return( 0 );
2056}
2057#endif /* MBEDTLS_SSL_CID */
2058
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002059/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002060 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002061static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002062 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002063 mbedtls_record *rec )
2064{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002065 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002066 *
2067 * additional_data = seq_num + TLSCompressed.type +
2068 * TLSCompressed.version + TLSCompressed.length;
2069 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002070 * For the CID extension, this is extended as follows
2071 * (quoting draft-ietf-tls-dtls-connection-id-05,
2072 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002073 *
2074 * additional_data = seq_num + DTLSPlaintext.type +
2075 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002076 * cid +
2077 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002078 * length_of_DTLSInnerPlaintext;
2079 */
2080
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002081 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2082 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002083 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002084
2085#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002086 if( rec->cid_len != 0 )
2087 {
2088 memcpy( add_data + 11, rec->cid, rec->cid_len );
2089 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2090 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2091 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2092 *add_data_len = 13 + 1 + rec->cid_len;
2093 }
2094 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002095#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002096 {
2097 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2098 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2099 *add_data_len = 13;
2100 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002101}
2102
Hanno Beckera18d1322018-01-03 14:27:32 +00002103int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2104 mbedtls_ssl_transform *transform,
2105 mbedtls_record *rec,
2106 int (*f_rng)(void *, unsigned char *, size_t),
2107 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002110 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002111 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002112 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002113 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002114 size_t post_avail;
2115
2116 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002117#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002118 ((void) ssl);
2119#endif
2120
2121 /* The PRNG is used for dynamic IV generation that's used
2122 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2123#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2124 ( defined(MBEDTLS_AES_C) || \
2125 defined(MBEDTLS_ARIA_C) || \
2126 defined(MBEDTLS_CAMELLIA_C) ) && \
2127 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2128 ((void) f_rng);
2129 ((void) p_rng);
2130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002135 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2138 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002139 if( rec == NULL
2140 || rec->buf == NULL
2141 || rec->buf_len < rec->data_offset
2142 || rec->buf_len - rec->data_offset < rec->data_len
2143#if defined(MBEDTLS_SSL_CID)
2144 || rec->cid_len != 0
2145#endif
2146 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002147 {
2148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002150 }
2151
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002152 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002153 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002155 data, rec->data_len );
2156
2157 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2158
2159 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2162 (unsigned) rec->data_len,
2163 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2165 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002166
Hanno Beckercab87e62019-04-29 13:52:53 +01002167#if defined(MBEDTLS_SSL_CID)
2168 /*
2169 * Add CID information
2170 */
2171 rec->cid_len = transform->out_cid_len;
2172 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2173 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002174
2175 if( rec->cid_len != 0 )
2176 {
2177 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002178 * Wrap plaintext into DTLSInnerPlaintext structure.
2179 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002180 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002181 * Note that this changes `rec->data_len`, and hence
2182 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002183 */
2184 if( ssl_cid_build_inner_plaintext( data,
2185 &rec->data_len,
2186 post_avail,
2187 rec->type ) != 0 )
2188 {
2189 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2190 }
2191
2192 rec->type = MBEDTLS_SSL_MSG_CID;
2193 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002194#endif /* MBEDTLS_SSL_CID */
2195
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002196 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2197
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002199 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 */
Hanno Becker52344c22018-01-03 15:24:20 +00002201#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 if( mode == MBEDTLS_MODE_STREAM ||
2203 ( mode == MBEDTLS_MODE_CBC
2204#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002205 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002206#endif
2207 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002209 if( post_avail < transform->maclen )
2210 {
2211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2212 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2213 }
2214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002217 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002218 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2220 data, rec->data_len, rec->ctr, rec->type, mac );
2221 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002222 }
2223 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2226 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002228 {
Hanno Becker992b6872017-11-09 18:57:39 +00002229 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2230
Hanno Beckercab87e62019-04-29 13:52:53 +01002231 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002232
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002234 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002235 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2236 data, rec->data_len );
2237 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2238 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2239
2240 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 }
2242 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002243#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002247 }
2248
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002249 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2250 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002251
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002252 rec->data_len += transform->maclen;
2253 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002254 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002255 }
Hanno Becker52344c22018-01-03 15:24:20 +00002256#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002258 /*
2259 * Encrypt
2260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2262 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002264 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002265 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002267 "including %d bytes of padding",
2268 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002270 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2271 transform->iv_enc, transform->ivlen,
2272 data, rec->data_len,
2273 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002276 return( ret );
2277 }
2278
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002279 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2282 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
Paul Bakker68884e32013-01-07 18:20:04 +01002285 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002287
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002288#if defined(MBEDTLS_GCM_C) || \
2289 defined(MBEDTLS_CCM_C) || \
2290 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002292 mode == MBEDTLS_MODE_CCM ||
2293 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002294 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002295 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002296 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002297 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002298
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002299 /* Check that there's space for both the authentication tag
2300 * and the explicit IV before and after the record content. */
2301 if( post_avail < transform->taglen ||
2302 rec->data_offset < explicit_iv_len )
2303 {
2304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2305 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2306 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002307
Paul Bakker68884e32013-01-07 18:20:04 +01002308 /*
2309 * Generate IV
2310 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002311 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2312 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002313 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002314 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002315 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2316 explicit_iv_len );
2317 /* Prefix record content with explicit IV. */
2318 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002319 }
2320 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2321 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002322 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002323 unsigned char i;
2324
2325 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2326
2327 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002328 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002329 }
2330 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002331 {
2332 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2334 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002335 }
2336
Hanno Beckercab87e62019-04-29 13:52:53 +01002337 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002338
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2340 iv, transform->ivlen );
2341 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002342 data - explicit_iv_len, explicit_iv_len );
2343 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002344 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002347 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002348
Paul Bakker68884e32013-01-07 18:20:04 +01002349 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002350 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002351 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002354 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002355 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002356 data, rec->data_len, /* source */
2357 data, &rec->data_len, /* destination */
2358 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002361 return( ret );
2362 }
2363
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002364 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2365 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002366
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002367 rec->data_len += transform->taglen + explicit_iv_len;
2368 rec->data_offset -= explicit_iv_len;
2369 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002370 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2374#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002375 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002378 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 size_t padlen, i;
2380 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002381
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002382 /* Currently we're always using minimal padding
2383 * (up to 255 bytes would be allowed). */
2384 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2385 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 padlen = 0;
2387
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 /* Check there's enough space in the buffer for the padding. */
2389 if( post_avail < padlen + 1 )
2390 {
2391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2392 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2393 }
2394
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002396 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 rec->data_len += padlen + 1;
2399 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002402 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002403 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2404 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002405 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002407 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002408 if( f_rng == NULL )
2409 {
2410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2412 }
2413
2414 if( rec->data_offset < transform->ivlen )
2415 {
2416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2417 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2418 }
2419
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420 /*
2421 * Generate IV
2422 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002423 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002424 if( ret != 0 )
2425 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002427 memcpy( data - transform->ivlen, transform->iv_enc,
2428 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002430 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002434 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002435 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002436 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2439 transform->iv_enc,
2440 transform->ivlen,
2441 data, rec->data_len,
2442 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002445 return( ret );
2446 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002447
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002448 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002452 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002455 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002456 {
2457 /*
2458 * Save IV in SSL3 and TLS1
2459 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2461 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002463 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002464#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465 {
2466 data -= transform->ivlen;
2467 rec->data_offset -= transform->ivlen;
2468 rec->data_len += transform->ivlen;
2469 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002472 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002474 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2475
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002476 /*
2477 * MAC(MAC_write_key, seq_num +
2478 * TLSCipherText.type +
2479 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002480 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002481 * IV + // except for TLS 1.0
2482 * ENC(content + padding + padding_length));
2483 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002484
2485 if( post_avail < transform->maclen)
2486 {
2487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2488 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2489 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002490
Hanno Beckercab87e62019-04-29 13:52:53 +01002491 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002495 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002497 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002498 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2500 data, rec->data_len );
2501 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2502 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002503
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002504 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002506 rec->data_len += transform->maclen;
2507 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002508 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002512 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002514 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002520 /* Make extra sure authentication was performed, exactly once */
2521 if( auth_done != 1 )
2522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002525 }
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
2529 return( 0 );
2530}
2531
Hanno Beckera18d1322018-01-03 14:27:32 +00002532int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2533 mbedtls_ssl_transform *transform,
2534 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002535{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002536 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002538 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002539#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002540 size_t padlen = 0, correct = 1;
2541#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002542 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002543 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002544 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002545
Hanno Beckera18d1322018-01-03 14:27:32 +00002546#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002547 ((void) ssl);
2548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002551 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002552 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2555 }
2556 if( rec == NULL ||
2557 rec->buf == NULL ||
2558 rec->buf_len < rec->data_offset ||
2559 rec->buf_len - rec->data_offset < rec->data_len )
2560 {
2561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002563 }
2564
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002565 data = rec->buf + rec->data_offset;
2566 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Hanno Beckercab87e62019-04-29 13:52:53 +01002568#if defined(MBEDTLS_SSL_CID)
2569 /*
2570 * Match record's CID with incoming CID.
2571 */
Hanno Becker938489a2019-05-08 13:02:22 +01002572 if( rec->cid_len != transform->in_cid_len ||
2573 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2574 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002575 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002576 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002577#endif /* MBEDTLS_SSL_CID */
2578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2580 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002581 {
2582 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002583 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2584 transform->iv_dec,
2585 transform->ivlen,
2586 data, rec->data_len,
2587 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002590 return( ret );
2591 }
2592
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
Paul Bakker68884e32013-01-07 18:20:04 +01002599 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002601#if defined(MBEDTLS_GCM_C) || \
2602 defined(MBEDTLS_CCM_C) || \
2603 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002605 mode == MBEDTLS_MODE_CCM ||
2606 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002607 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002608 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002610
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002611 /*
2612 * Compute and update sizes
2613 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002614 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 "+ taglen (%d)", rec->data_len,
2618 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002620 }
Paul Bakker68884e32013-01-07 18:20:04 +01002621
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002622 /*
2623 * Prepare IV
2624 */
2625 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2626 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002627 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002628 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002629 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002630
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002631 }
2632 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2633 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002634 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002635 unsigned char i;
2636
2637 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2638
2639 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002640 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002641 }
2642 else
2643 {
2644 /* Reminder if we ever add an AEAD mode with a different size */
2645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2647 }
2648
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649 data += explicit_iv_len;
2650 rec->data_offset += explicit_iv_len;
2651 rec->data_len -= explicit_iv_len + transform->taglen;
2652
Hanno Beckercab87e62019-04-29 13:52:53 +01002653 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002654 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002655 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656
2657 memcpy( transform->iv_dec + transform->fixed_ivlen,
2658 data - explicit_iv_len, explicit_iv_len );
2659
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002660 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002661 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002662 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002663
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002665 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002666 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002667 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2669 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002670 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 data, rec->data_len,
2672 data, &olen,
2673 data + rec->data_len,
2674 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2679 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002680
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002681 return( ret );
2682 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002683 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002684
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002685 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002689 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2693#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002694 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002697 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002698
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 /*
Paul Bakker45829992013-01-03 14:52:21 +01002700 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2704 {
2705 /* The ciphertext is prefixed with the CBC IV. */
2706 minlen += transform->ivlen;
2707 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002708#endif
Paul Bakker45829992013-01-03 14:52:21 +01002709
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002710 /* Size considerations:
2711 *
2712 * - The CBC cipher text must not be empty and hence
2713 * at least of size transform->ivlen.
2714 *
2715 * Together with the potential IV-prefix, this explains
2716 * the first of the two checks below.
2717 *
2718 * - The record must contain a MAC, either in plain or
2719 * encrypted, depending on whether Encrypt-then-MAC
2720 * is used or not.
2721 * - If it is, the message contains the IV-prefix,
2722 * the CBC ciphertext, and the MAC.
2723 * - If it is not, the padded plaintext, and hence
2724 * the CBC ciphertext, has at least length maclen + 1
2725 * because there is at least the padding length byte.
2726 *
2727 * As the CBC ciphertext is not empty, both cases give the
2728 * lower bound minlen + maclen + 1 on the record size, which
2729 * we test for in the second check below.
2730 */
2731 if( rec->data_len < minlen + transform->ivlen ||
2732 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002735 "+ 1 ) ( + expl IV )", rec->data_len,
2736 transform->ivlen,
2737 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002739 }
2740
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002741 /*
2742 * Authenticate before decrypt if enabled
2743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002745 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002746 {
Hanno Becker992b6872017-11-09 18:57:39 +00002747 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002750
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002751 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2752 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002753
Hanno Beckercab87e62019-04-29 13:52:53 +01002754 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002755
Hanno Beckercab87e62019-04-29 13:52:53 +01002756 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2757 add_data_len );
2758 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2759 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002760 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2761 data, rec->data_len );
2762 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2763 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002764
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002765 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2766 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002767 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002769
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002770 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2771 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002775 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002776 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002779
2780 /*
2781 * Check length sanity
2782 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002783 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002786 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 }
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002791 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002792 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002793 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 /* This is safe because data_len >= minlen + maclen + 1 initially,
2797 * and at this point we have at most subtracted maclen (note that
2798 * minlen == transform->ivlen here). */
2799 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002800
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002801 data += transform->ivlen;
2802 rec->data_offset += transform->ivlen;
2803 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002806
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2808 transform->iv_dec, transform->ivlen,
2809 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002812 return( ret );
2813 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002814
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002815 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002819 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002822 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002823 {
2824 /*
2825 * Save IV in SSL3 and TLS1
2826 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002827 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2828 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002830#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 /* Safe since data_len >= minlen + maclen + 1, so after having
2833 * subtracted at most minlen and maclen up to this point,
2834 * data_len > 0. */
2835 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002836
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002837 if( auth_done == 1 )
2838 {
2839 correct *= ( rec->data_len >= padlen + 1 );
2840 padlen *= ( rec->data_len >= padlen + 1 );
2841 }
2842 else
Paul Bakker45829992013-01-03 14:52:21 +01002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002845 if( rec->data_len < transform->maclen + padlen + 1 )
2846 {
2847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2848 rec->data_len,
2849 transform->maclen,
2850 padlen + 1 ) );
2851 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002852#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002853
2854 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2855 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002858 padlen++;
2859
2860 /* Regardless of the validity of the padding,
2861 * we have data_len >= padlen here. */
2862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002866 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_DEBUG_ALL)
2869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 "should be no more than %d",
2871 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002872#endif
Paul Bakker45829992013-01-03 14:52:21 +01002873 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 }
2875 }
2876 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2878#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2879 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002880 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 /* The padding check involves a series of up to 256
2883 * consecutive memory reads at the end of the record
2884 * plaintext buffer. In order to hide the length and
2885 * validity of the padding, always perform exactly
2886 * `min(256,plaintext_len)` reads (but take into account
2887 * only the last `padlen` bytes for the padding check). */
2888 size_t pad_count = 0;
2889 size_t real_count = 0;
2890 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002891
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002892 /* Index of first padding byte; it has been ensured above
2893 * that the subtraction is safe. */
2894 size_t const padding_idx = rec->data_len - padlen;
2895 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2896 size_t const start_idx = rec->data_len - num_checks;
2897 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002898
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002900 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002901 real_count |= ( idx >= padding_idx );
2902 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002903 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002907 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002909#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002910 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002912 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2914 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002918 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002919
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002920 /* If the padding was found to be invalid, padlen == 0
2921 * and the subtraction is safe. If the padding was found valid,
2922 * padlen hasn't been changed and the previous assertion
2923 * data_len >= padlen still holds. */
2924 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002926 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002928 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002932 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002934#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002936 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002937#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
2939 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002940 * Authenticate if not done yet.
2941 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 */
Hanno Becker52344c22018-01-03 15:24:20 +00002943#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002944 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002945 {
Hanno Becker992b6872017-11-09 18:57:39 +00002946 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002947
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002948 /* If the initial value of padlen was such that
2949 * data_len < maclen + padlen + 1, then padlen
2950 * got reset to 1, and the initial check
2951 * data_len >= minlen + maclen + 1
2952 * guarantees that at this point we still
2953 * have at least data_len >= maclen.
2954 *
2955 * If the initial value of padlen was such that
2956 * data_len >= maclen + padlen + 1, then we have
2957 * subtracted either padlen + 1 (if the padding was correct)
2958 * or 0 (if the padding was incorrect) since then,
2959 * hence data_len >= maclen in any case.
2960 */
2961 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Hanno Beckercab87e62019-04-29 13:52:53 +01002963 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002966 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002967 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002968 ssl_mac( &transform->md_ctx_dec,
2969 transform->mac_dec,
2970 data, rec->data_len,
2971 rec->ctr, rec->type,
2972 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002973 }
2974 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2976#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2977 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002978 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002979 {
2980 /*
2981 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002982 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002983 *
2984 * Known timing attacks:
2985 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2986 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002987 * To compensate for different timings for the MAC calculation
2988 * depending on how much padding was removed (which is determined
2989 * by padlen), process extra_run more blocks through the hash
2990 * function.
2991 *
2992 * The formula in the paper is
2993 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2994 * where L1 is the size of the header plus the decrypted message
2995 * plus CBC padding and L2 is the size of the header plus the
2996 * decrypted message. This is for an underlying hash function
2997 * with 64-byte blocks.
2998 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2999 * correctly. We round down instead of up, so -56 is the correct
3000 * value for our calculations instead of -55.
3001 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003002 * Repeat the formula rather than defining a block_size variable.
3003 * This avoids requiring division by a variable at runtime
3004 * (which would be marginally less efficient and would require
3005 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003006 */
3007 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003008 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003009
3010 /*
3011 * The next two sizes are the minimum and maximum values of
3012 * in_msglen over all padlen values.
3013 *
3014 * They're independent of padlen, since we previously did
3015 * in_msglen -= padlen.
3016 *
3017 * Note that max_len + maclen is never more than the buffer
3018 * length, as we previously did in_msglen -= maclen too.
3019 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003020 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003021 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3022
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003023 memset( tmp, 0, sizeof( tmp ) );
3024
3025 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003026 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003027#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3028 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003029 case MBEDTLS_MD_MD5:
3030 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003031 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003032 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003033 extra_run =
3034 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3035 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003036 break;
3037#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003038#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003039 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003040 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003041 extra_run =
3042 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3043 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003044 break;
3045#endif
3046 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3049 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003050
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003051 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003052
Hanno Beckercab87e62019-04-29 13:52:53 +01003053 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3054 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003055 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3056 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003057 /* Make sure we access everything even when padlen > 0. This
3058 * makes the synchronisation requirements for just-in-time
3059 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003060 ssl_read_memory( data + rec->data_len, padlen );
3061 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003062
3063 /* Call mbedtls_md_process at least once due to cache attacks
3064 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003065 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003066 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003067
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003068 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003069
3070 /* Make sure we access all the memory that could contain the MAC,
3071 * before we check it in the next code block. This makes the
3072 * synchronisation requirements for just-in-time Prime+Probe
3073 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003074 ssl_read_memory( data + min_len,
3075 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003076 }
3077 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3079 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003085#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003086 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3087 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3091 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#if defined(MBEDTLS_SSL_DEBUG_ALL)
3094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003095#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003096 correct = 0;
3097 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003098 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003099 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003100
3101 /*
3102 * Finally check the correct flag
3103 */
3104 if( correct == 0 )
3105 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003106#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003107
3108 /* Make extra sure authentication was performed, exactly once */
3109 if( auth_done != 1 )
3110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003115#if defined(MBEDTLS_SSL_CID)
3116 if( rec->cid_len != 0 )
3117 {
3118 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3119 &rec->type );
3120 if( ret != 0 )
3121 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3122 }
3123#endif /* MBEDTLS_SSL_CID */
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
3127 return( 0 );
3128}
3129
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003130#undef MAC_NONE
3131#undef MAC_PLAINTEXT
3132#undef MAC_CIPHERTEXT
3133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003135/*
3136 * Compression/decompression functions
3137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003139{
3140 int ret;
3141 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003142 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003143 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003144 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003147
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003148 if( len_pre == 0 )
3149 return( 0 );
3150
Paul Bakker2770fbd2012-07-03 13:30:23 +00003151 memcpy( msg_pre, ssl->out_msg, len_pre );
3152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003154 ssl->out_msglen ) );
3155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003157 ssl->out_msg, ssl->out_msglen );
3158
Paul Bakker48916f92012-09-16 19:57:18 +00003159 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3160 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3161 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003162 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003163
Paul Bakker48916f92012-09-16 19:57:18 +00003164 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003165 if( ret != Z_OK )
3166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3168 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003169 }
3170
Angus Grattond8213d02016-05-25 20:56:48 +10003171 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003172 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175 ssl->out_msglen ) );
3176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003178 ssl->out_msg, ssl->out_msglen );
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181
3182 return( 0 );
3183}
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186{
3187 int ret;
3188 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003189 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003191 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003195 if( len_pre == 0 )
3196 return( 0 );
3197
Paul Bakker2770fbd2012-07-03 13:30:23 +00003198 memcpy( msg_pre, ssl->in_msg, len_pre );
3199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003201 ssl->in_msglen ) );
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003204 ssl->in_msg, ssl->in_msglen );
3205
Paul Bakker48916f92012-09-16 19:57:18 +00003206 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3207 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3208 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003209 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003210 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003211
Paul Bakker48916f92012-09-16 19:57:18 +00003212 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003213 if( ret != Z_OK )
3214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3216 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003217 }
3218
Angus Grattond8213d02016-05-25 20:56:48 +10003219 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003220 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003223 ssl->in_msglen ) );
3224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003226 ssl->in_msg, ssl->in_msglen );
3227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003229
3230 return( 0 );
3231}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3235static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237#if defined(MBEDTLS_SSL_PROTO_DTLS)
3238static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003239{
3240 /* If renegotiation is not enforced, retransmit until we would reach max
3241 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003242 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003243 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003244 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003245 unsigned char doublings = 1;
3246
3247 while( ratio != 0 )
3248 {
3249 ++doublings;
3250 ratio >>= 1;
3251 }
3252
3253 if( ++ssl->renego_records_seen > doublings )
3254 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003256 return( 0 );
3257 }
3258 }
3259
3260 return( ssl_write_hello_request( ssl ) );
3261}
3262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003264
Paul Bakker5121ce52009-01-03 21:22:43 +00003265/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003266 * Fill the input message buffer by appending data to it.
3267 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003268 *
3269 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3270 * available (from this read and/or a previous one). Otherwise, an error code
3271 * is returned (possibly EOF or WANT_READ).
3272 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003273 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3274 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3275 * since we always read a whole datagram at once.
3276 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003277 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003278 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003281{
Paul Bakker23986e52011-04-24 08:57:21 +00003282 int ret;
3283 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003287 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003290 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003292 }
3293
Angus Grattond8213d02016-05-25 20:56:48 +10003294 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003298 }
3299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003301 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003303 uint32_t timeout;
3304
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003305 /* Just to be sure */
3306 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3307 {
3308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3309 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3311 }
3312
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003313 /*
3314 * The point is, we need to always read a full datagram at once, so we
3315 * sometimes read more then requested, and handle the additional data.
3316 * It could be the rest of the current record (while fetching the
3317 * header) and/or some other records in the same datagram.
3318 */
3319
3320 /*
3321 * Move to the next record in the already read datagram if applicable
3322 */
3323 if( ssl->next_record_offset != 0 )
3324 {
3325 if( ssl->in_left < ssl->next_record_offset )
3326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003329 }
3330
3331 ssl->in_left -= ssl->next_record_offset;
3332
3333 if( ssl->in_left != 0 )
3334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003336 ssl->next_record_offset ) );
3337 memmove( ssl->in_hdr,
3338 ssl->in_hdr + ssl->next_record_offset,
3339 ssl->in_left );
3340 }
3341
3342 ssl->next_record_offset = 0;
3343 }
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003347
3348 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003349 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003350 */
3351 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003354 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003355 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003356
3357 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003358 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003359 * are not at the beginning of a new record, the caller did something
3360 * wrong.
3361 */
3362 if( ssl->in_left != 0 )
3363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3365 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003366 }
3367
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003368 /*
3369 * Don't even try to read if time's out already.
3370 * This avoids by-passing the timer when repeatedly receiving messages
3371 * that will end up being dropped.
3372 */
3373 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003374 {
3375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003376 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003377 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003378 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003379 {
Angus Grattond8213d02016-05-25 20:56:48 +10003380 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003383 timeout = ssl->handshake->retransmit_timeout;
3384 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003385 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003388
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003389 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003390 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3391 timeout );
3392 else
3393 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003396
3397 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399 }
3400
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003401 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003404 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003407 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003408 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003411 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003412 }
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003417 return( ret );
3418 }
3419
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003420 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003421 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003423 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003425 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003426 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003429 return( ret );
3430 }
3431
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003432 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003433 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003435 }
3436
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 if( ret < 0 )
3438 return( ret );
3439
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003440 ssl->in_left = ret;
3441 }
3442 else
3443#endif
3444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003446 ssl->in_left, nb_want ) );
3447
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003448 while( ssl->in_left < nb_want )
3449 {
3450 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003451
3452 if( ssl_check_timer( ssl ) != 0 )
3453 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3454 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003455 {
3456 if( ssl->f_recv_timeout != NULL )
3457 {
3458 ret = ssl->f_recv_timeout( ssl->p_bio,
3459 ssl->in_hdr + ssl->in_left, len,
3460 ssl->conf->read_timeout );
3461 }
3462 else
3463 {
3464 ret = ssl->f_recv( ssl->p_bio,
3465 ssl->in_hdr + ssl->in_left, len );
3466 }
3467 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003470 ssl->in_left, nb_want ) );
3471 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003472
3473 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003475
3476 if( ret < 0 )
3477 return( ret );
3478
mohammad160352aecb92018-03-28 23:41:40 -07003479 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003480 {
Darryl Green11999bb2018-03-13 15:22:58 +00003481 MBEDTLS_SSL_DEBUG_MSG( 1,
3482 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003483 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3485 }
3486
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003487 ssl->in_left += ret;
3488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 }
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003492
3493 return( 0 );
3494}
3495
3496/*
3497 * Flush any data not yet written
3498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003500{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003501 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003502 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003505
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003506 if( ssl->f_send == NULL )
3507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003509 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003511 }
3512
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003513 /* Avoid incrementing counter if data is flushed */
3514 if( ssl->out_left == 0 )
3515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003517 return( 0 );
3518 }
3519
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 while( ssl->out_left > 0 )
3521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003523 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Hanno Becker2b1e3542018-08-06 11:19:13 +01003525 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003526 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
3530 if( ret <= 0 )
3531 return( ret );
3532
mohammad160352aecb92018-03-28 23:41:40 -07003533 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003534 {
Darryl Green11999bb2018-03-13 15:22:58 +00003535 MBEDTLS_SSL_DEBUG_MSG( 1,
3536 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003537 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3539 }
3540
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 ssl->out_left -= ret;
3542 }
3543
Hanno Becker2b1e3542018-08-06 11:19:13 +01003544#if defined(MBEDTLS_SSL_PROTO_DTLS)
3545 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003546 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003547 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003548 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003549 else
3550#endif
3551 {
3552 ssl->out_hdr = ssl->out_buf + 8;
3553 }
3554 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003557
3558 return( 0 );
3559}
3560
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003561/*
3562 * Functions to handle the DTLS retransmission state machine
3563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003565/*
3566 * Append current handshake message to current outgoing flight
3567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3572 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3573 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003574
3575 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003576 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003580 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 }
3582
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003583 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003584 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003587 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003588 }
3589
3590 /* Copy current handshake message with headers */
3591 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3592 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003593 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003594 msg->next = NULL;
3595
3596 /* Append to the current flight */
3597 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003598 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003599 else
3600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003602 while( cur->next != NULL )
3603 cur = cur->next;
3604 cur->next = msg;
3605 }
3606
Hanno Becker3b235902018-08-06 09:54:53 +01003607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003608 return( 0 );
3609}
3610
3611/*
3612 * Free the current flight of handshake messages
3613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 mbedtls_ssl_flight_item *cur = flight;
3617 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618
3619 while( cur != NULL )
3620 {
3621 next = cur->next;
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 mbedtls_free( cur->p );
3624 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625
3626 cur = next;
3627 }
3628}
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3631static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003632#endif
3633
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003634/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003635 * Swap transform_out and out_ctr with the alternative ones
3636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003640 unsigned char tmp_out_ctr[8];
3641
3642 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003645 return;
3646 }
3647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003649
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003650 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003651 tmp_transform = ssl->transform_out;
3652 ssl->transform_out = ssl->handshake->alt_transform_out;
3653 ssl->handshake->alt_transform_out = tmp_transform;
3654
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003655 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003656 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3657 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003658 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003659
3660 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003661 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3664 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3669 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003670 }
3671 }
3672#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003673}
3674
3675/*
3676 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003677 */
3678int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3679{
3680 int ret = 0;
3681
3682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3683
3684 ret = mbedtls_ssl_flight_transmit( ssl );
3685
3686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3687
3688 return( ret );
3689}
3690
3691/*
3692 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003693 *
3694 * Need to remember the current message in case flush_output returns
3695 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003696 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003697 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003698int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003699{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003700 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003704 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003706
3707 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003708 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003709 ssl_swap_epochs( ssl );
3710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003712 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003713
3714 while( ssl->handshake->cur_msg != NULL )
3715 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003716 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003717 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003718
Hanno Beckere1dcb032018-08-17 16:47:58 +01003719 int const is_finished =
3720 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3721 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3722
Hanno Becker04da1892018-08-14 13:22:10 +01003723 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3724 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3725
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003726 /* Swap epochs before sending Finished: we can't do it after
3727 * sending ChangeCipherSpec, in case write returns WANT_READ.
3728 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003729 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003730 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003732 ssl_swap_epochs( ssl );
3733 }
3734
Hanno Becker67bc7c32018-08-06 11:33:50 +01003735 ret = ssl_get_remaining_payload_in_datagram( ssl );
3736 if( ret < 0 )
3737 return( ret );
3738 max_frag_len = (size_t) ret;
3739
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003740 /* CCS is copied as is, while HS messages may need fragmentation */
3741 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3742 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003743 if( max_frag_len == 0 )
3744 {
3745 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3746 return( ret );
3747
3748 continue;
3749 }
3750
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003751 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003752 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003753 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003754
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003755 /* Update position inside current message */
3756 ssl->handshake->cur_msg_p += cur->len;
3757 }
3758 else
3759 {
3760 const unsigned char * const p = ssl->handshake->cur_msg_p;
3761 const size_t hs_len = cur->len - 12;
3762 const size_t frag_off = p - ( cur->p + 12 );
3763 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003764 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003765
Hanno Beckere1dcb032018-08-17 16:47:58 +01003766 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003767 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003768 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003770
Hanno Becker67bc7c32018-08-06 11:33:50 +01003771 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3772 return( ret );
3773
3774 continue;
3775 }
3776 max_hs_frag_len = max_frag_len - 12;
3777
3778 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3779 max_hs_frag_len : rem_len;
3780
3781 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003782 {
3783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003784 (unsigned) cur_hs_frag_len,
3785 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003786 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003787
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003788 /* Messages are stored with handshake headers as if not fragmented,
3789 * copy beginning of headers then fill fragmentation fields.
3790 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3791 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003792
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003793 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3794 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3795 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3796
Hanno Becker67bc7c32018-08-06 11:33:50 +01003797 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3798 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3799 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003800
3801 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3802
Hanno Becker3f7b9732018-08-28 09:53:25 +01003803 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003804 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3805 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003806 ssl->out_msgtype = cur->type;
3807
3808 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003809 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003810 }
3811
3812 /* If done with the current message move to the next one if any */
3813 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3814 {
3815 if( cur->next != NULL )
3816 {
3817 ssl->handshake->cur_msg = cur->next;
3818 ssl->handshake->cur_msg_p = cur->next->p + 12;
3819 }
3820 else
3821 {
3822 ssl->handshake->cur_msg = NULL;
3823 ssl->handshake->cur_msg_p = NULL;
3824 }
3825 }
3826
3827 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003828 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003831 return( ret );
3832 }
3833 }
3834
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3836 return( ret );
3837
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003838 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3840 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003841 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003844 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3845 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003846
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003848
3849 return( 0 );
3850}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003851
3852/*
3853 * To be called when the last message of an incoming flight is received.
3854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003856{
3857 /* We won't need to resend that one any more */
3858 ssl_flight_free( ssl->handshake->flight );
3859 ssl->handshake->flight = NULL;
3860 ssl->handshake->cur_msg = NULL;
3861
3862 /* The next incoming flight will start with this msg_seq */
3863 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3864
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003865 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003866 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003867
Hanno Becker0271f962018-08-16 13:23:47 +01003868 /* Clear future message buffering structure. */
3869 ssl_buffering_free( ssl );
3870
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003871 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003872 ssl_set_timer( ssl, 0 );
3873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3875 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003878 }
3879 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003881}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003882
3883/*
3884 * To be called when the last message of an outgoing flight is send.
3885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003887{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003888 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003889 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3892 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003895 }
3896 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003900
Paul Bakker5121ce52009-01-03 21:22:43 +00003901/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003902 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003903 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003904
3905/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003906 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003907 *
3908 * - fill in handshake headers
3909 * - update handshake checksum
3910 * - DTLS: save message for resending
3911 * - then pass to the record layer
3912 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003913 * DTLS: except for HelloRequest, messages are only queued, and will only be
3914 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003915 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003916 * Inputs:
3917 * - ssl->out_msglen: 4 + actual handshake message len
3918 * (4 is the size of handshake headers for TLS)
3919 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3920 * - ssl->out_msg + 4: the handshake message body
3921 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003922 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003923 * - ssl->out_msglen: the length of the record contents
3924 * (including handshake headers but excluding record headers)
3925 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003926 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003927int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003928{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003929 int ret;
3930 const size_t hs_len = ssl->out_msglen - 4;
3931 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003932
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3934
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003935 /*
3936 * Sanity checks
3937 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003938 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003939 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3940 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003941 /* In SSLv3, the client might send a NoCertificate alert. */
3942#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3943 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3944 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3945 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3946#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3947 {
3948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3950 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003953 /* Whenever we send anything different from a
3954 * HelloRequest we should be in a handshake - double check. */
3955 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3956 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003957 ssl->handshake == NULL )
3958 {
3959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003965 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003967 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3969 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003971#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003972
Hanno Beckerb50a2532018-08-06 11:52:54 +01003973 /* Double-check that we did not exceed the bounds
3974 * of the outgoing record buffer.
3975 * This should never fail as the various message
3976 * writing functions must obey the bounds of the
3977 * outgoing record buffer, but better be safe.
3978 *
3979 * Note: We deliberately do not check for the MTU or MFL here.
3980 */
3981 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3982 {
3983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3984 "size %u, maximum %u",
3985 (unsigned) ssl->out_msglen,
3986 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3988 }
3989
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003990 /*
3991 * Fill handshake headers
3992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003994 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003995 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3996 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3997 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003999 /*
4000 * DTLS has additional fields in the Handshake layer,
4001 * between the length field and the actual payload:
4002 * uint16 message_seq;
4003 * uint24 fragment_offset;
4004 * uint24 fragment_length;
4005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004007 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004008 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004009 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004010 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004011 {
4012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4013 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004014 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004015 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4017 }
4018
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004019 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004020 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004021
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004022 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004023 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004024 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004025 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4026 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4027 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004028 }
4029 else
4030 {
4031 ssl->out_msg[4] = 0;
4032 ssl->out_msg[5] = 0;
4033 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004034
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004035 /* Handshake hashes are computed without fragmentation,
4036 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004037 memset( ssl->out_msg + 6, 0x00, 3 );
4038 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004041
Hanno Becker0207e532018-08-28 10:28:28 +01004042 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004043 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4044 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 }
4046
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004047 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004050 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4051 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004052 {
4053 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056 return( ret );
4057 }
4058 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004059 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004060#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004061 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004062 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004063 {
4064 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4065 return( ret );
4066 }
4067 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004068
4069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4070
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004072}
4073
4074/*
4075 * Record layer functions
4076 */
4077
4078/*
4079 * Write current record.
4080 *
4081 * Uses:
4082 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4083 * - ssl->out_msglen: length of the record content (excl headers)
4084 * - ssl->out_msg: record content
4085 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004086int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004087{
4088 int ret, done = 0;
4089 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004090 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004091
4092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004095 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004097 {
4098 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004101 return( ret );
4102 }
4103
4104 len = ssl->out_msglen;
4105 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4109 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113 ret = mbedtls_ssl_hw_record_write( ssl );
4114 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4117 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004118 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004119
4120 if( ret == 0 )
4121 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004122 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004124 if( !done )
4125 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004126 unsigned i;
4127 size_t protected_record_size;
4128
Hanno Becker6430faf2019-05-08 11:57:13 +01004129 /* Skip writing the record content type to after the encryption,
4130 * as it may change when using the CID extension. */
4131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004133 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004134
Hanno Becker19859472018-08-06 09:40:20 +01004135 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004136 ssl->out_len[0] = (unsigned char)( len >> 8 );
4137 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004138
Paul Bakker48916f92012-09-16 19:57:18 +00004139 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004140 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004141 mbedtls_record rec;
4142
4143 rec.buf = ssl->out_iv;
4144 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4145 ( ssl->out_iv - ssl->out_buf );
4146 rec.data_len = ssl->out_msglen;
4147 rec.data_offset = ssl->out_msg - rec.buf;
4148
4149 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4150 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4151 ssl->conf->transport, rec.ver );
4152 rec.type = ssl->out_msgtype;
4153
Hanno Becker43c24b82019-05-01 09:45:57 +01004154#if defined(MBEDTLS_SSL_CID)
4155 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004156 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004157#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004158
Hanno Beckera18d1322018-01-03 14:27:32 +00004159 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004160 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004163 return( ret );
4164 }
4165
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004166 if( rec.data_offset != 0 )
4167 {
4168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4170 }
4171
Hanno Becker6430faf2019-05-08 11:57:13 +01004172 /* Update the record content type and CID. */
4173 ssl->out_msgtype = rec.type;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004174#if defined(MBEDTLS_SSL_CID )
4175 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
4176#endif /* MBEDTLS_SSL_CID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004177 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004178 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4179 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004180 }
4181
Hanno Becker5903de42019-05-03 14:46:38 +01004182 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004183
4184#if defined(MBEDTLS_SSL_PROTO_DTLS)
4185 /* In case of DTLS, double-check that we don't exceed
4186 * the remaining space in the datagram. */
4187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4188 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004189 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004190 if( ret < 0 )
4191 return( ret );
4192
4193 if( protected_record_size > (size_t) ret )
4194 {
4195 /* Should never happen */
4196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4197 }
4198 }
4199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004200
Hanno Becker6430faf2019-05-08 11:57:13 +01004201 /* Now write the potentially updated record content type. */
4202 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004205 "version = [%d:%d], msglen = %d",
4206 ssl->out_hdr[0], ssl->out_hdr[1],
4207 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004210 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004211
4212 ssl->out_left += protected_record_size;
4213 ssl->out_hdr += protected_record_size;
4214 ssl_update_out_pointers( ssl, ssl->transform_out );
4215
Hanno Becker04484622018-08-06 09:49:38 +01004216 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4217 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4218 break;
4219
4220 /* The loop goes to its end iff the counter is wrapping */
4221 if( i == ssl_ep_len( ssl ) )
4222 {
4223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4224 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4225 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004226 }
4227
Hanno Becker67bc7c32018-08-06 11:33:50 +01004228#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4230 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004231 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004232 size_t remaining;
4233 ret = ssl_get_remaining_payload_in_datagram( ssl );
4234 if( ret < 0 )
4235 {
4236 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4237 ret );
4238 return( ret );
4239 }
4240
4241 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004243 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004244 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004245 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004246 else
4247 {
Hanno Becker513815a2018-08-20 11:56:09 +01004248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004249 }
4250 }
4251#endif /* MBEDTLS_SSL_PROTO_DTLS */
4252
4253 if( ( flush == SSL_FORCE_FLUSH ) &&
4254 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 return( ret );
4258 }
4259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004261
4262 return( 0 );
4263}
4264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004266
4267static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4268{
4269 if( ssl->in_msglen < ssl->in_hslen ||
4270 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4271 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4272 {
4273 return( 1 );
4274 }
4275 return( 0 );
4276}
Hanno Becker44650b72018-08-16 12:51:11 +01004277
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004278static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004279{
4280 return( ( ssl->in_msg[9] << 16 ) |
4281 ( ssl->in_msg[10] << 8 ) |
4282 ssl->in_msg[11] );
4283}
4284
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004285static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004286{
4287 return( ( ssl->in_msg[6] << 16 ) |
4288 ( ssl->in_msg[7] << 8 ) |
4289 ssl->in_msg[8] );
4290}
4291
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004292static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004293{
4294 uint32_t msg_len, frag_off, frag_len;
4295
4296 msg_len = ssl_get_hs_total_len( ssl );
4297 frag_off = ssl_get_hs_frag_off( ssl );
4298 frag_len = ssl_get_hs_frag_len( ssl );
4299
4300 if( frag_off > msg_len )
4301 return( -1 );
4302
4303 if( frag_len > msg_len - frag_off )
4304 return( -1 );
4305
4306 if( frag_len + 12 > ssl->in_msglen )
4307 return( -1 );
4308
4309 return( 0 );
4310}
4311
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004312/*
4313 * Mark bits in bitmask (used for DTLS HS reassembly)
4314 */
4315static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4316{
4317 unsigned int start_bits, end_bits;
4318
4319 start_bits = 8 - ( offset % 8 );
4320 if( start_bits != 8 )
4321 {
4322 size_t first_byte_idx = offset / 8;
4323
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004324 /* Special case */
4325 if( len <= start_bits )
4326 {
4327 for( ; len != 0; len-- )
4328 mask[first_byte_idx] |= 1 << ( start_bits - len );
4329
4330 /* Avoid potential issues with offset or len becoming invalid */
4331 return;
4332 }
4333
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004334 offset += start_bits; /* Now offset % 8 == 0 */
4335 len -= start_bits;
4336
4337 for( ; start_bits != 0; start_bits-- )
4338 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4339 }
4340
4341 end_bits = len % 8;
4342 if( end_bits != 0 )
4343 {
4344 size_t last_byte_idx = ( offset + len ) / 8;
4345
4346 len -= end_bits; /* Now len % 8 == 0 */
4347
4348 for( ; end_bits != 0; end_bits-- )
4349 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4350 }
4351
4352 memset( mask + offset / 8, 0xFF, len / 8 );
4353}
4354
4355/*
4356 * Check that bitmask is full
4357 */
4358static int ssl_bitmask_check( unsigned char *mask, size_t len )
4359{
4360 size_t i;
4361
4362 for( i = 0; i < len / 8; i++ )
4363 if( mask[i] != 0xFF )
4364 return( -1 );
4365
4366 for( i = 0; i < len % 8; i++ )
4367 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4368 return( -1 );
4369
4370 return( 0 );
4371}
4372
Hanno Becker56e205e2018-08-16 09:06:12 +01004373/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004374static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004375 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004376{
Hanno Becker56e205e2018-08-16 09:06:12 +01004377 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004378
Hanno Becker56e205e2018-08-16 09:06:12 +01004379 alloc_len = 12; /* Handshake header */
4380 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004381
Hanno Beckerd07df862018-08-16 09:14:58 +01004382 if( add_bitmap )
4383 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004384
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004385 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004386}
Hanno Becker56e205e2018-08-16 09:06:12 +01004387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004389
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004390static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004391{
4392 return( ( ssl->in_msg[1] << 16 ) |
4393 ( ssl->in_msg[2] << 8 ) |
4394 ssl->in_msg[3] );
4395}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004396
Simon Butcher99000142016-10-13 17:21:01 +01004397int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004402 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004404 }
4405
Hanno Becker12555c62018-08-16 12:47:53 +01004406 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004409 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004410 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004413 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004414 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004415 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004416 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004417
Hanno Becker44650b72018-08-16 12:51:11 +01004418 if( ssl_check_hs_header( ssl ) != 0 )
4419 {
4420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4421 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4422 }
4423
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004424 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004425 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4426 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4427 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4428 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004429 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004430 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4431 {
4432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4433 recv_msg_seq,
4434 ssl->handshake->in_msg_seq ) );
4435 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4436 }
4437
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004438 /* Retransmit only on last message from previous flight, to avoid
4439 * too many retransmissions.
4440 * Besides, No sane server ever retransmits HelloVerifyRequest */
4441 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004445 "message_seq = %d, start_of_flight = %d",
4446 recv_msg_seq,
4447 ssl->handshake->in_flight_start_seq ) );
4448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004452 return( ret );
4453 }
4454 }
4455 else
4456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004458 "message_seq = %d, expected = %d",
4459 recv_msg_seq,
4460 ssl->handshake->in_msg_seq ) );
4461 }
4462
Hanno Becker90333da2017-10-10 11:27:13 +01004463 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004464 }
4465 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004466
Hanno Becker6d97ef52018-08-16 13:09:04 +01004467 /* Message reassembly is handled alongside buffering of future
4468 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004469 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004470 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004471 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004474 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004475 }
4476 }
4477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004479 /* With TLS we don't handle fragmentation (for now) */
4480 if( ssl->in_msglen < ssl->in_hslen )
4481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4483 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004484 }
4485
Simon Butcher99000142016-10-13 17:21:01 +01004486 return( 0 );
4487}
4488
4489void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4490{
Hanno Becker0271f962018-08-16 13:23:47 +01004491 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004492
Hanno Becker0271f962018-08-16 13:23:47 +01004493 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004494 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004495 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004496 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004497
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004498 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004501 ssl->handshake != NULL )
4502 {
Hanno Becker0271f962018-08-16 13:23:47 +01004503 unsigned offset;
4504 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004505
Hanno Becker0271f962018-08-16 13:23:47 +01004506 /* Increment handshake sequence number */
4507 hs->in_msg_seq++;
4508
4509 /*
4510 * Clear up handshake buffering and reassembly structure.
4511 */
4512
4513 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004514 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004515
4516 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004517 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4518 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004519 offset++, hs_buf++ )
4520 {
4521 *hs_buf = *(hs_buf + 1);
4522 }
4523
4524 /* Create a fresh last entry */
4525 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004526 }
4527#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004528}
4529
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004530/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004531 * DTLS anti-replay: RFC 6347 4.1.2.6
4532 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004533 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4534 * Bit n is set iff record number in_window_top - n has been seen.
4535 *
4536 * Usually, in_window_top is the last record number seen and the lsb of
4537 * in_window is set. The only exception is the initial state (record number 0
4538 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4541static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004542{
4543 ssl->in_window_top = 0;
4544 ssl->in_window = 0;
4545}
4546
4547static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4548{
4549 return( ( (uint64_t) buf[0] << 40 ) |
4550 ( (uint64_t) buf[1] << 32 ) |
4551 ( (uint64_t) buf[2] << 24 ) |
4552 ( (uint64_t) buf[3] << 16 ) |
4553 ( (uint64_t) buf[4] << 8 ) |
4554 ( (uint64_t) buf[5] ) );
4555}
4556
4557/*
4558 * Return 0 if sequence number is acceptable, -1 otherwise
4559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004561{
4562 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4563 uint64_t bit;
4564
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004565 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004566 return( 0 );
4567
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004568 if( rec_seqnum > ssl->in_window_top )
4569 return( 0 );
4570
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004571 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004572
4573 if( bit >= 64 )
4574 return( -1 );
4575
4576 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4577 return( -1 );
4578
4579 return( 0 );
4580}
4581
4582/*
4583 * Update replay window on new validated record
4584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004586{
4587 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4588
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004589 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004590 return;
4591
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004592 if( rec_seqnum > ssl->in_window_top )
4593 {
4594 /* Update window_top and the contents of the window */
4595 uint64_t shift = rec_seqnum - ssl->in_window_top;
4596
4597 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004598 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004599 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004600 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004601 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004602 ssl->in_window |= 1;
4603 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004604
4605 ssl->in_window_top = rec_seqnum;
4606 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004607 else
4608 {
4609 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004610 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611
4612 if( bit < 64 ) /* Always true, but be extra sure */
4613 ssl->in_window |= (uint64_t) 1 << bit;
4614 }
4615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004617
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004618#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004619/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004620static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4621
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004622/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004623 * Without any SSL context, check if a datagram looks like a ClientHello with
4624 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004625 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004626 *
4627 * - if cookie is valid, return 0
4628 * - if ClientHello looks superficially valid but cookie is not,
4629 * fill obuf and set olen, then
4630 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4631 * - otherwise return a specific error code
4632 */
4633static int ssl_check_dtls_clihlo_cookie(
4634 mbedtls_ssl_cookie_write_t *f_cookie_write,
4635 mbedtls_ssl_cookie_check_t *f_cookie_check,
4636 void *p_cookie,
4637 const unsigned char *cli_id, size_t cli_id_len,
4638 const unsigned char *in, size_t in_len,
4639 unsigned char *obuf, size_t buf_len, size_t *olen )
4640{
4641 size_t sid_len, cookie_len;
4642 unsigned char *p;
4643
4644 if( f_cookie_write == NULL || f_cookie_check == NULL )
4645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4646
4647 /*
4648 * Structure of ClientHello with record and handshake headers,
4649 * and expected values. We don't need to check a lot, more checks will be
4650 * done when actually parsing the ClientHello - skipping those checks
4651 * avoids code duplication and does not make cookie forging any easier.
4652 *
4653 * 0-0 ContentType type; copied, must be handshake
4654 * 1-2 ProtocolVersion version; copied
4655 * 3-4 uint16 epoch; copied, must be 0
4656 * 5-10 uint48 sequence_number; copied
4657 * 11-12 uint16 length; (ignored)
4658 *
4659 * 13-13 HandshakeType msg_type; (ignored)
4660 * 14-16 uint24 length; (ignored)
4661 * 17-18 uint16 message_seq; copied
4662 * 19-21 uint24 fragment_offset; copied, must be 0
4663 * 22-24 uint24 fragment_length; (ignored)
4664 *
4665 * 25-26 ProtocolVersion client_version; (ignored)
4666 * 27-58 Random random; (ignored)
4667 * 59-xx SessionID session_id; 1 byte len + sid_len content
4668 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4669 * ...
4670 *
4671 * Minimum length is 61 bytes.
4672 */
4673 if( in_len < 61 ||
4674 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4675 in[3] != 0 || in[4] != 0 ||
4676 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4677 {
4678 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4679 }
4680
4681 sid_len = in[59];
4682 if( sid_len > in_len - 61 )
4683 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4684
4685 cookie_len = in[60 + sid_len];
4686 if( cookie_len > in_len - 60 )
4687 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4688
4689 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4690 cli_id, cli_id_len ) == 0 )
4691 {
4692 /* Valid cookie */
4693 return( 0 );
4694 }
4695
4696 /*
4697 * If we get here, we've got an invalid cookie, let's prepare HVR.
4698 *
4699 * 0-0 ContentType type; copied
4700 * 1-2 ProtocolVersion version; copied
4701 * 3-4 uint16 epoch; copied
4702 * 5-10 uint48 sequence_number; copied
4703 * 11-12 uint16 length; olen - 13
4704 *
4705 * 13-13 HandshakeType msg_type; hello_verify_request
4706 * 14-16 uint24 length; olen - 25
4707 * 17-18 uint16 message_seq; copied
4708 * 19-21 uint24 fragment_offset; copied
4709 * 22-24 uint24 fragment_length; olen - 25
4710 *
4711 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4712 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4713 *
4714 * Minimum length is 28.
4715 */
4716 if( buf_len < 28 )
4717 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4718
4719 /* Copy most fields and adapt others */
4720 memcpy( obuf, in, 25 );
4721 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4722 obuf[25] = 0xfe;
4723 obuf[26] = 0xff;
4724
4725 /* Generate and write actual cookie */
4726 p = obuf + 28;
4727 if( f_cookie_write( p_cookie,
4728 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4729 {
4730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4731 }
4732
4733 *olen = p - obuf;
4734
4735 /* Go back and fill length fields */
4736 obuf[27] = (unsigned char)( *olen - 28 );
4737
4738 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4739 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4740 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4741
4742 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4743 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4744
4745 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4746}
4747
4748/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004749 * Handle possible client reconnect with the same UDP quadruplet
4750 * (RFC 6347 Section 4.2.8).
4751 *
4752 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4753 * that looks like a ClientHello.
4754 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004755 * - if the input looks like a ClientHello without cookies,
4756 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004757 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004758 * - if the input looks like a ClientHello with a valid cookie,
4759 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004760 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004761 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004762 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004763 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004764 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4765 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766 */
4767static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4768{
4769 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004770 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004771
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004772 ret = ssl_check_dtls_clihlo_cookie(
4773 ssl->conf->f_cookie_write,
4774 ssl->conf->f_cookie_check,
4775 ssl->conf->p_cookie,
4776 ssl->cli_id, ssl->cli_id_len,
4777 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004778 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004779
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4781
4782 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004783 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004784 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004785 * If the error is permanent we'll catch it later,
4786 * if it's not, then hopefully it'll work next time. */
4787 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4788
4789 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790 }
4791
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004792 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004793 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004794 /* Got a valid cookie, partially reset context */
4795 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4796 {
4797 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4798 return( ret );
4799 }
4800
4801 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004802 }
4803
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004804 return( ret );
4805}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004806#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004807
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004808static int ssl_check_record_type( uint8_t record_type )
4809{
4810 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4811 record_type != MBEDTLS_SSL_MSG_ALERT &&
4812 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4813 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4814 {
4815 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4816 }
4817
4818 return( 0 );
4819}
4820
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004821/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004822 * ContentType type;
4823 * ProtocolVersion version;
4824 * uint16 epoch; // DTLS only
4825 * uint48 sequence_number; // DTLS only
4826 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004827 *
4828 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004829 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004830 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4831 *
4832 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004833 * 1. proceed with the record if this function returns 0
4834 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4835 * 3. return CLIENT_RECONNECT if this function return that value
4836 * 4. drop the whole datagram if this function returns anything else.
4837 * Point 2 is needed when the peer is resending, and we have already received
4838 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004841{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004842 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004843 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004844
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004845 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004846
Paul Bakker5121ce52009-01-03 21:22:43 +00004847 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004848 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004849
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004850 /* Check record type */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004851#if defined(MBEDTLS_SSL_CID)
4852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4853 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4854 ssl->conf->cid_len != 0 )
4855 {
4856 /* Shift pointers to account for record header including CID
4857 * struct {
4858 * ContentType special_type = tls12_cid;
4859 * ProtocolVersion version;
4860 * uint16 epoch;
4861 * uint48 sequence_number;
4862 * opaque cid[cid_length]; // New field
4863 * uint16 length;
4864 * opaque enc_content[DTLSCiphertext.length];
4865 * } DTLSCiphertext;
4866 */
4867
4868 /* So far, we only support static CID lengths
4869 * fixed in the configuration. */
4870 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4871 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4872 }
4873 else
4874#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004875 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004878
4879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004880 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4881 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004882 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4883#endif /* MBEDTLS_SSL_PROTO_DTLS */
4884 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4885 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004888 }
4889
4890 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004891 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4894 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004895 }
4896
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004897 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4900 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 }
4902
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004903 /* Now that the total length of the record header is known, ensure
4904 * that the current datagram is large enough to hold it.
4905 * This would fail, for example, if we received a datagram of
4906 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4907 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4908 if( ret != 0 )
4909 {
4910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4911 return( ret );
4912 }
4913 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4914
4915 /* Parse and validate record length
4916 * This must happen after the CID parsing because
4917 * its position in the record header depends on
4918 * the presence of a CID. */
4919
4920 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004921 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004922 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4925 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004926 }
4927
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4929 "version = [%d:%d], msglen = %d",
4930 ssl->in_msgtype,
4931 major_ver, minor_ver, ssl->in_msglen ) );
4932
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004933 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004934 * DTLS-related tests.
4935 * Check epoch before checking length constraint because
4936 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4937 * message gets duplicated before the corresponding Finished message,
4938 * the second ChangeCipherSpec should be discarded because it belongs
4939 * to an old epoch, but not because its length is shorter than
4940 * the minimum record length for packets using the new record transform.
4941 * Note that these two kinds of failures are handled differently,
4942 * as an unexpected record is silently skipped but an invalid
4943 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004944 */
4945#if defined(MBEDTLS_SSL_PROTO_DTLS)
4946 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4947 {
4948 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4949
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004950 /* Check epoch (and sequence number) with DTLS */
4951 if( rec_epoch != ssl->in_epoch )
4952 {
4953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4954 "expected %d, received %d",
4955 ssl->in_epoch, rec_epoch ) );
4956
4957#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4958 /*
4959 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4960 * access the first byte of record content (handshake type), as we
4961 * have an active transform (possibly iv_len != 0), so use the
4962 * fact that the record header len is 13 instead.
4963 */
4964 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4965 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4966 rec_epoch == 0 &&
4967 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4968 ssl->in_left > 13 &&
4969 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4970 {
4971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4972 "from the same port" ) );
4973 return( ssl_handle_possible_reconnect( ssl ) );
4974 }
4975 else
4976#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004977 {
4978 /* Consider buffering the record. */
4979 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4980 {
4981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4982 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4983 }
4984
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004985 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004986 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004987 }
4988
4989#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4990 /* Replay detection only works for the current epoch */
4991 if( rec_epoch == ssl->in_epoch &&
4992 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4993 {
4994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4995 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4996 }
4997#endif
4998 }
4999#endif /* MBEDTLS_SSL_PROTO_DTLS */
5000
Hanno Becker52c6dc62017-05-26 16:07:36 +01005001
5002 /* Check length against bounds of the current transform and version */
5003 if( ssl->transform_in == NULL )
5004 {
5005 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005006 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005007 {
5008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5009 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5010 }
5011 }
5012 else
5013 {
5014 if( ssl->in_msglen < ssl->transform_in->minlen )
5015 {
5016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5017 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5018 }
5019
5020#if defined(MBEDTLS_SSL_PROTO_SSL3)
5021 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005022 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005023 {
5024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5025 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5026 }
5027#endif
5028#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5029 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5030 /*
5031 * TLS encrypted messages can have up to 256 bytes of padding
5032 */
5033 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5034 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005035 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005036 {
5037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5038 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5039 }
5040#endif
5041 }
5042
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005043 return( 0 );
5044}
Paul Bakker5121ce52009-01-03 21:22:43 +00005045
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005046/*
5047 * If applicable, decrypt (and decompress) record content
5048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005049static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005050{
5051 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005054 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5057 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061 ret = mbedtls_ssl_hw_record_read( ssl );
5062 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5065 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005066 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005067
5068 if( ret == 0 )
5069 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005072 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005073 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005074 mbedtls_record rec;
5075
5076 rec.buf = ssl->in_iv;
5077 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5078 - ( ssl->in_iv - ssl->in_buf );
5079 rec.data_len = ssl->in_msglen;
5080 rec.data_offset = 0;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005081#if defined(MBEDTLS_SSL_CID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005082 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005083 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
5084#endif /* MBEDTLS_SSL_CID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005085
5086 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5087 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5088 ssl->conf->transport, rec.ver );
5089 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005090 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5091 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005094
5095#if defined(MBEDTLS_SSL_CID)
5096 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5097 ssl->conf->ignore_unexpected_cid
5098 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5099 {
Hanno Becker16ded982019-05-08 13:02:55 +01005100 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005101 }
5102#endif /* MBEDTLS_SSL_CID */
Hanno Becker16ded982019-05-08 13:02:55 +01005103
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 return( ret );
5105 }
5106
Hanno Becker6430faf2019-05-08 11:57:13 +01005107 if( ssl->in_msgtype != rec.type )
5108 {
5109 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5110 ssl->in_msgtype, rec.type ) );
5111 }
5112
5113 /* The record content type may change during decryption,
5114 * so re-read it. */
5115 ssl->in_msgtype = rec.type;
5116 /* Also update the input buffer, because unfortunately
5117 * the server-side ssl_parse_client_hello() reparses the
5118 * record header when receiving a ClientHello initiating
5119 * a renegotiation. */
5120 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005121 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005122 ssl->in_msglen = rec.data_len;
5123 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5124 ssl->in_len[1] = (unsigned char)( rec.data_len );
5125
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005126 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5127 ssl->in_msg, ssl->in_msglen );
5128
Hanno Becker6430faf2019-05-08 11:57:13 +01005129#if defined(MBEDTLS_SSL_CID)
5130 /* We have already checked the record content type
5131 * in ssl_parse_record_header(), failing or silently
5132 * dropping the record in the case of an unknown type.
5133 *
5134 * Since with the use of CIDs, the record content type
5135 * might change during decryption, re-check the record
5136 * content type, but treat a failure as fatal this time. */
5137 if( ssl_check_record_type( ssl->in_msgtype ) )
5138 {
5139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5140 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5141 }
5142#endif /* MBEDTLS_SSL_CID */
5143
Angus Grattond8213d02016-05-25 20:56:48 +10005144 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5147 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005148 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005149 else if( ssl->in_msglen == 0 )
5150 {
5151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5152 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5153 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5154 {
5155 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5157 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5158 }
5159#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5160
5161 ssl->nb_zero++;
5162
5163 /*
5164 * Three or more empty messages may be a DoS attack
5165 * (excessive CPU consumption).
5166 */
5167 if( ssl->nb_zero > 3 )
5168 {
5169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005170 "messages, possible DoS attack" ) );
5171 /* Treat the records as if they were not properly authenticated,
5172 * thereby failing the connection if we see more than allowed
5173 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005174 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5175 }
5176 }
5177 else
5178 ssl->nb_zero = 0;
5179
5180#if defined(MBEDTLS_SSL_PROTO_DTLS)
5181 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5182 {
5183 ; /* in_ctr read from peer, not maintained internally */
5184 }
5185 else
5186#endif
5187 {
5188 unsigned i;
5189 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5190 if( ++ssl->in_ctr[i - 1] != 0 )
5191 break;
5192
5193 /* The loop goes to its end iff the counter is wrapping */
5194 if( i == ssl_ep_len( ssl ) )
5195 {
5196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5197 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5198 }
5199 }
5200
Paul Bakker5121ce52009-01-03 21:22:43 +00005201 }
5202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005204 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005206 {
5207 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005210 return( ret );
5211 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005212 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005219 }
5220#endif
5221
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005222 return( 0 );
5223}
5224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005226
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005227/*
5228 * Read a record.
5229 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005230 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5231 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5232 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005233 */
Hanno Becker1097b342018-08-15 14:09:41 +01005234
5235/* Helper functions for mbedtls_ssl_read_record(). */
5236static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005237static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5238static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005239
Hanno Becker327c93b2018-08-15 13:56:18 +01005240int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005241 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005242{
5243 int ret;
5244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005246
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005247 if( ssl->keep_current_message == 0 )
5248 {
5249 do {
Simon Butcher99000142016-10-13 17:21:01 +01005250
Hanno Becker26994592018-08-15 14:14:59 +01005251 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005252 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005253 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005254
Hanno Beckere74d5562018-08-15 14:26:08 +01005255 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005256 {
Hanno Becker40f50842018-08-15 14:48:01 +01005257#if defined(MBEDTLS_SSL_PROTO_DTLS)
5258 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005259
Hanno Becker40f50842018-08-15 14:48:01 +01005260 /* We only check for buffered messages if the
5261 * current datagram is fully consumed. */
5262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005263 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005264 {
Hanno Becker40f50842018-08-15 14:48:01 +01005265 if( ssl_load_buffered_message( ssl ) == 0 )
5266 have_buffered = 1;
5267 }
5268
5269 if( have_buffered == 0 )
5270#endif /* MBEDTLS_SSL_PROTO_DTLS */
5271 {
5272 ret = ssl_get_next_record( ssl );
5273 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5274 continue;
5275
5276 if( ret != 0 )
5277 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005278 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005279 return( ret );
5280 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005281 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005282 }
5283
5284 ret = mbedtls_ssl_handle_message_type( ssl );
5285
Hanno Becker40f50842018-08-15 14:48:01 +01005286#if defined(MBEDTLS_SSL_PROTO_DTLS)
5287 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5288 {
5289 /* Buffer future message */
5290 ret = ssl_buffer_message( ssl );
5291 if( ret != 0 )
5292 return( ret );
5293
5294 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5295 }
5296#endif /* MBEDTLS_SSL_PROTO_DTLS */
5297
Hanno Becker90333da2017-10-10 11:27:13 +01005298 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5299 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005300
5301 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005302 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005303 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005304 return( ret );
5305 }
5306
Hanno Becker327c93b2018-08-15 13:56:18 +01005307 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005308 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005309 {
5310 mbedtls_ssl_update_handshake_status( ssl );
5311 }
Simon Butcher99000142016-10-13 17:21:01 +01005312 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005313 else
Simon Butcher99000142016-10-13 17:21:01 +01005314 {
Hanno Becker02f59072018-08-15 14:00:24 +01005315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005316 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005317 }
5318
5319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5320
5321 return( 0 );
5322}
5323
Hanno Becker40f50842018-08-15 14:48:01 +01005324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005325static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005326{
Hanno Becker40f50842018-08-15 14:48:01 +01005327 if( ssl->in_left > ssl->next_record_offset )
5328 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005329
Hanno Becker40f50842018-08-15 14:48:01 +01005330 return( 0 );
5331}
5332
5333static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5334{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005335 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005336 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005337 int ret = 0;
5338
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005339 if( hs == NULL )
5340 return( -1 );
5341
Hanno Beckere00ae372018-08-20 09:39:42 +01005342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5343
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005344 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5345 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5346 {
5347 /* Check if we have seen a ChangeCipherSpec before.
5348 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005349 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005350 {
5351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5352 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005353 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005354 }
5355
Hanno Becker39b8bc92018-08-28 17:17:13 +01005356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005357 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5358 ssl->in_msglen = 1;
5359 ssl->in_msg[0] = 1;
5360
5361 /* As long as they are equal, the exact value doesn't matter. */
5362 ssl->in_left = 0;
5363 ssl->next_record_offset = 0;
5364
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005365 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005366 goto exit;
5367 }
Hanno Becker37f95322018-08-16 13:55:32 +01005368
Hanno Beckerb8f50142018-08-28 10:01:34 +01005369#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005370 /* Debug only */
5371 {
5372 unsigned offset;
5373 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5374 {
5375 hs_buf = &hs->buffering.hs[offset];
5376 if( hs_buf->is_valid == 1 )
5377 {
5378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5379 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005380 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005381 }
5382 }
5383 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005384#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005385
5386 /* Check if we have buffered and/or fully reassembled the
5387 * next handshake message. */
5388 hs_buf = &hs->buffering.hs[0];
5389 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5390 {
5391 /* Synthesize a record containing the buffered HS message. */
5392 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5393 ( hs_buf->data[2] << 8 ) |
5394 hs_buf->data[3];
5395
5396 /* Double-check that we haven't accidentally buffered
5397 * a message that doesn't fit into the input buffer. */
5398 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5399 {
5400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5402 }
5403
5404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5405 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5406 hs_buf->data, msg_len + 12 );
5407
5408 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5409 ssl->in_hslen = msg_len + 12;
5410 ssl->in_msglen = msg_len + 12;
5411 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5412
5413 ret = 0;
5414 goto exit;
5415 }
5416 else
5417 {
5418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5419 hs->in_msg_seq ) );
5420 }
5421
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005422 ret = -1;
5423
5424exit:
5425
5426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5427 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005428}
5429
Hanno Beckera02b0b42018-08-21 17:20:27 +01005430static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5431 size_t desired )
5432{
5433 int offset;
5434 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5436 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005437
Hanno Becker01315ea2018-08-21 17:22:17 +01005438 /* Get rid of future records epoch first, if such exist. */
5439 ssl_free_buffered_record( ssl );
5440
5441 /* Check if we have enough space available now. */
5442 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5443 hs->buffering.total_bytes_buffered ) )
5444 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005446 return( 0 );
5447 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005448
Hanno Becker4f432ad2018-08-28 10:02:32 +01005449 /* We don't have enough space to buffer the next expected handshake
5450 * message. Remove buffers used for future messages to gain space,
5451 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005452 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5453 offset >= 0; offset-- )
5454 {
5455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5456 offset ) );
5457
Hanno Beckerb309b922018-08-23 13:18:05 +01005458 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005459
5460 /* Check if we have enough space available now. */
5461 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5462 hs->buffering.total_bytes_buffered ) )
5463 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005465 return( 0 );
5466 }
5467 }
5468
5469 return( -1 );
5470}
5471
Hanno Becker40f50842018-08-15 14:48:01 +01005472static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5473{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005474 int ret = 0;
5475 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5476
5477 if( hs == NULL )
5478 return( 0 );
5479
5480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5481
5482 switch( ssl->in_msgtype )
5483 {
5484 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005486
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005487 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005488 break;
5489
5490 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005491 {
5492 unsigned recv_msg_seq_offset;
5493 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5494 mbedtls_ssl_hs_buffer *hs_buf;
5495 size_t msg_len = ssl->in_hslen - 12;
5496
5497 /* We should never receive an old handshake
5498 * message - double-check nonetheless. */
5499 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5500 {
5501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5502 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5503 }
5504
5505 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5506 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5507 {
5508 /* Silently ignore -- message too far in the future */
5509 MBEDTLS_SSL_DEBUG_MSG( 2,
5510 ( "Ignore future HS message with sequence number %u, "
5511 "buffering window %u - %u",
5512 recv_msg_seq, ssl->handshake->in_msg_seq,
5513 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5514
5515 goto exit;
5516 }
5517
5518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5519 recv_msg_seq, recv_msg_seq_offset ) );
5520
5521 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5522
5523 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005524 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005525 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005526 size_t reassembly_buf_sz;
5527
Hanno Becker37f95322018-08-16 13:55:32 +01005528 hs_buf->is_fragmented =
5529 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5530
5531 /* We copy the message back into the input buffer
5532 * after reassembly, so check that it's not too large.
5533 * This is an implementation-specific limitation
5534 * and not one from the standard, hence it is not
5535 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005536 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005537 {
5538 /* Ignore message */
5539 goto exit;
5540 }
5541
Hanno Beckere0b150f2018-08-21 15:51:03 +01005542 /* Check if we have enough space to buffer the message. */
5543 if( hs->buffering.total_bytes_buffered >
5544 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5548 }
5549
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005550 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5551 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005552
5553 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5554 hs->buffering.total_bytes_buffered ) )
5555 {
5556 if( recv_msg_seq_offset > 0 )
5557 {
5558 /* If we can't buffer a future message because
5559 * of space limitations -- ignore. */
5560 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",
5561 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5562 (unsigned) hs->buffering.total_bytes_buffered ) );
5563 goto exit;
5564 }
Hanno Beckere1801392018-08-21 16:51:05 +01005565 else
5566 {
5567 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",
5568 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5569 (unsigned) hs->buffering.total_bytes_buffered ) );
5570 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005571
Hanno Beckera02b0b42018-08-21 17:20:27 +01005572 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005573 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005574 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",
5575 (unsigned) msg_len,
5576 (unsigned) reassembly_buf_sz,
5577 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005578 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005579 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5580 goto exit;
5581 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005582 }
5583
5584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5585 msg_len ) );
5586
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005587 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5588 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005589 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005590 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005591 goto exit;
5592 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005593 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005594
5595 /* Prepare final header: copy msg_type, length and message_seq,
5596 * then add standardised fragment_offset and fragment_length */
5597 memcpy( hs_buf->data, ssl->in_msg, 6 );
5598 memset( hs_buf->data + 6, 0, 3 );
5599 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5600
5601 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005602
5603 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005604 }
5605 else
5606 {
5607 /* Make sure msg_type and length are consistent */
5608 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5609 {
5610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5611 /* Ignore */
5612 goto exit;
5613 }
5614 }
5615
Hanno Becker4422bbb2018-08-20 09:40:19 +01005616 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005617 {
5618 size_t frag_len, frag_off;
5619 unsigned char * const msg = hs_buf->data + 12;
5620
5621 /*
5622 * Check and copy current fragment
5623 */
5624
5625 /* Validation of header fields already done in
5626 * mbedtls_ssl_prepare_handshake_record(). */
5627 frag_off = ssl_get_hs_frag_off( ssl );
5628 frag_len = ssl_get_hs_frag_len( ssl );
5629
5630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5631 frag_off, frag_len ) );
5632 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5633
5634 if( hs_buf->is_fragmented )
5635 {
5636 unsigned char * const bitmask = msg + msg_len;
5637 ssl_bitmask_set( bitmask, frag_off, frag_len );
5638 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5639 msg_len ) == 0 );
5640 }
5641 else
5642 {
5643 hs_buf->is_complete = 1;
5644 }
5645
5646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5647 hs_buf->is_complete ? "" : "not yet " ) );
5648 }
5649
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005650 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005651 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005652
5653 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005654 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005655 break;
5656 }
5657
5658exit:
5659
5660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5661 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005662}
5663#endif /* MBEDTLS_SSL_PROTO_DTLS */
5664
Hanno Becker1097b342018-08-15 14:09:41 +01005665static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005666{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005667 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005668 * Consume last content-layer message and potentially
5669 * update in_msglen which keeps track of the contents'
5670 * consumption state.
5671 *
5672 * (1) Handshake messages:
5673 * Remove last handshake message, move content
5674 * and adapt in_msglen.
5675 *
5676 * (2) Alert messages:
5677 * Consume whole record content, in_msglen = 0.
5678 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005679 * (3) Change cipher spec:
5680 * Consume whole record content, in_msglen = 0.
5681 *
5682 * (4) Application data:
5683 * Don't do anything - the record layer provides
5684 * the application data as a stream transport
5685 * and consumes through mbedtls_ssl_read only.
5686 *
5687 */
5688
5689 /* Case (1): Handshake messages */
5690 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005691 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005692 /* Hard assertion to be sure that no application data
5693 * is in flight, as corrupting ssl->in_msglen during
5694 * ssl->in_offt != NULL is fatal. */
5695 if( ssl->in_offt != NULL )
5696 {
5697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5699 }
5700
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005701 /*
5702 * Get next Handshake message in the current record
5703 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005704
Hanno Becker4a810fb2017-05-24 16:27:30 +01005705 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005706 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005707 * current handshake content: If DTLS handshake
5708 * fragmentation is used, that's the fragment
5709 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005710 * size here is faulty and should be changed at
5711 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005712 * (2) While it doesn't seem to cause problems, one
5713 * has to be very careful not to assume that in_hslen
5714 * is always <= in_msglen in a sensible communication.
5715 * Again, it's wrong for DTLS handshake fragmentation.
5716 * The following check is therefore mandatory, and
5717 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005718 * Additionally, ssl->in_hslen might be arbitrarily out of
5719 * bounds after handling a DTLS message with an unexpected
5720 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005721 */
5722 if( ssl->in_hslen < ssl->in_msglen )
5723 {
5724 ssl->in_msglen -= ssl->in_hslen;
5725 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5726 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005727
Hanno Becker4a810fb2017-05-24 16:27:30 +01005728 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5729 ssl->in_msg, ssl->in_msglen );
5730 }
5731 else
5732 {
5733 ssl->in_msglen = 0;
5734 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005735
Hanno Becker4a810fb2017-05-24 16:27:30 +01005736 ssl->in_hslen = 0;
5737 }
5738 /* Case (4): Application data */
5739 else if( ssl->in_offt != NULL )
5740 {
5741 return( 0 );
5742 }
5743 /* Everything else (CCS & Alerts) */
5744 else
5745 {
5746 ssl->in_msglen = 0;
5747 }
5748
Hanno Becker1097b342018-08-15 14:09:41 +01005749 return( 0 );
5750}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005751
Hanno Beckere74d5562018-08-15 14:26:08 +01005752static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5753{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005754 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005755 return( 1 );
5756
5757 return( 0 );
5758}
5759
Hanno Becker5f066e72018-08-16 14:56:31 +01005760#if defined(MBEDTLS_SSL_PROTO_DTLS)
5761
5762static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5763{
5764 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5765 if( hs == NULL )
5766 return;
5767
Hanno Becker01315ea2018-08-21 17:22:17 +01005768 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005769 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005770 hs->buffering.total_bytes_buffered -=
5771 hs->buffering.future_record.len;
5772
5773 mbedtls_free( hs->buffering.future_record.data );
5774 hs->buffering.future_record.data = NULL;
5775 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005776}
5777
5778static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5779{
5780 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5781 unsigned char * rec;
5782 size_t rec_len;
5783 unsigned rec_epoch;
5784
5785 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5786 return( 0 );
5787
5788 if( hs == NULL )
5789 return( 0 );
5790
Hanno Becker5f066e72018-08-16 14:56:31 +01005791 rec = hs->buffering.future_record.data;
5792 rec_len = hs->buffering.future_record.len;
5793 rec_epoch = hs->buffering.future_record.epoch;
5794
5795 if( rec == NULL )
5796 return( 0 );
5797
Hanno Becker4cb782d2018-08-20 11:19:05 +01005798 /* Only consider loading future records if the
5799 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005800 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005801 return( 0 );
5802
Hanno Becker5f066e72018-08-16 14:56:31 +01005803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5804
5805 if( rec_epoch != ssl->in_epoch )
5806 {
5807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5808 goto exit;
5809 }
5810
5811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5812
5813 /* Double-check that the record is not too large */
5814 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5815 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5816 {
5817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5819 }
5820
5821 memcpy( ssl->in_hdr, rec, rec_len );
5822 ssl->in_left = rec_len;
5823 ssl->next_record_offset = 0;
5824
5825 ssl_free_buffered_record( ssl );
5826
5827exit:
5828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5829 return( 0 );
5830}
5831
5832static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5833{
5834 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5835 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005836 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005837
5838 /* Don't buffer future records outside handshakes. */
5839 if( hs == NULL )
5840 return( 0 );
5841
5842 /* Only buffer handshake records (we are only interested
5843 * in Finished messages). */
5844 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5845 return( 0 );
5846
5847 /* Don't buffer more than one future epoch record. */
5848 if( hs->buffering.future_record.data != NULL )
5849 return( 0 );
5850
Hanno Becker01315ea2018-08-21 17:22:17 +01005851 /* Don't buffer record if there's not enough buffering space remaining. */
5852 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5853 hs->buffering.total_bytes_buffered ) )
5854 {
5855 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",
5856 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5857 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005858 return( 0 );
5859 }
5860
Hanno Becker5f066e72018-08-16 14:56:31 +01005861 /* Buffer record */
5862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5863 ssl->in_epoch + 1 ) );
5864 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5865 rec_hdr_len + ssl->in_msglen );
5866
5867 /* ssl_parse_record_header() only considers records
5868 * of the next epoch as candidates for buffering. */
5869 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005870 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005871
5872 hs->buffering.future_record.data =
5873 mbedtls_calloc( 1, hs->buffering.future_record.len );
5874 if( hs->buffering.future_record.data == NULL )
5875 {
5876 /* If we run out of RAM trying to buffer a
5877 * record from the next epoch, just ignore. */
5878 return( 0 );
5879 }
5880
Hanno Becker01315ea2018-08-21 17:22:17 +01005881 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005882
Hanno Becker01315ea2018-08-21 17:22:17 +01005883 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005884 return( 0 );
5885}
5886
5887#endif /* MBEDTLS_SSL_PROTO_DTLS */
5888
Hanno Beckere74d5562018-08-15 14:26:08 +01005889static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005890{
5891 int ret;
5892
Hanno Becker5f066e72018-08-16 14:56:31 +01005893#if defined(MBEDTLS_SSL_PROTO_DTLS)
5894 /* We might have buffered a future record; if so,
5895 * and if the epoch matches now, load it.
5896 * On success, this call will set ssl->in_left to
5897 * the length of the buffered record, so that
5898 * the calls to ssl_fetch_input() below will
5899 * essentially be no-ops. */
5900 ret = ssl_load_buffered_record( ssl );
5901 if( ret != 0 )
5902 return( ret );
5903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005904
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005905 /* Reset in pointers to default state for TLS/DTLS records,
5906 * assuming no CID and no offset between record content and
5907 * record plaintext. */
5908 ssl_update_in_pointers( ssl );
5909
5910 /* Ensure that we have enough space available for the default form
5911 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5912 * with no space for CIDs counted in). */
5913 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5914 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005917 return( ret );
5918 }
5919
5920 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005923 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5924 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005925 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005926 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5927 {
5928 ret = ssl_buffer_future_record( ssl );
5929 if( ret != 0 )
5930 return( ret );
5931
5932 /* Fall through to handling of unexpected records */
5933 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5934 }
5935
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005936 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5937 {
5938 /* Skip unexpected record (but not whole datagram) */
5939 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005940 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005941
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5943 "(header)" ) );
5944 }
5945 else
5946 {
5947 /* Skip invalid record and the rest of the datagram */
5948 ssl->next_record_offset = 0;
5949 ssl->in_left = 0;
5950
5951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5952 "(header)" ) );
5953 }
5954
5955 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005956 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005957 }
5958#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005959 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005960 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005961
5962 /*
5963 * Read and optionally decrypt the message contents
5964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005966 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005969 return( ret );
5970 }
5971
5972 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005974 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005975 {
Hanno Becker5903de42019-05-03 14:46:38 +01005976 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005977 if( ssl->next_record_offset < ssl->in_left )
5978 {
5979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5980 }
5981 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005982 else
5983#endif
5984 ssl->in_left = 0;
5985
5986 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005989 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005990 {
5991 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005992 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005993 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005994 /* Except when waiting for Finished as a bad mac here
5995 * probably means something went wrong in the handshake
5996 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5997 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5998 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5999 {
6000#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6001 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6002 {
6003 mbedtls_ssl_send_alert_message( ssl,
6004 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6005 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6006 }
6007#endif
6008 return( ret );
6009 }
6010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006012 if( ssl->conf->badmac_limit != 0 &&
6013 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6016 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006017 }
6018#endif
6019
Hanno Becker4a810fb2017-05-24 16:27:30 +01006020 /* As above, invalid records cause
6021 * dismissal of the whole datagram. */
6022
6023 ssl->next_record_offset = 0;
6024 ssl->in_left = 0;
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006027 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006028 }
6029
6030 return( ret );
6031 }
6032 else
6033#endif
6034 {
6035 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6037 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 mbedtls_ssl_send_alert_message( ssl,
6040 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6041 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006042 }
6043#endif
6044 return( ret );
6045 }
6046 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006047
Simon Butcher99000142016-10-13 17:21:01 +01006048 return( 0 );
6049}
6050
6051int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6052{
6053 int ret;
6054
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006055 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006056 * Handle particular types of records
6057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 {
Simon Butcher99000142016-10-13 17:21:01 +01006060 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6061 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006062 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 }
6065
Hanno Beckere678eaa2018-08-21 14:57:46 +01006066 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006067 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006068 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006069 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6071 ssl->in_msglen ) );
6072 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006073 }
6074
Hanno Beckere678eaa2018-08-21 14:57:46 +01006075 if( ssl->in_msg[0] != 1 )
6076 {
6077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6078 ssl->in_msg[0] ) );
6079 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6080 }
6081
6082#if defined(MBEDTLS_SSL_PROTO_DTLS)
6083 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6084 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6085 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6086 {
6087 if( ssl->handshake == NULL )
6088 {
6089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6090 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6091 }
6092
6093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6094 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6095 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006096#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006097 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006100 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006101 if( ssl->in_msglen != 2 )
6102 {
6103 /* Note: Standard allows for more than one 2 byte alert
6104 to be packed in a single message, but Mbed TLS doesn't
6105 currently support this. */
6106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6107 ssl->in_msglen ) );
6108 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6109 }
6110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 ssl->in_msg[0], ssl->in_msg[1] ) );
6113
6114 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006115 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006120 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 }
6123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6125 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6128 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006130
6131#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6132 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6133 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6134 {
Hanno Becker90333da2017-10-10 11:27:13 +01006135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006136 /* Will be handled when trying to parse ServerHello */
6137 return( 0 );
6138 }
6139#endif
6140
6141#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6142 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6143 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6144 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6145 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6146 {
6147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6148 /* Will be handled in mbedtls_ssl_parse_certificate() */
6149 return( 0 );
6150 }
6151#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6152
6153 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006154 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 }
6156
Hanno Beckerc76c6192017-06-06 10:03:17 +01006157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006159 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006160 /* Drop unexpected ApplicationData records,
6161 * except at the beginning of renegotiations */
6162 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6163 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6164#if defined(MBEDTLS_SSL_RENEGOTIATION)
6165 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6166 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006167#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006168 )
6169 {
6170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6171 return( MBEDTLS_ERR_SSL_NON_FATAL );
6172 }
6173
6174 if( ssl->handshake != NULL &&
6175 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6176 {
6177 ssl_handshake_wrapup_free_hs_transform( ssl );
6178 }
6179 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006180#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006181
Paul Bakker5121ce52009-01-03 21:22:43 +00006182 return( 0 );
6183}
6184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006186{
6187 int ret;
6188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6190 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6191 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006192 {
6193 return( ret );
6194 }
6195
6196 return( 0 );
6197}
6198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006200 unsigned char level,
6201 unsigned char message )
6202{
6203 int ret;
6204
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006205 if( ssl == NULL || ssl->conf == NULL )
6206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006212 ssl->out_msglen = 2;
6213 ssl->out_msg[0] = level;
6214 ssl->out_msg[1] = message;
6215
Hanno Becker67bc7c32018-08-06 11:33:50 +01006216 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006219 return( ret );
6220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006222
6223 return( 0 );
6224}
6225
Hanno Beckerb9d44792019-02-08 07:19:04 +00006226#if defined(MBEDTLS_X509_CRT_PARSE_C)
6227static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6228{
6229#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6230 if( session->peer_cert != NULL )
6231 {
6232 mbedtls_x509_crt_free( session->peer_cert );
6233 mbedtls_free( session->peer_cert );
6234 session->peer_cert = NULL;
6235 }
6236#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6237 if( session->peer_cert_digest != NULL )
6238 {
6239 /* Zeroization is not necessary. */
6240 mbedtls_free( session->peer_cert_digest );
6241 session->peer_cert_digest = NULL;
6242 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6243 session->peer_cert_digest_len = 0;
6244 }
6245#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6246}
6247#endif /* MBEDTLS_X509_CRT_PARSE_C */
6248
Paul Bakker5121ce52009-01-03 21:22:43 +00006249/*
6250 * Handshake functions
6251 */
Hanno Becker21489932019-02-05 13:20:55 +00006252#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006253/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006255{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006256 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6257 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006260
Hanno Becker7177a882019-02-05 13:36:46 +00006261 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006264 ssl->state++;
6265 return( 0 );
6266 }
6267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006270}
6271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006273{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006274 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6275 ssl->handshake->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, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006278
Hanno Becker7177a882019-02-05 13:36:46 +00006279 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006282 ssl->state++;
6283 return( 0 );
6284 }
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006288}
Gilles Peskinef9828522017-05-03 12:28:43 +02006289
Hanno Becker21489932019-02-05 13:20:55 +00006290#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006291/* Some certificate support -> implement write and parse */
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006296 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006298 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6299 ssl->handshake->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, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006302
Hanno Becker7177a882019-02-05 13:36:46 +00006303 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006306 ssl->state++;
6307 return( 0 );
6308 }
6309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006311 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006312 {
6313 if( ssl->client_auth == 0 )
6314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006316 ssl->state++;
6317 return( 0 );
6318 }
6319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006321 /*
6322 * If using SSLv3 and got no cert, send an Alert message
6323 * (otherwise an empty Certificate message will be sent).
6324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6326 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006327 {
6328 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6330 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6331 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006334 goto write_msg;
6335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338#endif /* MBEDTLS_SSL_CLI_C */
6339#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6345 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006346 }
6347 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006348#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006351
6352 /*
6353 * 0 . 0 handshake type
6354 * 1 . 3 handshake length
6355 * 4 . 6 length of all certs
6356 * 7 . 9 length of cert. 1
6357 * 10 . n-1 peer certificate
6358 * n . n+2 length of cert. 2
6359 * n+3 . ... upper level cert, etc.
6360 */
6361 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006363
Paul Bakker29087132010-03-21 21:03:34 +00006364 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006365 {
6366 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006367 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006370 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 }
6373
6374 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6375 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6376 ssl->out_msg[i + 2] = (unsigned char)( n );
6377
6378 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6379 i += n; crt = crt->next;
6380 }
6381
6382 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6383 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6384 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6385
6386 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6388 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006389
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006390#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006391write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006392#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006393
6394 ssl->state++;
6395
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006396 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006397 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006399 return( ret );
6400 }
6401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006403
Paul Bakkered27a042013-04-18 22:46:23 +02006404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006405}
6406
Hanno Becker84879e32019-01-31 07:44:03 +00006407#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006408
6409#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006410static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6411 unsigned char *crt_buf,
6412 size_t crt_buf_len )
6413{
6414 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6415
6416 if( peer_crt == NULL )
6417 return( -1 );
6418
6419 if( peer_crt->raw.len != crt_buf_len )
6420 return( -1 );
6421
Hanno Becker46f34d02019-02-08 14:00:04 +00006422 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006423}
Hanno Becker177475a2019-02-05 17:02:46 +00006424#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6425static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6426 unsigned char *crt_buf,
6427 size_t crt_buf_len )
6428{
6429 int ret;
6430 unsigned char const * const peer_cert_digest =
6431 ssl->session->peer_cert_digest;
6432 mbedtls_md_type_t const peer_cert_digest_type =
6433 ssl->session->peer_cert_digest_type;
6434 mbedtls_md_info_t const * const digest_info =
6435 mbedtls_md_info_from_type( peer_cert_digest_type );
6436 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6437 size_t digest_len;
6438
6439 if( peer_cert_digest == NULL || digest_info == NULL )
6440 return( -1 );
6441
6442 digest_len = mbedtls_md_get_size( digest_info );
6443 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6444 return( -1 );
6445
6446 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6447 if( ret != 0 )
6448 return( -1 );
6449
6450 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6451}
6452#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006453#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006454
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006455/*
6456 * Once the certificate message is read, parse it into a cert chain and
6457 * perform basic checks, but leave actual verification to the caller
6458 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006459static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6460 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006461{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006462 int ret;
6463#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6464 int crt_cnt=0;
6465#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006466 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006467 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006472 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6473 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006475 }
6476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6478 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006481 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6482 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006484 }
6485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006487
Paul Bakker5121ce52009-01-03 21:22:43 +00006488 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006490 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006491 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006492
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006493 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006497 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6498 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 }
6501
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006502 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6503 i += 3;
6504
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006505 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006506 while( i < ssl->in_hslen )
6507 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006508 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006509 if ( i + 3 > ssl->in_hslen ) {
6510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006511 mbedtls_ssl_send_alert_message( ssl,
6512 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6513 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006514 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6515 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006516 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6517 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006518 if( ssl->in_msg[i] != 0 )
6519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006521 mbedtls_ssl_send_alert_message( ssl,
6522 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6523 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006525 }
6526
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006527 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006528 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6529 | (unsigned int) ssl->in_msg[i + 2];
6530 i += 3;
6531
6532 if( n < 128 || i + n > ssl->in_hslen )
6533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006535 mbedtls_ssl_send_alert_message( ssl,
6536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6537 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 }
6540
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006541 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006542#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6543 if( crt_cnt++ == 0 &&
6544 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6545 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006546 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006547 /* During client-side renegotiation, check that the server's
6548 * end-CRTs hasn't changed compared to the initial handshake,
6549 * mitigating the triple handshake attack. On success, reuse
6550 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6552 if( ssl_check_peer_crt_unchanged( ssl,
6553 &ssl->in_msg[i],
6554 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006555 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6557 mbedtls_ssl_send_alert_message( ssl,
6558 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6559 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6560 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006561 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006562
6563 /* Now we can safely free the original chain. */
6564 ssl_clear_peer_cert( ssl->session );
6565 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006566#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6567
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006568 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006569#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006570 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006571#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006572 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006573 * it in-place from the input buffer instead of making a copy. */
6574 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6575#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006576 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006578 case 0: /*ok*/
6579 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6580 /* Ignore certificate with an unknown algorithm: maybe a
6581 prior certificate was already trusted. */
6582 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006583
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006584 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6585 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6586 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006587
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006588 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6589 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6590 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006591
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006592 default:
6593 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6594 crt_parse_der_failed:
6595 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6596 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6597 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 }
6599
6600 i += n;
6601 }
6602
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006603 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006604 return( 0 );
6605}
6606
Hanno Becker4a55f632019-02-05 12:49:06 +00006607#if defined(MBEDTLS_SSL_SRV_C)
6608static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6609{
6610 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6611 return( -1 );
6612
6613#if defined(MBEDTLS_SSL_PROTO_SSL3)
6614 /*
6615 * Check if the client sent an empty certificate
6616 */
6617 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6618 {
6619 if( ssl->in_msglen == 2 &&
6620 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6621 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6622 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6623 {
6624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6625 return( 0 );
6626 }
6627
6628 return( -1 );
6629 }
6630#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6631
6632#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6633 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6634 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6635 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6636 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6637 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6638 {
6639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6640 return( 0 );
6641 }
6642
6643 return( -1 );
6644#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6645 MBEDTLS_SSL_PROTO_TLS1_2 */
6646}
6647#endif /* MBEDTLS_SSL_SRV_C */
6648
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006649/* Check if a certificate message is expected.
6650 * Return either
6651 * - SSL_CERTIFICATE_EXPECTED, or
6652 * - SSL_CERTIFICATE_SKIP
6653 * indicating whether a Certificate message is expected or not.
6654 */
6655#define SSL_CERTIFICATE_EXPECTED 0
6656#define SSL_CERTIFICATE_SKIP 1
6657static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6658 int authmode )
6659{
6660 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006661 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006662
6663 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6664 return( SSL_CERTIFICATE_SKIP );
6665
6666#if defined(MBEDTLS_SSL_SRV_C)
6667 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6668 {
6669 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6670 return( SSL_CERTIFICATE_SKIP );
6671
6672 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6673 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006674 ssl->session_negotiate->verify_result =
6675 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6676 return( SSL_CERTIFICATE_SKIP );
6677 }
6678 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006679#else
6680 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006681#endif /* MBEDTLS_SSL_SRV_C */
6682
6683 return( SSL_CERTIFICATE_EXPECTED );
6684}
6685
Hanno Becker68636192019-02-05 14:36:34 +00006686static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6687 int authmode,
6688 mbedtls_x509_crt *chain,
6689 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006690{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006691 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006692 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006693 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006694 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006695
Hanno Becker8927c832019-04-03 12:52:50 +01006696 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6697 void *p_vrfy;
6698
Hanno Becker68636192019-02-05 14:36:34 +00006699 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6700 return( 0 );
6701
Hanno Becker8927c832019-04-03 12:52:50 +01006702 if( ssl->f_vrfy != NULL )
6703 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006705 f_vrfy = ssl->f_vrfy;
6706 p_vrfy = ssl->p_vrfy;
6707 }
6708 else
6709 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006711 f_vrfy = ssl->conf->f_vrfy;
6712 p_vrfy = ssl->conf->p_vrfy;
6713 }
6714
Hanno Becker68636192019-02-05 14:36:34 +00006715 /*
6716 * Main check: verify certificate
6717 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006718#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6719 if( ssl->conf->f_ca_cb != NULL )
6720 {
6721 ((void) rs_ctx);
6722 have_ca_chain = 1;
6723
6724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006725 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006726 chain,
6727 ssl->conf->f_ca_cb,
6728 ssl->conf->p_ca_cb,
6729 ssl->conf->cert_profile,
6730 ssl->hostname,
6731 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006732 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006733 }
6734 else
6735#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6736 {
6737 mbedtls_x509_crt *ca_chain;
6738 mbedtls_x509_crl *ca_crl;
6739
6740#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6741 if( ssl->handshake->sni_ca_chain != NULL )
6742 {
6743 ca_chain = ssl->handshake->sni_ca_chain;
6744 ca_crl = ssl->handshake->sni_ca_crl;
6745 }
6746 else
6747#endif
6748 {
6749 ca_chain = ssl->conf->ca_chain;
6750 ca_crl = ssl->conf->ca_crl;
6751 }
6752
6753 if( ca_chain != NULL )
6754 have_ca_chain = 1;
6755
6756 ret = mbedtls_x509_crt_verify_restartable(
6757 chain,
6758 ca_chain, ca_crl,
6759 ssl->conf->cert_profile,
6760 ssl->hostname,
6761 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006762 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006763 }
Hanno Becker68636192019-02-05 14:36:34 +00006764
6765 if( ret != 0 )
6766 {
6767 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6768 }
6769
6770#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6771 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6772 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6773#endif
6774
6775 /*
6776 * Secondary checks: always done, but change 'ret' only if it was 0
6777 */
6778
6779#if defined(MBEDTLS_ECP_C)
6780 {
6781 const mbedtls_pk_context *pk = &chain->pk;
6782
6783 /* If certificate uses an EC key, make sure the curve is OK */
6784 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6785 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6786 {
6787 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6788
6789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6790 if( ret == 0 )
6791 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6792 }
6793 }
6794#endif /* MBEDTLS_ECP_C */
6795
6796 if( mbedtls_ssl_check_cert_usage( chain,
6797 ciphersuite_info,
6798 ! ssl->conf->endpoint,
6799 &ssl->session_negotiate->verify_result ) != 0 )
6800 {
6801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6802 if( ret == 0 )
6803 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6804 }
6805
6806 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6807 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6808 * with details encoded in the verification flags. All other kinds
6809 * of error codes, including those from the user provided f_vrfy
6810 * functions, are treated as fatal and lead to a failure of
6811 * ssl_parse_certificate even if verification was optional. */
6812 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6813 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6814 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6815 {
6816 ret = 0;
6817 }
6818
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006819 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006820 {
6821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6822 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6823 }
6824
6825 if( ret != 0 )
6826 {
6827 uint8_t alert;
6828
6829 /* The certificate may have been rejected for several reasons.
6830 Pick one and send the corresponding alert. Which alert to send
6831 may be a subject of debate in some cases. */
6832 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6833 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6834 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6835 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6836 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6837 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6838 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6839 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6840 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6841 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6842 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6843 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6844 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6845 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6846 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6847 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6848 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6849 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6850 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6851 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6852 else
6853 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6854 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6855 alert );
6856 }
6857
6858#if defined(MBEDTLS_DEBUG_C)
6859 if( ssl->session_negotiate->verify_result != 0 )
6860 {
6861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6862 ssl->session_negotiate->verify_result ) );
6863 }
6864 else
6865 {
6866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6867 }
6868#endif /* MBEDTLS_DEBUG_C */
6869
6870 return( ret );
6871}
6872
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006873#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6874static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6875 unsigned char *start, size_t len )
6876{
6877 int ret;
6878 /* Remember digest of the peer's end-CRT. */
6879 ssl->session_negotiate->peer_cert_digest =
6880 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6881 if( ssl->session_negotiate->peer_cert_digest == NULL )
6882 {
6883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6884 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6885 mbedtls_ssl_send_alert_message( ssl,
6886 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6887 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6888
6889 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6890 }
6891
6892 ret = mbedtls_md( mbedtls_md_info_from_type(
6893 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6894 start, len,
6895 ssl->session_negotiate->peer_cert_digest );
6896
6897 ssl->session_negotiate->peer_cert_digest_type =
6898 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6899 ssl->session_negotiate->peer_cert_digest_len =
6900 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6901
6902 return( ret );
6903}
6904
6905static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6906 unsigned char *start, size_t len )
6907{
6908 unsigned char *end = start + len;
6909 int ret;
6910
6911 /* Make a copy of the peer's raw public key. */
6912 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6913 ret = mbedtls_pk_parse_subpubkey( &start, end,
6914 &ssl->handshake->peer_pubkey );
6915 if( ret != 0 )
6916 {
6917 /* We should have parsed the public key before. */
6918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6919 }
6920
6921 return( 0 );
6922}
6923#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6924
Hanno Becker68636192019-02-05 14:36:34 +00006925int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6926{
6927 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006928 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006929#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6930 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6931 ? ssl->handshake->sni_authmode
6932 : ssl->conf->authmode;
6933#else
6934 const int authmode = ssl->conf->authmode;
6935#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006936 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006937 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006938
6939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6940
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006941 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6942 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006943 {
6944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006945 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006946 }
6947
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006948#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6949 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006950 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006951 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006952 chain = ssl->handshake->ecrs_peer_cert;
6953 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006954 goto crt_verify;
6955 }
6956#endif
6957
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006958 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006959 {
6960 /* mbedtls_ssl_read_record may have sent an alert already. We
6961 let it decide whether to alert. */
6962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006963 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006964 }
6965
Hanno Becker4a55f632019-02-05 12:49:06 +00006966#if defined(MBEDTLS_SSL_SRV_C)
6967 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6968 {
6969 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006970
6971 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006972 ret = 0;
6973 else
6974 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006975
Hanno Becker6bdfab22019-02-05 13:11:17 +00006976 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006977 }
6978#endif /* MBEDTLS_SSL_SRV_C */
6979
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006980 /* Clear existing peer CRT structure in case we tried to
6981 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006982 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006983
6984 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6985 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006986 {
6987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6988 sizeof( mbedtls_x509_crt ) ) );
6989 mbedtls_ssl_send_alert_message( ssl,
6990 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6991 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006992
Hanno Becker3dad3112019-02-05 17:19:52 +00006993 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6994 goto exit;
6995 }
6996 mbedtls_x509_crt_init( chain );
6997
6998 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006999 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007000 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007001
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007002#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7003 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007004 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007005
7006crt_verify:
7007 if( ssl->handshake->ecrs_enabled)
7008 rs_ctx = &ssl->handshake->ecrs_ctx;
7009#endif
7010
Hanno Becker68636192019-02-05 14:36:34 +00007011 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007012 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007013 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007014 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007016#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007017 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007018 unsigned char *crt_start, *pk_start;
7019 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007020
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007021 /* We parse the CRT chain without copying, so
7022 * these pointers point into the input buffer,
7023 * and are hence still valid after freeing the
7024 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007025
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007026 crt_start = chain->raw.p;
7027 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007028
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007029 pk_start = chain->pk_raw.p;
7030 pk_len = chain->pk_raw.len;
7031
7032 /* Free the CRT structures before computing
7033 * digest and copying the peer's public key. */
7034 mbedtls_x509_crt_free( chain );
7035 mbedtls_free( chain );
7036 chain = NULL;
7037
7038 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007039 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007040 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007041
7042 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7043 if( ret != 0 )
7044 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007045 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007046#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7047 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007048 ssl->session_negotiate->peer_cert = chain;
7049 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007050#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007053
Hanno Becker6bdfab22019-02-05 13:11:17 +00007054exit:
7055
Hanno Becker3dad3112019-02-05 17:19:52 +00007056 if( ret == 0 )
7057 ssl->state++;
7058
7059#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7060 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7061 {
7062 ssl->handshake->ecrs_peer_cert = chain;
7063 chain = NULL;
7064 }
7065#endif
7066
7067 if( chain != NULL )
7068 {
7069 mbedtls_x509_crt_free( chain );
7070 mbedtls_free( chain );
7071 }
7072
Paul Bakker5121ce52009-01-03 21:22:43 +00007073 return( ret );
7074}
Hanno Becker21489932019-02-05 13:20:55 +00007075#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007078{
7079 int ret;
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007084 ssl->out_msglen = 1;
7085 ssl->out_msg[0] = 1;
7086
Paul Bakker5121ce52009-01-03 21:22:43 +00007087 ssl->state++;
7088
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007089 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007090 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 return( ret );
7093 }
7094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007096
7097 return( 0 );
7098}
7099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007101{
7102 int ret;
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Hanno Becker327c93b2018-08-15 13:56:18 +01007106 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007109 return( ret );
7110 }
7111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007115 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7116 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007118 }
7119
Hanno Beckere678eaa2018-08-21 14:57:46 +01007120 /* CCS records are only accepted if they have length 1 and content '1',
7121 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007122
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007123 /*
7124 * Switch to our negotiated transform and session parameters for inbound
7125 * data.
7126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007128 ssl->transform_in = ssl->transform_negotiate;
7129 ssl->session_in = ssl->session_negotiate;
7130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007132 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007135 ssl_dtls_replay_reset( ssl );
7136#endif
7137
7138 /* Increment epoch */
7139 if( ++ssl->in_epoch == 0 )
7140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007142 /* This is highly unlikely to happen for legitimate reasons, so
7143 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007145 }
7146 }
7147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007149 memset( ssl->in_ctr, 0, 8 );
7150
Hanno Becker79594fd2019-05-08 09:38:41 +01007151 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7154 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007159 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7160 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007162 }
7163 }
7164#endif
7165
Paul Bakker5121ce52009-01-03 21:22:43 +00007166 ssl->state++;
7167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007169
7170 return( 0 );
7171}
7172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7174 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007175{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007176 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7179 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7180 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007181 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007182 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7185#if defined(MBEDTLS_SHA512_C)
7186 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007187 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7188 else
7189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190#if defined(MBEDTLS_SHA256_C)
7191 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007192 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007193 else
7194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007198 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007199 }
Paul Bakker380da532012-04-18 16:10:25 +00007200}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7205 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007206 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7207 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7210#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007211#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007212 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007213 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7214#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007215 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007216#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007219#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007220 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007221 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007222#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007223 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007224#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007227}
7228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007230 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7233 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007234 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7235 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7238#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007239#if defined(MBEDTLS_USE_PSA_CRYPTO)
7240 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7241#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007242 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007243#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007246#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007247 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007248#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007249 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007250#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007253}
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7256 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7257static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007258 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007259{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007260 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7261 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007262}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007263#endif
Paul Bakker380da532012-04-18 16:10:25 +00007264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7266#if defined(MBEDTLS_SHA256_C)
7267static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007268 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007269{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007270#if defined(MBEDTLS_USE_PSA_CRYPTO)
7271 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7272#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007273 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007274#endif
Paul Bakker380da532012-04-18 16:10:25 +00007275}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007276#endif
Paul Bakker380da532012-04-18 16:10:25 +00007277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SHA512_C)
7279static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007280 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007281{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007282#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007283 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007284#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007285 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007286#endif
Paul Bakker380da532012-04-18 16:10:25 +00007287}
Paul Bakker769075d2012-11-24 11:26:46 +01007288#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007292static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007294{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007295 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007296 mbedtls_md5_context md5;
7297 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007298
Paul Bakker5121ce52009-01-03 21:22:43 +00007299 unsigned char padbuf[48];
7300 unsigned char md5sum[16];
7301 unsigned char sha1sum[20];
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007304 if( !session )
7305 session = ssl->session;
7306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007308
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007309 mbedtls_md5_init( &md5 );
7310 mbedtls_sha1_init( &sha1 );
7311
7312 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7313 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007314
7315 /*
7316 * SSLv3:
7317 * hash =
7318 * MD5( master + pad2 +
7319 * MD5( handshake + sender + master + pad1 ) )
7320 * + SHA1( master + pad2 +
7321 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007322 */
7323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007325 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7326 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007330 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7331 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007335 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007336
Paul Bakker1ef83d62012-04-11 12:09:53 +00007337 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007338
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007339 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7340 mbedtls_md5_update_ret( &md5, session->master, 48 );
7341 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7342 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007343
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007344 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7345 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7346 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7347 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
Paul Bakker1ef83d62012-04-11 12:09:53 +00007349 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007351 mbedtls_md5_starts_ret( &md5 );
7352 mbedtls_md5_update_ret( &md5, session->master, 48 );
7353 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7354 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7355 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007356
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007357 mbedtls_sha1_starts_ret( &sha1 );
7358 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7359 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7360 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7361 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007364
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007365 mbedtls_md5_free( &md5 );
7366 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007367
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007368 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7369 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7370 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007377static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007379{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007380 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007381 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007382 mbedtls_md5_context md5;
7383 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007384 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007387 if( !session )
7388 session = ssl->session;
7389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007391
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007392 mbedtls_md5_init( &md5 );
7393 mbedtls_sha1_init( &sha1 );
7394
7395 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7396 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
Paul Bakker1ef83d62012-04-11 12:09:53 +00007398 /*
7399 * TLSv1:
7400 * hash = PRF( master, finished_label,
7401 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7402 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007405 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7406 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007407#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007410 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7411 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007412#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007415 ? "client finished"
7416 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007417
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007418 mbedtls_md5_finish_ret( &md5, padbuf );
7419 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007420
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007421 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007422 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007425
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007426 mbedtls_md5_free( &md5 );
7427 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007428
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007429 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7436#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007437static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007439{
7440 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007441 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007443#if defined(MBEDTLS_USE_PSA_CRYPTO)
7444 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007445 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007446 psa_status_t status;
7447#else
7448 mbedtls_sha256_context sha256;
7449#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007452 if( !session )
7453 session = ssl->session;
7454
Andrzej Kurekeb342242019-01-29 09:14:33 -05007455 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7456 ? "client finished"
7457 : "server finished";
7458
7459#if defined(MBEDTLS_USE_PSA_CRYPTO)
7460 sha256_psa = psa_hash_operation_init();
7461
7462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7463
7464 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7465 if( status != PSA_SUCCESS )
7466 {
7467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7468 return;
7469 }
7470
7471 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7472 if( status != PSA_SUCCESS )
7473 {
7474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7475 return;
7476 }
7477 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7478#else
7479
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007480 mbedtls_sha256_init( &sha256 );
7481
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007483
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007484 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007485
7486 /*
7487 * TLSv1.2:
7488 * hash = PRF( master, finished_label,
7489 * Hash( handshake ) )[0.11]
7490 */
7491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492#if !defined(MBEDTLS_SHA256_ALT)
7493 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007494 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007495#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007496
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007497 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007498 mbedtls_sha256_free( &sha256 );
7499#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007500
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007501 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007502 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007505
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007506 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007513static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007515{
7516 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007517 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007518 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007519#if defined(MBEDTLS_USE_PSA_CRYPTO)
7520 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007521 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007522 psa_status_t status;
7523#else
7524 mbedtls_sha512_context sha512;
7525#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007528 if( !session )
7529 session = ssl->session;
7530
Andrzej Kurekeb342242019-01-29 09:14:33 -05007531 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7532 ? "client finished"
7533 : "server finished";
7534
7535#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007536 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007537
7538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7539
Andrzej Kurek972fba52019-01-30 03:29:12 -05007540 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007541 if( status != PSA_SUCCESS )
7542 {
7543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7544 return;
7545 }
7546
Andrzej Kurek972fba52019-01-30 03:29:12 -05007547 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007548 if( status != PSA_SUCCESS )
7549 {
7550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7551 return;
7552 }
7553 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7554#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007555 mbedtls_sha512_init( &sha512 );
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007558
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007559 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007560
7561 /*
7562 * TLSv1.2:
7563 * hash = PRF( master, finished_label,
7564 * Hash( handshake ) )[0.11]
7565 */
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007568 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7569 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007570#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007571
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007572 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007573 mbedtls_sha512_free( &sha512 );
7574#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007575
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007576 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007577 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007580
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007581 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007584}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585#endif /* MBEDTLS_SHA512_C */
7586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007591
7592 /*
7593 * Free our handshake params
7594 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007595 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007597 ssl->handshake = NULL;
7598
7599 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007600 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007601 */
7602 if( ssl->transform )
7603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 mbedtls_ssl_transform_free( ssl->transform );
7605 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007606 }
7607 ssl->transform = ssl->transform_negotiate;
7608 ssl->transform_negotiate = NULL;
7609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007611}
7612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007614{
7615 int resume = ssl->handshake->resume;
7616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619#if defined(MBEDTLS_SSL_RENEGOTIATION)
7620 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007623 ssl->renego_records_seen = 0;
7624 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007625#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007626
7627 /*
7628 * Free the previous session and switch in the current one
7629 */
Paul Bakker0a597072012-09-25 21:55:46 +00007630 if( ssl->session )
7631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007633 /* RFC 7366 3.1: keep the EtM state */
7634 ssl->session_negotiate->encrypt_then_mac =
7635 ssl->session->encrypt_then_mac;
7636#endif
7637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 mbedtls_ssl_session_free( ssl->session );
7639 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007640 }
7641 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007642 ssl->session_negotiate = NULL;
7643
Paul Bakker0a597072012-09-25 21:55:46 +00007644 /*
7645 * Add cache entry
7646 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007647 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007648 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007649 resume == 0 )
7650 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007651 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007653 }
Paul Bakker0a597072012-09-25 21:55:46 +00007654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007656 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007657 ssl->handshake->flight != NULL )
7658 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007659 /* Cancel handshake timer */
7660 ssl_set_timer( ssl, 0 );
7661
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007662 /* Keep last flight around in case we need to resend it:
7663 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007665 }
7666 else
7667#endif
7668 ssl_handshake_wrapup_free_hs_transform( ssl );
7669
Paul Bakker48916f92012-09-16 19:57:18 +00007670 ssl->state++;
7671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007673}
7674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007676{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007677 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007681 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007682
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007683 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007685 /*
7686 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7687 * may define some other value. Currently (early 2016), no defined
7688 * ciphersuite does this (and this is unlikely to change as activity has
7689 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007694 ssl->verify_data_len = hash_len;
7695 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007696#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007697
Paul Bakker5121ce52009-01-03 21:22:43 +00007698 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7700 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
7702 /*
7703 * In case of session resuming, invert the client and server
7704 * ChangeCipherSpec messages order.
7705 */
Paul Bakker0a597072012-09-25 21:55:46 +00007706 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007709 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007713 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007715#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007716 }
7717 else
7718 ssl->state++;
7719
Paul Bakker48916f92012-09-16 19:57:18 +00007720 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007721 * Switch to our negotiated transform and session parameters for outbound
7722 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007727 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007728 {
7729 unsigned char i;
7730
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007731 /* Remember current epoch settings for resending */
7732 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007733 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007734
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007735 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007736 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007737
7738 /* Increment epoch */
7739 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007740 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007741 break;
7742
7743 /* The loop goes to its end iff the counter is wrapping */
7744 if( i == 0 )
7745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7747 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007748 }
7749 }
7750 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007752 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007753
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007754 ssl->transform_out = ssl->transform_negotiate;
7755 ssl->session_out = ssl->session_negotiate;
7756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7758 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7763 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007764 }
7765 }
7766#endif
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007769 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007771#endif
7772
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007773 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007774 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007776 return( ret );
7777 }
7778
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007779#if defined(MBEDTLS_SSL_PROTO_DTLS)
7780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7781 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7782 {
7783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7784 return( ret );
7785 }
7786#endif
7787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007789
7790 return( 0 );
7791}
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007794#define SSL_MAX_HASH_LEN 36
7795#else
7796#define SSL_MAX_HASH_LEN 12
7797#endif
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007800{
Paul Bakker23986e52011-04-24 08:57:21 +00007801 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007802 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007803 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007806
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007807 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808
Hanno Becker327c93b2018-08-15 13:56:18 +01007809 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007812 return( ret );
7813 }
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007818 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7819 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007821 }
7822
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007823 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824#if defined(MBEDTLS_SSL_PROTO_SSL3)
7825 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007826 hash_len = 36;
7827 else
7828#endif
7829 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7832 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007835 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7836 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007838 }
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007841 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007844 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7845 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 }
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007850 ssl->verify_data_len = hash_len;
7851 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007852#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007853
Paul Bakker0a597072012-09-25 21:55:46 +00007854 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007857 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007859#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007861 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007863#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007864 }
7865 else
7866 ssl->state++;
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007871#endif
7872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007874
7875 return( 0 );
7876}
7877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7883 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7884 mbedtls_md5_init( &handshake->fin_md5 );
7885 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007886 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7887 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7890#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007891#if defined(MBEDTLS_USE_PSA_CRYPTO)
7892 handshake->fin_sha256_psa = psa_hash_operation_init();
7893 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7894#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007896 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007897#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007898#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007900#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007901 handshake->fin_sha384_psa = psa_hash_operation_init();
7902 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007903#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007905 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007906#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007909
7910 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007911
7912#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7913 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7914 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7915#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917#if defined(MBEDTLS_DHM_C)
7918 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_ECDH_C)
7921 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007922#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007923#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007924 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007925#if defined(MBEDTLS_SSL_CLI_C)
7926 handshake->ecjpake_cache = NULL;
7927 handshake->ecjpake_cache_len = 0;
7928#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007929#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007930
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007931#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007932 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007933#endif
7934
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007935#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7936 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7937#endif
Hanno Becker75173122019-02-06 16:18:31 +00007938
7939#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7940 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7941 mbedtls_pk_init( &handshake->peer_pubkey );
7942#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007943}
7944
Hanno Beckera18d1322018-01-03 14:27:32 +00007945void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007946{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7950 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007951
Hanno Beckerd56ed242018-01-03 15:32:51 +00007952#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 mbedtls_md_init( &transform->md_ctx_enc );
7954 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007955#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956}
7957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007961}
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007964{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007965 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007966 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007970 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007971 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972
7973 /*
7974 * Either the pointers are now NULL or cleared properly and can be freed.
7975 * Now allocate missing structures.
7976 */
7977 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007978 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007979 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007980 }
Paul Bakker48916f92012-09-16 19:57:18 +00007981
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007983 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007984 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007985 }
Paul Bakker48916f92012-09-16 19:57:18 +00007986
Paul Bakker82788fb2014-10-20 13:59:19 +02007987 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007988 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007989 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007990 }
Paul Bakker48916f92012-09-16 19:57:18 +00007991
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007992 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007993 if( ssl->handshake == NULL ||
7994 ssl->transform_negotiate == NULL ||
7995 ssl->session_negotiate == NULL )
7996 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 mbedtls_free( ssl->handshake );
8000 mbedtls_free( ssl->transform_negotiate );
8001 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008002
8003 ssl->handshake = NULL;
8004 ssl->transform_negotiate = NULL;
8005 ssl->session_negotiate = NULL;
8006
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008007 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008008 }
8009
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008010 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008012 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008013 ssl_handshake_params_init( ssl->handshake );
8014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008016 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8017 {
8018 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008019
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008020 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8021 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8022 else
8023 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008024
8025 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008026 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008027#endif
8028
Paul Bakker48916f92012-09-16 19:57:18 +00008029 return( 0 );
8030}
8031
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008032#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008033/* Dummy cookie callbacks for defaults */
8034static int ssl_cookie_write_dummy( void *ctx,
8035 unsigned char **p, unsigned char *end,
8036 const unsigned char *cli_id, size_t cli_id_len )
8037{
8038 ((void) ctx);
8039 ((void) p);
8040 ((void) end);
8041 ((void) cli_id);
8042 ((void) cli_id_len);
8043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008045}
8046
8047static int ssl_cookie_check_dummy( void *ctx,
8048 const unsigned char *cookie, size_t cookie_len,
8049 const unsigned char *cli_id, size_t cli_id_len )
8050{
8051 ((void) ctx);
8052 ((void) cookie);
8053 ((void) cookie_len);
8054 ((void) cli_id);
8055 ((void) cli_id_len);
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008058}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008059#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008060
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008061/* Once ssl->out_hdr as the address of the beginning of the
8062 * next outgoing record is set, deduce the other pointers.
8063 *
8064 * Note: For TLS, we save the implicit record sequence number
8065 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8066 * and the caller has to make sure there's space for this.
8067 */
8068
8069static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8070 mbedtls_ssl_transform *transform )
8071{
8072#if defined(MBEDTLS_SSL_PROTO_DTLS)
8073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8074 {
8075 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008076#if defined(MBEDTLS_SSL_CID)
8077 ssl->out_cid = ssl->out_ctr + 8;
8078 ssl->out_len = ssl->out_cid;
8079 if( transform != NULL )
8080 ssl->out_len += transform->out_cid_len;
8081#else /* MBEDTLS_SSL_CID */
8082 ssl->out_len = ssl->out_ctr + 8;
8083#endif /* MBEDTLS_SSL_CID */
8084 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008085 }
8086 else
8087#endif
8088 {
8089 ssl->out_ctr = ssl->out_hdr - 8;
8090 ssl->out_len = ssl->out_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008091#if defined(MBEDTLS_SSL_CID)
8092 ssl->out_cid = ssl->out_len;
8093#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008094 ssl->out_iv = ssl->out_hdr + 5;
8095 }
8096
8097 /* Adjust out_msg to make space for explicit IV, if used. */
8098 if( transform != NULL &&
8099 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8100 {
8101 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8102 }
8103 else
8104 ssl->out_msg = ssl->out_iv;
8105}
8106
8107/* Once ssl->in_hdr as the address of the beginning of the
8108 * next incoming record is set, deduce the other pointers.
8109 *
8110 * Note: For TLS, we save the implicit record sequence number
8111 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8112 * and the caller has to make sure there's space for this.
8113 */
8114
Hanno Becker79594fd2019-05-08 09:38:41 +01008115static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008116{
Hanno Becker79594fd2019-05-08 09:38:41 +01008117 /* This function sets the pointers to match the case
8118 * of unprotected TLS/DTLS records, with both ssl->in_iv
8119 * and ssl->in_msg pointing to the beginning of the record
8120 * content.
8121 *
8122 * When decrypting a protected record, ssl->in_msg
8123 * will be shifted to point to the beginning of the
8124 * record plaintext.
8125 */
8126
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008127#if defined(MBEDTLS_SSL_PROTO_DTLS)
8128 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8129 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008130 /* This sets the header pointers to match records
8131 * without CID. When we receive a record containing
8132 * a CID, the fields are shifted accordingly in
8133 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008134 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008135#if defined(MBEDTLS_SSL_CID)
8136 ssl->in_cid = ssl->in_ctr + 8;
8137 ssl->in_len = ssl->in_cid; /* Default: no CID */
8138#else /* MBEDTLS_SSL_CID */
8139 ssl->in_len = ssl->in_ctr + 8;
8140#endif /* MBEDTLS_SSL_CID */
8141 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008142 }
8143 else
8144#endif
8145 {
8146 ssl->in_ctr = ssl->in_hdr - 8;
8147 ssl->in_len = ssl->in_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008148#if defined(MBEDTLS_SSL_CID)
8149 ssl->in_cid = ssl->in_len;
8150#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008151 ssl->in_iv = ssl->in_hdr + 5;
8152 }
8153
Hanno Becker79594fd2019-05-08 09:38:41 +01008154 /* This will be adjusted at record decryption time. */
8155 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008156}
8157
Paul Bakker5121ce52009-01-03 21:22:43 +00008158/*
8159 * Initialize an SSL context
8160 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008161void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8162{
8163 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8164}
8165
8166/*
8167 * Setup an SSL context
8168 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008169
8170static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8171{
8172 /* Set the incoming and outgoing record pointers. */
8173#if defined(MBEDTLS_SSL_PROTO_DTLS)
8174 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8175 {
8176 ssl->out_hdr = ssl->out_buf;
8177 ssl->in_hdr = ssl->in_buf;
8178 }
8179 else
8180#endif /* MBEDTLS_SSL_PROTO_DTLS */
8181 {
8182 ssl->out_hdr = ssl->out_buf + 8;
8183 ssl->in_hdr = ssl->in_buf + 8;
8184 }
8185
8186 /* Derive other internal pointers. */
8187 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008188 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008189}
8190
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008191int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008192 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008193{
Paul Bakker48916f92012-09-16 19:57:18 +00008194 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008195
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008196 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008197
8198 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008199 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008200 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008201
8202 /* Set to NULL in case of an error condition */
8203 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008204
Angus Grattond8213d02016-05-25 20:56:48 +10008205 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8206 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008207 {
Angus Grattond8213d02016-05-25 20:56:48 +10008208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008209 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008210 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008211 }
8212
8213 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8214 if( ssl->out_buf == NULL )
8215 {
8216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008217 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008218 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008219 }
8220
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008221 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008222
Paul Bakker48916f92012-09-16 19:57:18 +00008223 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008224 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008225
8226 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008227
8228error:
8229 mbedtls_free( ssl->in_buf );
8230 mbedtls_free( ssl->out_buf );
8231
8232 ssl->conf = NULL;
8233
8234 ssl->in_buf = NULL;
8235 ssl->out_buf = NULL;
8236
8237 ssl->in_hdr = NULL;
8238 ssl->in_ctr = NULL;
8239 ssl->in_len = NULL;
8240 ssl->in_iv = NULL;
8241 ssl->in_msg = NULL;
8242
8243 ssl->out_hdr = NULL;
8244 ssl->out_ctr = NULL;
8245 ssl->out_len = NULL;
8246 ssl->out_iv = NULL;
8247 ssl->out_msg = NULL;
8248
k-stachowiak9f7798e2018-07-31 16:52:32 +02008249 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008250}
8251
8252/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008253 * Reset an initialized and used SSL context for re-use while retaining
8254 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008255 *
8256 * If partial is non-zero, keep data in the input buffer and client ID.
8257 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008258 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008259static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008260{
Paul Bakker48916f92012-09-16 19:57:18 +00008261 int ret;
8262
Hanno Becker7e772132018-08-10 12:38:21 +01008263#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8264 !defined(MBEDTLS_SSL_SRV_C)
8265 ((void) partial);
8266#endif
8267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008269
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008270 /* Cancel any possibly running timer */
8271 ssl_set_timer( ssl, 0 );
8272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#if defined(MBEDTLS_SSL_RENEGOTIATION)
8274 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008275 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008276
8277 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8279 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008282
Paul Bakker7eb013f2011-10-06 12:37:39 +00008283 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008284 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008285
8286 ssl->in_msgtype = 0;
8287 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008289 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008290 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008293 ssl_dtls_replay_reset( ssl );
8294#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008295
8296 ssl->in_hslen = 0;
8297 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008298
8299 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008300
8301 ssl->out_msgtype = 0;
8302 ssl->out_msglen = 0;
8303 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8305 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008306 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008307#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008308
Hanno Becker19859472018-08-06 09:40:20 +01008309 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8310
Paul Bakker48916f92012-09-16 19:57:18 +00008311 ssl->transform_in = NULL;
8312 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008313
Hanno Becker78640902018-08-13 16:35:15 +01008314 ssl->session_in = NULL;
8315 ssl->session_out = NULL;
8316
Angus Grattond8213d02016-05-25 20:56:48 +10008317 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008318
8319#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008320 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008321#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8322 {
8323 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008324 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008325 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8328 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8331 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8334 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008335 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008336 }
8337#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008338
Paul Bakker48916f92012-09-16 19:57:18 +00008339 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 mbedtls_ssl_transform_free( ssl->transform );
8342 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008343 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008344 }
Paul Bakker48916f92012-09-16 19:57:18 +00008345
Paul Bakkerc0463502013-02-14 11:19:38 +01008346 if( ssl->session )
8347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 mbedtls_ssl_session_free( ssl->session );
8349 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008350 ssl->session = NULL;
8351 }
8352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008354 ssl->alpn_chosen = NULL;
8355#endif
8356
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008357#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008358#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008359 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008360#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008361 {
8362 mbedtls_free( ssl->cli_id );
8363 ssl->cli_id = NULL;
8364 ssl->cli_id_len = 0;
8365 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008366#endif
8367
Paul Bakker48916f92012-09-16 19:57:18 +00008368 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8369 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008370
8371 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008372}
8373
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008374/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008375 * Reset an initialized and used SSL context for re-use while retaining
8376 * all application-set variables, function pointers and data.
8377 */
8378int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8379{
8380 return( ssl_session_reset_int( ssl, 0 ) );
8381}
8382
8383/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008384 * SSL set accessors
8385 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008386void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008387{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008388 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008389}
8390
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008391void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008392{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008393 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008394}
8395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008397void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008398{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008399 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008400}
8401#endif
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008404void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008405{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008406 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008407}
8408#endif
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008411
Hanno Becker1841b0a2018-08-24 11:13:57 +01008412void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8413 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008414{
8415 ssl->disable_datagram_packing = !allow_packing;
8416}
8417
8418void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8419 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008420{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008421 conf->hs_timeout_min = min;
8422 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008423}
8424#endif
8425
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008426void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008428 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008429}
8430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008432void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008433 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008434 void *p_vrfy )
8435{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008436 conf->f_vrfy = f_vrfy;
8437 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008440
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008441void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008442 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008443 void *p_rng )
8444{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008445 conf->f_rng = f_rng;
8446 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008447}
8448
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008449void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008450 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008451 void *p_dbg )
8452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008453 conf->f_dbg = f_dbg;
8454 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008455}
8456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008458 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008459 mbedtls_ssl_send_t *f_send,
8460 mbedtls_ssl_recv_t *f_recv,
8461 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008462{
8463 ssl->p_bio = p_bio;
8464 ssl->f_send = f_send;
8465 ssl->f_recv = f_recv;
8466 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008467}
8468
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008469#if defined(MBEDTLS_SSL_PROTO_DTLS)
8470void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8471{
8472 ssl->mtu = mtu;
8473}
8474#endif
8475
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008476void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008477{
8478 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008479}
8480
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008481void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8482 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008483 mbedtls_ssl_set_timer_t *f_set_timer,
8484 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008485{
8486 ssl->p_timer = p_timer;
8487 ssl->f_set_timer = f_set_timer;
8488 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008489
8490 /* Make sure we start with no timer running */
8491 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008492}
8493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008495void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008496 void *p_cache,
8497 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8498 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008499{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008500 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008501 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008502 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506#if defined(MBEDTLS_SSL_CLI_C)
8507int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008508{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008509 int ret;
8510
8511 if( ssl == NULL ||
8512 session == NULL ||
8513 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008514 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008517 }
8518
Hanno Becker52055ae2019-02-06 14:30:46 +00008519 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8520 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008521 return( ret );
8522
Paul Bakker0a597072012-09-25 21:55:46 +00008523 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008524
8525 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008528
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008529void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008530 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008532 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8533 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8534 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8535 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008536}
8537
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008538void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008539 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008540 int major, int minor )
8541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008543 return;
8544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008546 return;
8547
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008548 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008549}
8550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008552void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008553 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008554{
8555 conf->cert_profile = profile;
8556}
8557
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008558/* Append a new keycert entry to a (possibly empty) list */
8559static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8560 mbedtls_x509_crt *cert,
8561 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008562{
niisato8ee24222018-06-25 19:05:48 +09008563 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008564
niisato8ee24222018-06-25 19:05:48 +09008565 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8566 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008567 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008568
niisato8ee24222018-06-25 19:05:48 +09008569 new_cert->cert = cert;
8570 new_cert->key = key;
8571 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008572
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008573 /* Update head is the list was null, else add to the end */
8574 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008575 {
niisato8ee24222018-06-25 19:05:48 +09008576 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008577 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008578 else
8579 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008580 mbedtls_ssl_key_cert *cur = *head;
8581 while( cur->next != NULL )
8582 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008583 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008584 }
8585
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008586 return( 0 );
8587}
8588
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008589int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008590 mbedtls_x509_crt *own_cert,
8591 mbedtls_pk_context *pk_key )
8592{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008593 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008594}
8595
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008596void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008597 mbedtls_x509_crt *ca_chain,
8598 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008599{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008600 conf->ca_chain = ca_chain;
8601 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008602
8603#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8604 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8605 * cannot be used together. */
8606 conf->f_ca_cb = NULL;
8607 conf->p_ca_cb = NULL;
8608#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008609}
Hanno Becker5adaad92019-03-27 16:54:37 +00008610
8611#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8612void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008613 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008614 void *p_ca_cb )
8615{
8616 conf->f_ca_cb = f_ca_cb;
8617 conf->p_ca_cb = p_ca_cb;
8618
8619 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8620 * cannot be used together. */
8621 conf->ca_chain = NULL;
8622 conf->ca_crl = NULL;
8623}
8624#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008626
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008627#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8628int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8629 mbedtls_x509_crt *own_cert,
8630 mbedtls_pk_context *pk_key )
8631{
8632 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8633 own_cert, pk_key ) );
8634}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008635
8636void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8637 mbedtls_x509_crt *ca_chain,
8638 mbedtls_x509_crl *ca_crl )
8639{
8640 ssl->handshake->sni_ca_chain = ca_chain;
8641 ssl->handshake->sni_ca_crl = ca_crl;
8642}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008643
8644void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8645 int authmode )
8646{
8647 ssl->handshake->sni_authmode = authmode;
8648}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008649#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8650
Hanno Becker8927c832019-04-03 12:52:50 +01008651#if defined(MBEDTLS_X509_CRT_PARSE_C)
8652void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8653 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8654 void *p_vrfy )
8655{
8656 ssl->f_vrfy = f_vrfy;
8657 ssl->p_vrfy = p_vrfy;
8658}
8659#endif
8660
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008661#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008662/*
8663 * Set EC J-PAKE password for current handshake
8664 */
8665int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8666 const unsigned char *pw,
8667 size_t pw_len )
8668{
8669 mbedtls_ecjpake_role role;
8670
Janos Follath8eb64132016-06-03 15:40:57 +01008671 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8673
8674 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8675 role = MBEDTLS_ECJPAKE_SERVER;
8676 else
8677 role = MBEDTLS_ECJPAKE_CLIENT;
8678
8679 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8680 role,
8681 MBEDTLS_MD_SHA256,
8682 MBEDTLS_ECP_DP_SECP256R1,
8683 pw, pw_len ) );
8684}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008685#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008688
8689static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8690{
8691 /* Remove reference to existing PSK, if any. */
8692#if defined(MBEDTLS_USE_PSA_CRYPTO)
8693 if( conf->psk_opaque != 0 )
8694 {
8695 /* The maintenance of the PSK key slot is the
8696 * user's responsibility. */
8697 conf->psk_opaque = 0;
8698 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008699 /* This and the following branch should never
8700 * be taken simultaenously as we maintain the
8701 * invariant that raw and opaque PSKs are never
8702 * configured simultaneously. As a safeguard,
8703 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008704#endif /* MBEDTLS_USE_PSA_CRYPTO */
8705 if( conf->psk != NULL )
8706 {
8707 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8708
8709 mbedtls_free( conf->psk );
8710 conf->psk = NULL;
8711 conf->psk_len = 0;
8712 }
8713
8714 /* Remove reference to PSK identity, if any. */
8715 if( conf->psk_identity != NULL )
8716 {
8717 mbedtls_free( conf->psk_identity );
8718 conf->psk_identity = NULL;
8719 conf->psk_identity_len = 0;
8720 }
8721}
8722
Hanno Becker7390c712018-11-15 13:33:04 +00008723/* This function assumes that PSK identity in the SSL config is unset.
8724 * It checks that the provided identity is well-formed and attempts
8725 * to make a copy of it in the SSL config.
8726 * On failure, the PSK identity in the config remains unset. */
8727static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8728 unsigned char const *psk_identity,
8729 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008730{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008731 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008732 if( psk_identity == NULL ||
8733 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008734 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008735 {
8736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8737 }
8738
Hanno Becker7390c712018-11-15 13:33:04 +00008739 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8740 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008741 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008742
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008743 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008744 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008745
8746 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008747}
8748
Hanno Becker7390c712018-11-15 13:33:04 +00008749int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8750 const unsigned char *psk, size_t psk_len,
8751 const unsigned char *psk_identity, size_t psk_identity_len )
8752{
8753 int ret;
8754 /* Remove opaque/raw PSK + PSK Identity */
8755 ssl_conf_remove_psk( conf );
8756
8757 /* Check and set raw PSK */
8758 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8760 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8761 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8762 conf->psk_len = psk_len;
8763 memcpy( conf->psk, psk, conf->psk_len );
8764
8765 /* Check and set PSK Identity */
8766 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8767 if( ret != 0 )
8768 ssl_conf_remove_psk( conf );
8769
8770 return( ret );
8771}
8772
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008773static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8774{
8775#if defined(MBEDTLS_USE_PSA_CRYPTO)
8776 if( ssl->handshake->psk_opaque != 0 )
8777 {
8778 ssl->handshake->psk_opaque = 0;
8779 }
8780 else
8781#endif /* MBEDTLS_USE_PSA_CRYPTO */
8782 if( ssl->handshake->psk != NULL )
8783 {
8784 mbedtls_platform_zeroize( ssl->handshake->psk,
8785 ssl->handshake->psk_len );
8786 mbedtls_free( ssl->handshake->psk );
8787 ssl->handshake->psk_len = 0;
8788 }
8789}
8790
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008791int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8792 const unsigned char *psk, size_t psk_len )
8793{
8794 if( psk == NULL || ssl->handshake == NULL )
8795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8796
8797 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8799
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008800 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008801
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008802 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008803 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008804
8805 ssl->handshake->psk_len = psk_len;
8806 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8807
8808 return( 0 );
8809}
8810
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008811#if defined(MBEDTLS_USE_PSA_CRYPTO)
8812int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008813 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008814 const unsigned char *psk_identity,
8815 size_t psk_identity_len )
8816{
Hanno Becker7390c712018-11-15 13:33:04 +00008817 int ret;
8818 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008819 ssl_conf_remove_psk( conf );
8820
Hanno Becker7390c712018-11-15 13:33:04 +00008821 /* Check and set opaque PSK */
8822 if( psk_slot == 0 )
8823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008824 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008825
8826 /* Check and set PSK Identity */
8827 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8828 psk_identity_len );
8829 if( ret != 0 )
8830 ssl_conf_remove_psk( conf );
8831
8832 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008833}
8834
8835int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008836 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008837{
8838 if( psk_slot == 0 || ssl->handshake == NULL )
8839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8840
8841 ssl_remove_psk( ssl );
8842 ssl->handshake->psk_opaque = psk_slot;
8843 return( 0 );
8844}
8845#endif /* MBEDTLS_USE_PSA_CRYPTO */
8846
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008847void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008849 size_t),
8850 void *p_psk )
8851{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008852 conf->f_psk = f_psk;
8853 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008856
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008857#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008858
8859#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008860int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008861{
8862 int ret;
8863
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008864 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8865 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8866 {
8867 mbedtls_mpi_free( &conf->dhm_P );
8868 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008869 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008871
8872 return( 0 );
8873}
Hanno Becker470a8c42017-10-04 15:28:46 +01008874#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
Hanno Beckera90658f2017-10-04 15:29:08 +01008876int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8877 const unsigned char *dhm_P, size_t P_len,
8878 const unsigned char *dhm_G, size_t G_len )
8879{
8880 int ret;
8881
8882 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8883 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8884 {
8885 mbedtls_mpi_free( &conf->dhm_P );
8886 mbedtls_mpi_free( &conf->dhm_G );
8887 return( ret );
8888 }
8889
8890 return( 0 );
8891}
Paul Bakker5121ce52009-01-03 21:22:43 +00008892
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008893int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008894{
8895 int ret;
8896
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008897 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8898 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8899 {
8900 mbedtls_mpi_free( &conf->dhm_P );
8901 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008902 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008903 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008904
8905 return( 0 );
8906}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008907#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008908
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008909#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8910/*
8911 * Set the minimum length for Diffie-Hellman parameters
8912 */
8913void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8914 unsigned int bitlen )
8915{
8916 conf->dhm_min_bitlen = bitlen;
8917}
8918#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8919
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008920#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008921/*
8922 * Set allowed/preferred hashes for handshake signatures
8923 */
8924void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8925 const int *hashes )
8926{
8927 conf->sig_hashes = hashes;
8928}
Hanno Becker947194e2017-04-07 13:25:49 +01008929#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008930
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008931#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008932/*
8933 * Set the allowed elliptic curves
8934 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008935void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008936 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008937{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008938 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008939}
Hanno Becker947194e2017-04-07 13:25:49 +01008940#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008941
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008942#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008944{
Hanno Becker947194e2017-04-07 13:25:49 +01008945 /* Initialize to suppress unnecessary compiler warning */
8946 size_t hostname_len = 0;
8947
8948 /* Check if new hostname is valid before
8949 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008950 if( hostname != NULL )
8951 {
8952 hostname_len = strlen( hostname );
8953
8954 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8956 }
8957
8958 /* Now it's clear that we will overwrite the old hostname,
8959 * so we can free it safely */
8960
8961 if( ssl->hostname != NULL )
8962 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008963 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008964 mbedtls_free( ssl->hostname );
8965 }
8966
8967 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008968
Paul Bakker5121ce52009-01-03 21:22:43 +00008969 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008970 {
8971 ssl->hostname = NULL;
8972 }
8973 else
8974 {
8975 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008976 if( ssl->hostname == NULL )
8977 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008978
Hanno Becker947194e2017-04-07 13:25:49 +01008979 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008980
Hanno Becker947194e2017-04-07 13:25:49 +01008981 ssl->hostname[hostname_len] = '\0';
8982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008983
8984 return( 0 );
8985}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008986#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008987
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008988#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008989void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008991 const unsigned char *, size_t),
8992 void *p_sni )
8993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008994 conf->f_sni = f_sni;
8995 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009000int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009001{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009002 size_t cur_len, tot_len;
9003 const char **p;
9004
9005 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009006 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9007 * MUST NOT be truncated."
9008 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009009 */
9010 tot_len = 0;
9011 for( p = protos; *p != NULL; p++ )
9012 {
9013 cur_len = strlen( *p );
9014 tot_len += cur_len;
9015
9016 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009018 }
9019
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009020 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009021
9022 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009023}
9024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009026{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009027 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009028}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009030
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009031void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009032{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009033 conf->max_major_ver = major;
9034 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009035}
9036
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009037void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009038{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009039 conf->min_major_ver = major;
9040 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009041}
9042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009044void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009045{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009046 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009047}
9048#endif
9049
Janos Follath088ce432017-04-10 12:42:31 +01009050#if defined(MBEDTLS_SSL_SRV_C)
9051void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9052 char cert_req_ca_list )
9053{
9054 conf->cert_req_ca_list = cert_req_ca_list;
9055}
9056#endif
9057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009059void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009060{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009061 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009062}
9063#endif
9064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009066void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009067{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009068 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009069}
9070#endif
9071
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009072#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009073void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009075 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009076}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009077#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009080int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009083 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009086 }
9087
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009088 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009089
9090 return( 0 );
9091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009095void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009096{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009097 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009102void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009103{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009104 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009105}
9106#endif
9107
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009108void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009109{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009110 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009111}
9112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009114void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009116 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009117}
9118
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009119void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009121 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009122}
9123
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009124void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009125 const unsigned char period[8] )
9126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009132#if defined(MBEDTLS_SSL_CLI_C)
9133void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009134{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009135 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009136}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009137#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009138
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009139#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009140void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9141 mbedtls_ssl_ticket_write_t *f_ticket_write,
9142 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9143 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009144{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009145 conf->f_ticket_write = f_ticket_write;
9146 conf->f_ticket_parse = f_ticket_parse;
9147 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009148}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009151
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009152#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9153void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9154 mbedtls_ssl_export_keys_t *f_export_keys,
9155 void *p_export_keys )
9156{
9157 conf->f_export_keys = f_export_keys;
9158 conf->p_export_keys = p_export_keys;
9159}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009160
9161void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9162 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9163 void *p_export_keys )
9164{
9165 conf->f_export_keys_ext = f_export_keys_ext;
9166 conf->p_export_keys = p_export_keys;
9167}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009168#endif
9169
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009170#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009171void mbedtls_ssl_conf_async_private_cb(
9172 mbedtls_ssl_config *conf,
9173 mbedtls_ssl_async_sign_t *f_async_sign,
9174 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9175 mbedtls_ssl_async_resume_t *f_async_resume,
9176 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009177 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009178{
9179 conf->f_async_sign_start = f_async_sign;
9180 conf->f_async_decrypt_start = f_async_decrypt;
9181 conf->f_async_resume = f_async_resume;
9182 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009183 conf->p_async_config_data = async_config_data;
9184}
9185
Gilles Peskine8f97af72018-04-26 11:46:10 +02009186void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9187{
9188 return( conf->p_async_config_data );
9189}
9190
Gilles Peskine1febfef2018-04-30 11:54:39 +02009191void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009192{
9193 if( ssl->handshake == NULL )
9194 return( NULL );
9195 else
9196 return( ssl->handshake->user_async_ctx );
9197}
9198
Gilles Peskine1febfef2018-04-30 11:54:39 +02009199void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009200 void *ctx )
9201{
9202 if( ssl->handshake != NULL )
9203 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009204}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009205#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009206
Paul Bakker5121ce52009-01-03 21:22:43 +00009207/*
9208 * SSL get accessors
9209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009211{
9212 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9213}
9214
Hanno Becker8b170a02017-10-10 11:51:19 +01009215int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9216{
9217 /*
9218 * Case A: We're currently holding back
9219 * a message for further processing.
9220 */
9221
9222 if( ssl->keep_current_message == 1 )
9223 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009225 return( 1 );
9226 }
9227
9228 /*
9229 * Case B: Further records are pending in the current datagram.
9230 */
9231
9232#if defined(MBEDTLS_SSL_PROTO_DTLS)
9233 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9234 ssl->in_left > ssl->next_record_offset )
9235 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009237 return( 1 );
9238 }
9239#endif /* MBEDTLS_SSL_PROTO_DTLS */
9240
9241 /*
9242 * Case C: A handshake message is being processed.
9243 */
9244
Hanno Becker8b170a02017-10-10 11:51:19 +01009245 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9246 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009248 return( 1 );
9249 }
9250
9251 /*
9252 * Case D: An application data message is being processed
9253 */
9254 if( ssl->in_offt != NULL )
9255 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009257 return( 1 );
9258 }
9259
9260 /*
9261 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009262 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009263 * we implement support for multiple alerts in single records.
9264 */
9265
9266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9267 return( 0 );
9268}
9269
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009270uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009271{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009272 if( ssl->session != NULL )
9273 return( ssl->session->verify_result );
9274
9275 if( ssl->session_negotiate != NULL )
9276 return( ssl->session_negotiate->verify_result );
9277
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009278 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009279}
9280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009282{
Paul Bakker926c8e42013-03-06 10:23:34 +01009283 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009284 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009287}
9288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009292 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009293 {
9294 switch( ssl->minor_ver )
9295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009297 return( "DTLSv1.0" );
9298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009300 return( "DTLSv1.2" );
9301
9302 default:
9303 return( "unknown (DTLS)" );
9304 }
9305 }
9306#endif
9307
Paul Bakker43ca69c2011-01-15 17:35:19 +00009308 switch( ssl->minor_ver )
9309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009311 return( "SSLv3.0" );
9312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009314 return( "TLSv1.0" );
9315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009317 return( "TLSv1.1" );
9318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009320 return( "TLSv1.2" );
9321
Paul Bakker43ca69c2011-01-15 17:35:19 +00009322 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009323 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009324 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009325}
9326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009328{
Hanno Becker3136ede2018-08-17 15:28:19 +01009329 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009331 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009332
Hanno Becker5903de42019-05-03 14:46:38 +01009333 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9334
Hanno Becker78640902018-08-13 16:35:15 +01009335 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009336 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338#if defined(MBEDTLS_ZLIB_SUPPORT)
9339 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9340 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009341#endif
9342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345 case MBEDTLS_MODE_GCM:
9346 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009347 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009349 transform_expansion = transform->minlen;
9350 break;
9351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009353
9354 block_size = mbedtls_cipher_get_block_size(
9355 &transform->cipher_ctx_enc );
9356
Hanno Becker3136ede2018-08-17 15:28:19 +01009357 /* Expansion due to the addition of the MAC. */
9358 transform_expansion += transform->maclen;
9359
9360 /* Expansion due to the addition of CBC padding;
9361 * Theoretically up to 256 bytes, but we never use
9362 * more than the block size of the underlying cipher. */
9363 transform_expansion += block_size;
9364
9365 /* For TLS 1.1 or higher, an explicit IV is added
9366 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009367#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9368 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009369 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009370#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009371
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009372 break;
9373
9374 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009377 }
9378
Hanno Becker6cbad552019-05-08 15:40:11 +01009379#if defined(MBEDTLS_SSL_CID)
9380 if( transform->out_cid_len != 0 )
9381 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
9382#endif /* MBEDTLS_SSL_CID */
9383
Hanno Becker5903de42019-05-03 14:46:38 +01009384 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009385}
9386
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009387#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9388size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9389{
9390 size_t max_len;
9391
9392 /*
9393 * Assume mfl_code is correct since it was checked when set
9394 */
Angus Grattond8213d02016-05-25 20:56:48 +10009395 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009396
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009397 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009398 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009399 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009400 {
Angus Grattond8213d02016-05-25 20:56:48 +10009401 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009402 }
9403
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009404 /* During a handshake, use the value being negotiated */
9405 if( ssl->session_negotiate != NULL &&
9406 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9407 {
9408 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9409 }
9410
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009411 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009412}
9413#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9414
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009415#if defined(MBEDTLS_SSL_PROTO_DTLS)
9416static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9417{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009418 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9419 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9420 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9421 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9422 return ( 0 );
9423
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009424 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9425 return( ssl->mtu );
9426
9427 if( ssl->mtu == 0 )
9428 return( ssl->handshake->mtu );
9429
9430 return( ssl->mtu < ssl->handshake->mtu ?
9431 ssl->mtu : ssl->handshake->mtu );
9432}
9433#endif /* MBEDTLS_SSL_PROTO_DTLS */
9434
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009435int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9436{
9437 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9438
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009439#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9440 !defined(MBEDTLS_SSL_PROTO_DTLS)
9441 (void) ssl;
9442#endif
9443
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009444#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9445 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9446
9447 if( max_len > mfl )
9448 max_len = mfl;
9449#endif
9450
9451#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009452 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009453 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009454 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009455 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9456 const size_t overhead = (size_t) ret;
9457
9458 if( ret < 0 )
9459 return( ret );
9460
9461 if( mtu <= overhead )
9462 {
9463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9464 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9465 }
9466
9467 if( max_len > mtu - overhead )
9468 max_len = mtu - overhead;
9469 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009470#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009471
Hanno Becker0defedb2018-08-10 12:35:02 +01009472#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9473 !defined(MBEDTLS_SSL_PROTO_DTLS)
9474 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009475#endif
9476
9477 return( (int) max_len );
9478}
9479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480#if defined(MBEDTLS_X509_CRT_PARSE_C)
9481const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009482{
9483 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009484 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009485
Hanno Beckere6824572019-02-07 13:18:46 +00009486#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009487 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009488#else
9489 return( NULL );
9490#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009495int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9496 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009497{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009498 if( ssl == NULL ||
9499 dst == NULL ||
9500 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009501 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009504 }
9505
Hanno Becker52055ae2019-02-06 14:30:46 +00009506 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009509
Paul Bakker5121ce52009-01-03 21:22:43 +00009510/*
Paul Bakker1961b702013-01-25 14:49:24 +01009511 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009516
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009517 if( ssl == NULL || ssl->conf == NULL )
9518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009521 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009525 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009527#endif
9528
Paul Bakker1961b702013-01-25 14:49:24 +01009529 return( ret );
9530}
9531
9532/*
9533 * Perform the SSL handshake
9534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009536{
9537 int ret = 0;
9538
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009539 if( ssl == NULL || ssl->conf == NULL )
9540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009547
9548 if( ret != 0 )
9549 break;
9550 }
9551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009553
9554 return( ret );
9555}
9556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557#if defined(MBEDTLS_SSL_RENEGOTIATION)
9558#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009559/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009560 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009563{
9564 int ret;
9565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009567
9568 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9570 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009571
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009572 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009573 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009575 return( ret );
9576 }
9577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009579
9580 return( 0 );
9581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009583
9584/*
9585 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 * - any side: calling mbedtls_ssl_renegotiate(),
9587 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9588 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009589 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009590 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009594{
9595 int ret;
9596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009598
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009599 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9600 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009601
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009602 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9603 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009605 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009607 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009609 ssl->handshake->out_msg_seq = 1;
9610 else
9611 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009612 }
9613#endif
9614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9616 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009621 return( ret );
9622 }
9623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009625
9626 return( 0 );
9627}
9628
9629/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009630 * Renegotiate current connection on client,
9631 * or request renegotiation on server
9632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009636
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009637 if( ssl == NULL || ssl->conf == NULL )
9638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009641 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009648
9649 /* Did we already try/start sending HelloRequest? */
9650 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009652
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009653 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009654 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009658 /*
9659 * On client, either start the renegotiation process or,
9660 * if already in progress, continue the handshake
9661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009666
9667 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670 return( ret );
9671 }
9672 }
9673 else
9674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009678 return( ret );
9679 }
9680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009683 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009684}
9685
9686/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009687 * Check record counters and renegotiate if they're above the limit.
9688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009690{
Andres AG2196c7f2016-12-15 17:01:16 +00009691 size_t ep_len = ssl_ep_len( ssl );
9692 int in_ctr_cmp;
9693 int out_ctr_cmp;
9694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9696 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009697 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009698 {
9699 return( 0 );
9700 }
9701
Andres AG2196c7f2016-12-15 17:01:16 +00009702 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9703 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009704 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009705 ssl->conf->renego_period + ep_len, 8 - ep_len );
9706
9707 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009708 {
9709 return( 0 );
9710 }
9711
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009716
9717/*
9718 * Receive application data decrypted from the SSL layer
9719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009721{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009722 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009723 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009724
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009725 if( ssl == NULL || ssl->conf == NULL )
9726 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009731 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009734 return( ret );
9735
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009736 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009738 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009739 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009740 return( ret );
9741 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009742 }
9743#endif
9744
Hanno Becker4a810fb2017-05-24 16:27:30 +01009745 /*
9746 * Check if renegotiation is necessary and/or handshake is
9747 * in process. If yes, perform/continue, and fall through
9748 * if an unexpected packet is received while the client
9749 * is waiting for the ServerHello.
9750 *
9751 * (There is no equivalent to the last condition on
9752 * the server-side as it is not treated as within
9753 * a handshake while waiting for the ClientHello
9754 * after a renegotiation request.)
9755 */
9756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009758 ret = ssl_check_ctr_renegotiate( ssl );
9759 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9760 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009763 return( ret );
9764 }
9765#endif
9766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009770 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9771 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009774 return( ret );
9775 }
9776 }
9777
Hanno Beckere41158b2017-10-23 13:30:32 +01009778 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009779 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009780 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009781 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009782 if( ssl->f_get_timer != NULL &&
9783 ssl->f_get_timer( ssl->p_timer ) == -1 )
9784 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009785 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009786 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009787
Hanno Becker327c93b2018-08-15 13:56:18 +01009788 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009789 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009790 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9791 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009792
Hanno Becker4a810fb2017-05-24 16:27:30 +01009793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9794 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009795 }
9796
9797 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009799 {
9800 /*
9801 * OpenSSL sends empty messages to randomize the IV
9802 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009803 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009806 return( 0 );
9807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009809 return( ret );
9810 }
9811 }
9812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009816
Hanno Becker4a810fb2017-05-24 16:27:30 +01009817 /*
9818 * - For client-side, expect SERVER_HELLO_REQUEST.
9819 * - For server-side, expect CLIENT_HELLO.
9820 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9821 */
9822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009824 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009826 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009829
9830 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009833 {
9834 continue;
9835 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009836#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009838 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009839#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009840
Hanno Becker4a810fb2017-05-24 16:27:30 +01009841#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009842 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009846
9847 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009849 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009850 {
9851 continue;
9852 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009855 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009856#endif /* MBEDTLS_SSL_SRV_C */
9857
Hanno Becker21df7f92017-10-17 11:03:26 +01009858#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009859 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009860 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9861 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9862 ssl->conf->allow_legacy_renegotiation ==
9863 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9864 {
9865 /*
9866 * Accept renegotiation request
9867 */
Paul Bakker48916f92012-09-16 19:57:18 +00009868
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009869 /* DTLS clients need to know renego is server-initiated */
9870#if defined(MBEDTLS_SSL_PROTO_DTLS)
9871 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9872 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9873 {
9874 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9875 }
9876#endif
9877 ret = ssl_start_renegotiation( ssl );
9878 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9879 ret != 0 )
9880 {
9881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9882 return( ret );
9883 }
9884 }
9885 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009886#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009887 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009888 /*
9889 * Refuse renegotiation
9890 */
9891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894#if defined(MBEDTLS_SSL_PROTO_SSL3)
9895 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009896 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009897 /* SSLv3 does not have a "no_renegotiation" warning, so
9898 we send a fatal alert and abort the connection. */
9899 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9900 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9901 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009902 }
9903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9905#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9906 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9907 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9910 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9911 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009912 {
9913 return( ret );
9914 }
Paul Bakker48916f92012-09-16 19:57:18 +00009915 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009916 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9918 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009922 }
Paul Bakker48916f92012-09-16 19:57:18 +00009923 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009924
Hanno Becker90333da2017-10-10 11:27:13 +01009925 /* At this point, we don't know whether the renegotiation has been
9926 * completed or not. The cases to consider are the following:
9927 * 1) The renegotiation is complete. In this case, no new record
9928 * has been read yet.
9929 * 2) The renegotiation is incomplete because the client received
9930 * an application data record while awaiting the ServerHello.
9931 * 3) The renegotiation is incomplete because the client received
9932 * a non-handshake, non-application data message while awaiting
9933 * the ServerHello.
9934 * In each of these case, looping will be the proper action:
9935 * - For 1), the next iteration will read a new record and check
9936 * if it's application data.
9937 * - For 2), the loop condition isn't satisfied as application data
9938 * is present, hence continue is the same as break
9939 * - For 3), the loop condition is satisfied and read_record
9940 * will re-deliver the message that was held back by the client
9941 * when expecting the ServerHello.
9942 */
9943 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009944 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009945#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009947 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009948 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009949 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009950 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009953 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009955 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009956 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9961 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009964 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009965 }
9966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9970 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009971 }
9972
9973 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009974
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009975 /* We're going to return something now, cancel timer,
9976 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009978 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009979
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009981 /* If we requested renego but received AppData, resend HelloRequest.
9982 * Do it now, after setting in_offt, to avoid taking this branch
9983 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009985 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009987 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009988 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009991 return( ret );
9992 }
9993 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009995#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009996 }
9997
9998 n = ( len < ssl->in_msglen )
9999 ? len : ssl->in_msglen;
10000
10001 memcpy( buf, ssl->in_offt, n );
10002 ssl->in_msglen -= n;
10003
10004 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010005 {
10006 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010007 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010008 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010009 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010010 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010011 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010012 /* more data available */
10013 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010014 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010017
Paul Bakker23986e52011-04-24 08:57:21 +000010018 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010019}
10020
10021/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010022 * Send application data to be encrypted by the SSL layer, taking care of max
10023 * fragment length and buffer size.
10024 *
10025 * According to RFC 5246 Section 6.2.1:
10026 *
10027 * Zero-length fragments of Application data MAY be sent as they are
10028 * potentially useful as a traffic analysis countermeasure.
10029 *
10030 * Therefore, it is possible that the input message length is 0 and the
10031 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010032 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010033static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010034 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010035{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010036 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10037 const size_t max_len = (size_t) ret;
10038
10039 if( ret < 0 )
10040 {
10041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10042 return( ret );
10043 }
10044
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010045 if( len > max_len )
10046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010051 "maximum fragment length: %d > %d",
10052 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010054 }
10055 else
10056#endif
10057 len = max_len;
10058 }
Paul Bakker887bd502011-06-08 13:10:54 +000010059
Paul Bakker5121ce52009-01-03 21:22:43 +000010060 if( ssl->out_left != 0 )
10061 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010062 /*
10063 * The user has previously tried to send the data and
10064 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10065 * written. In this case, we expect the high-level write function
10066 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010071 return( ret );
10072 }
10073 }
Paul Bakker887bd502011-06-08 13:10:54 +000010074 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010075 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010076 /*
10077 * The user is trying to send a message the first time, so we need to
10078 * copy the data into the internal buffers and setup the data structure
10079 * to keep track of partial writes
10080 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010081 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010083 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010084
Hanno Becker67bc7c32018-08-06 11:33:50 +010010085 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010088 return( ret );
10089 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010090 }
10091
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010092 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010093}
10094
10095/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010096 * Write application data, doing 1/n-1 splitting if necessary.
10097 *
10098 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010099 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010100 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010103static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010104 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010105{
10106 int ret;
10107
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010108 if( ssl->conf->cbc_record_splitting ==
10109 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010110 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10112 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10113 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010114 {
10115 return( ssl_write_real( ssl, buf, len ) );
10116 }
10117
10118 if( ssl->split_done == 0 )
10119 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010120 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010121 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010122 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010123 }
10124
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010125 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10126 return( ret );
10127 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010128
10129 return( ret + 1 );
10130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010132
10133/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010134 * Write application data (public-facing wrapper)
10135 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010136int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010137{
10138 int ret;
10139
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010141
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010142 if( ssl == NULL || ssl->conf == NULL )
10143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10144
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010145#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010146 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10147 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010148 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010149 return( ret );
10150 }
10151#endif
10152
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010153 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010154 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010155 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010156 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010158 return( ret );
10159 }
10160 }
10161
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010162#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010163 ret = ssl_write_split( ssl, buf, len );
10164#else
10165 ret = ssl_write_real( ssl, buf, len );
10166#endif
10167
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010169
10170 return( ret );
10171}
10172
10173/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010174 * Notify the peer that the connection is being closed
10175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010177{
10178 int ret;
10179
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010180 if( ssl == NULL || ssl->conf == NULL )
10181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010184
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010185 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10191 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10192 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010195 return( ret );
10196 }
10197 }
10198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010200
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010201 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010202}
10203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010205{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010206 if( transform == NULL )
10207 return;
10208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010210 deflateEnd( &transform->ctx_deflate );
10211 inflateEnd( &transform->ctx_inflate );
10212#endif
10213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10215 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010216
Hanno Beckerd56ed242018-01-03 15:32:51 +000010217#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 mbedtls_md_free( &transform->md_ctx_enc );
10219 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010220#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010221
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010222 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010223}
10224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225#if defined(MBEDTLS_X509_CRT_PARSE_C)
10226static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010229
10230 while( cur != NULL )
10231 {
10232 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010234 cur = next;
10235 }
10236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010238
Hanno Becker0271f962018-08-16 13:23:47 +010010239#if defined(MBEDTLS_SSL_PROTO_DTLS)
10240
10241static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10242{
10243 unsigned offset;
10244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10245
10246 if( hs == NULL )
10247 return;
10248
Hanno Becker283f5ef2018-08-24 09:34:47 +010010249 ssl_free_buffered_record( ssl );
10250
Hanno Becker0271f962018-08-16 13:23:47 +010010251 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010252 ssl_buffering_free_slot( ssl, offset );
10253}
10254
10255static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10256 uint8_t slot )
10257{
10258 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10259 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010260
10261 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10262 return;
10263
Hanno Beckere605b192018-08-21 15:59:07 +010010264 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010265 {
Hanno Beckere605b192018-08-21 15:59:07 +010010266 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010267 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010268 mbedtls_free( hs_buf->data );
10269 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010270 }
10271}
10272
10273#endif /* MBEDTLS_SSL_PROTO_DTLS */
10274
Gilles Peskine9b562d52018-04-25 20:32:43 +020010275void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010276{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010277 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10278
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010279 if( handshake == NULL )
10280 return;
10281
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010282#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10283 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10284 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010285 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010286 handshake->async_in_progress = 0;
10287 }
10288#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10289
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010290#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10291 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10292 mbedtls_md5_free( &handshake->fin_md5 );
10293 mbedtls_sha1_free( &handshake->fin_sha1 );
10294#endif
10295#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10296#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010297#if defined(MBEDTLS_USE_PSA_CRYPTO)
10298 psa_hash_abort( &handshake->fin_sha256_psa );
10299#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010300 mbedtls_sha256_free( &handshake->fin_sha256 );
10301#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010302#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010303#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010304#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010305 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010306#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010307 mbedtls_sha512_free( &handshake->fin_sha512 );
10308#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010309#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010310#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312#if defined(MBEDTLS_DHM_C)
10313 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315#if defined(MBEDTLS_ECDH_C)
10316 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010317#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010318#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010319 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010320#if defined(MBEDTLS_SSL_CLI_C)
10321 mbedtls_free( handshake->ecjpake_cache );
10322 handshake->ecjpake_cache = NULL;
10323 handshake->ecjpake_cache_len = 0;
10324#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010325#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010326
Janos Follath4ae5c292016-02-10 11:27:43 +000010327#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10328 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010329 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010331#endif
10332
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010333#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10334 if( handshake->psk != NULL )
10335 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010336 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010337 mbedtls_free( handshake->psk );
10338 }
10339#endif
10340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10342 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010343 /*
10344 * Free only the linked list wrapper, not the keys themselves
10345 * since the belong to the SNI callback
10346 */
10347 if( handshake->sni_key_cert != NULL )
10348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010350
10351 while( cur != NULL )
10352 {
10353 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010355 cur = next;
10356 }
10357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010359
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010360#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010361 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010362 if( handshake->ecrs_peer_cert != NULL )
10363 {
10364 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10365 mbedtls_free( handshake->ecrs_peer_cert );
10366 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010367#endif
10368
Hanno Becker75173122019-02-06 16:18:31 +000010369#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10370 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10371 mbedtls_pk_free( &handshake->peer_pubkey );
10372#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374#if defined(MBEDTLS_SSL_PROTO_DTLS)
10375 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010376 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010377 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010378#endif
10379
Hanno Becker4a63ed42019-01-08 11:39:35 +000010380#if defined(MBEDTLS_ECDH_C) && \
10381 defined(MBEDTLS_USE_PSA_CRYPTO)
10382 psa_destroy_key( handshake->ecdh_psa_privkey );
10383#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10384
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010385 mbedtls_platform_zeroize( handshake,
10386 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010387}
10388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010390{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010391 if( session == NULL )
10392 return;
10393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010395 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010396#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010397
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010398#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010400#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010401
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010402 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010403}
10404
Paul Bakker5121ce52009-01-03 21:22:43 +000010405/*
10406 * Free an SSL context
10407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010409{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010410 if( ssl == NULL )
10411 return;
10412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010414
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010415 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010416 {
Angus Grattond8213d02016-05-25 20:56:48 +100010417 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010419 }
10420
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010421 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010422 {
Angus Grattond8213d02016-05-25 20:56:48 +100010423 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010425 }
10426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010428 if( ssl->compress_buf != NULL )
10429 {
Angus Grattond8213d02016-05-25 20:56:48 +100010430 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010432 }
10433#endif
10434
Paul Bakker48916f92012-09-16 19:57:18 +000010435 if( ssl->transform )
10436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 mbedtls_ssl_transform_free( ssl->transform );
10438 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010439 }
10440
10441 if( ssl->handshake )
10442 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010443 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10445 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447 mbedtls_free( ssl->handshake );
10448 mbedtls_free( ssl->transform_negotiate );
10449 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010450 }
10451
Paul Bakkerc0463502013-02-14 11:19:38 +010010452 if( ssl->session )
10453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454 mbedtls_ssl_session_free( ssl->session );
10455 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010456 }
10457
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010458#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010459 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010460 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010461 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010463 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010464#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10467 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10470 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010471 }
10472#endif
10473
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010474#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010476#endif
10477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010479
Paul Bakker86f04f42013-02-14 11:20:09 +010010480 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010481 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010482}
10483
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010484/*
10485 * Initialze mbedtls_ssl_config
10486 */
10487void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10488{
10489 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10490}
10491
Simon Butcherc97b6972015-12-27 23:48:17 +000010492#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010493static int ssl_preset_default_hashes[] = {
10494#if defined(MBEDTLS_SHA512_C)
10495 MBEDTLS_MD_SHA512,
10496 MBEDTLS_MD_SHA384,
10497#endif
10498#if defined(MBEDTLS_SHA256_C)
10499 MBEDTLS_MD_SHA256,
10500 MBEDTLS_MD_SHA224,
10501#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010502#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010503 MBEDTLS_MD_SHA1,
10504#endif
10505 MBEDTLS_MD_NONE
10506};
Simon Butcherc97b6972015-12-27 23:48:17 +000010507#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010508
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010509static int ssl_preset_suiteb_ciphersuites[] = {
10510 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10511 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10512 0
10513};
10514
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010515#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010516static int ssl_preset_suiteb_hashes[] = {
10517 MBEDTLS_MD_SHA256,
10518 MBEDTLS_MD_SHA384,
10519 MBEDTLS_MD_NONE
10520};
10521#endif
10522
10523#if defined(MBEDTLS_ECP_C)
10524static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10525 MBEDTLS_ECP_DP_SECP256R1,
10526 MBEDTLS_ECP_DP_SECP384R1,
10527 MBEDTLS_ECP_DP_NONE
10528};
10529#endif
10530
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010531/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010532 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010533 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010534int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010535 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010536{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010537#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010538 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010539#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010540
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010541 /* Use the functions here so that they are covered in tests,
10542 * but otherwise access member directly for efficiency */
10543 mbedtls_ssl_conf_endpoint( conf, endpoint );
10544 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010545
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010546 /*
10547 * Things that are common to all presets
10548 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010549#if defined(MBEDTLS_SSL_CLI_C)
10550 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10551 {
10552 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10553#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10554 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10555#endif
10556 }
10557#endif
10558
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010559#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010560 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010561#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010562
10563#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10564 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10565#endif
10566
10567#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10568 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10569#endif
10570
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010571#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10572 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10573#endif
10574
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010575#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010576 conf->f_cookie_write = ssl_cookie_write_dummy;
10577 conf->f_cookie_check = ssl_cookie_check_dummy;
10578#endif
10579
10580#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10581 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10582#endif
10583
Janos Follath088ce432017-04-10 12:42:31 +010010584#if defined(MBEDTLS_SSL_SRV_C)
10585 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10586#endif
10587
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010588#if defined(MBEDTLS_SSL_PROTO_DTLS)
10589 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10590 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10591#endif
10592
10593#if defined(MBEDTLS_SSL_RENEGOTIATION)
10594 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010595 memset( conf->renego_period, 0x00, 2 );
10596 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010597#endif
10598
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010599#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10600 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10601 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010602 const unsigned char dhm_p[] =
10603 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10604 const unsigned char dhm_g[] =
10605 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10606
Hanno Beckera90658f2017-10-04 15:29:08 +010010607 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10608 dhm_p, sizeof( dhm_p ),
10609 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010610 {
10611 return( ret );
10612 }
10613 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010614#endif
10615
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010616 /*
10617 * Preset-specific defaults
10618 */
10619 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010620 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010621 /*
10622 * NSA Suite B
10623 */
10624 case MBEDTLS_SSL_PRESET_SUITEB:
10625 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10626 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10627 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10628 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10629
10630 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10631 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10632 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10633 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10634 ssl_preset_suiteb_ciphersuites;
10635
10636#if defined(MBEDTLS_X509_CRT_PARSE_C)
10637 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010638#endif
10639
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010640#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010641 conf->sig_hashes = ssl_preset_suiteb_hashes;
10642#endif
10643
10644#if defined(MBEDTLS_ECP_C)
10645 conf->curve_list = ssl_preset_suiteb_curves;
10646#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010647 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010648
10649 /*
10650 * Default
10651 */
10652 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010653 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10654 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10655 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10656 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10657 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10658 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10659 MBEDTLS_SSL_MIN_MINOR_VERSION :
10660 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010661 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10662 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10663
10664#if defined(MBEDTLS_SSL_PROTO_DTLS)
10665 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10666 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10667#endif
10668
10669 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10670 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10671 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10672 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10673 mbedtls_ssl_list_ciphersuites();
10674
10675#if defined(MBEDTLS_X509_CRT_PARSE_C)
10676 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10677#endif
10678
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010679#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010680 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010681#endif
10682
10683#if defined(MBEDTLS_ECP_C)
10684 conf->curve_list = mbedtls_ecp_grp_id_list();
10685#endif
10686
10687#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10688 conf->dhm_min_bitlen = 1024;
10689#endif
10690 }
10691
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010692 return( 0 );
10693}
10694
10695/*
10696 * Free mbedtls_ssl_config
10697 */
10698void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10699{
10700#if defined(MBEDTLS_DHM_C)
10701 mbedtls_mpi_free( &conf->dhm_P );
10702 mbedtls_mpi_free( &conf->dhm_G );
10703#endif
10704
10705#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10706 if( conf->psk != NULL )
10707 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010708 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010709 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010710 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010711 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010712 }
10713
10714 if( conf->psk_identity != NULL )
10715 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010716 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010717 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010718 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010719 conf->psk_identity_len = 0;
10720 }
10721#endif
10722
10723#if defined(MBEDTLS_X509_CRT_PARSE_C)
10724 ssl_key_cert_free( conf->key_cert );
10725#endif
10726
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010727 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010728}
10729
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010730#if defined(MBEDTLS_PK_C) && \
10731 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010732/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737#if defined(MBEDTLS_RSA_C)
10738 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10739 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741#if defined(MBEDTLS_ECDSA_C)
10742 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10743 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010746}
10747
Hanno Becker7e5437a2017-04-28 17:15:26 +010010748unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10749{
10750 switch( type ) {
10751 case MBEDTLS_PK_RSA:
10752 return( MBEDTLS_SSL_SIG_RSA );
10753 case MBEDTLS_PK_ECDSA:
10754 case MBEDTLS_PK_ECKEY:
10755 return( MBEDTLS_SSL_SIG_ECDSA );
10756 default:
10757 return( MBEDTLS_SSL_SIG_ANON );
10758 }
10759}
10760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010762{
10763 switch( sig )
10764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765#if defined(MBEDTLS_RSA_C)
10766 case MBEDTLS_SSL_SIG_RSA:
10767 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769#if defined(MBEDTLS_ECDSA_C)
10770 case MBEDTLS_SSL_SIG_ECDSA:
10771 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010772#endif
10773 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010775 }
10776}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010777#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010778
Hanno Becker7e5437a2017-04-28 17:15:26 +010010779#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10780 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10781
10782/* Find an entry in a signature-hash set matching a given hash algorithm. */
10783mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10784 mbedtls_pk_type_t sig_alg )
10785{
10786 switch( sig_alg )
10787 {
10788 case MBEDTLS_PK_RSA:
10789 return( set->rsa );
10790 case MBEDTLS_PK_ECDSA:
10791 return( set->ecdsa );
10792 default:
10793 return( MBEDTLS_MD_NONE );
10794 }
10795}
10796
10797/* Add a signature-hash-pair to a signature-hash set */
10798void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10799 mbedtls_pk_type_t sig_alg,
10800 mbedtls_md_type_t md_alg )
10801{
10802 switch( sig_alg )
10803 {
10804 case MBEDTLS_PK_RSA:
10805 if( set->rsa == MBEDTLS_MD_NONE )
10806 set->rsa = md_alg;
10807 break;
10808
10809 case MBEDTLS_PK_ECDSA:
10810 if( set->ecdsa == MBEDTLS_MD_NONE )
10811 set->ecdsa = md_alg;
10812 break;
10813
10814 default:
10815 break;
10816 }
10817}
10818
10819/* Allow exactly one hash algorithm for each signature. */
10820void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10821 mbedtls_md_type_t md_alg )
10822{
10823 set->rsa = md_alg;
10824 set->ecdsa = md_alg;
10825}
10826
10827#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10828 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10829
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010830/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010831 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010834{
10835 switch( hash )
10836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837#if defined(MBEDTLS_MD5_C)
10838 case MBEDTLS_SSL_HASH_MD5:
10839 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841#if defined(MBEDTLS_SHA1_C)
10842 case MBEDTLS_SSL_HASH_SHA1:
10843 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845#if defined(MBEDTLS_SHA256_C)
10846 case MBEDTLS_SSL_HASH_SHA224:
10847 return( MBEDTLS_MD_SHA224 );
10848 case MBEDTLS_SSL_HASH_SHA256:
10849 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851#if defined(MBEDTLS_SHA512_C)
10852 case MBEDTLS_SSL_HASH_SHA384:
10853 return( MBEDTLS_MD_SHA384 );
10854 case MBEDTLS_SSL_HASH_SHA512:
10855 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010856#endif
10857 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010859 }
10860}
10861
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010862/*
10863 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10864 */
10865unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10866{
10867 switch( md )
10868 {
10869#if defined(MBEDTLS_MD5_C)
10870 case MBEDTLS_MD_MD5:
10871 return( MBEDTLS_SSL_HASH_MD5 );
10872#endif
10873#if defined(MBEDTLS_SHA1_C)
10874 case MBEDTLS_MD_SHA1:
10875 return( MBEDTLS_SSL_HASH_SHA1 );
10876#endif
10877#if defined(MBEDTLS_SHA256_C)
10878 case MBEDTLS_MD_SHA224:
10879 return( MBEDTLS_SSL_HASH_SHA224 );
10880 case MBEDTLS_MD_SHA256:
10881 return( MBEDTLS_SSL_HASH_SHA256 );
10882#endif
10883#if defined(MBEDTLS_SHA512_C)
10884 case MBEDTLS_MD_SHA384:
10885 return( MBEDTLS_SSL_HASH_SHA384 );
10886 case MBEDTLS_MD_SHA512:
10887 return( MBEDTLS_SSL_HASH_SHA512 );
10888#endif
10889 default:
10890 return( MBEDTLS_SSL_HASH_NONE );
10891 }
10892}
10893
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010894#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010895/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010896 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010897 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010898 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010899int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010902
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010903 if( ssl->conf->curve_list == NULL )
10904 return( -1 );
10905
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010906 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010907 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010908 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010909
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010910 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010911}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010912#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010913
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010914#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010915/*
10916 * Check if a hash proposed by the peer is in our list.
10917 * Return 0 if we're willing to use it, -1 otherwise.
10918 */
10919int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10920 mbedtls_md_type_t md )
10921{
10922 const int *cur;
10923
10924 if( ssl->conf->sig_hashes == NULL )
10925 return( -1 );
10926
10927 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10928 if( *cur == (int) md )
10929 return( 0 );
10930
10931 return( -1 );
10932}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010933#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935#if defined(MBEDTLS_X509_CRT_PARSE_C)
10936int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10937 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010938 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010939 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010940{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010941 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010943 int usage = 0;
10944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010946 const char *ext_oid;
10947 size_t ext_len;
10948#endif
10949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10951 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010952 ((void) cert);
10953 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010954 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010955#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10958 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010959 {
10960 /* Server part of the key exchange */
10961 switch( ciphersuite->key_exchange )
10962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963 case MBEDTLS_KEY_EXCHANGE_RSA:
10964 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010965 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010966 break;
10967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10969 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10970 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10971 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010972 break;
10973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10975 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010976 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010977 break;
10978
10979 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010980 case MBEDTLS_KEY_EXCHANGE_NONE:
10981 case MBEDTLS_KEY_EXCHANGE_PSK:
10982 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10983 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010984 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010985 usage = 0;
10986 }
10987 }
10988 else
10989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10991 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010992 }
10993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010995 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010996 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010997 ret = -1;
10998 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010999#else
11000 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11004 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11007 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011008 }
11009 else
11010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011011 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11012 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011013 }
11014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011016 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011017 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011018 ret = -1;
11019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011021
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011022 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011023}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011025
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011026/*
11027 * Convert version numbers to/from wire format
11028 * and, for DTLS, to/from TLS equivalent.
11029 *
11030 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011031 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011032 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11033 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011036 unsigned char ver[2] )
11037{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038#if defined(MBEDTLS_SSL_PROTO_DTLS)
11039 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011042 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11043
11044 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11045 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11046 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011047 else
11048#else
11049 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011050#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011051 {
11052 ver[0] = (unsigned char) major;
11053 ver[1] = (unsigned char) minor;
11054 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011055}
11056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011058 const unsigned char ver[2] )
11059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060#if defined(MBEDTLS_SSL_PROTO_DTLS)
11061 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011062 {
11063 *major = 255 - ver[0] + 2;
11064 *minor = 255 - ver[1] + 1;
11065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011067 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11068 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011069 else
11070#else
11071 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011072#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011073 {
11074 *major = ver[0];
11075 *minor = ver[1];
11076 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011077}
11078
Simon Butcher99000142016-10-13 17:21:01 +010011079int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11080{
11081#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11082 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11083 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11084
11085 switch( md )
11086 {
11087#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11088#if defined(MBEDTLS_MD5_C)
11089 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011090 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011091#endif
11092#if defined(MBEDTLS_SHA1_C)
11093 case MBEDTLS_SSL_HASH_SHA1:
11094 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11095 break;
11096#endif
11097#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11098#if defined(MBEDTLS_SHA512_C)
11099 case MBEDTLS_SSL_HASH_SHA384:
11100 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11101 break;
11102#endif
11103#if defined(MBEDTLS_SHA256_C)
11104 case MBEDTLS_SSL_HASH_SHA256:
11105 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11106 break;
11107#endif
11108 default:
11109 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11110 }
11111
11112 return 0;
11113#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11114 (void) ssl;
11115 (void) md;
11116
11117 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11118#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11119}
11120
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011121#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11122 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11123int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11124 unsigned char *output,
11125 unsigned char *data, size_t data_len )
11126{
11127 int ret = 0;
11128 mbedtls_md5_context mbedtls_md5;
11129 mbedtls_sha1_context mbedtls_sha1;
11130
11131 mbedtls_md5_init( &mbedtls_md5 );
11132 mbedtls_sha1_init( &mbedtls_sha1 );
11133
11134 /*
11135 * digitally-signed struct {
11136 * opaque md5_hash[16];
11137 * opaque sha_hash[20];
11138 * };
11139 *
11140 * md5_hash
11141 * MD5(ClientHello.random + ServerHello.random
11142 * + ServerParams);
11143 * sha_hash
11144 * SHA(ClientHello.random + ServerHello.random
11145 * + ServerParams);
11146 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011147 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011148 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011150 goto exit;
11151 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011152 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011153 ssl->handshake->randbytes, 64 ) ) != 0 )
11154 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011156 goto exit;
11157 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011158 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011159 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011160 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011161 goto exit;
11162 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011163 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011164 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011166 goto exit;
11167 }
11168
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011169 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011170 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 goto exit;
11173 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011174 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 ssl->handshake->randbytes, 64 ) ) != 0 )
11176 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011178 goto exit;
11179 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011180 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011181 data_len ) ) != 0 )
11182 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011184 goto exit;
11185 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011186 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011187 output + 16 ) ) != 0 )
11188 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011190 goto exit;
11191 }
11192
11193exit:
11194 mbedtls_md5_free( &mbedtls_md5 );
11195 mbedtls_sha1_free( &mbedtls_sha1 );
11196
11197 if( ret != 0 )
11198 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11199 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11200
11201 return( ret );
11202
11203}
11204#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11205 MBEDTLS_SSL_PROTO_TLS1_1 */
11206
11207#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11208 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011209
11210#if defined(MBEDTLS_USE_PSA_CRYPTO)
11211int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11212 unsigned char *hash, size_t *hashlen,
11213 unsigned char *data, size_t data_len,
11214 mbedtls_md_type_t md_alg )
11215{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011216 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011217 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011218 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11219
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011221
11222 if( ( status = psa_hash_setup( &hash_operation,
11223 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011224 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011225 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011226 goto exit;
11227 }
11228
Andrzej Kurek814feff2019-01-14 04:35:19 -050011229 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11230 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011231 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011232 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011233 goto exit;
11234 }
11235
Andrzej Kurek814feff2019-01-14 04:35:19 -050011236 if( ( status = psa_hash_update( &hash_operation,
11237 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011238 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011239 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011240 goto exit;
11241 }
11242
Andrzej Kurek814feff2019-01-14 04:35:19 -050011243 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11244 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011245 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011246 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 goto exit;
11248 }
11249
11250exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011251 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011252 {
11253 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11254 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011255 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 {
11257 case PSA_ERROR_NOT_SUPPORTED:
11258 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011259 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011260 case PSA_ERROR_BUFFER_TOO_SMALL:
11261 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11262 case PSA_ERROR_INSUFFICIENT_MEMORY:
11263 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11264 default:
11265 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11266 }
11267 }
11268 return( 0 );
11269}
11270
11271#else
11272
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011273int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011274 unsigned char *hash, size_t *hashlen,
11275 unsigned char *data, size_t data_len,
11276 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011277{
11278 int ret = 0;
11279 mbedtls_md_context_t ctx;
11280 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011281 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011282
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011284
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011285 mbedtls_md_init( &ctx );
11286
11287 /*
11288 * digitally-signed struct {
11289 * opaque client_random[32];
11290 * opaque server_random[32];
11291 * ServerDHParams params;
11292 * };
11293 */
11294 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11295 {
11296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11297 goto exit;
11298 }
11299 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11300 {
11301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11302 goto exit;
11303 }
11304 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11305 {
11306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11307 goto exit;
11308 }
11309 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11310 {
11311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11312 goto exit;
11313 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011314 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011315 {
11316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11317 goto exit;
11318 }
11319
11320exit:
11321 mbedtls_md_free( &ctx );
11322
11323 if( ret != 0 )
11324 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11325 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11326
11327 return( ret );
11328}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011329#endif /* MBEDTLS_USE_PSA_CRYPTO */
11330
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11332 MBEDTLS_SSL_PROTO_TLS1_2 */
11333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011334#endif /* MBEDTLS_SSL_TLS_C */