blob: df11bb604a4c009c09675b74bd821722e4daddfd [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
115#define SSL_DONT_FORCE_FLUSH 0
116#define SSL_FORCE_FLUSH 1
117
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100119
Hanno Becker35c36a62019-04-23 12:31:42 +0100120#if defined(MBEDTLS_SSL_CID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100121/* Top-level Connection ID API */
122
Hanno Beckerca092242019-04-25 16:01:49 +0100123/* WARNING: The CID feature isn't fully implemented yet
124 * and will not be used. */
Hanno Becker8367ccc2019-05-14 11:30:10 +0100125int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
126 size_t len,
127 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100128{
129 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131
Hanno Becker8367ccc2019-05-14 11:30:10 +0100132 conf->ignore_unexpected_cid =
133 ( ignore_other_cid == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
138/* WARNING: The CID feature isn't fully implemented yet
139 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
141 int enable,
142 unsigned char const *own_cid,
143 size_t own_cid_len )
144{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100145 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
147
Hanno Beckerca092242019-04-25 16:01:49 +0100148 ssl->negotiate_cid = enable;
149 if( enable == MBEDTLS_SSL_CID_DISABLED )
150 {
151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
152 return( 0 );
153 }
154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100155 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100156
Hanno Beckerad4a1372019-05-03 13:06:44 +0100157 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100158 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
160 (unsigned) own_cid_len,
161 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
163 }
164
165 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100166 /* Truncation is not an issue here because
167 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
168 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100169
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100170 return( 0 );
171}
172
173int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
174 int *enabled,
175 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
176 size_t *peer_cid_len )
177{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100178 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100179
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
181 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
182 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100184 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100185
Hanno Beckerc5f24222019-05-03 12:54:52 +0100186 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
187 * were used, but client and server requested the empty CID.
188 * This is indistinguishable from not using the CID extension
189 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100190 if( ssl->transform_in->in_cid_len == 0 &&
191 ssl->transform_in->out_cid_len == 0 )
192 {
193 return( 0 );
194 }
195
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 memcpy( peer_cid, ssl->transform_in->out_cid,
198 ssl->transform_in->out_cid_len );
199
200 *enabled = MBEDTLS_SSL_CID_ENABLED;
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202 return( 0 );
203}
Hanno Becker35c36a62019-04-23 12:31:42 +0100204#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100205
Hanno Beckerd5847772018-08-28 10:09:23 +0100206/* Forward declarations for functions related to message buffering. */
207static void ssl_buffering_free( mbedtls_ssl_context *ssl );
208static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
209 uint8_t slot );
210static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
211static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
212static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
213static int ssl_buffer_message( mbedtls_ssl_context *ssl );
214static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100215static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100216
Hanno Beckera67dee22018-08-22 10:05:20 +0100217static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100218static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100219{
Hanno Becker11682cc2018-08-22 14:41:02 +0100220 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100221
222 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100223 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100224
225 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
226}
227
Hanno Becker67bc7c32018-08-06 11:33:50 +0100228static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
229{
Hanno Becker11682cc2018-08-22 14:41:02 +0100230 size_t const bytes_written = ssl->out_left;
231 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232
233 /* Double-check that the write-index hasn't gone
234 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100235 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236 {
237 /* Should never happen... */
238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
239 }
240
241 return( (int) ( mtu - bytes_written ) );
242}
243
244static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
245{
246 int ret;
247 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400248 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100249
250#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
251 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
252
253 if( max_len > mfl )
254 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100255
256 /* By the standard (RFC 6066 Sect. 4), the MFL extension
257 * only limits the maximum record payload size, so in theory
258 * we would be allowed to pack multiple records of payload size
259 * MFL into a single datagram. However, this would mean that there's
260 * no way to explicitly communicate MTU restrictions to the peer.
261 *
262 * The following reduction of max_len makes sure that we never
263 * write datagrams larger than MFL + Record Expansion Overhead.
264 */
265 if( max_len <= ssl->out_left )
266 return( 0 );
267
268 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100269#endif
270
271 ret = ssl_get_remaining_space_in_datagram( ssl );
272 if( ret < 0 )
273 return( ret );
274 remaining = (size_t) ret;
275
276 ret = mbedtls_ssl_get_record_expansion( ssl );
277 if( ret < 0 )
278 return( ret );
279 expansion = (size_t) ret;
280
281 if( remaining <= expansion )
282 return( 0 );
283
284 remaining -= expansion;
285 if( remaining >= max_len )
286 remaining = max_len;
287
288 return( (int) remaining );
289}
290
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200291/*
292 * Double the retransmit timeout value, within the allowed range,
293 * returning -1 if the maximum value has already been reached.
294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296{
297 uint32_t new_timeout;
298
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200299 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300 return( -1 );
301
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200302 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
303 * in the following way: after the initial transmission and a first
304 * retransmission, back off to a temporary estimated MTU of 508 bytes.
305 * This value is guaranteed to be deliverable (if not guaranteed to be
306 * delivered) of any compliant IPv4 (and IPv6) network, and should work
307 * on most non-IP stacks too. */
308 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400309 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200310 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
312 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200313
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200314 new_timeout = 2 * ssl->handshake->retransmit_timeout;
315
316 /* Avoid arithmetic overflow and range overflow */
317 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200318 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200320 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200321 }
322
323 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 ssl->handshake->retransmit_timeout ) );
326
327 return( 0 );
328}
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200331{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200332 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200334 ssl->handshake->retransmit_timeout ) );
335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200339/*
340 * Convert max_fragment_length codes to length.
341 * RFC 6066 says:
342 * enum{
343 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
344 * } MaxFragmentLength;
345 * and we add 0 -> extension unused
346 */
Angus Grattond8213d02016-05-25 20:56:48 +1000347static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200348{
Angus Grattond8213d02016-05-25 20:56:48 +1000349 switch( mfl )
350 {
351 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
352 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
353 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
354 return 512;
355 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
356 return 1024;
357 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
358 return 2048;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
360 return 4096;
361 default:
362 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
363 }
364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200366
Hanno Becker52055ae2019-02-06 14:30:46 +0000367int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
368 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_ssl_session_free( dst );
371 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000374
375#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376 if( src->peer_cert != NULL )
377 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200378 int ret;
379
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200380 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200381 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200382 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200387 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390 dst->peer_cert = NULL;
391 return( ret );
392 }
393 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000394#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000395 if( src->peer_cert_digest != NULL )
396 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000397 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000398 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000399 if( dst->peer_cert_digest == NULL )
400 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
401
402 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
403 src->peer_cert_digest_len );
404 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000405 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000406 }
407#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200410
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200411#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200412 if( src->ticket != NULL )
413 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200414 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200415 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200416 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200417
418 memcpy( dst->ticket, src->ticket, src->ticket_len );
419 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200420#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200421
422 return( 0 );
423}
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
426int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200427 const unsigned char *key_enc, const unsigned char *key_dec,
428 size_t keylen,
429 const unsigned char *iv_enc, const unsigned char *iv_dec,
430 size_t ivlen,
431 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200432 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
434int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
435int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
436int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
437int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
438#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000439
Paul Bakker5121ce52009-01-03 21:22:43 +0000440/*
441 * Key material generation
442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200444static int ssl3_prf( const unsigned char *secret, size_t slen,
445 const char *label,
446 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000447 unsigned char *dstbuf, size_t dlen )
448{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100449 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000450 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200451 mbedtls_md5_context md5;
452 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000453 unsigned char padding[16];
454 unsigned char sha1sum[20];
455 ((void)label);
456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200457 mbedtls_md5_init( &md5 );
458 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200459
Paul Bakker5f70b252012-09-13 14:23:06 +0000460 /*
461 * SSLv3:
462 * block =
463 * MD5( secret + SHA1( 'A' + secret + random ) ) +
464 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
465 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
466 * ...
467 */
468 for( i = 0; i < dlen / 16; i++ )
469 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200470 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000471
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 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, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100479 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000482
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100485 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100486 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000491 }
492
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200494 mbedtls_md5_free( &md5 );
495 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000496
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500497 mbedtls_platform_zeroize( padding, sizeof( padding ) );
498 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000499
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100500 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200505static int tls1_prf( const unsigned char *secret, size_t slen,
506 const char *label,
507 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000508 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000509{
Paul Bakker23986e52011-04-24 08:57:21 +0000510 size_t nb, hs;
511 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200512 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300513 unsigned char *tmp;
514 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 const mbedtls_md_info_t *md_info;
517 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100518 int ret;
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Ron Eldor3b350852019-05-07 18:31:49 +0300522 tmp_len = 20 + strlen( label ) + rlen;
523 tmp = mbedtls_calloc( 1, tmp_len );
524 if( tmp == NULL )
525 {
526 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
527 goto exit;
528 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530 hs = ( slen + 1 ) / 2;
531 S1 = secret;
532 S2 = secret + slen - hs;
533
534 nb = strlen( label );
535 memcpy( tmp + 20, label, nb );
536 memcpy( tmp + 20 + nb, random, rlen );
537 nb += rlen;
538
539 /*
540 * First compute P_md5(secret,label+random)[0..dlen]
541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300543 {
544 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
545 goto exit;
546 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300549 {
550 goto exit;
551 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
554 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
555 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 for( i = 0; i < dlen; i += 16 )
558 {
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 + nb );
561 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_md_hmac_reset ( &md_ctx );
564 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
565 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
568
569 for( j = 0; j < k; j++ )
570 dstbuf[i + j] = h_i[j];
571 }
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100574
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 /*
576 * XOR out with P_sha1(secret,label+random)[0..dlen]
577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300579 {
580 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
581 goto exit;
582 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300585 {
586 goto exit;
587 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
590 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
591 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 for( i = 0; i < dlen; i += 20 )
594 {
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 + nb );
597 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_md_hmac_reset ( &md_ctx );
600 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
601 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
604
605 for( j = 0; j < k; j++ )
606 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
607 }
608
Ron Eldor3b350852019-05-07 18:31:49 +0300609exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100611
Ron Eldor3b350852019-05-07 18:31:49 +0300612 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500613 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Ron Eldor3b350852019-05-07 18:31:49 +0300615 mbedtls_free( tmp );
616 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500621#if defined(MBEDTLS_USE_PSA_CRYPTO)
622static int tls_prf_generic( mbedtls_md_type_t md_type,
623 const unsigned char *secret, size_t slen,
624 const char *label,
625 const unsigned char *random, size_t rlen,
626 unsigned char *dstbuf, size_t dlen )
627{
628 psa_status_t status;
629 psa_algorithm_t alg;
630 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500631 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500632 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
633
Andrzej Kurek2f760752019-01-28 08:08:15 -0500634 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500635 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500636
Andrzej Kurekc929a822019-01-14 03:51:11 -0500637 if( md_type == MBEDTLS_MD_SHA384 )
638 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
639 else
640 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
641
Andrzej Kurek2f760752019-01-28 08:08:15 -0500642 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500643 psa_key_policy_set_usage( &policy,
644 PSA_KEY_USAGE_DERIVE,
645 alg );
646 status = psa_set_key_policy( master_slot, &policy );
647 if( status != PSA_SUCCESS )
648 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
649
650 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
651 if( status != PSA_SUCCESS )
652 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
653
654 status = psa_key_derivation( &generator,
655 master_slot, alg,
656 random, rlen,
657 (unsigned char const *) label,
658 (size_t) strlen( label ),
659 dlen );
660 if( status != PSA_SUCCESS )
661 {
662 psa_generator_abort( &generator );
663 psa_destroy_key( master_slot );
664 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
665 }
666
667 status = psa_generator_read( &generator, dstbuf, dlen );
668 if( status != PSA_SUCCESS )
669 {
670 psa_generator_abort( &generator );
671 psa_destroy_key( master_slot );
672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
673 }
674
675 status = psa_generator_abort( &generator );
676 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500677 {
678 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500679 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500680 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500681
682 status = psa_destroy_key( master_slot );
683 if( status != PSA_SUCCESS )
684 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
685
Andrzej Kurek33171262019-01-15 03:25:18 -0500686 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500687}
688
689#else /* MBEDTLS_USE_PSA_CRYPTO */
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000696{
697 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100698 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300699 unsigned char *tmp;
700 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
702 const mbedtls_md_info_t *md_info;
703 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100704 int ret;
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100712
Ron Eldor3b350852019-05-07 18:31:49 +0300713 tmp_len = md_len + strlen( label ) + rlen;
714 tmp = mbedtls_calloc( 1, tmp_len );
715 if( tmp == NULL )
716 {
717 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
718 goto exit;
719 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000720
721 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100722 memcpy( tmp + md_len, label, nb );
723 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000724 nb += rlen;
725
726 /*
727 * Compute P_<hash>(secret, label + random)[0..dlen]
728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300730 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
733 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
734 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100735
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100736 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000737 {
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 + nb );
740 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_md_hmac_reset ( &md_ctx );
743 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
744 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000745
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100746 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000747
748 for( j = 0; j < k; j++ )
749 dstbuf[i + j] = h_i[j];
750 }
751
Ron Eldor3b350852019-05-07 18:31:49 +0300752exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100754
Ron Eldor3b350852019-05-07 18:31:49 +0300755 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500756 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000757
Ron Eldor3b350852019-05-07 18:31:49 +0300758 mbedtls_free( tmp );
759
760 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500762#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100764static int tls_prf_sha256( const unsigned char *secret, size_t slen,
765 const char *label,
766 const unsigned char *random, size_t rlen,
767 unsigned char *dstbuf, size_t dlen )
768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100770 label, random, rlen, dstbuf, dlen ) );
771}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200775static int tls_prf_sha384( const unsigned char *secret, size_t slen,
776 const char *label,
777 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000778 unsigned char *dstbuf, size_t dlen )
779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100781 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_SHA512_C */
784#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
789 defined(MBEDTLS_SSL_PROTO_TLS1_1)
790static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200791#endif
Paul Bakker380da532012-04-18 16:10:25 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_SSL_PROTO_SSL3)
794static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
795static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200796#endif
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
799static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
800static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200801#endif
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
804#if defined(MBEDTLS_SHA256_C)
805static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
806static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
807static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_SHA512_C)
811static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
812static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
813static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000816
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100817#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100818 defined(MBEDTLS_USE_PSA_CRYPTO)
819static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
820{
821 if( ssl->conf->f_psk != NULL )
822 {
823 /* If we've used a callback to select the PSK,
824 * the static configuration is irrelevant. */
825 if( ssl->handshake->psk_opaque != 0 )
826 return( 1 );
827
828 return( 0 );
829 }
830
831 if( ssl->conf->psk_opaque != 0 )
832 return( 1 );
833
834 return( 0 );
835}
836#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100837 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100838
Ron Eldorcf280092019-05-14 20:19:13 +0300839#if defined(MBEDTLS_SSL_EXPORT_KEYS)
840static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
841{
842#if defined(MBEDTLS_SSL_PROTO_SSL3)
843 if( tls_prf == ssl3_prf )
844 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300845 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300846 }
847 else
848#endif
849#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
850 if( tls_prf == tls1_prf )
851 {
852 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
853 }
854 else
855#endif
856#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
857#if defined(MBEDTLS_SHA512_C)
858 if( tls_prf == tls_prf_sha384 )
859 {
860 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
861 }
862 else
863#endif
864#if defined(MBEDTLS_SHA256_C)
865 if( tls_prf == tls_prf_sha256 )
866 {
867 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
868 }
869 else
870#endif
871#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
872 return( MBEDTLS_SSL_TLS_PRF_NONE );
873}
874#endif /* MBEDTLS_SSL_EXPORT_KEYS */
875
Ron Eldor51d3ab52019-05-12 14:54:30 +0300876int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
877 const unsigned char *secret, size_t slen,
878 const char *label,
879 const unsigned char *random, size_t rlen,
880 unsigned char *dstbuf, size_t dlen )
881{
882 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
883
884 switch( prf )
885 {
886#if defined(MBEDTLS_SSL_PROTO_SSL3)
887 case MBEDTLS_SSL_TLS_PRF_SSL3:
888 tls_prf = ssl3_prf;
889 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300890#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300891#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
892 case MBEDTLS_SSL_TLS_PRF_TLS1:
893 tls_prf = tls1_prf;
894 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300895#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
896
897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300898#if defined(MBEDTLS_SHA512_C)
899 case MBEDTLS_SSL_TLS_PRF_SHA384:
900 tls_prf = tls_prf_sha384;
901 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300902#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300903#if defined(MBEDTLS_SHA256_C)
904 case MBEDTLS_SSL_TLS_PRF_SHA256:
905 tls_prf = tls_prf_sha256;
906 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300907#endif /* MBEDTLS_SHA256_C */
908#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300909 default:
910 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
911 }
912
913 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
914}
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000917{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200918 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000919#if defined(MBEDTLS_USE_PSA_CRYPTO)
920 int psa_fallthrough;
921#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 unsigned char keyblk[256];
924 unsigned char *key1;
925 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100926 unsigned char *mac_enc;
927 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000928 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200929 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000930 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000931 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 const mbedtls_cipher_info_t *cipher_info;
933 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100934
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000935 /* cf. RFC 5246, Section 8.1:
936 * "The master secret is always exactly 48 bytes in length." */
937 size_t const master_secret_len = 48;
938
Hanno Becker35b23c72018-10-23 12:10:41 +0100939#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
940 unsigned char session_hash[48];
941#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_ssl_session *session = ssl->session_negotiate;
944 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
945 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
950 transform->encrypt_then_mac = session->encrypt_then_mac;
951#endif
952 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000953
954 ciphersuite_info = handshake->ciphersuite_info;
955 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100956 if( cipher_info == NULL )
957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000959 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100961 }
962
Hanno Beckere694c3e2017-12-27 21:34:08 +0000963 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100964 if( md_info == NULL )
965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000967 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100969 }
970
Hanno Becker4bf74652019-04-26 16:22:27 +0100971#if defined(MBEDTLS_SSL_CID)
972 /* Copy own and peer's CID if the use of the CID
973 * extension has been negotiated. */
974 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
975 {
976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100977
Hanno Becker05154c32019-05-03 15:23:51 +0100978 transform->in_cid_len = ssl->own_cid_len;
979 transform->out_cid_len = ssl->handshake->peer_cid_len;
980 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
981 memcpy( transform->out_cid, ssl->handshake->peer_cid,
982 ssl->handshake->peer_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100983
984 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
985 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100986 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100987 transform->in_cid_len );
988 }
989#endif /* MBEDTLS_SSL_CID */
990
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000992 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#if defined(MBEDTLS_SSL_PROTO_SSL3)
995 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000996 {
Paul Bakker48916f92012-09-16 19:57:18 +0000997 handshake->tls_prf = ssl3_prf;
998 handshake->calc_verify = ssl_calc_verify_ssl;
999 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001000 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001001 else
1002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1004 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001005 {
Paul Bakker48916f92012-09-16 19:57:18 +00001006 handshake->tls_prf = tls1_prf;
1007 handshake->calc_verify = ssl_calc_verify_tls;
1008 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001010 else
1011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1013#if defined(MBEDTLS_SHA512_C)
1014 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001015 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 {
Paul Bakker48916f92012-09-16 19:57:18 +00001017 handshake->tls_prf = tls_prf_sha384;
1018 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1019 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001020 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001021 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SHA256_C)
1024 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001025 {
Paul Bakker48916f92012-09-16 19:57:18 +00001026 handshake->tls_prf = tls_prf_sha256;
1027 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1028 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001030 else
1031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001036 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001037
1038 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 * SSLv3:
1040 * master =
1041 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1042 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1043 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001044 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001045 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 * master = PRF( premaster, "master secret", randbytes )[0..47]
1047 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001048 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1051 }
1052 else
1053 {
1054 /* The label for the KDF used for key expansion.
1055 * This is either "master secret" or "extended master secret"
1056 * depending on whether the Extended Master Secret extension
1057 * is used. */
1058 char const *lbl = "master secret";
1059
1060 /* The salt for the KDF used for key expansion.
1061 * - If the Extended Master Secret extension is not used,
1062 * this is ClientHello.Random + ServerHello.Random
1063 * (see Sect. 8.1 in RFC 5246).
1064 * - If the Extended Master Secret extension is used,
1065 * this is the transcript of the handshake so far.
1066 * (see Sect. 4 in RFC 7627). */
1067 unsigned char const *salt = handshake->randbytes;
1068 size_t salt_len = 64;
1069
1070#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001074
Hanno Becker35b23c72018-10-23 12:10:41 +01001075 lbl = "extended master secret";
1076 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001077 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1079 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001082 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1083 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001084 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001085 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001086 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001087#endif /* MBEDTLS_SHA512_C */
1088 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001089 }
1090 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001092 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001093
Hanno Becker35b23c72018-10-23 12:10:41 +01001094 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001095 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001096#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1097
Hanno Becker7d0a5692018-10-23 15:26:22 +01001098#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1099 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1100 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1101 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1102 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001103 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001104 /* Perform PSK-to-MS expansion in a single step. */
1105 psa_status_t status;
1106 psa_algorithm_t alg;
1107 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001108 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001109
1110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1111
1112 psk = ssl->conf->psk_opaque;
1113 if( ssl->handshake->psk_opaque != 0 )
1114 psk = ssl->handshake->psk_opaque;
1115
Hanno Becker22bf1452019-04-05 11:21:08 +01001116 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001117 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1118 else
1119 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1120
1121 status = psa_key_derivation( &generator, psk, alg,
1122 salt, salt_len,
1123 (unsigned char const *) lbl,
1124 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001125 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001126 if( status != PSA_SUCCESS )
1127 {
1128 psa_generator_abort( &generator );
1129 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1130 }
1131
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001132 status = psa_generator_read( &generator, session->master,
1133 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001134 if( status != PSA_SUCCESS )
1135 {
1136 psa_generator_abort( &generator );
1137 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1138 }
1139
1140 status = psa_generator_abort( &generator );
1141 if( status != PSA_SUCCESS )
1142 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001143 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001144 else
1145#endif
1146 {
1147 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1148 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001149 session->master,
1150 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001151 if( ret != 0 )
1152 {
1153 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1154 return( ret );
1155 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001156
Hanno Becker7d0a5692018-10-23 15:26:22 +01001157 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1158 handshake->premaster,
1159 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001160
Hanno Becker7d0a5692018-10-23 15:26:22 +01001161 mbedtls_platform_zeroize( handshake->premaster,
1162 sizeof(handshake->premaster) );
1163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166 /*
1167 * Swap the client and server random values.
1168 */
Paul Bakker48916f92012-09-16 19:57:18 +00001169 memcpy( tmp, handshake->randbytes, 64 );
1170 memcpy( handshake->randbytes, tmp + 32, 32 );
1171 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001172 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
1174 /*
1175 * SSLv3:
1176 * key block =
1177 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1178 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1179 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1180 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1181 * ...
1182 *
1183 * TLSv1:
1184 * key block = PRF( master, "key expansion", randbytes )
1185 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001186 ret = handshake->tls_prf( session->master, 48, "key expansion",
1187 handshake->randbytes, 64, keyblk, 256 );
1188 if( ret != 0 )
1189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001191 return( ret );
1192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1195 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1196 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1197 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1198 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 /*
1201 * Determine the appropriate key, IV and MAC length.
1202 */
Paul Bakker68884e32013-01-07 18:20:04 +01001203
Hanno Becker88aaf652017-12-27 08:17:40 +00001204 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001205
Hanno Becker8031d062018-01-03 15:32:31 +00001206#if defined(MBEDTLS_GCM_C) || \
1207 defined(MBEDTLS_CCM_C) || \
1208 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001210 cipher_info->mode == MBEDTLS_MODE_CCM ||
1211 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001213 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001214
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001215 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001216 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001217 transform->taglen =
1218 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001219
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001220 /* All modes haves 96-bit IVs;
1221 * GCM and CCM has 4 implicit and 8 explicit bytes
1222 * ChachaPoly has all 12 bytes implicit
1223 */
Paul Bakker68884e32013-01-07 18:20:04 +01001224 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001225 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1226 transform->fixed_ivlen = 12;
1227 else
1228 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001229
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001230 /* Minimum length of encrypted record */
1231 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001232 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001233 }
1234 else
Hanno Becker8031d062018-01-03 15:32:31 +00001235#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1236#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1237 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1238 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001239 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001240 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1242 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001245 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001246 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001248 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001249 mac_key_len = mbedtls_md_get_size( md_info );
1250 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001253 /*
1254 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1255 * (rfc 6066 page 13 or rfc 2104 section 4),
1256 * so we only need to adjust the length here.
1257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001261
1262#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1263 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001264 * HMAC implementation which also truncates the key
1265 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001266 mac_key_len = transform->maclen;
1267#endif
1268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001270
1271 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001272 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001274 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001276 transform->minlen = transform->maclen;
1277 else
Paul Bakker68884e32013-01-07 18:20:04 +01001278 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001279 /*
1280 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001281 * 1. if EtM is in use: one block plus MAC
1282 * otherwise: * first multiple of blocklen greater than maclen
1283 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1286 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001287 {
1288 transform->minlen = transform->maclen
1289 + cipher_info->block_size;
1290 }
1291 else
1292#endif
1293 {
1294 transform->minlen = transform->maclen
1295 + cipher_info->block_size
1296 - transform->maclen % cipher_info->block_size;
1297 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1300 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1301 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001302 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001303 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1306 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1307 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001308 {
1309 transform->minlen += transform->ivlen;
1310 }
1311 else
1312#endif
1313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001315 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1316 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001317 }
Paul Bakker68884e32013-01-07 18:20:04 +01001318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 }
Hanno Becker8031d062018-01-03 15:32:31 +00001320 else
1321#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1322 {
1323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Hanno Becker88aaf652017-12-27 08:17:40 +00001327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1328 (unsigned) keylen,
1329 (unsigned) transform->minlen,
1330 (unsigned) transform->ivlen,
1331 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 /*
1334 * Finally setup the cipher contexts, IVs and MAC secrets.
1335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001337 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001339 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001340 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker68884e32013-01-07 18:20:04 +01001342 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001343 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001345 /*
1346 * This is not used in TLS v1.1.
1347 */
Paul Bakker48916f92012-09-16 19:57:18 +00001348 iv_copy_len = ( transform->fixed_ivlen ) ?
1349 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001350 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1351 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001352 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
1354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_SSL_CLI_C */
1356#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001359 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001360 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Hanno Becker81c7b182017-11-09 18:39:33 +00001362 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001363 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001365 /*
1366 * This is not used in TLS v1.1.
1367 */
Paul Bakker48916f92012-09-16 19:57:18 +00001368 iv_copy_len = ( transform->fixed_ivlen ) ?
1369 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001370 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1371 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001372 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001374 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001378 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1379 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Hanno Beckerd56ed242018-01-03 15:32:51 +00001382#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_SSL_PROTO_SSL3)
1384 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001385 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001386 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001389 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1390 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001391 }
1392
Hanno Becker81c7b182017-11-09 18:39:33 +00001393 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1394 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001395 }
1396 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1398#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1399 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1400 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001401 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001402 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1403 For AEAD-based ciphersuites, there is nothing to do here. */
1404 if( mac_key_len != 0 )
1405 {
1406 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1407 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1408 }
Paul Bakker68884e32013-01-07 18:20:04 +01001409 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001410 else
1411#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001414 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1415 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001416 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001417#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1420 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001421 {
1422 int ret = 0;
1423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001425
Hanno Becker88aaf652017-12-27 08:17:40 +00001426 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001427 transform->iv_enc, transform->iv_dec,
1428 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001429 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001430 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001433 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1434 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001435 }
1436 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001437#else
1438 ((void) mac_dec);
1439 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001441
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001442#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1443 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001444 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001445 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1446 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001447 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001448 iv_copy_len );
1449 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001450
1451 if( ssl->conf->f_export_keys_ext != NULL )
1452 {
1453 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1454 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001455 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001456 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001457 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001458 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001459 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001460 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001461#endif
1462
Hanno Beckerf704bef2018-11-16 15:21:18 +00001463#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001464
1465 /* Only use PSA-based ciphers for TLS-1.2.
1466 * That's relevant at least for TLS-1.0, where
1467 * we assume that mbedtls_cipher_crypt() updates
1468 * the structure field for the IV, which the PSA-based
1469 * implementation currently doesn't. */
1470#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1471 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001472 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001473 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001474 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001475 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1476 {
1477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001478 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001479 }
1480
1481 if( ret == 0 )
1482 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001484 psa_fallthrough = 0;
1485 }
1486 else
1487 {
1488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1489 psa_fallthrough = 1;
1490 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001491 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001492 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001493 psa_fallthrough = 1;
1494#else
1495 psa_fallthrough = 1;
1496#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001497
Hanno Beckercb1cc802018-11-17 22:27:38 +00001498 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001499#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001500 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001501 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001504 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001505 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001506
Hanno Beckerf704bef2018-11-16 15:21:18 +00001507#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001508 /* Only use PSA-based ciphers for TLS-1.2.
1509 * That's relevant at least for TLS-1.0, where
1510 * we assume that mbedtls_cipher_crypt() updates
1511 * the structure field for the IV, which the PSA-based
1512 * implementation currently doesn't. */
1513#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1514 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001515 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001516 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001517 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001518 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1519 {
1520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001521 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001522 }
1523
1524 if( ret == 0 )
1525 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001527 psa_fallthrough = 0;
1528 }
1529 else
1530 {
1531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1532 psa_fallthrough = 1;
1533 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001534 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001535 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001536 psa_fallthrough = 1;
1537#else
1538 psa_fallthrough = 1;
1539#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001540
Hanno Beckercb1cc802018-11-17 22:27:38 +00001541 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001542#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001543 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001544 cipher_info ) ) != 0 )
1545 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001547 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001551 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001555 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001559 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001563 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_CIPHER_MODE_CBC)
1567 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1570 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001573 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001574 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1577 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001580 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001587 // Initialize compression
1588 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001590 {
Paul Bakker16770332013-10-11 09:59:44 +02001591 if( ssl->compress_buf == NULL )
1592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001594 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001595 if( ssl->compress_buf == NULL )
1596 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001598 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001599 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1600 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001601 }
1602 }
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001605
Paul Bakker48916f92012-09-16 19:57:18 +00001606 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1607 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001608
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001609 if( deflateInit( &transform->ctx_deflate,
1610 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001611 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001614 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1615 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616 }
1617 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001621end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001622 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1623 mbedtls_platform_zeroize( handshake->randbytes,
1624 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001625 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001626}
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_PROTO_SSL3)
1629void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001630{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001631 mbedtls_md5_context md5;
1632 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 unsigned char pad_1[48];
1634 unsigned char pad_2[48];
1635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001638 mbedtls_md5_init( &md5 );
1639 mbedtls_sha1_init( &sha1 );
1640
1641 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1642 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
Paul Bakker380da532012-04-18 16:10:25 +00001644 memset( pad_1, 0x36, 48 );
1645 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001647 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1648 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1649 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001651 mbedtls_md5_starts_ret( &md5 );
1652 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1653 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1654 mbedtls_md5_update_ret( &md5, hash, 16 );
1655 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001657 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1658 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1659 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001660
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001661 mbedtls_sha1_starts_ret( &sha1 );
1662 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1663 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1664 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1665 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001669
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001670 mbedtls_md5_free( &md5 );
1671 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001672
Paul Bakker380da532012-04-18 16:10:25 +00001673 return;
1674}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1678void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001679{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001680 mbedtls_md5_context md5;
1681 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001684
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001685 mbedtls_md5_init( &md5 );
1686 mbedtls_sha1_init( &sha1 );
1687
1688 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1689 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001690
Andrzej Kurekeb342242019-01-29 09:14:33 -05001691 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001692 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001696
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001697 mbedtls_md5_free( &md5 );
1698 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001699
Paul Bakker380da532012-04-18 16:10:25 +00001700 return;
1701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1705#if defined(MBEDTLS_SHA256_C)
1706void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001707{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001708#if defined(MBEDTLS_USE_PSA_CRYPTO)
1709 size_t hash_size;
1710 psa_status_t status;
1711 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1712
1713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1714 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1715 if( status != PSA_SUCCESS )
1716 {
1717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1718 return;
1719 }
1720
1721 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1722 if( status != PSA_SUCCESS )
1723 {
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1725 return;
1726 }
1727 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1729#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001730 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001731
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001732 mbedtls_sha256_init( &sha256 );
1733
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001735
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001736 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001737 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001741
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001742 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001743#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001744 return;
1745}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#if defined(MBEDTLS_SHA512_C)
1749void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001750{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001751#if defined(MBEDTLS_USE_PSA_CRYPTO)
1752 size_t hash_size;
1753 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001754 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001755
1756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001757 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001758 if( status != PSA_SUCCESS )
1759 {
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1761 return;
1762 }
1763
Andrzej Kurek972fba52019-01-30 03:29:12 -05001764 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001765 if( status != PSA_SUCCESS )
1766 {
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1768 return;
1769 }
1770 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1772#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001773 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001774
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001775 mbedtls_sha512_init( &sha512 );
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001778
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001779 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001780 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001785 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001786#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 return;
1788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#endif /* MBEDTLS_SHA512_C */
1790#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1793int 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 +02001794{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001795 unsigned char *p = ssl->handshake->premaster;
1796 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001797 const unsigned char *psk = ssl->conf->psk;
1798 size_t psk_len = ssl->conf->psk_len;
1799
1800 /* If the psk callback was called, use its result */
1801 if( ssl->handshake->psk != NULL )
1802 {
1803 psk = ssl->handshake->psk;
1804 psk_len = ssl->handshake->psk_len;
1805 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001806
1807 /*
1808 * PMS = struct {
1809 * opaque other_secret<0..2^16-1>;
1810 * opaque psk<0..2^16-1>;
1811 * };
1812 * with "other_secret" depending on the particular key exchange
1813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1815 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001816 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001817 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001819
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001820 *(p++) = (unsigned char)( psk_len >> 8 );
1821 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001822
1823 if( end < p || (size_t)( end - p ) < psk_len )
1824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1825
1826 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001827 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001828 }
1829 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1831#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1832 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001833 {
1834 /*
1835 * other_secret already set by the ClientKeyExchange message,
1836 * and is 48 bytes long
1837 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001838 if( end - p < 2 )
1839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1840
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001841 *p++ = 0;
1842 *p++ = 48;
1843 p += 48;
1844 }
1845 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1847#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1848 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001849 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001850 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001851 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001852
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001853 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001855 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001856 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001859 return( ret );
1860 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001861 *(p++) = (unsigned char)( len >> 8 );
1862 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001863 p += len;
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001866 }
1867 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1869#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1870 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001871 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001872 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 size_t zlen;
1874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001876 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001877 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001880 return( ret );
1881 }
1882
1883 *(p++) = (unsigned char)( zlen >> 8 );
1884 *(p++) = (unsigned char)( zlen );
1885 p += zlen;
1886
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001887 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1888 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001889 }
1890 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001895 }
1896
1897 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001898 if( end - p < 2 )
1899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001900
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001901 *(p++) = (unsigned char)( psk_len >> 8 );
1902 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001903
1904 if( end < p || (size_t)( end - p ) < psk_len )
1905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1906
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001907 memcpy( p, psk, psk_len );
1908 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001909
1910 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1911
1912 return( 0 );
1913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001917/*
1918 * SSLv3.0 MAC functions
1919 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001920#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001921static void ssl_mac( mbedtls_md_context_t *md_ctx,
1922 const unsigned char *secret,
1923 const unsigned char *buf, size_t len,
1924 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001925 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001926{
1927 unsigned char header[11];
1928 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001929 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1931 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001932
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001933 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001935 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001936 else
Paul Bakker68884e32013-01-07 18:20:04 +01001937 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 memcpy( header, ctr, 8 );
1940 header[ 8] = (unsigned char) type;
1941 header[ 9] = (unsigned char)( len >> 8 );
1942 header[10] = (unsigned char)( len );
1943
Paul Bakker68884e32013-01-07 18:20:04 +01001944 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 mbedtls_md_starts( md_ctx );
1946 mbedtls_md_update( md_ctx, secret, md_size );
1947 mbedtls_md_update( md_ctx, padding, padlen );
1948 mbedtls_md_update( md_ctx, header, 11 );
1949 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001950 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Paul Bakker68884e32013-01-07 18:20:04 +01001952 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_md_starts( md_ctx );
1954 mbedtls_md_update( md_ctx, secret, md_size );
1955 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001956 mbedtls_md_update( md_ctx, out, md_size );
1957 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001960
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001961/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001962 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001963#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001964 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1965 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1966 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1967/* This function makes sure every byte in the memory region is accessed
1968 * (in ascending addresses order) */
1969static void ssl_read_memory( unsigned char *p, size_t len )
1970{
1971 unsigned char acc = 0;
1972 volatile unsigned char force;
1973
1974 for( ; len != 0; p++, len-- )
1975 acc ^= *p;
1976
1977 force = acc;
1978 (void) force;
1979}
1980#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1981
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001982/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001984 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001985
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001986#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001987/* This functions transforms a DTLS plaintext fragment and a record content
1988 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001989 *
1990 * struct {
1991 * opaque content[DTLSPlaintext.length];
1992 * ContentType real_type;
1993 * uint8 zeros[length_of_padding];
1994 * } DTLSInnerPlaintext;
1995 *
1996 * Input:
1997 * - `content`: The beginning of the buffer holding the
1998 * plaintext to be wrapped.
1999 * - `*content_size`: The length of the plaintext in Bytes.
2000 * - `max_len`: The number of Bytes available starting from
2001 * `content`. This must be `>= *content_size`.
2002 * - `rec_type`: The desired record content type.
2003 *
2004 * Output:
2005 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2006 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2007 *
2008 * Returns:
2009 * - `0` on success.
2010 * - A negative error code if `max_len` didn't offer enough space
2011 * for the expansion.
2012 */
2013static int ssl_cid_build_inner_plaintext( unsigned char *content,
2014 size_t *content_size,
2015 size_t remaining,
2016 uint8_t rec_type )
2017{
2018 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002019 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2020 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2021 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002022
2023 /* Write real content type */
2024 if( remaining == 0 )
2025 return( -1 );
2026 content[ len ] = rec_type;
2027 len++;
2028 remaining--;
2029
2030 if( remaining < pad )
2031 return( -1 );
2032 memset( content + len, 0, pad );
2033 len += pad;
2034 remaining -= pad;
2035
2036 *content_size = len;
2037 return( 0 );
2038}
2039
Hanno Becker07dc97d2019-05-20 15:08:01 +01002040/* This function parses a DTLSInnerPlaintext structure.
2041 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002042static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2043 size_t *content_size,
2044 uint8_t *rec_type )
2045{
2046 size_t remaining = *content_size;
2047
2048 /* Determine length of padding by skipping zeroes from the back. */
2049 do
2050 {
2051 if( remaining == 0 )
2052 return( -1 );
2053 remaining--;
2054 } while( content[ remaining ] == 0 );
2055
2056 *content_size = remaining;
2057 *rec_type = content[ remaining ];
2058
2059 return( 0 );
2060}
2061#endif /* MBEDTLS_SSL_CID */
2062
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002063/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002064 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002065static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002066 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002067 mbedtls_record *rec )
2068{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002069 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002070 *
2071 * additional_data = seq_num + TLSCompressed.type +
2072 * TLSCompressed.version + TLSCompressed.length;
2073 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002074 * For the CID extension, this is extended as follows
2075 * (quoting draft-ietf-tls-dtls-connection-id-05,
2076 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002077 *
2078 * additional_data = seq_num + DTLSPlaintext.type +
2079 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002080 * cid +
2081 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002082 * length_of_DTLSInnerPlaintext;
2083 */
2084
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2086 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002087 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002088
2089#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002090 if( rec->cid_len != 0 )
2091 {
2092 memcpy( add_data + 11, rec->cid, rec->cid_len );
2093 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2094 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2095 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2096 *add_data_len = 13 + 1 + rec->cid_len;
2097 }
2098 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002099#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002100 {
2101 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2102 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2103 *add_data_len = 13;
2104 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002105}
2106
Hanno Beckera18d1322018-01-03 14:27:32 +00002107int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2108 mbedtls_ssl_transform *transform,
2109 mbedtls_record *rec,
2110 int (*f_rng)(void *, unsigned char *, size_t),
2111 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002114 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002115 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002116 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002117 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002118 size_t post_avail;
2119
2120 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002121#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 ((void) ssl);
2123#endif
2124
2125 /* The PRNG is used for dynamic IV generation that's used
2126 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2127#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2128 ( defined(MBEDTLS_AES_C) || \
2129 defined(MBEDTLS_ARIA_C) || \
2130 defined(MBEDTLS_CAMELLIA_C) ) && \
2131 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2132 ((void) f_rng);
2133 ((void) p_rng);
2134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002139 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2142 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002143 if( rec == NULL
2144 || rec->buf == NULL
2145 || rec->buf_len < rec->data_offset
2146 || rec->buf_len - rec->data_offset < rec->data_len
2147#if defined(MBEDTLS_SSL_CID)
2148 || rec->cid_len != 0
2149#endif
2150 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002151 {
2152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002154 }
2155
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002156 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002157 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002159 data, rec->data_len );
2160
2161 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2162
2163 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2164 {
2165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2166 (unsigned) rec->data_len,
2167 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2169 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002170
Hanno Beckercab87e62019-04-29 13:52:53 +01002171#if defined(MBEDTLS_SSL_CID)
2172 /*
2173 * Add CID information
2174 */
2175 rec->cid_len = transform->out_cid_len;
2176 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2177 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002178
2179 if( rec->cid_len != 0 )
2180 {
2181 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002182 * Wrap plaintext into DTLSInnerPlaintext structure.
2183 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002184 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002185 * Note that this changes `rec->data_len`, and hence
2186 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002187 */
2188 if( ssl_cid_build_inner_plaintext( data,
2189 &rec->data_len,
2190 post_avail,
2191 rec->type ) != 0 )
2192 {
2193 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2194 }
2195
2196 rec->type = MBEDTLS_SSL_MSG_CID;
2197 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002198#endif /* MBEDTLS_SSL_CID */
2199
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002200 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2201
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002203 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 */
Hanno Becker52344c22018-01-03 15:24:20 +00002205#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 if( mode == MBEDTLS_MODE_STREAM ||
2207 ( mode == MBEDTLS_MODE_CBC
2208#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002209 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002210#endif
2211 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002213 if( post_avail < transform->maclen )
2214 {
2215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2216 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2217 }
2218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002220 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002221 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002222 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002223 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2224 data, rec->data_len, rec->ctr, rec->type, mac );
2225 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002226 }
2227 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2230 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002231 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002232 {
Hanno Becker992b6872017-11-09 18:57:39 +00002233 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2234
Hanno Beckercab87e62019-04-29 13:52:53 +01002235 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002236
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002237 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002238 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002239 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2240 data, rec->data_len );
2241 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2242 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2243
2244 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002245 }
2246 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002247#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002251 }
2252
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002253 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2254 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002255
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002256 rec->data_len += transform->maclen;
2257 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002258 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002259 }
Hanno Becker52344c22018-01-03 15:24:20 +00002260#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002262 /*
2263 * Encrypt
2264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2266 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002268 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002269 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002271 "including %d bytes of padding",
2272 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002274 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2275 transform->iv_enc, transform->ivlen,
2276 data, rec->data_len,
2277 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002280 return( ret );
2281 }
2282
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 }
Paul Bakker68884e32013-01-07 18:20:04 +01002289 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002291
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002292#if defined(MBEDTLS_GCM_C) || \
2293 defined(MBEDTLS_CCM_C) || \
2294 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002296 mode == MBEDTLS_MODE_CCM ||
2297 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002298 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002299 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002300 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002301 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002302
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 /* Check that there's space for both the authentication tag
2304 * and the explicit IV before and after the record content. */
2305 if( post_avail < transform->taglen ||
2306 rec->data_offset < explicit_iv_len )
2307 {
2308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2309 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2310 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002311
Paul Bakker68884e32013-01-07 18:20:04 +01002312 /*
2313 * Generate IV
2314 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002315 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2316 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002317 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002318 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002319 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2320 explicit_iv_len );
2321 /* Prefix record content with explicit IV. */
2322 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002323 }
2324 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2325 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002326 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002327 unsigned char i;
2328
2329 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2330
2331 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002332 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002333 }
2334 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002335 {
2336 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002339 }
2340
Hanno Beckercab87e62019-04-29 13:52:53 +01002341 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002342
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002343 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2344 iv, transform->ivlen );
2345 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002346 data - explicit_iv_len, explicit_iv_len );
2347 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002348 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002351 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002352
Paul Bakker68884e32013-01-07 18:20:04 +01002353 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002354 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002355 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002356
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002358 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002359 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002360 data, rec->data_len, /* source */
2361 data, &rec->data_len, /* destination */
2362 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002365 return( ret );
2366 }
2367
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2369 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002370
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 rec->data_len += transform->taglen + explicit_iv_len;
2372 rec->data_offset -= explicit_iv_len;
2373 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002374 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2378#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002379 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002382 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002383 size_t padlen, i;
2384 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002385
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002386 /* Currently we're always using minimal padding
2387 * (up to 255 bytes would be allowed). */
2388 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2389 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 padlen = 0;
2391
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002392 /* Check there's enough space in the buffer for the padding. */
2393 if( post_avail < padlen + 1 )
2394 {
2395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2396 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2397 }
2398
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002400 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002402 rec->data_len += padlen + 1;
2403 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002407 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2408 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002409 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002410 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002412 if( f_rng == NULL )
2413 {
2414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2416 }
2417
2418 if( rec->data_offset < transform->ivlen )
2419 {
2420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2421 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2422 }
2423
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002424 /*
2425 * Generate IV
2426 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002427 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002428 if( ret != 0 )
2429 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002430
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 memcpy( data - transform->ivlen, transform->iv_enc,
2432 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002433
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002438 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002439 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002440 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002442 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2443 transform->iv_enc,
2444 transform->ivlen,
2445 data, rec->data_len,
2446 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002449 return( ret );
2450 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002451
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002456 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002460 {
2461 /*
2462 * Save IV in SSL3 and TLS1
2463 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002464 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2465 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002467 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002468#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002469 {
2470 data -= transform->ivlen;
2471 rec->data_offset -= transform->ivlen;
2472 rec->data_len += transform->ivlen;
2473 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002476 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002477 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002478 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2479
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002480 /*
2481 * MAC(MAC_write_key, seq_num +
2482 * TLSCipherText.type +
2483 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002484 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002485 * IV + // except for TLS 1.0
2486 * ENC(content + padding + padding_length));
2487 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488
2489 if( post_avail < transform->maclen)
2490 {
2491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2492 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2493 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Hanno Beckercab87e62019-04-29 13:52:53 +01002495 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002498 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002499 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002500
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002501 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002502 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002503 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2504 data, rec->data_len );
2505 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2506 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002510 rec->data_len += transform->maclen;
2511 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002512 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002513 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002516 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002518 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002524 /* Make extra sure authentication was performed, exactly once */
2525 if( auth_done != 1 )
2526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002529 }
2530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
2533 return( 0 );
2534}
2535
Hanno Beckera18d1322018-01-03 14:27:32 +00002536int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2537 mbedtls_ssl_transform *transform,
2538 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002539{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002540 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002542 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002543#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002544 size_t padlen = 0, correct = 1;
2545#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002547 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002548 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002549
Hanno Beckera18d1322018-01-03 14:27:32 +00002550#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002551 ((void) ssl);
2552#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002555 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002556 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2559 }
2560 if( rec == NULL ||
2561 rec->buf == NULL ||
2562 rec->buf_len < rec->data_offset ||
2563 rec->buf_len - rec->data_offset < rec->data_len )
2564 {
2565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002567 }
2568
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002569 data = rec->buf + rec->data_offset;
2570 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Hanno Beckercab87e62019-04-29 13:52:53 +01002572#if defined(MBEDTLS_SSL_CID)
2573 /*
2574 * Match record's CID with incoming CID.
2575 */
Hanno Becker938489a2019-05-08 13:02:22 +01002576 if( rec->cid_len != transform->in_cid_len ||
2577 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2578 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002579 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002580 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002581#endif /* MBEDTLS_SSL_CID */
2582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2584 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002585 {
2586 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002587 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2588 transform->iv_dec,
2589 transform->ivlen,
2590 data, rec->data_len,
2591 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002594 return( ret );
2595 }
2596
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002597 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 }
Paul Bakker68884e32013-01-07 18:20:04 +01002603 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002605#if defined(MBEDTLS_GCM_C) || \
2606 defined(MBEDTLS_CCM_C) || \
2607 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 mode == MBEDTLS_MODE_CCM ||
2610 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002611 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002612 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002614
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002615 /*
2616 * Compute and update sizes
2617 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002621 "+ taglen (%d)", rec->data_len,
2622 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002624 }
Paul Bakker68884e32013-01-07 18:20:04 +01002625
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002626 /*
2627 * Prepare IV
2628 */
2629 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2630 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002631 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002634
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002635 }
2636 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2637 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002638 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 unsigned char i;
2640
2641 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2642
2643 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002644 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002645 }
2646 else
2647 {
2648 /* Reminder if we ever add an AEAD mode with a different size */
2649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2651 }
2652
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002653 data += explicit_iv_len;
2654 rec->data_offset += explicit_iv_len;
2655 rec->data_len -= explicit_iv_len + transform->taglen;
2656
Hanno Beckercab87e62019-04-29 13:52:53 +01002657 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002658 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002659 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660
2661 memcpy( transform->iv_dec + transform->fixed_ivlen,
2662 data - explicit_iv_len, explicit_iv_len );
2663
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002664 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002666 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002667
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002669 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002670 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002671 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2673 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002674 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002675 data, rec->data_len,
2676 data, &olen,
2677 data + rec->data_len,
2678 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2683 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002684
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002685 return( ret );
2686 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002687 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002688
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002689 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002693 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2697#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002698 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002701 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002702
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 /*
Paul Bakker45829992013-01-03 14:52:21 +01002704 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002707 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2708 {
2709 /* The ciphertext is prefixed with the CBC IV. */
2710 minlen += transform->ivlen;
2711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002712#endif
Paul Bakker45829992013-01-03 14:52:21 +01002713
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 /* Size considerations:
2715 *
2716 * - The CBC cipher text must not be empty and hence
2717 * at least of size transform->ivlen.
2718 *
2719 * Together with the potential IV-prefix, this explains
2720 * the first of the two checks below.
2721 *
2722 * - The record must contain a MAC, either in plain or
2723 * encrypted, depending on whether Encrypt-then-MAC
2724 * is used or not.
2725 * - If it is, the message contains the IV-prefix,
2726 * the CBC ciphertext, and the MAC.
2727 * - If it is not, the padded plaintext, and hence
2728 * the CBC ciphertext, has at least length maclen + 1
2729 * because there is at least the padding length byte.
2730 *
2731 * As the CBC ciphertext is not empty, both cases give the
2732 * lower bound minlen + maclen + 1 on the record size, which
2733 * we test for in the second check below.
2734 */
2735 if( rec->data_len < minlen + transform->ivlen ||
2736 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002739 "+ 1 ) ( + expl IV )", rec->data_len,
2740 transform->ivlen,
2741 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002743 }
2744
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002745 /*
2746 * Authenticate before decrypt if enabled
2747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002750 {
Hanno Becker992b6872017-11-09 18:57:39 +00002751 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002754
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002755 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2756 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002757
Hanno Beckercab87e62019-04-29 13:52:53 +01002758 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002759
Hanno Beckercab87e62019-04-29 13:52:53 +01002760 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2761 add_data_len );
2762 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2763 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002764 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2765 data, rec->data_len );
2766 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2767 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002768
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2770 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002771 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002772 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2775 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002779 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002780 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783
2784 /*
2785 * Check length sanity
2786 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002787 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002790 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002792 }
2793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002796 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002797 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002798 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 /* This is safe because data_len >= minlen + maclen + 1 initially,
2801 * and at this point we have at most subtracted maclen (note that
2802 * minlen == transform->ivlen here). */
2803 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002805 data += transform->ivlen;
2806 rec->data_offset += transform->ivlen;
2807 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002808 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002810
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2812 transform->iv_dec, transform->ivlen,
2813 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002816 return( ret );
2817 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002818
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002819 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002823 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002827 {
2828 /*
2829 * Save IV in SSL3 and TLS1
2830 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2832 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002834#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002836 /* Safe since data_len >= minlen + maclen + 1, so after having
2837 * subtracted at most minlen and maclen up to this point,
2838 * data_len > 0. */
2839 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002840
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002841 if( auth_done == 1 )
2842 {
2843 correct *= ( rec->data_len >= padlen + 1 );
2844 padlen *= ( rec->data_len >= padlen + 1 );
2845 }
2846 else
Paul Bakker45829992013-01-03 14:52:21 +01002847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002849 if( rec->data_len < transform->maclen + padlen + 1 )
2850 {
2851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2852 rec->data_len,
2853 transform->maclen,
2854 padlen + 1 ) );
2855 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002856#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857
2858 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2859 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002862 padlen++;
2863
2864 /* Regardless of the validity of the padding,
2865 * we have data_len >= padlen here. */
2866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002868 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872#if defined(MBEDTLS_SSL_DEBUG_ALL)
2873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 "should be no more than %d",
2875 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002876#endif
Paul Bakker45829992013-01-03 14:52:21 +01002877 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 }
2879 }
2880 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2882#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2883 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002884 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 /* The padding check involves a series of up to 256
2887 * consecutive memory reads at the end of the record
2888 * plaintext buffer. In order to hide the length and
2889 * validity of the padding, always perform exactly
2890 * `min(256,plaintext_len)` reads (but take into account
2891 * only the last `padlen` bytes for the padding check). */
2892 size_t pad_count = 0;
2893 size_t real_count = 0;
2894 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002895
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002896 /* Index of first padding byte; it has been ensured above
2897 * that the subtraction is safe. */
2898 size_t const padding_idx = rec->data_len - padlen;
2899 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2900 size_t const start_idx = rec->data_len - num_checks;
2901 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002902
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002904 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002905 real_count |= ( idx >= padding_idx );
2906 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002907 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002908 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002911 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002913#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002914 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002916 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2918 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002922 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002923
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002924 /* If the padding was found to be invalid, padlen == 0
2925 * and the subtraction is safe. If the padding was found valid,
2926 * padlen hasn't been changed and the previous assertion
2927 * data_len >= padlen still holds. */
2928 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002932 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002938#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002941#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
2943 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002944 * Authenticate if not done yet.
2945 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 */
Hanno Becker52344c22018-01-03 15:24:20 +00002947#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002948 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002949 {
Hanno Becker992b6872017-11-09 18:57:39 +00002950 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002951
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002952 /* If the initial value of padlen was such that
2953 * data_len < maclen + padlen + 1, then padlen
2954 * got reset to 1, and the initial check
2955 * data_len >= minlen + maclen + 1
2956 * guarantees that at this point we still
2957 * have at least data_len >= maclen.
2958 *
2959 * If the initial value of padlen was such that
2960 * data_len >= maclen + padlen + 1, then we have
2961 * subtracted either padlen + 1 (if the padding was correct)
2962 * or 0 (if the padding was incorrect) since then,
2963 * hence data_len >= maclen in any case.
2964 */
2965 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Hanno Beckercab87e62019-04-29 13:52:53 +01002967 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002970 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002971 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002972 ssl_mac( &transform->md_ctx_dec,
2973 transform->mac_dec,
2974 data, rec->data_len,
2975 rec->ctr, rec->type,
2976 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002977 }
2978 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2980#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2981 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002982 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002983 {
2984 /*
2985 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002986 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002987 *
2988 * Known timing attacks:
2989 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2990 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002991 * To compensate for different timings for the MAC calculation
2992 * depending on how much padding was removed (which is determined
2993 * by padlen), process extra_run more blocks through the hash
2994 * function.
2995 *
2996 * The formula in the paper is
2997 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2998 * where L1 is the size of the header plus the decrypted message
2999 * plus CBC padding and L2 is the size of the header plus the
3000 * decrypted message. This is for an underlying hash function
3001 * with 64-byte blocks.
3002 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3003 * correctly. We round down instead of up, so -56 is the correct
3004 * value for our calculations instead of -55.
3005 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003006 * Repeat the formula rather than defining a block_size variable.
3007 * This avoids requiring division by a variable at runtime
3008 * (which would be marginally less efficient and would require
3009 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003010 */
3011 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003012 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003013
3014 /*
3015 * The next two sizes are the minimum and maximum values of
3016 * in_msglen over all padlen values.
3017 *
3018 * They're independent of padlen, since we previously did
3019 * in_msglen -= padlen.
3020 *
3021 * Note that max_len + maclen is never more than the buffer
3022 * length, as we previously did in_msglen -= maclen too.
3023 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003024 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003025 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3026
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003027 memset( tmp, 0, sizeof( tmp ) );
3028
3029 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003030 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003031#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3032 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003033 case MBEDTLS_MD_MD5:
3034 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003035 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003036 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003037 extra_run =
3038 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3039 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003040 break;
3041#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003042#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003044 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003045 extra_run =
3046 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3047 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003048 break;
3049#endif
3050 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3053 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003054
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003055 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003056
Hanno Beckercab87e62019-04-29 13:52:53 +01003057 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3058 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3060 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003061 /* Make sure we access everything even when padlen > 0. This
3062 * makes the synchronisation requirements for just-in-time
3063 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003064 ssl_read_memory( data + rec->data_len, padlen );
3065 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003066
3067 /* Call mbedtls_md_process at least once due to cache attacks
3068 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003069 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003071
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003073
3074 /* Make sure we access all the memory that could contain the MAC,
3075 * before we check it in the next code block. This makes the
3076 * synchronisation requirements for just-in-time Prime+Probe
3077 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003078 ssl_read_memory( data + min_len,
3079 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003080 }
3081 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3083 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003089#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3091 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3095 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#if defined(MBEDTLS_SSL_DEBUG_ALL)
3098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003099#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003100 correct = 0;
3101 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003102 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003103 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003104
3105 /*
3106 * Finally check the correct flag
3107 */
3108 if( correct == 0 )
3109 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003110#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003111
3112 /* Make extra sure authentication was performed, exactly once */
3113 if( auth_done != 1 )
3114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003119#if defined(MBEDTLS_SSL_CID)
3120 if( rec->cid_len != 0 )
3121 {
3122 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3123 &rec->type );
3124 if( ret != 0 )
3125 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3126 }
3127#endif /* MBEDTLS_SSL_CID */
3128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
3131 return( 0 );
3132}
3133
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003134#undef MAC_NONE
3135#undef MAC_PLAINTEXT
3136#undef MAC_CIPHERTEXT
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003139/*
3140 * Compression/decompression functions
3141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003143{
3144 int ret;
3145 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003146 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003147 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003148 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003151
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003152 if( len_pre == 0 )
3153 return( 0 );
3154
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155 memcpy( msg_pre, ssl->out_msg, len_pre );
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003158 ssl->out_msglen ) );
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003161 ssl->out_msg, ssl->out_msglen );
3162
Paul Bakker48916f92012-09-16 19:57:18 +00003163 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3164 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3165 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003166 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167
Paul Bakker48916f92012-09-16 19:57:18 +00003168 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003169 if( ret != Z_OK )
3170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3172 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173 }
3174
Angus Grattond8213d02016-05-25 20:56:48 +10003175 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003176 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179 ssl->out_msglen ) );
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003182 ssl->out_msg, ssl->out_msglen );
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185
3186 return( 0 );
3187}
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190{
3191 int ret;
3192 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003193 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003195 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003198
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003199 if( len_pre == 0 )
3200 return( 0 );
3201
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 memcpy( msg_pre, ssl->in_msg, len_pre );
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205 ssl->in_msglen ) );
3206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003208 ssl->in_msg, ssl->in_msglen );
3209
Paul Bakker48916f92012-09-16 19:57:18 +00003210 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3211 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3212 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003213 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003214 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215
Paul Bakker48916f92012-09-16 19:57:18 +00003216 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003217 if( ret != Z_OK )
3218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3220 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221 }
3222
Angus Grattond8213d02016-05-25 20:56:48 +10003223 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003224 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003227 ssl->in_msglen ) );
3228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003230 ssl->in_msg, ssl->in_msglen );
3231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003233
3234 return( 0 );
3235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3239static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#if defined(MBEDTLS_SSL_PROTO_DTLS)
3242static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003243{
3244 /* If renegotiation is not enforced, retransmit until we would reach max
3245 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003246 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003247 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003248 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003249 unsigned char doublings = 1;
3250
3251 while( ratio != 0 )
3252 {
3253 ++doublings;
3254 ratio >>= 1;
3255 }
3256
3257 if( ++ssl->renego_records_seen > doublings )
3258 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003260 return( 0 );
3261 }
3262 }
3263
3264 return( ssl_write_hello_request( ssl ) );
3265}
3266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003268
Paul Bakker5121ce52009-01-03 21:22:43 +00003269/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003270 * Fill the input message buffer by appending data to it.
3271 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003272 *
3273 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3274 * available (from this read and/or a previous one). Otherwise, an error code
3275 * is returned (possibly EOF or WANT_READ).
3276 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003277 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3278 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3279 * since we always read a whole datagram at once.
3280 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003281 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003282 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003285{
Paul Bakker23986e52011-04-24 08:57:21 +00003286 int ret;
3287 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003291 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003294 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003296 }
3297
Angus Grattond8213d02016-05-25 20:56:48 +10003298 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003302 }
3303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003307 uint32_t timeout;
3308
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003309 /* Just to be sure */
3310 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3311 {
3312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3313 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3315 }
3316
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003317 /*
3318 * The point is, we need to always read a full datagram at once, so we
3319 * sometimes read more then requested, and handle the additional data.
3320 * It could be the rest of the current record (while fetching the
3321 * header) and/or some other records in the same datagram.
3322 */
3323
3324 /*
3325 * Move to the next record in the already read datagram if applicable
3326 */
3327 if( ssl->next_record_offset != 0 )
3328 {
3329 if( ssl->in_left < ssl->next_record_offset )
3330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003333 }
3334
3335 ssl->in_left -= ssl->next_record_offset;
3336
3337 if( ssl->in_left != 0 )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003340 ssl->next_record_offset ) );
3341 memmove( ssl->in_hdr,
3342 ssl->in_hdr + ssl->next_record_offset,
3343 ssl->in_left );
3344 }
3345
3346 ssl->next_record_offset = 0;
3347 }
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003351
3352 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003353 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003354 */
3355 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003358 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003359 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003360
3361 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003362 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003363 * are not at the beginning of a new record, the caller did something
3364 * wrong.
3365 */
3366 if( ssl->in_left != 0 )
3367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3369 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003370 }
3371
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003372 /*
3373 * Don't even try to read if time's out already.
3374 * This avoids by-passing the timer when repeatedly receiving messages
3375 * that will end up being dropped.
3376 */
3377 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003378 {
3379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003380 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003381 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003382 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003383 {
Angus Grattond8213d02016-05-25 20:56:48 +10003384 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003387 timeout = ssl->handshake->retransmit_timeout;
3388 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003389 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003393 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003394 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3395 timeout );
3396 else
3397 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400
3401 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003403 }
3404
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003405 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003408 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003411 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003412 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003415 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003416 }
3417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003421 return( ret );
3422 }
3423
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003424 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003427 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003429 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003430 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003433 return( ret );
3434 }
3435
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003436 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003437 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003439 }
3440
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 if( ret < 0 )
3442 return( ret );
3443
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003444 ssl->in_left = ret;
3445 }
3446 else
3447#endif
3448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003450 ssl->in_left, nb_want ) );
3451
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003452 while( ssl->in_left < nb_want )
3453 {
3454 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003455
3456 if( ssl_check_timer( ssl ) != 0 )
3457 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3458 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003459 {
3460 if( ssl->f_recv_timeout != NULL )
3461 {
3462 ret = ssl->f_recv_timeout( ssl->p_bio,
3463 ssl->in_hdr + ssl->in_left, len,
3464 ssl->conf->read_timeout );
3465 }
3466 else
3467 {
3468 ret = ssl->f_recv( ssl->p_bio,
3469 ssl->in_hdr + ssl->in_left, len );
3470 }
3471 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003474 ssl->in_left, nb_want ) );
3475 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003476
3477 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003479
3480 if( ret < 0 )
3481 return( ret );
3482
mohammad160352aecb92018-03-28 23:41:40 -07003483 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003484 {
Darryl Green11999bb2018-03-13 15:22:58 +00003485 MBEDTLS_SSL_DEBUG_MSG( 1,
3486 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003487 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3489 }
3490
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003491 ssl->in_left += ret;
3492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 }
3494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003496
3497 return( 0 );
3498}
3499
3500/*
3501 * Flush any data not yet written
3502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003504{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003505 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003506 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003510 if( ssl->f_send == NULL )
3511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003513 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003515 }
3516
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003517 /* Avoid incrementing counter if data is flushed */
3518 if( ssl->out_left == 0 )
3519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003521 return( 0 );
3522 }
3523
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 while( ssl->out_left > 0 )
3525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003527 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Hanno Becker2b1e3542018-08-06 11:19:13 +01003529 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003530 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
3534 if( ret <= 0 )
3535 return( ret );
3536
mohammad160352aecb92018-03-28 23:41:40 -07003537 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003538 {
Darryl Green11999bb2018-03-13 15:22:58 +00003539 MBEDTLS_SSL_DEBUG_MSG( 1,
3540 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003541 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3543 }
3544
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 ssl->out_left -= ret;
3546 }
3547
Hanno Becker2b1e3542018-08-06 11:19:13 +01003548#if defined(MBEDTLS_SSL_PROTO_DTLS)
3549 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003550 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003551 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003552 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003553 else
3554#endif
3555 {
3556 ssl->out_hdr = ssl->out_buf + 8;
3557 }
3558 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003561
3562 return( 0 );
3563}
3564
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003565/*
3566 * Functions to handle the DTLS retransmission state machine
3567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003569/*
3570 * Append current handshake message to current outgoing flight
3571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3576 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3577 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578
3579 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003580 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003584 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585 }
3586
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003587 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003588 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 }
3593
3594 /* Copy current handshake message with headers */
3595 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3596 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003597 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598 msg->next = NULL;
3599
3600 /* Append to the current flight */
3601 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003602 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003603 else
3604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606 while( cur->next != NULL )
3607 cur = cur->next;
3608 cur->next = msg;
3609 }
3610
Hanno Becker3b235902018-08-06 09:54:53 +01003611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 return( 0 );
3613}
3614
3615/*
3616 * Free the current flight of handshake messages
3617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 mbedtls_ssl_flight_item *cur = flight;
3621 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003622
3623 while( cur != NULL )
3624 {
3625 next = cur->next;
3626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 mbedtls_free( cur->p );
3628 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003629
3630 cur = next;
3631 }
3632}
3633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3635static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003636#endif
3637
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003638/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003639 * Swap transform_out and out_ctr with the alternative ones
3640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003642{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003644 unsigned char tmp_out_ctr[8];
3645
3646 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003649 return;
3650 }
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003653
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003654 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003655 tmp_transform = ssl->transform_out;
3656 ssl->transform_out = ssl->handshake->alt_transform_out;
3657 ssl->handshake->alt_transform_out = tmp_transform;
3658
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003659 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003660 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3661 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003663
3664 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003665 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3668 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3673 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003674 }
3675 }
3676#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003677}
3678
3679/*
3680 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003681 */
3682int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3683{
3684 int ret = 0;
3685
3686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3687
3688 ret = mbedtls_ssl_flight_transmit( ssl );
3689
3690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3691
3692 return( ret );
3693}
3694
3695/*
3696 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003697 *
3698 * Need to remember the current message in case flush_output returns
3699 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003700 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003702int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003704 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003708 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003710
3711 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003712 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003713 ssl_swap_epochs( ssl );
3714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003716 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003717
3718 while( ssl->handshake->cur_msg != NULL )
3719 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003720 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003721 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003722
Hanno Beckere1dcb032018-08-17 16:47:58 +01003723 int const is_finished =
3724 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3725 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3726
Hanno Becker04da1892018-08-14 13:22:10 +01003727 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3728 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3729
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003730 /* Swap epochs before sending Finished: we can't do it after
3731 * sending ChangeCipherSpec, in case write returns WANT_READ.
3732 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003733 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003734 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003736 ssl_swap_epochs( ssl );
3737 }
3738
Hanno Becker67bc7c32018-08-06 11:33:50 +01003739 ret = ssl_get_remaining_payload_in_datagram( ssl );
3740 if( ret < 0 )
3741 return( ret );
3742 max_frag_len = (size_t) ret;
3743
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003744 /* CCS is copied as is, while HS messages may need fragmentation */
3745 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3746 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003747 if( max_frag_len == 0 )
3748 {
3749 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3750 return( ret );
3751
3752 continue;
3753 }
3754
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003755 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003756 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003757 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003758
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003759 /* Update position inside current message */
3760 ssl->handshake->cur_msg_p += cur->len;
3761 }
3762 else
3763 {
3764 const unsigned char * const p = ssl->handshake->cur_msg_p;
3765 const size_t hs_len = cur->len - 12;
3766 const size_t frag_off = p - ( cur->p + 12 );
3767 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003768 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003769
Hanno Beckere1dcb032018-08-17 16:47:58 +01003770 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003771 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003772 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003773 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003774
Hanno Becker67bc7c32018-08-06 11:33:50 +01003775 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3776 return( ret );
3777
3778 continue;
3779 }
3780 max_hs_frag_len = max_frag_len - 12;
3781
3782 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3783 max_hs_frag_len : rem_len;
3784
3785 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003788 (unsigned) cur_hs_frag_len,
3789 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003790 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003791
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003792 /* Messages are stored with handshake headers as if not fragmented,
3793 * copy beginning of headers then fill fragmentation fields.
3794 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3795 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003796
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003797 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3798 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3799 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3800
Hanno Becker67bc7c32018-08-06 11:33:50 +01003801 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3802 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3803 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003804
3805 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3806
Hanno Becker3f7b9732018-08-28 09:53:25 +01003807 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3809 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003810 ssl->out_msgtype = cur->type;
3811
3812 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003813 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003814 }
3815
3816 /* If done with the current message move to the next one if any */
3817 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3818 {
3819 if( cur->next != NULL )
3820 {
3821 ssl->handshake->cur_msg = cur->next;
3822 ssl->handshake->cur_msg_p = cur->next->p + 12;
3823 }
3824 else
3825 {
3826 ssl->handshake->cur_msg = NULL;
3827 ssl->handshake->cur_msg_p = NULL;
3828 }
3829 }
3830
3831 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003832 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003835 return( ret );
3836 }
3837 }
3838
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3840 return( ret );
3841
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003842 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3844 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003845 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003848 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3849 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003850
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003852
3853 return( 0 );
3854}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003855
3856/*
3857 * To be called when the last message of an incoming flight is received.
3858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003860{
3861 /* We won't need to resend that one any more */
3862 ssl_flight_free( ssl->handshake->flight );
3863 ssl->handshake->flight = NULL;
3864 ssl->handshake->cur_msg = NULL;
3865
3866 /* The next incoming flight will start with this msg_seq */
3867 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3868
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003869 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003870 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003871
Hanno Becker0271f962018-08-16 13:23:47 +01003872 /* Clear future message buffering structure. */
3873 ssl_buffering_free( ssl );
3874
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003875 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003876 ssl_set_timer( ssl, 0 );
3877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3879 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003882 }
3883 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003885}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003886
3887/*
3888 * To be called when the last message of an outgoing flight is send.
3889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003891{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003892 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003893 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3896 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003899 }
3900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003904
Paul Bakker5121ce52009-01-03 21:22:43 +00003905/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003906 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003907 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003908
3909/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003910 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003911 *
3912 * - fill in handshake headers
3913 * - update handshake checksum
3914 * - DTLS: save message for resending
3915 * - then pass to the record layer
3916 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003917 * DTLS: except for HelloRequest, messages are only queued, and will only be
3918 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003919 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003920 * Inputs:
3921 * - ssl->out_msglen: 4 + actual handshake message len
3922 * (4 is the size of handshake headers for TLS)
3923 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3924 * - ssl->out_msg + 4: the handshake message body
3925 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003926 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003927 * - ssl->out_msglen: the length of the record contents
3928 * (including handshake headers but excluding record headers)
3929 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003930 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003931int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003932{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003933 int ret;
3934 const size_t hs_len = ssl->out_msglen - 4;
3935 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003936
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3938
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003939 /*
3940 * Sanity checks
3941 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003942 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003943 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3944 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003945 /* In SSLv3, the client might send a NoCertificate alert. */
3946#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3947 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3948 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3949 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3950#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3951 {
3952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3954 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003957 /* Whenever we send anything different from a
3958 * HelloRequest we should be in a handshake - double check. */
3959 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3960 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003961 ssl->handshake == NULL )
3962 {
3963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003968 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003971 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003974 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003976
Hanno Beckerb50a2532018-08-06 11:52:54 +01003977 /* Double-check that we did not exceed the bounds
3978 * of the outgoing record buffer.
3979 * This should never fail as the various message
3980 * writing functions must obey the bounds of the
3981 * outgoing record buffer, but better be safe.
3982 *
3983 * Note: We deliberately do not check for the MTU or MFL here.
3984 */
3985 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3986 {
3987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3988 "size %u, maximum %u",
3989 (unsigned) ssl->out_msglen,
3990 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3992 }
3993
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003994 /*
3995 * Fill handshake headers
3996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003999 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4000 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4001 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004002
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004003 /*
4004 * DTLS has additional fields in the Handshake layer,
4005 * between the length field and the actual payload:
4006 * uint16 message_seq;
4007 * uint24 fragment_offset;
4008 * uint24 fragment_length;
4009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004011 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004012 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004013 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004014 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004015 {
4016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4017 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004018 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004019 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4021 }
4022
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004023 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004024 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004025
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004026 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004027 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004028 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004029 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4030 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4031 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004032 }
4033 else
4034 {
4035 ssl->out_msg[4] = 0;
4036 ssl->out_msg[5] = 0;
4037 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004038
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004039 /* Handshake hashes are computed without fragmentation,
4040 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004041 memset( ssl->out_msg + 6, 0x00, 3 );
4042 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004045
Hanno Becker0207e532018-08-28 10:28:28 +01004046 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004047 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4048 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004049 }
4050
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004051 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004054 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4055 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056 {
4057 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004060 return( ret );
4061 }
4062 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004063 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004065 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004066 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004067 {
4068 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4069 return( ret );
4070 }
4071 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004072
4073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4074
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004075 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004076}
4077
4078/*
4079 * Record layer functions
4080 */
4081
4082/*
4083 * Write current record.
4084 *
4085 * Uses:
4086 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4087 * - ssl->out_msglen: length of the record content (excl headers)
4088 * - ssl->out_msg: record content
4089 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004090int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004091{
4092 int ret, done = 0;
4093 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004094 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004095
4096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004099 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004101 {
4102 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004105 return( ret );
4106 }
4107
4108 len = ssl->out_msglen;
4109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4113 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 ret = mbedtls_ssl_hw_record_write( ssl );
4118 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4121 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004122 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004123
4124 if( ret == 0 )
4125 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004126 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004128 if( !done )
4129 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004130 unsigned i;
4131 size_t protected_record_size;
4132
Hanno Becker6430faf2019-05-08 11:57:13 +01004133 /* Skip writing the record content type to after the encryption,
4134 * as it may change when using the CID extension. */
4135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004137 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004138
Hanno Becker19859472018-08-06 09:40:20 +01004139 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004140 ssl->out_len[0] = (unsigned char)( len >> 8 );
4141 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004142
Paul Bakker48916f92012-09-16 19:57:18 +00004143 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004144 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004145 mbedtls_record rec;
4146
4147 rec.buf = ssl->out_iv;
4148 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4149 ( ssl->out_iv - ssl->out_buf );
4150 rec.data_len = ssl->out_msglen;
4151 rec.data_offset = ssl->out_msg - rec.buf;
4152
4153 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4154 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4155 ssl->conf->transport, rec.ver );
4156 rec.type = ssl->out_msgtype;
4157
Hanno Becker43c24b82019-05-01 09:45:57 +01004158#if defined(MBEDTLS_SSL_CID)
4159 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004160 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004161#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004162
Hanno Beckera18d1322018-01-03 14:27:32 +00004163 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004164 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004167 return( ret );
4168 }
4169
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004170 if( rec.data_offset != 0 )
4171 {
4172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4174 }
4175
Hanno Becker6430faf2019-05-08 11:57:13 +01004176 /* Update the record content type and CID. */
4177 ssl->out_msgtype = rec.type;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004178#if defined(MBEDTLS_SSL_CID )
4179 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
4180#endif /* MBEDTLS_SSL_CID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004181 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004182 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4183 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004184 }
4185
Hanno Becker5903de42019-05-03 14:46:38 +01004186 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004187
4188#if defined(MBEDTLS_SSL_PROTO_DTLS)
4189 /* In case of DTLS, double-check that we don't exceed
4190 * the remaining space in the datagram. */
4191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4192 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004193 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004194 if( ret < 0 )
4195 return( ret );
4196
4197 if( protected_record_size > (size_t) ret )
4198 {
4199 /* Should never happen */
4200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4201 }
4202 }
4203#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004204
Hanno Becker6430faf2019-05-08 11:57:13 +01004205 /* Now write the potentially updated record content type. */
4206 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004209 "version = [%d:%d], msglen = %d",
4210 ssl->out_hdr[0], ssl->out_hdr[1],
4211 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004214 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004215
4216 ssl->out_left += protected_record_size;
4217 ssl->out_hdr += protected_record_size;
4218 ssl_update_out_pointers( ssl, ssl->transform_out );
4219
Hanno Becker04484622018-08-06 09:49:38 +01004220 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4221 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4222 break;
4223
4224 /* The loop goes to its end iff the counter is wrapping */
4225 if( i == ssl_ep_len( ssl ) )
4226 {
4227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4228 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4229 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004230 }
4231
Hanno Becker67bc7c32018-08-06 11:33:50 +01004232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004233 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4234 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004235 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004236 size_t remaining;
4237 ret = ssl_get_remaining_payload_in_datagram( ssl );
4238 if( ret < 0 )
4239 {
4240 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4241 ret );
4242 return( ret );
4243 }
4244
4245 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004246 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004247 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004248 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004249 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004250 else
4251 {
Hanno Becker513815a2018-08-20 11:56:09 +01004252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004253 }
4254 }
4255#endif /* MBEDTLS_SSL_PROTO_DTLS */
4256
4257 if( ( flush == SSL_FORCE_FLUSH ) &&
4258 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004261 return( ret );
4262 }
4263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004265
4266 return( 0 );
4267}
4268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004270
4271static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4272{
4273 if( ssl->in_msglen < ssl->in_hslen ||
4274 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4275 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4276 {
4277 return( 1 );
4278 }
4279 return( 0 );
4280}
Hanno Becker44650b72018-08-16 12:51:11 +01004281
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004282static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004283{
4284 return( ( ssl->in_msg[9] << 16 ) |
4285 ( ssl->in_msg[10] << 8 ) |
4286 ssl->in_msg[11] );
4287}
4288
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004289static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004290{
4291 return( ( ssl->in_msg[6] << 16 ) |
4292 ( ssl->in_msg[7] << 8 ) |
4293 ssl->in_msg[8] );
4294}
4295
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004296static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004297{
4298 uint32_t msg_len, frag_off, frag_len;
4299
4300 msg_len = ssl_get_hs_total_len( ssl );
4301 frag_off = ssl_get_hs_frag_off( ssl );
4302 frag_len = ssl_get_hs_frag_len( ssl );
4303
4304 if( frag_off > msg_len )
4305 return( -1 );
4306
4307 if( frag_len > msg_len - frag_off )
4308 return( -1 );
4309
4310 if( frag_len + 12 > ssl->in_msglen )
4311 return( -1 );
4312
4313 return( 0 );
4314}
4315
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004316/*
4317 * Mark bits in bitmask (used for DTLS HS reassembly)
4318 */
4319static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4320{
4321 unsigned int start_bits, end_bits;
4322
4323 start_bits = 8 - ( offset % 8 );
4324 if( start_bits != 8 )
4325 {
4326 size_t first_byte_idx = offset / 8;
4327
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004328 /* Special case */
4329 if( len <= start_bits )
4330 {
4331 for( ; len != 0; len-- )
4332 mask[first_byte_idx] |= 1 << ( start_bits - len );
4333
4334 /* Avoid potential issues with offset or len becoming invalid */
4335 return;
4336 }
4337
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004338 offset += start_bits; /* Now offset % 8 == 0 */
4339 len -= start_bits;
4340
4341 for( ; start_bits != 0; start_bits-- )
4342 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4343 }
4344
4345 end_bits = len % 8;
4346 if( end_bits != 0 )
4347 {
4348 size_t last_byte_idx = ( offset + len ) / 8;
4349
4350 len -= end_bits; /* Now len % 8 == 0 */
4351
4352 for( ; end_bits != 0; end_bits-- )
4353 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4354 }
4355
4356 memset( mask + offset / 8, 0xFF, len / 8 );
4357}
4358
4359/*
4360 * Check that bitmask is full
4361 */
4362static int ssl_bitmask_check( unsigned char *mask, size_t len )
4363{
4364 size_t i;
4365
4366 for( i = 0; i < len / 8; i++ )
4367 if( mask[i] != 0xFF )
4368 return( -1 );
4369
4370 for( i = 0; i < len % 8; i++ )
4371 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4372 return( -1 );
4373
4374 return( 0 );
4375}
4376
Hanno Becker56e205e2018-08-16 09:06:12 +01004377/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004378static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004379 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004380{
Hanno Becker56e205e2018-08-16 09:06:12 +01004381 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004382
Hanno Becker56e205e2018-08-16 09:06:12 +01004383 alloc_len = 12; /* Handshake header */
4384 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004385
Hanno Beckerd07df862018-08-16 09:14:58 +01004386 if( add_bitmap )
4387 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004388
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004389 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004390}
Hanno Becker56e205e2018-08-16 09:06:12 +01004391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004393
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004394static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004395{
4396 return( ( ssl->in_msg[1] << 16 ) |
4397 ( ssl->in_msg[2] << 8 ) |
4398 ssl->in_msg[3] );
4399}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004400
Simon Butcher99000142016-10-13 17:21:01 +01004401int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004406 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004408 }
4409
Hanno Becker12555c62018-08-16 12:47:53 +01004410 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004413 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004414 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004417 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004418 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004419 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004420 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004421
Hanno Becker44650b72018-08-16 12:51:11 +01004422 if( ssl_check_hs_header( ssl ) != 0 )
4423 {
4424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4425 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4426 }
4427
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004428 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004429 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4430 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4431 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4432 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004433 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004434 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4435 {
4436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4437 recv_msg_seq,
4438 ssl->handshake->in_msg_seq ) );
4439 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4440 }
4441
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004442 /* Retransmit only on last message from previous flight, to avoid
4443 * too many retransmissions.
4444 * Besides, No sane server ever retransmits HelloVerifyRequest */
4445 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004449 "message_seq = %d, start_of_flight = %d",
4450 recv_msg_seq,
4451 ssl->handshake->in_flight_start_seq ) );
4452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004456 return( ret );
4457 }
4458 }
4459 else
4460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004462 "message_seq = %d, expected = %d",
4463 recv_msg_seq,
4464 ssl->handshake->in_msg_seq ) );
4465 }
4466
Hanno Becker90333da2017-10-10 11:27:13 +01004467 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004468 }
4469 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004470
Hanno Becker6d97ef52018-08-16 13:09:04 +01004471 /* Message reassembly is handled alongside buffering of future
4472 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004473 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004474 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004475 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004478 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004479 }
4480 }
4481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004483 /* With TLS we don't handle fragmentation (for now) */
4484 if( ssl->in_msglen < ssl->in_hslen )
4485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4487 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004488 }
4489
Simon Butcher99000142016-10-13 17:21:01 +01004490 return( 0 );
4491}
4492
4493void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4494{
Hanno Becker0271f962018-08-16 13:23:47 +01004495 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004496
Hanno Becker0271f962018-08-16 13:23:47 +01004497 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004498 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004499 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004500 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004501
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004502 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004505 ssl->handshake != NULL )
4506 {
Hanno Becker0271f962018-08-16 13:23:47 +01004507 unsigned offset;
4508 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004509
Hanno Becker0271f962018-08-16 13:23:47 +01004510 /* Increment handshake sequence number */
4511 hs->in_msg_seq++;
4512
4513 /*
4514 * Clear up handshake buffering and reassembly structure.
4515 */
4516
4517 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004518 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004519
4520 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004521 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4522 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004523 offset++, hs_buf++ )
4524 {
4525 *hs_buf = *(hs_buf + 1);
4526 }
4527
4528 /* Create a fresh last entry */
4529 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004530 }
4531#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004532}
4533
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004534/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004535 * DTLS anti-replay: RFC 6347 4.1.2.6
4536 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004537 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4538 * Bit n is set iff record number in_window_top - n has been seen.
4539 *
4540 * Usually, in_window_top is the last record number seen and the lsb of
4541 * in_window is set. The only exception is the initial state (record number 0
4542 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4545static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004546{
4547 ssl->in_window_top = 0;
4548 ssl->in_window = 0;
4549}
4550
4551static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4552{
4553 return( ( (uint64_t) buf[0] << 40 ) |
4554 ( (uint64_t) buf[1] << 32 ) |
4555 ( (uint64_t) buf[2] << 24 ) |
4556 ( (uint64_t) buf[3] << 16 ) |
4557 ( (uint64_t) buf[4] << 8 ) |
4558 ( (uint64_t) buf[5] ) );
4559}
4560
4561/*
4562 * Return 0 if sequence number is acceptable, -1 otherwise
4563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004565{
4566 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4567 uint64_t bit;
4568
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004569 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004570 return( 0 );
4571
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004572 if( rec_seqnum > ssl->in_window_top )
4573 return( 0 );
4574
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004575 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004576
4577 if( bit >= 64 )
4578 return( -1 );
4579
4580 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4581 return( -1 );
4582
4583 return( 0 );
4584}
4585
4586/*
4587 * Update replay window on new validated record
4588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004590{
4591 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4592
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004593 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004594 return;
4595
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596 if( rec_seqnum > ssl->in_window_top )
4597 {
4598 /* Update window_top and the contents of the window */
4599 uint64_t shift = rec_seqnum - ssl->in_window_top;
4600
4601 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004602 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004603 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004604 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004605 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004606 ssl->in_window |= 1;
4607 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004608
4609 ssl->in_window_top = rec_seqnum;
4610 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611 else
4612 {
4613 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004614 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004615
4616 if( bit < 64 ) /* Always true, but be extra sure */
4617 ssl->in_window |= (uint64_t) 1 << bit;
4618 }
4619}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004621
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004622#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004623/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004624static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4625
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004626/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004627 * Without any SSL context, check if a datagram looks like a ClientHello with
4628 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004629 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004630 *
4631 * - if cookie is valid, return 0
4632 * - if ClientHello looks superficially valid but cookie is not,
4633 * fill obuf and set olen, then
4634 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4635 * - otherwise return a specific error code
4636 */
4637static int ssl_check_dtls_clihlo_cookie(
4638 mbedtls_ssl_cookie_write_t *f_cookie_write,
4639 mbedtls_ssl_cookie_check_t *f_cookie_check,
4640 void *p_cookie,
4641 const unsigned char *cli_id, size_t cli_id_len,
4642 const unsigned char *in, size_t in_len,
4643 unsigned char *obuf, size_t buf_len, size_t *olen )
4644{
4645 size_t sid_len, cookie_len;
4646 unsigned char *p;
4647
4648 if( f_cookie_write == NULL || f_cookie_check == NULL )
4649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4650
4651 /*
4652 * Structure of ClientHello with record and handshake headers,
4653 * and expected values. We don't need to check a lot, more checks will be
4654 * done when actually parsing the ClientHello - skipping those checks
4655 * avoids code duplication and does not make cookie forging any easier.
4656 *
4657 * 0-0 ContentType type; copied, must be handshake
4658 * 1-2 ProtocolVersion version; copied
4659 * 3-4 uint16 epoch; copied, must be 0
4660 * 5-10 uint48 sequence_number; copied
4661 * 11-12 uint16 length; (ignored)
4662 *
4663 * 13-13 HandshakeType msg_type; (ignored)
4664 * 14-16 uint24 length; (ignored)
4665 * 17-18 uint16 message_seq; copied
4666 * 19-21 uint24 fragment_offset; copied, must be 0
4667 * 22-24 uint24 fragment_length; (ignored)
4668 *
4669 * 25-26 ProtocolVersion client_version; (ignored)
4670 * 27-58 Random random; (ignored)
4671 * 59-xx SessionID session_id; 1 byte len + sid_len content
4672 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4673 * ...
4674 *
4675 * Minimum length is 61 bytes.
4676 */
4677 if( in_len < 61 ||
4678 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4679 in[3] != 0 || in[4] != 0 ||
4680 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4681 {
4682 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4683 }
4684
4685 sid_len = in[59];
4686 if( sid_len > in_len - 61 )
4687 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4688
4689 cookie_len = in[60 + sid_len];
4690 if( cookie_len > in_len - 60 )
4691 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4692
4693 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4694 cli_id, cli_id_len ) == 0 )
4695 {
4696 /* Valid cookie */
4697 return( 0 );
4698 }
4699
4700 /*
4701 * If we get here, we've got an invalid cookie, let's prepare HVR.
4702 *
4703 * 0-0 ContentType type; copied
4704 * 1-2 ProtocolVersion version; copied
4705 * 3-4 uint16 epoch; copied
4706 * 5-10 uint48 sequence_number; copied
4707 * 11-12 uint16 length; olen - 13
4708 *
4709 * 13-13 HandshakeType msg_type; hello_verify_request
4710 * 14-16 uint24 length; olen - 25
4711 * 17-18 uint16 message_seq; copied
4712 * 19-21 uint24 fragment_offset; copied
4713 * 22-24 uint24 fragment_length; olen - 25
4714 *
4715 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4716 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4717 *
4718 * Minimum length is 28.
4719 */
4720 if( buf_len < 28 )
4721 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4722
4723 /* Copy most fields and adapt others */
4724 memcpy( obuf, in, 25 );
4725 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4726 obuf[25] = 0xfe;
4727 obuf[26] = 0xff;
4728
4729 /* Generate and write actual cookie */
4730 p = obuf + 28;
4731 if( f_cookie_write( p_cookie,
4732 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4733 {
4734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4735 }
4736
4737 *olen = p - obuf;
4738
4739 /* Go back and fill length fields */
4740 obuf[27] = (unsigned char)( *olen - 28 );
4741
4742 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4743 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4744 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4745
4746 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4747 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4748
4749 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4750}
4751
4752/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004753 * Handle possible client reconnect with the same UDP quadruplet
4754 * (RFC 6347 Section 4.2.8).
4755 *
4756 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4757 * that looks like a ClientHello.
4758 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004759 * - if the input looks like a ClientHello without cookies,
4760 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004761 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004762 * - if the input looks like a ClientHello with a valid cookie,
4763 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004764 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004765 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004767 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004768 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4769 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004770 */
4771static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4772{
4773 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004774 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004775
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004776 ret = ssl_check_dtls_clihlo_cookie(
4777 ssl->conf->f_cookie_write,
4778 ssl->conf->f_cookie_check,
4779 ssl->conf->p_cookie,
4780 ssl->cli_id, ssl->cli_id_len,
4781 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004782 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004783
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004784 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4785
4786 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004787 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004788 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004789 * If the error is permanent we'll catch it later,
4790 * if it's not, then hopefully it'll work next time. */
4791 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4792
4793 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004794 }
4795
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004796 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004797 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004798 /* Got a valid cookie, partially reset context */
4799 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4800 {
4801 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4802 return( ret );
4803 }
4804
4805 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004806 }
4807
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004808 return( ret );
4809}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004810#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004811
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004812static int ssl_check_record_type( uint8_t record_type )
4813{
4814 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4815 record_type != MBEDTLS_SSL_MSG_ALERT &&
4816 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4817 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4818 {
4819 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4820 }
4821
4822 return( 0 );
4823}
4824
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004825/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004826 * ContentType type;
4827 * ProtocolVersion version;
4828 * uint16 epoch; // DTLS only
4829 * uint48 sequence_number; // DTLS only
4830 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004831 *
4832 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004833 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004834 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4835 *
4836 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004837 * 1. proceed with the record if this function returns 0
4838 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4839 * 3. return CLIENT_RECONNECT if this function return that value
4840 * 4. drop the whole datagram if this function returns anything else.
4841 * Point 2 is needed when the peer is resending, and we have already received
4842 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004845{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004846 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004847 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004848
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004849 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004850
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004852 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004853
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004854 /* Check record type */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004855#if defined(MBEDTLS_SSL_CID)
4856 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4857 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4858 ssl->conf->cid_len != 0 )
4859 {
4860 /* Shift pointers to account for record header including CID
4861 * struct {
4862 * ContentType special_type = tls12_cid;
4863 * ProtocolVersion version;
4864 * uint16 epoch;
4865 * uint48 sequence_number;
4866 * opaque cid[cid_length]; // New field
4867 * uint16 length;
4868 * opaque enc_content[DTLSCiphertext.length];
4869 * } DTLSCiphertext;
4870 */
4871
4872 /* So far, we only support static CID lengths
4873 * fixed in the configuration. */
4874 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4875 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4876 }
4877 else
4878#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004879 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004882
4883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004884 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4885 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004886 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4887#endif /* MBEDTLS_SSL_PROTO_DTLS */
4888 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4889 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004892 }
4893
4894 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004895 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4898 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004899 }
4900
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004901 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 }
4906
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004907 /* Now that the total length of the record header is known, ensure
4908 * that the current datagram is large enough to hold it.
4909 * This would fail, for example, if we received a datagram of
4910 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4911 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4912 if( ret != 0 )
4913 {
4914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4915 return( ret );
4916 }
4917 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4918
4919 /* Parse and validate record length
4920 * This must happen after the CID parsing because
4921 * its position in the record header depends on
4922 * the presence of a CID. */
4923
4924 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004925 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004926 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4929 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004930 }
4931
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4933 "version = [%d:%d], msglen = %d",
4934 ssl->in_msgtype,
4935 major_ver, minor_ver, ssl->in_msglen ) );
4936
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004937 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004938 * DTLS-related tests.
4939 * Check epoch before checking length constraint because
4940 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4941 * message gets duplicated before the corresponding Finished message,
4942 * the second ChangeCipherSpec should be discarded because it belongs
4943 * to an old epoch, but not because its length is shorter than
4944 * the minimum record length for packets using the new record transform.
4945 * Note that these two kinds of failures are handled differently,
4946 * as an unexpected record is silently skipped but an invalid
4947 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004948 */
4949#if defined(MBEDTLS_SSL_PROTO_DTLS)
4950 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4951 {
4952 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4953
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004954 /* Check epoch (and sequence number) with DTLS */
4955 if( rec_epoch != ssl->in_epoch )
4956 {
4957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4958 "expected %d, received %d",
4959 ssl->in_epoch, rec_epoch ) );
4960
4961#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4962 /*
4963 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4964 * access the first byte of record content (handshake type), as we
4965 * have an active transform (possibly iv_len != 0), so use the
4966 * fact that the record header len is 13 instead.
4967 */
4968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4969 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4970 rec_epoch == 0 &&
4971 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4972 ssl->in_left > 13 &&
4973 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4974 {
4975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4976 "from the same port" ) );
4977 return( ssl_handle_possible_reconnect( ssl ) );
4978 }
4979 else
4980#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004981 {
4982 /* Consider buffering the record. */
4983 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4984 {
4985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4986 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4987 }
4988
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004989 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004990 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004991 }
4992
4993#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4994 /* Replay detection only works for the current epoch */
4995 if( rec_epoch == ssl->in_epoch &&
4996 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4997 {
4998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4999 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5000 }
5001#endif
5002 }
5003#endif /* MBEDTLS_SSL_PROTO_DTLS */
5004
Hanno Becker52c6dc62017-05-26 16:07:36 +01005005
5006 /* Check length against bounds of the current transform and version */
5007 if( ssl->transform_in == NULL )
5008 {
5009 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005010 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005011 {
5012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5013 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5014 }
5015 }
5016 else
5017 {
5018 if( ssl->in_msglen < ssl->transform_in->minlen )
5019 {
5020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5021 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5022 }
5023
5024#if defined(MBEDTLS_SSL_PROTO_SSL3)
5025 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005026 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005027 {
5028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5029 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5030 }
5031#endif
5032#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5033 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5034 /*
5035 * TLS encrypted messages can have up to 256 bytes of padding
5036 */
5037 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5038 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005039 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005040 {
5041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5042 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5043 }
5044#endif
5045 }
5046
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005047 return( 0 );
5048}
Paul Bakker5121ce52009-01-03 21:22:43 +00005049
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005050/*
5051 * If applicable, decrypt (and decompress) record content
5052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005054{
5055 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005058 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5061 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 ret = mbedtls_ssl_hw_record_read( ssl );
5066 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5069 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005070 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005071
5072 if( ret == 0 )
5073 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005076 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005077 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005078 mbedtls_record rec;
5079
5080 rec.buf = ssl->in_iv;
5081 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5082 - ( ssl->in_iv - ssl->in_buf );
5083 rec.data_len = ssl->in_msglen;
5084 rec.data_offset = 0;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005085#if defined(MBEDTLS_SSL_CID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005086 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005087 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
5088#endif /* MBEDTLS_SSL_CID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005089
5090 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5091 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5092 ssl->conf->transport, rec.ver );
5093 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005094 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5095 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005098
5099#if defined(MBEDTLS_SSL_CID)
5100 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5101 ssl->conf->ignore_unexpected_cid
5102 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5103 {
Hanno Becker16ded982019-05-08 13:02:55 +01005104 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005105 }
5106#endif /* MBEDTLS_SSL_CID */
Hanno Becker16ded982019-05-08 13:02:55 +01005107
Paul Bakker5121ce52009-01-03 21:22:43 +00005108 return( ret );
5109 }
5110
Hanno Becker6430faf2019-05-08 11:57:13 +01005111 if( ssl->in_msgtype != rec.type )
5112 {
5113 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5114 ssl->in_msgtype, rec.type ) );
5115 }
5116
5117 /* The record content type may change during decryption,
5118 * so re-read it. */
5119 ssl->in_msgtype = rec.type;
5120 /* Also update the input buffer, because unfortunately
5121 * the server-side ssl_parse_client_hello() reparses the
5122 * record header when receiving a ClientHello initiating
5123 * a renegotiation. */
5124 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005125 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005126 ssl->in_msglen = rec.data_len;
5127 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5128 ssl->in_len[1] = (unsigned char)( rec.data_len );
5129
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005130 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5131 ssl->in_msg, ssl->in_msglen );
5132
Hanno Becker6430faf2019-05-08 11:57:13 +01005133#if defined(MBEDTLS_SSL_CID)
5134 /* We have already checked the record content type
5135 * in ssl_parse_record_header(), failing or silently
5136 * dropping the record in the case of an unknown type.
5137 *
5138 * Since with the use of CIDs, the record content type
5139 * might change during decryption, re-check the record
5140 * content type, but treat a failure as fatal this time. */
5141 if( ssl_check_record_type( ssl->in_msgtype ) )
5142 {
5143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5144 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5145 }
5146#endif /* MBEDTLS_SSL_CID */
5147
Angus Grattond8213d02016-05-25 20:56:48 +10005148 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5151 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005153 else if( ssl->in_msglen == 0 )
5154 {
5155#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5156 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5157 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5158 {
5159 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5161 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5162 }
5163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5164
5165 ssl->nb_zero++;
5166
5167 /*
5168 * Three or more empty messages may be a DoS attack
5169 * (excessive CPU consumption).
5170 */
5171 if( ssl->nb_zero > 3 )
5172 {
5173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005174 "messages, possible DoS attack" ) );
5175 /* Treat the records as if they were not properly authenticated,
5176 * thereby failing the connection if we see more than allowed
5177 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005178 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5179 }
5180 }
5181 else
5182 ssl->nb_zero = 0;
5183
5184#if defined(MBEDTLS_SSL_PROTO_DTLS)
5185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5186 {
5187 ; /* in_ctr read from peer, not maintained internally */
5188 }
5189 else
5190#endif
5191 {
5192 unsigned i;
5193 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5194 if( ++ssl->in_ctr[i - 1] != 0 )
5195 break;
5196
5197 /* The loop goes to its end iff the counter is wrapping */
5198 if( i == ssl_ep_len( ssl ) )
5199 {
5200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5201 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5202 }
5203 }
5204
Paul Bakker5121ce52009-01-03 21:22:43 +00005205 }
5206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005208 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005210 {
5211 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005214 return( ret );
5215 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005216 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005220 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005223 }
5224#endif
5225
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005226 return( 0 );
5227}
5228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005230
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005231/*
5232 * Read a record.
5233 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005234 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5235 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5236 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005237 */
Hanno Becker1097b342018-08-15 14:09:41 +01005238
5239/* Helper functions for mbedtls_ssl_read_record(). */
5240static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005241static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5242static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005243
Hanno Becker327c93b2018-08-15 13:56:18 +01005244int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005245 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005246{
5247 int ret;
5248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005250
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005251 if( ssl->keep_current_message == 0 )
5252 {
5253 do {
Simon Butcher99000142016-10-13 17:21:01 +01005254
Hanno Becker26994592018-08-15 14:14:59 +01005255 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005256 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005257 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005258
Hanno Beckere74d5562018-08-15 14:26:08 +01005259 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005260 {
Hanno Becker40f50842018-08-15 14:48:01 +01005261#if defined(MBEDTLS_SSL_PROTO_DTLS)
5262 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005263
Hanno Becker40f50842018-08-15 14:48:01 +01005264 /* We only check for buffered messages if the
5265 * current datagram is fully consumed. */
5266 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005267 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005268 {
Hanno Becker40f50842018-08-15 14:48:01 +01005269 if( ssl_load_buffered_message( ssl ) == 0 )
5270 have_buffered = 1;
5271 }
5272
5273 if( have_buffered == 0 )
5274#endif /* MBEDTLS_SSL_PROTO_DTLS */
5275 {
5276 ret = ssl_get_next_record( ssl );
5277 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5278 continue;
5279
5280 if( ret != 0 )
5281 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005282 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005283 return( ret );
5284 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005285 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005286 }
5287
5288 ret = mbedtls_ssl_handle_message_type( ssl );
5289
Hanno Becker40f50842018-08-15 14:48:01 +01005290#if defined(MBEDTLS_SSL_PROTO_DTLS)
5291 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5292 {
5293 /* Buffer future message */
5294 ret = ssl_buffer_message( ssl );
5295 if( ret != 0 )
5296 return( ret );
5297
5298 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5299 }
5300#endif /* MBEDTLS_SSL_PROTO_DTLS */
5301
Hanno Becker90333da2017-10-10 11:27:13 +01005302 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5303 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005304
5305 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005306 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005307 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005308 return( ret );
5309 }
5310
Hanno Becker327c93b2018-08-15 13:56:18 +01005311 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005312 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005313 {
5314 mbedtls_ssl_update_handshake_status( ssl );
5315 }
Simon Butcher99000142016-10-13 17:21:01 +01005316 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005317 else
Simon Butcher99000142016-10-13 17:21:01 +01005318 {
Hanno Becker02f59072018-08-15 14:00:24 +01005319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005320 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005321 }
5322
5323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5324
5325 return( 0 );
5326}
5327
Hanno Becker40f50842018-08-15 14:48:01 +01005328#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005329static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005330{
Hanno Becker40f50842018-08-15 14:48:01 +01005331 if( ssl->in_left > ssl->next_record_offset )
5332 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005333
Hanno Becker40f50842018-08-15 14:48:01 +01005334 return( 0 );
5335}
5336
5337static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5338{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005339 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005340 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005341 int ret = 0;
5342
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005343 if( hs == NULL )
5344 return( -1 );
5345
Hanno Beckere00ae372018-08-20 09:39:42 +01005346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5347
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005348 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5349 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5350 {
5351 /* Check if we have seen a ChangeCipherSpec before.
5352 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005353 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005354 {
5355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5356 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005357 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005358 }
5359
Hanno Becker39b8bc92018-08-28 17:17:13 +01005360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005361 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5362 ssl->in_msglen = 1;
5363 ssl->in_msg[0] = 1;
5364
5365 /* As long as they are equal, the exact value doesn't matter. */
5366 ssl->in_left = 0;
5367 ssl->next_record_offset = 0;
5368
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005369 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005370 goto exit;
5371 }
Hanno Becker37f95322018-08-16 13:55:32 +01005372
Hanno Beckerb8f50142018-08-28 10:01:34 +01005373#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005374 /* Debug only */
5375 {
5376 unsigned offset;
5377 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5378 {
5379 hs_buf = &hs->buffering.hs[offset];
5380 if( hs_buf->is_valid == 1 )
5381 {
5382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5383 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005384 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005385 }
5386 }
5387 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005388#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005389
5390 /* Check if we have buffered and/or fully reassembled the
5391 * next handshake message. */
5392 hs_buf = &hs->buffering.hs[0];
5393 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5394 {
5395 /* Synthesize a record containing the buffered HS message. */
5396 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5397 ( hs_buf->data[2] << 8 ) |
5398 hs_buf->data[3];
5399
5400 /* Double-check that we haven't accidentally buffered
5401 * a message that doesn't fit into the input buffer. */
5402 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5403 {
5404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5406 }
5407
5408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5409 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5410 hs_buf->data, msg_len + 12 );
5411
5412 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5413 ssl->in_hslen = msg_len + 12;
5414 ssl->in_msglen = msg_len + 12;
5415 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5416
5417 ret = 0;
5418 goto exit;
5419 }
5420 else
5421 {
5422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5423 hs->in_msg_seq ) );
5424 }
5425
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005426 ret = -1;
5427
5428exit:
5429
5430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5431 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005432}
5433
Hanno Beckera02b0b42018-08-21 17:20:27 +01005434static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5435 size_t desired )
5436{
5437 int offset;
5438 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5440 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005441
Hanno Becker01315ea2018-08-21 17:22:17 +01005442 /* Get rid of future records epoch first, if such exist. */
5443 ssl_free_buffered_record( ssl );
5444
5445 /* Check if we have enough space available now. */
5446 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5447 hs->buffering.total_bytes_buffered ) )
5448 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005450 return( 0 );
5451 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005452
Hanno Becker4f432ad2018-08-28 10:02:32 +01005453 /* We don't have enough space to buffer the next expected handshake
5454 * message. Remove buffers used for future messages to gain space,
5455 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005456 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5457 offset >= 0; offset-- )
5458 {
5459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5460 offset ) );
5461
Hanno Beckerb309b922018-08-23 13:18:05 +01005462 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005463
5464 /* Check if we have enough space available now. */
5465 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5466 hs->buffering.total_bytes_buffered ) )
5467 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005469 return( 0 );
5470 }
5471 }
5472
5473 return( -1 );
5474}
5475
Hanno Becker40f50842018-08-15 14:48:01 +01005476static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5477{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005478 int ret = 0;
5479 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5480
5481 if( hs == NULL )
5482 return( 0 );
5483
5484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5485
5486 switch( ssl->in_msgtype )
5487 {
5488 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005490
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005491 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005492 break;
5493
5494 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005495 {
5496 unsigned recv_msg_seq_offset;
5497 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5498 mbedtls_ssl_hs_buffer *hs_buf;
5499 size_t msg_len = ssl->in_hslen - 12;
5500
5501 /* We should never receive an old handshake
5502 * message - double-check nonetheless. */
5503 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5504 {
5505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5507 }
5508
5509 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5510 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5511 {
5512 /* Silently ignore -- message too far in the future */
5513 MBEDTLS_SSL_DEBUG_MSG( 2,
5514 ( "Ignore future HS message with sequence number %u, "
5515 "buffering window %u - %u",
5516 recv_msg_seq, ssl->handshake->in_msg_seq,
5517 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5518
5519 goto exit;
5520 }
5521
5522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5523 recv_msg_seq, recv_msg_seq_offset ) );
5524
5525 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5526
5527 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005528 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005529 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005530 size_t reassembly_buf_sz;
5531
Hanno Becker37f95322018-08-16 13:55:32 +01005532 hs_buf->is_fragmented =
5533 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5534
5535 /* We copy the message back into the input buffer
5536 * after reassembly, so check that it's not too large.
5537 * This is an implementation-specific limitation
5538 * and not one from the standard, hence it is not
5539 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005540 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005541 {
5542 /* Ignore message */
5543 goto exit;
5544 }
5545
Hanno Beckere0b150f2018-08-21 15:51:03 +01005546 /* Check if we have enough space to buffer the message. */
5547 if( hs->buffering.total_bytes_buffered >
5548 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5549 {
5550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5552 }
5553
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005554 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5555 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005556
5557 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5558 hs->buffering.total_bytes_buffered ) )
5559 {
5560 if( recv_msg_seq_offset > 0 )
5561 {
5562 /* If we can't buffer a future message because
5563 * of space limitations -- ignore. */
5564 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",
5565 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5566 (unsigned) hs->buffering.total_bytes_buffered ) );
5567 goto exit;
5568 }
Hanno Beckere1801392018-08-21 16:51:05 +01005569 else
5570 {
5571 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",
5572 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5573 (unsigned) hs->buffering.total_bytes_buffered ) );
5574 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005575
Hanno Beckera02b0b42018-08-21 17:20:27 +01005576 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005577 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005578 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",
5579 (unsigned) msg_len,
5580 (unsigned) reassembly_buf_sz,
5581 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005582 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005583 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5584 goto exit;
5585 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005586 }
5587
5588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5589 msg_len ) );
5590
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005591 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5592 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005593 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005594 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005595 goto exit;
5596 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005597 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005598
5599 /* Prepare final header: copy msg_type, length and message_seq,
5600 * then add standardised fragment_offset and fragment_length */
5601 memcpy( hs_buf->data, ssl->in_msg, 6 );
5602 memset( hs_buf->data + 6, 0, 3 );
5603 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5604
5605 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005606
5607 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005608 }
5609 else
5610 {
5611 /* Make sure msg_type and length are consistent */
5612 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5613 {
5614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5615 /* Ignore */
5616 goto exit;
5617 }
5618 }
5619
Hanno Becker4422bbb2018-08-20 09:40:19 +01005620 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005621 {
5622 size_t frag_len, frag_off;
5623 unsigned char * const msg = hs_buf->data + 12;
5624
5625 /*
5626 * Check and copy current fragment
5627 */
5628
5629 /* Validation of header fields already done in
5630 * mbedtls_ssl_prepare_handshake_record(). */
5631 frag_off = ssl_get_hs_frag_off( ssl );
5632 frag_len = ssl_get_hs_frag_len( ssl );
5633
5634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5635 frag_off, frag_len ) );
5636 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5637
5638 if( hs_buf->is_fragmented )
5639 {
5640 unsigned char * const bitmask = msg + msg_len;
5641 ssl_bitmask_set( bitmask, frag_off, frag_len );
5642 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5643 msg_len ) == 0 );
5644 }
5645 else
5646 {
5647 hs_buf->is_complete = 1;
5648 }
5649
5650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5651 hs_buf->is_complete ? "" : "not yet " ) );
5652 }
5653
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005654 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005655 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005656
5657 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005658 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005659 break;
5660 }
5661
5662exit:
5663
5664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5665 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005666}
5667#endif /* MBEDTLS_SSL_PROTO_DTLS */
5668
Hanno Becker1097b342018-08-15 14:09:41 +01005669static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005670{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005671 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005672 * Consume last content-layer message and potentially
5673 * update in_msglen which keeps track of the contents'
5674 * consumption state.
5675 *
5676 * (1) Handshake messages:
5677 * Remove last handshake message, move content
5678 * and adapt in_msglen.
5679 *
5680 * (2) Alert messages:
5681 * Consume whole record content, in_msglen = 0.
5682 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005683 * (3) Change cipher spec:
5684 * Consume whole record content, in_msglen = 0.
5685 *
5686 * (4) Application data:
5687 * Don't do anything - the record layer provides
5688 * the application data as a stream transport
5689 * and consumes through mbedtls_ssl_read only.
5690 *
5691 */
5692
5693 /* Case (1): Handshake messages */
5694 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005695 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005696 /* Hard assertion to be sure that no application data
5697 * is in flight, as corrupting ssl->in_msglen during
5698 * ssl->in_offt != NULL is fatal. */
5699 if( ssl->in_offt != NULL )
5700 {
5701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5703 }
5704
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005705 /*
5706 * Get next Handshake message in the current record
5707 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708
Hanno Becker4a810fb2017-05-24 16:27:30 +01005709 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005710 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005711 * current handshake content: If DTLS handshake
5712 * fragmentation is used, that's the fragment
5713 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005714 * size here is faulty and should be changed at
5715 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005716 * (2) While it doesn't seem to cause problems, one
5717 * has to be very careful not to assume that in_hslen
5718 * is always <= in_msglen in a sensible communication.
5719 * Again, it's wrong for DTLS handshake fragmentation.
5720 * The following check is therefore mandatory, and
5721 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005722 * Additionally, ssl->in_hslen might be arbitrarily out of
5723 * bounds after handling a DTLS message with an unexpected
5724 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005725 */
5726 if( ssl->in_hslen < ssl->in_msglen )
5727 {
5728 ssl->in_msglen -= ssl->in_hslen;
5729 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5730 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005731
Hanno Becker4a810fb2017-05-24 16:27:30 +01005732 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5733 ssl->in_msg, ssl->in_msglen );
5734 }
5735 else
5736 {
5737 ssl->in_msglen = 0;
5738 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005739
Hanno Becker4a810fb2017-05-24 16:27:30 +01005740 ssl->in_hslen = 0;
5741 }
5742 /* Case (4): Application data */
5743 else if( ssl->in_offt != NULL )
5744 {
5745 return( 0 );
5746 }
5747 /* Everything else (CCS & Alerts) */
5748 else
5749 {
5750 ssl->in_msglen = 0;
5751 }
5752
Hanno Becker1097b342018-08-15 14:09:41 +01005753 return( 0 );
5754}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005755
Hanno Beckere74d5562018-08-15 14:26:08 +01005756static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5757{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005758 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005759 return( 1 );
5760
5761 return( 0 );
5762}
5763
Hanno Becker5f066e72018-08-16 14:56:31 +01005764#if defined(MBEDTLS_SSL_PROTO_DTLS)
5765
5766static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5767{
5768 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5769 if( hs == NULL )
5770 return;
5771
Hanno Becker01315ea2018-08-21 17:22:17 +01005772 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005773 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005774 hs->buffering.total_bytes_buffered -=
5775 hs->buffering.future_record.len;
5776
5777 mbedtls_free( hs->buffering.future_record.data );
5778 hs->buffering.future_record.data = NULL;
5779 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005780}
5781
5782static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5783{
5784 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5785 unsigned char * rec;
5786 size_t rec_len;
5787 unsigned rec_epoch;
5788
5789 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5790 return( 0 );
5791
5792 if( hs == NULL )
5793 return( 0 );
5794
Hanno Becker5f066e72018-08-16 14:56:31 +01005795 rec = hs->buffering.future_record.data;
5796 rec_len = hs->buffering.future_record.len;
5797 rec_epoch = hs->buffering.future_record.epoch;
5798
5799 if( rec == NULL )
5800 return( 0 );
5801
Hanno Becker4cb782d2018-08-20 11:19:05 +01005802 /* Only consider loading future records if the
5803 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005804 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005805 return( 0 );
5806
Hanno Becker5f066e72018-08-16 14:56:31 +01005807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5808
5809 if( rec_epoch != ssl->in_epoch )
5810 {
5811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5812 goto exit;
5813 }
5814
5815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5816
5817 /* Double-check that the record is not too large */
5818 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5819 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5820 {
5821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5823 }
5824
5825 memcpy( ssl->in_hdr, rec, rec_len );
5826 ssl->in_left = rec_len;
5827 ssl->next_record_offset = 0;
5828
5829 ssl_free_buffered_record( ssl );
5830
5831exit:
5832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5833 return( 0 );
5834}
5835
5836static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5837{
5838 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5839 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005840 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005841
5842 /* Don't buffer future records outside handshakes. */
5843 if( hs == NULL )
5844 return( 0 );
5845
5846 /* Only buffer handshake records (we are only interested
5847 * in Finished messages). */
5848 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5849 return( 0 );
5850
5851 /* Don't buffer more than one future epoch record. */
5852 if( hs->buffering.future_record.data != NULL )
5853 return( 0 );
5854
Hanno Becker01315ea2018-08-21 17:22:17 +01005855 /* Don't buffer record if there's not enough buffering space remaining. */
5856 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5857 hs->buffering.total_bytes_buffered ) )
5858 {
5859 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",
5860 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5861 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005862 return( 0 );
5863 }
5864
Hanno Becker5f066e72018-08-16 14:56:31 +01005865 /* Buffer record */
5866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5867 ssl->in_epoch + 1 ) );
5868 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5869 rec_hdr_len + ssl->in_msglen );
5870
5871 /* ssl_parse_record_header() only considers records
5872 * of the next epoch as candidates for buffering. */
5873 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005874 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005875
5876 hs->buffering.future_record.data =
5877 mbedtls_calloc( 1, hs->buffering.future_record.len );
5878 if( hs->buffering.future_record.data == NULL )
5879 {
5880 /* If we run out of RAM trying to buffer a
5881 * record from the next epoch, just ignore. */
5882 return( 0 );
5883 }
5884
Hanno Becker01315ea2018-08-21 17:22:17 +01005885 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005886
Hanno Becker01315ea2018-08-21 17:22:17 +01005887 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005888 return( 0 );
5889}
5890
5891#endif /* MBEDTLS_SSL_PROTO_DTLS */
5892
Hanno Beckere74d5562018-08-15 14:26:08 +01005893static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005894{
5895 int ret;
5896
Hanno Becker5f066e72018-08-16 14:56:31 +01005897#if defined(MBEDTLS_SSL_PROTO_DTLS)
5898 /* We might have buffered a future record; if so,
5899 * and if the epoch matches now, load it.
5900 * On success, this call will set ssl->in_left to
5901 * the length of the buffered record, so that
5902 * the calls to ssl_fetch_input() below will
5903 * essentially be no-ops. */
5904 ret = ssl_load_buffered_record( ssl );
5905 if( ret != 0 )
5906 return( ret );
5907#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005908
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005909 /* Reset in pointers to default state for TLS/DTLS records,
5910 * assuming no CID and no offset between record content and
5911 * record plaintext. */
5912 ssl_update_in_pointers( ssl );
5913
5914 /* Ensure that we have enough space available for the default form
5915 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5916 * with no space for CIDs counted in). */
5917 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5918 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005921 return( ret );
5922 }
5923
5924 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005927 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5928 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005929 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005930 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5931 {
5932 ret = ssl_buffer_future_record( ssl );
5933 if( ret != 0 )
5934 return( ret );
5935
5936 /* Fall through to handling of unexpected records */
5937 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5938 }
5939
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005940 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5941 {
5942 /* Skip unexpected record (but not whole datagram) */
5943 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005944 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005945
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5947 "(header)" ) );
5948 }
5949 else
5950 {
5951 /* Skip invalid record and the rest of the datagram */
5952 ssl->next_record_offset = 0;
5953 ssl->in_left = 0;
5954
5955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5956 "(header)" ) );
5957 }
5958
5959 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005960 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005961 }
5962#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005963 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005964 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005965
5966 /*
5967 * Read and optionally decrypt the message contents
5968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005970 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005973 return( ret );
5974 }
5975
5976 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005978 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005979 {
Hanno Becker5903de42019-05-03 14:46:38 +01005980 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005981 if( ssl->next_record_offset < ssl->in_left )
5982 {
5983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5984 }
5985 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005986 else
5987#endif
5988 ssl->in_left = 0;
5989
5990 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005993 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005994 {
5995 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005996 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005997 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005998 /* Except when waiting for Finished as a bad mac here
5999 * probably means something went wrong in the handshake
6000 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6001 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6002 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6003 {
6004#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6005 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6006 {
6007 mbedtls_ssl_send_alert_message( ssl,
6008 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6009 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6010 }
6011#endif
6012 return( ret );
6013 }
6014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006016 if( ssl->conf->badmac_limit != 0 &&
6017 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6020 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006021 }
6022#endif
6023
Hanno Becker4a810fb2017-05-24 16:27:30 +01006024 /* As above, invalid records cause
6025 * dismissal of the whole datagram. */
6026
6027 ssl->next_record_offset = 0;
6028 ssl->in_left = 0;
6029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006031 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006032 }
6033
6034 return( ret );
6035 }
6036 else
6037#endif
6038 {
6039 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6041 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 mbedtls_ssl_send_alert_message( ssl,
6044 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6045 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006046 }
6047#endif
6048 return( ret );
6049 }
6050 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006051
Simon Butcher99000142016-10-13 17:21:01 +01006052 return( 0 );
6053}
6054
6055int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6056{
6057 int ret;
6058
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006059 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006060 * Handle particular types of records
6061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006063 {
Simon Butcher99000142016-10-13 17:21:01 +01006064 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6065 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006066 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 }
6069
Hanno Beckere678eaa2018-08-21 14:57:46 +01006070 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006071 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006072 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006073 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6075 ssl->in_msglen ) );
6076 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006077 }
6078
Hanno Beckere678eaa2018-08-21 14:57:46 +01006079 if( ssl->in_msg[0] != 1 )
6080 {
6081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6082 ssl->in_msg[0] ) );
6083 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6084 }
6085
6086#if defined(MBEDTLS_SSL_PROTO_DTLS)
6087 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6088 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6089 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6090 {
6091 if( ssl->handshake == NULL )
6092 {
6093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6094 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6095 }
6096
6097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6098 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6099 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006100#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006101 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006104 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006105 if( ssl->in_msglen != 2 )
6106 {
6107 /* Note: Standard allows for more than one 2 byte alert
6108 to be packed in a single message, but Mbed TLS doesn't
6109 currently support this. */
6110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6111 ssl->in_msglen ) );
6112 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6113 }
6114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 ssl->in_msg[0], ssl->in_msg[1] ) );
6117
6118 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006119 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006124 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 }
6127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6129 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6132 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006134
6135#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6136 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6137 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6138 {
Hanno Becker90333da2017-10-10 11:27:13 +01006139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006140 /* Will be handled when trying to parse ServerHello */
6141 return( 0 );
6142 }
6143#endif
6144
6145#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6146 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6147 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6148 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6149 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6150 {
6151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6152 /* Will be handled in mbedtls_ssl_parse_certificate() */
6153 return( 0 );
6154 }
6155#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6156
6157 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006158 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006159 }
6160
Hanno Beckerc76c6192017-06-06 10:03:17 +01006161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006163 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006164 /* Drop unexpected ApplicationData records,
6165 * except at the beginning of renegotiations */
6166 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6167 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6168#if defined(MBEDTLS_SSL_RENEGOTIATION)
6169 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6170 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006171#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006172 )
6173 {
6174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6175 return( MBEDTLS_ERR_SSL_NON_FATAL );
6176 }
6177
6178 if( ssl->handshake != NULL &&
6179 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6180 {
6181 ssl_handshake_wrapup_free_hs_transform( ssl );
6182 }
6183 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006184#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006185
Paul Bakker5121ce52009-01-03 21:22:43 +00006186 return( 0 );
6187}
6188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006190{
6191 int ret;
6192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6194 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6195 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006196 {
6197 return( ret );
6198 }
6199
6200 return( 0 );
6201}
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006204 unsigned char level,
6205 unsigned char message )
6206{
6207 int ret;
6208
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006209 if( ssl == NULL || ssl->conf == NULL )
6210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006216 ssl->out_msglen = 2;
6217 ssl->out_msg[0] = level;
6218 ssl->out_msg[1] = message;
6219
Hanno Becker67bc7c32018-08-06 11:33:50 +01006220 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006223 return( ret );
6224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006226
6227 return( 0 );
6228}
6229
Hanno Beckerb9d44792019-02-08 07:19:04 +00006230#if defined(MBEDTLS_X509_CRT_PARSE_C)
6231static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6232{
6233#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6234 if( session->peer_cert != NULL )
6235 {
6236 mbedtls_x509_crt_free( session->peer_cert );
6237 mbedtls_free( session->peer_cert );
6238 session->peer_cert = NULL;
6239 }
6240#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6241 if( session->peer_cert_digest != NULL )
6242 {
6243 /* Zeroization is not necessary. */
6244 mbedtls_free( session->peer_cert_digest );
6245 session->peer_cert_digest = NULL;
6246 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6247 session->peer_cert_digest_len = 0;
6248 }
6249#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6250}
6251#endif /* MBEDTLS_X509_CRT_PARSE_C */
6252
Paul Bakker5121ce52009-01-03 21:22:43 +00006253/*
6254 * Handshake functions
6255 */
Hanno Becker21489932019-02-05 13:20:55 +00006256#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006257/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006259{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006260 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6261 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006264
Hanno Becker7177a882019-02-05 13:36:46 +00006265 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006268 ssl->state++;
6269 return( 0 );
6270 }
6271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006274}
6275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006277{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006278 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6279 ssl->handshake->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, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006282
Hanno Becker7177a882019-02-05 13:36:46 +00006283 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006286 ssl->state++;
6287 return( 0 );
6288 }
6289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6291 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292}
Gilles Peskinef9828522017-05-03 12:28:43 +02006293
Hanno Becker21489932019-02-05 13:20:55 +00006294#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006295/* Some certificate support -> implement write and parse */
6296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006300 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006302 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6303 ssl->handshake->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, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006306
Hanno Becker7177a882019-02-05 13:36:46 +00006307 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006310 ssl->state++;
6311 return( 0 );
6312 }
6313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006315 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006316 {
6317 if( ssl->client_auth == 0 )
6318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006320 ssl->state++;
6321 return( 0 );
6322 }
6323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006325 /*
6326 * If using SSLv3 and got no cert, send an Alert message
6327 * (otherwise an empty Certificate message will be sent).
6328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6330 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 {
6332 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6334 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6335 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 goto write_msg;
6339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006341 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342#endif /* MBEDTLS_SSL_CLI_C */
6343#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6349 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006350 }
6351 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006355
6356 /*
6357 * 0 . 0 handshake type
6358 * 1 . 3 handshake length
6359 * 4 . 6 length of all certs
6360 * 7 . 9 length of cert. 1
6361 * 10 . n-1 peer certificate
6362 * n . n+2 length of cert. 2
6363 * n+3 . ... upper level cert, etc.
6364 */
6365 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006367
Paul Bakker29087132010-03-21 21:03:34 +00006368 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 {
6370 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006371 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006374 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006376 }
6377
6378 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6379 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6380 ssl->out_msg[i + 2] = (unsigned char)( n );
6381
6382 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6383 i += n; crt = crt->next;
6384 }
6385
6386 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6387 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6388 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6389
6390 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6392 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006393
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006394#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006395write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006396#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006397
6398 ssl->state++;
6399
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006400 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006401 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006403 return( ret );
6404 }
6405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006407
Paul Bakkered27a042013-04-18 22:46:23 +02006408 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006409}
6410
Hanno Becker84879e32019-01-31 07:44:03 +00006411#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006412
6413#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006414static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6415 unsigned char *crt_buf,
6416 size_t crt_buf_len )
6417{
6418 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6419
6420 if( peer_crt == NULL )
6421 return( -1 );
6422
6423 if( peer_crt->raw.len != crt_buf_len )
6424 return( -1 );
6425
Hanno Becker46f34d02019-02-08 14:00:04 +00006426 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006427}
Hanno Becker177475a2019-02-05 17:02:46 +00006428#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6429static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6430 unsigned char *crt_buf,
6431 size_t crt_buf_len )
6432{
6433 int ret;
6434 unsigned char const * const peer_cert_digest =
6435 ssl->session->peer_cert_digest;
6436 mbedtls_md_type_t const peer_cert_digest_type =
6437 ssl->session->peer_cert_digest_type;
6438 mbedtls_md_info_t const * const digest_info =
6439 mbedtls_md_info_from_type( peer_cert_digest_type );
6440 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6441 size_t digest_len;
6442
6443 if( peer_cert_digest == NULL || digest_info == NULL )
6444 return( -1 );
6445
6446 digest_len = mbedtls_md_get_size( digest_info );
6447 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6448 return( -1 );
6449
6450 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6451 if( ret != 0 )
6452 return( -1 );
6453
6454 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6455}
6456#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006457#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006458
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006459/*
6460 * Once the certificate message is read, parse it into a cert chain and
6461 * perform basic checks, but leave actual verification to the caller
6462 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006463static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6464 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006465{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006466 int ret;
6467#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6468 int crt_cnt=0;
6469#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006470 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006471 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006476 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6477 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006479 }
6480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6482 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6486 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006488 }
6489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006491
Paul Bakker5121ce52009-01-03 21:22:43 +00006492 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006495 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006496
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006497 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006501 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6502 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 }
6505
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006506 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6507 i += 3;
6508
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006509 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006510 while( i < ssl->in_hslen )
6511 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006512 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006513 if ( i + 3 > ssl->in_hslen ) {
6514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006515 mbedtls_ssl_send_alert_message( ssl,
6516 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6517 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006518 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6519 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006520 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6521 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006522 if( ssl->in_msg[i] != 0 )
6523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006525 mbedtls_ssl_send_alert_message( ssl,
6526 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6527 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 }
6530
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006531 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6533 | (unsigned int) ssl->in_msg[i + 2];
6534 i += 3;
6535
6536 if( n < 128 || i + n > ssl->in_hslen )
6537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006539 mbedtls_ssl_send_alert_message( ssl,
6540 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6541 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006543 }
6544
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006545 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006546#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6547 if( crt_cnt++ == 0 &&
6548 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6549 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006550 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006551 /* During client-side renegotiation, check that the server's
6552 * end-CRTs hasn't changed compared to the initial handshake,
6553 * mitigating the triple handshake attack. On success, reuse
6554 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6556 if( ssl_check_peer_crt_unchanged( ssl,
6557 &ssl->in_msg[i],
6558 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006559 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6561 mbedtls_ssl_send_alert_message( ssl,
6562 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6563 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6564 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006565 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006566
6567 /* Now we can safely free the original chain. */
6568 ssl_clear_peer_cert( ssl->session );
6569 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006570#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6571
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006572 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006573#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006574 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006575#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006576 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006577 * it in-place from the input buffer instead of making a copy. */
6578 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6579#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006580 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006582 case 0: /*ok*/
6583 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6584 /* Ignore certificate with an unknown algorithm: maybe a
6585 prior certificate was already trusted. */
6586 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006587
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006588 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6589 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6590 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006591
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006592 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6593 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6594 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006595
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006596 default:
6597 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6598 crt_parse_der_failed:
6599 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6600 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6601 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 }
6603
6604 i += n;
6605 }
6606
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006607 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006608 return( 0 );
6609}
6610
Hanno Becker4a55f632019-02-05 12:49:06 +00006611#if defined(MBEDTLS_SSL_SRV_C)
6612static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6613{
6614 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6615 return( -1 );
6616
6617#if defined(MBEDTLS_SSL_PROTO_SSL3)
6618 /*
6619 * Check if the client sent an empty certificate
6620 */
6621 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6622 {
6623 if( ssl->in_msglen == 2 &&
6624 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6625 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6626 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6627 {
6628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6629 return( 0 );
6630 }
6631
6632 return( -1 );
6633 }
6634#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6635
6636#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6637 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6638 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6639 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6640 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6641 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6644 return( 0 );
6645 }
6646
6647 return( -1 );
6648#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6649 MBEDTLS_SSL_PROTO_TLS1_2 */
6650}
6651#endif /* MBEDTLS_SSL_SRV_C */
6652
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006653/* Check if a certificate message is expected.
6654 * Return either
6655 * - SSL_CERTIFICATE_EXPECTED, or
6656 * - SSL_CERTIFICATE_SKIP
6657 * indicating whether a Certificate message is expected or not.
6658 */
6659#define SSL_CERTIFICATE_EXPECTED 0
6660#define SSL_CERTIFICATE_SKIP 1
6661static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6662 int authmode )
6663{
6664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006665 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006666
6667 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6668 return( SSL_CERTIFICATE_SKIP );
6669
6670#if defined(MBEDTLS_SSL_SRV_C)
6671 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6672 {
6673 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6674 return( SSL_CERTIFICATE_SKIP );
6675
6676 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6677 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006678 ssl->session_negotiate->verify_result =
6679 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6680 return( SSL_CERTIFICATE_SKIP );
6681 }
6682 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006683#else
6684 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006685#endif /* MBEDTLS_SSL_SRV_C */
6686
6687 return( SSL_CERTIFICATE_EXPECTED );
6688}
6689
Hanno Becker68636192019-02-05 14:36:34 +00006690static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6691 int authmode,
6692 mbedtls_x509_crt *chain,
6693 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006694{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006695 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006696 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006697 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006698 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006699
Hanno Becker8927c832019-04-03 12:52:50 +01006700 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6701 void *p_vrfy;
6702
Hanno Becker68636192019-02-05 14:36:34 +00006703 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6704 return( 0 );
6705
Hanno Becker8927c832019-04-03 12:52:50 +01006706 if( ssl->f_vrfy != NULL )
6707 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006709 f_vrfy = ssl->f_vrfy;
6710 p_vrfy = ssl->p_vrfy;
6711 }
6712 else
6713 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006715 f_vrfy = ssl->conf->f_vrfy;
6716 p_vrfy = ssl->conf->p_vrfy;
6717 }
6718
Hanno Becker68636192019-02-05 14:36:34 +00006719 /*
6720 * Main check: verify certificate
6721 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006722#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6723 if( ssl->conf->f_ca_cb != NULL )
6724 {
6725 ((void) rs_ctx);
6726 have_ca_chain = 1;
6727
6728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006729 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006730 chain,
6731 ssl->conf->f_ca_cb,
6732 ssl->conf->p_ca_cb,
6733 ssl->conf->cert_profile,
6734 ssl->hostname,
6735 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006736 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006737 }
6738 else
6739#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6740 {
6741 mbedtls_x509_crt *ca_chain;
6742 mbedtls_x509_crl *ca_crl;
6743
6744#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6745 if( ssl->handshake->sni_ca_chain != NULL )
6746 {
6747 ca_chain = ssl->handshake->sni_ca_chain;
6748 ca_crl = ssl->handshake->sni_ca_crl;
6749 }
6750 else
6751#endif
6752 {
6753 ca_chain = ssl->conf->ca_chain;
6754 ca_crl = ssl->conf->ca_crl;
6755 }
6756
6757 if( ca_chain != NULL )
6758 have_ca_chain = 1;
6759
6760 ret = mbedtls_x509_crt_verify_restartable(
6761 chain,
6762 ca_chain, ca_crl,
6763 ssl->conf->cert_profile,
6764 ssl->hostname,
6765 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006766 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006767 }
Hanno Becker68636192019-02-05 14:36:34 +00006768
6769 if( ret != 0 )
6770 {
6771 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6772 }
6773
6774#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6775 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6776 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6777#endif
6778
6779 /*
6780 * Secondary checks: always done, but change 'ret' only if it was 0
6781 */
6782
6783#if defined(MBEDTLS_ECP_C)
6784 {
6785 const mbedtls_pk_context *pk = &chain->pk;
6786
6787 /* If certificate uses an EC key, make sure the curve is OK */
6788 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6789 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6790 {
6791 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6792
6793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6794 if( ret == 0 )
6795 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6796 }
6797 }
6798#endif /* MBEDTLS_ECP_C */
6799
6800 if( mbedtls_ssl_check_cert_usage( chain,
6801 ciphersuite_info,
6802 ! ssl->conf->endpoint,
6803 &ssl->session_negotiate->verify_result ) != 0 )
6804 {
6805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6806 if( ret == 0 )
6807 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6808 }
6809
6810 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6811 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6812 * with details encoded in the verification flags. All other kinds
6813 * of error codes, including those from the user provided f_vrfy
6814 * functions, are treated as fatal and lead to a failure of
6815 * ssl_parse_certificate even if verification was optional. */
6816 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6817 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6818 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6819 {
6820 ret = 0;
6821 }
6822
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006823 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006824 {
6825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6826 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6827 }
6828
6829 if( ret != 0 )
6830 {
6831 uint8_t alert;
6832
6833 /* The certificate may have been rejected for several reasons.
6834 Pick one and send the corresponding alert. Which alert to send
6835 may be a subject of debate in some cases. */
6836 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6837 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6838 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6839 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6840 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6841 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6842 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6843 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6844 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6845 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6846 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6847 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6848 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6849 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6850 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6851 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6852 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6853 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6854 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6855 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6856 else
6857 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6858 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6859 alert );
6860 }
6861
6862#if defined(MBEDTLS_DEBUG_C)
6863 if( ssl->session_negotiate->verify_result != 0 )
6864 {
6865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6866 ssl->session_negotiate->verify_result ) );
6867 }
6868 else
6869 {
6870 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6871 }
6872#endif /* MBEDTLS_DEBUG_C */
6873
6874 return( ret );
6875}
6876
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006877#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6878static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6879 unsigned char *start, size_t len )
6880{
6881 int ret;
6882 /* Remember digest of the peer's end-CRT. */
6883 ssl->session_negotiate->peer_cert_digest =
6884 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6885 if( ssl->session_negotiate->peer_cert_digest == NULL )
6886 {
6887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6888 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6889 mbedtls_ssl_send_alert_message( ssl,
6890 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6891 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6892
6893 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6894 }
6895
6896 ret = mbedtls_md( mbedtls_md_info_from_type(
6897 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6898 start, len,
6899 ssl->session_negotiate->peer_cert_digest );
6900
6901 ssl->session_negotiate->peer_cert_digest_type =
6902 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6903 ssl->session_negotiate->peer_cert_digest_len =
6904 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6905
6906 return( ret );
6907}
6908
6909static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6910 unsigned char *start, size_t len )
6911{
6912 unsigned char *end = start + len;
6913 int ret;
6914
6915 /* Make a copy of the peer's raw public key. */
6916 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6917 ret = mbedtls_pk_parse_subpubkey( &start, end,
6918 &ssl->handshake->peer_pubkey );
6919 if( ret != 0 )
6920 {
6921 /* We should have parsed the public key before. */
6922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6923 }
6924
6925 return( 0 );
6926}
6927#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6928
Hanno Becker68636192019-02-05 14:36:34 +00006929int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6930{
6931 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006932 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006933#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6934 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6935 ? ssl->handshake->sni_authmode
6936 : ssl->conf->authmode;
6937#else
6938 const int authmode = ssl->conf->authmode;
6939#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006940 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006941 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006942
6943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6944
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006945 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6946 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006947 {
6948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006949 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006950 }
6951
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006952#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6953 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006954 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006955 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006956 chain = ssl->handshake->ecrs_peer_cert;
6957 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006958 goto crt_verify;
6959 }
6960#endif
6961
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006962 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006963 {
6964 /* mbedtls_ssl_read_record may have sent an alert already. We
6965 let it decide whether to alert. */
6966 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006967 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006968 }
6969
Hanno Becker4a55f632019-02-05 12:49:06 +00006970#if defined(MBEDTLS_SSL_SRV_C)
6971 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6972 {
6973 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006974
6975 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006976 ret = 0;
6977 else
6978 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006979
Hanno Becker6bdfab22019-02-05 13:11:17 +00006980 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006981 }
6982#endif /* MBEDTLS_SSL_SRV_C */
6983
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006984 /* Clear existing peer CRT structure in case we tried to
6985 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006986 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006987
6988 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6989 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006990 {
6991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6992 sizeof( mbedtls_x509_crt ) ) );
6993 mbedtls_ssl_send_alert_message( ssl,
6994 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6995 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006996
Hanno Becker3dad3112019-02-05 17:19:52 +00006997 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6998 goto exit;
6999 }
7000 mbedtls_x509_crt_init( chain );
7001
7002 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007003 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007004 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007005
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007006#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7007 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007008 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007009
7010crt_verify:
7011 if( ssl->handshake->ecrs_enabled)
7012 rs_ctx = &ssl->handshake->ecrs_ctx;
7013#endif
7014
Hanno Becker68636192019-02-05 14:36:34 +00007015 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007016 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007017 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007018 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007019
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007020#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007021 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007022 unsigned char *crt_start, *pk_start;
7023 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007024
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007025 /* We parse the CRT chain without copying, so
7026 * these pointers point into the input buffer,
7027 * and are hence still valid after freeing the
7028 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007029
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007030 crt_start = chain->raw.p;
7031 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007032
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007033 pk_start = chain->pk_raw.p;
7034 pk_len = chain->pk_raw.len;
7035
7036 /* Free the CRT structures before computing
7037 * digest and copying the peer's public key. */
7038 mbedtls_x509_crt_free( chain );
7039 mbedtls_free( chain );
7040 chain = NULL;
7041
7042 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007043 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007044 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007045
7046 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7047 if( ret != 0 )
7048 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007049 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007050#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7051 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007052 ssl->session_negotiate->peer_cert = chain;
7053 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007054#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
Hanno Becker6bdfab22019-02-05 13:11:17 +00007058exit:
7059
Hanno Becker3dad3112019-02-05 17:19:52 +00007060 if( ret == 0 )
7061 ssl->state++;
7062
7063#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7064 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7065 {
7066 ssl->handshake->ecrs_peer_cert = chain;
7067 chain = NULL;
7068 }
7069#endif
7070
7071 if( chain != NULL )
7072 {
7073 mbedtls_x509_crt_free( chain );
7074 mbedtls_free( chain );
7075 }
7076
Paul Bakker5121ce52009-01-03 21:22:43 +00007077 return( ret );
7078}
Hanno Becker21489932019-02-05 13:20:55 +00007079#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007082{
7083 int ret;
7084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 ssl->out_msglen = 1;
7089 ssl->out_msg[0] = 1;
7090
Paul Bakker5121ce52009-01-03 21:22:43 +00007091 ssl->state++;
7092
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007093 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007094 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007096 return( ret );
7097 }
7098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007100
7101 return( 0 );
7102}
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007105{
7106 int ret;
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007109
Hanno Becker327c93b2018-08-15 13:56:18 +01007110 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 return( ret );
7114 }
7115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7120 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122 }
7123
Hanno Beckere678eaa2018-08-21 14:57:46 +01007124 /* CCS records are only accepted if they have length 1 and content '1',
7125 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007126
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007127 /*
7128 * Switch to our negotiated transform and session parameters for inbound
7129 * data.
7130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007132 ssl->transform_in = ssl->transform_negotiate;
7133 ssl->session_in = ssl->session_negotiate;
7134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007139 ssl_dtls_replay_reset( ssl );
7140#endif
7141
7142 /* Increment epoch */
7143 if( ++ssl->in_epoch == 0 )
7144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007146 /* This is highly unlikely to happen for legitimate reasons, so
7147 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007149 }
7150 }
7151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007153 memset( ssl->in_ctr, 0, 8 );
7154
Hanno Becker79594fd2019-05-08 09:38:41 +01007155 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7158 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007163 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7164 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007166 }
7167 }
7168#endif
7169
Paul Bakker5121ce52009-01-03 21:22:43 +00007170 ssl->state++;
7171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007173
7174 return( 0 );
7175}
7176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7178 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007179{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007180 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7183 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7184 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007185 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007186 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7189#if defined(MBEDTLS_SHA512_C)
7190 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007191 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7192 else
7193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194#if defined(MBEDTLS_SHA256_C)
7195 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007196 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007197 else
7198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007202 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007203 }
Paul Bakker380da532012-04-18 16:10:25 +00007204}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7209 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007210 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7211 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7214#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007215#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007216 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007217 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7218#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007219 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007220#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007224 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007225 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007226#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007227 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007228#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007231}
7232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007234 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7237 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007238 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7239 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7242#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007243#if defined(MBEDTLS_USE_PSA_CRYPTO)
7244 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7245#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007246 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007247#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007250#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007251 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007252#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007253 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007254#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007257}
7258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7260 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7261static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007262 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007263{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007264 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7265 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007266}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007267#endif
Paul Bakker380da532012-04-18 16:10:25 +00007268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7270#if defined(MBEDTLS_SHA256_C)
7271static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007272 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007273{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007274#if defined(MBEDTLS_USE_PSA_CRYPTO)
7275 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7276#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007277 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007278#endif
Paul Bakker380da532012-04-18 16:10:25 +00007279}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007280#endif
Paul Bakker380da532012-04-18 16:10:25 +00007281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282#if defined(MBEDTLS_SHA512_C)
7283static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007284 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007285{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007286#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007287 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007288#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007289 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007290#endif
Paul Bakker380da532012-04-18 16:10:25 +00007291}
Paul Bakker769075d2012-11-24 11:26:46 +01007292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007296static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007298{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007299 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007300 mbedtls_md5_context md5;
7301 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007302
Paul Bakker5121ce52009-01-03 21:22:43 +00007303 unsigned char padbuf[48];
7304 unsigned char md5sum[16];
7305 unsigned char sha1sum[20];
7306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007308 if( !session )
7309 session = ssl->session;
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007312
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007313 mbedtls_md5_init( &md5 );
7314 mbedtls_sha1_init( &sha1 );
7315
7316 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7317 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007318
7319 /*
7320 * SSLv3:
7321 * hash =
7322 * MD5( master + pad2 +
7323 * MD5( handshake + sender + master + pad1 ) )
7324 * + SHA1( master + pad2 +
7325 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007326 */
7327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007329 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7330 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007334 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7335 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007339 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007340
Paul Bakker1ef83d62012-04-11 12:09:53 +00007341 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007342
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007343 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7344 mbedtls_md5_update_ret( &md5, session->master, 48 );
7345 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7346 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007348 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7349 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7350 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7351 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Paul Bakker1ef83d62012-04-11 12:09:53 +00007353 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007354
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007355 mbedtls_md5_starts_ret( &md5 );
7356 mbedtls_md5_update_ret( &md5, session->master, 48 );
7357 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7358 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7359 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007361 mbedtls_sha1_starts_ret( &sha1 );
7362 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7363 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7364 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7365 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007369 mbedtls_md5_free( &md5 );
7370 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007372 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7373 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7374 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007381static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007383{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007384 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007385 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007386 mbedtls_md5_context md5;
7387 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007388 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007391 if( !session )
7392 session = ssl->session;
7393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007395
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007396 mbedtls_md5_init( &md5 );
7397 mbedtls_sha1_init( &sha1 );
7398
7399 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7400 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007401
Paul Bakker1ef83d62012-04-11 12:09:53 +00007402 /*
7403 * TLSv1:
7404 * hash = PRF( master, finished_label,
7405 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7406 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007409 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7410 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007411#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007414 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7415 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007416#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007419 ? "client finished"
7420 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007421
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007422 mbedtls_md5_finish_ret( &md5, padbuf );
7423 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007424
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007425 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007426 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007429
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007430 mbedtls_md5_free( &md5 );
7431 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007432
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007433 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7440#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007441static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007443{
7444 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007445 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007447#if defined(MBEDTLS_USE_PSA_CRYPTO)
7448 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007449 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007450 psa_status_t status;
7451#else
7452 mbedtls_sha256_context sha256;
7453#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007456 if( !session )
7457 session = ssl->session;
7458
Andrzej Kurekeb342242019-01-29 09:14:33 -05007459 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7460 ? "client finished"
7461 : "server finished";
7462
7463#if defined(MBEDTLS_USE_PSA_CRYPTO)
7464 sha256_psa = psa_hash_operation_init();
7465
7466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7467
7468 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7469 if( status != PSA_SUCCESS )
7470 {
7471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7472 return;
7473 }
7474
7475 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7476 if( status != PSA_SUCCESS )
7477 {
7478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7479 return;
7480 }
7481 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7482#else
7483
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007484 mbedtls_sha256_init( &sha256 );
7485
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007488 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007489
7490 /*
7491 * TLSv1.2:
7492 * hash = PRF( master, finished_label,
7493 * Hash( handshake ) )[0.11]
7494 */
7495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496#if !defined(MBEDTLS_SHA256_ALT)
7497 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007498 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007499#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007500
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007501 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007502 mbedtls_sha256_free( &sha256 );
7503#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007504
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007505 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007506 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007510 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007517static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007519{
7520 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007521 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007522 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007523#if defined(MBEDTLS_USE_PSA_CRYPTO)
7524 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007525 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007526 psa_status_t status;
7527#else
7528 mbedtls_sha512_context sha512;
7529#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007532 if( !session )
7533 session = ssl->session;
7534
Andrzej Kurekeb342242019-01-29 09:14:33 -05007535 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7536 ? "client finished"
7537 : "server finished";
7538
7539#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007540 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007541
7542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7543
Andrzej Kurek972fba52019-01-30 03:29:12 -05007544 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545 if( status != PSA_SUCCESS )
7546 {
7547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7548 return;
7549 }
7550
Andrzej Kurek972fba52019-01-30 03:29:12 -05007551 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007552 if( status != PSA_SUCCESS )
7553 {
7554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7555 return;
7556 }
7557 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7558#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007559 mbedtls_sha512_init( &sha512 );
7560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007562
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007563 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007564
7565 /*
7566 * TLSv1.2:
7567 * hash = PRF( master, finished_label,
7568 * Hash( handshake ) )[0.11]
7569 */
7570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007572 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7573 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007574#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007575
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007576 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007577 mbedtls_sha512_free( &sha512 );
7578#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007579
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007580 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007581 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007584
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007585 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#endif /* MBEDTLS_SHA512_C */
7590#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007595
7596 /*
7597 * Free our handshake params
7598 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007599 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007601 ssl->handshake = NULL;
7602
7603 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007604 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007605 */
7606 if( ssl->transform )
7607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 mbedtls_ssl_transform_free( ssl->transform );
7609 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007610 }
7611 ssl->transform = ssl->transform_negotiate;
7612 ssl->transform_negotiate = NULL;
7613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007615}
7616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007618{
7619 int resume = ssl->handshake->resume;
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623#if defined(MBEDTLS_SSL_RENEGOTIATION)
7624 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007627 ssl->renego_records_seen = 0;
7628 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007629#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007630
7631 /*
7632 * Free the previous session and switch in the current one
7633 */
Paul Bakker0a597072012-09-25 21:55:46 +00007634 if( ssl->session )
7635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007637 /* RFC 7366 3.1: keep the EtM state */
7638 ssl->session_negotiate->encrypt_then_mac =
7639 ssl->session->encrypt_then_mac;
7640#endif
7641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 mbedtls_ssl_session_free( ssl->session );
7643 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007644 }
7645 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007646 ssl->session_negotiate = NULL;
7647
Paul Bakker0a597072012-09-25 21:55:46 +00007648 /*
7649 * Add cache entry
7650 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007651 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007652 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007653 resume == 0 )
7654 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007655 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007657 }
Paul Bakker0a597072012-09-25 21:55:46 +00007658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007661 ssl->handshake->flight != NULL )
7662 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007663 /* Cancel handshake timer */
7664 ssl_set_timer( ssl, 0 );
7665
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007666 /* Keep last flight around in case we need to resend it:
7667 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007669 }
7670 else
7671#endif
7672 ssl_handshake_wrapup_free_hs_transform( ssl );
7673
Paul Bakker48916f92012-09-16 19:57:18 +00007674 ssl->state++;
7675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007677}
7678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007681 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007685 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007686
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007687 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007689 /*
7690 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7691 * may define some other value. Currently (early 2016), no defined
7692 * ciphersuite does this (and this is unlikely to change as activity has
7693 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007698 ssl->verify_data_len = hash_len;
7699 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007700#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007701
Paul Bakker5121ce52009-01-03 21:22:43 +00007702 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7704 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007705
7706 /*
7707 * In case of session resuming, invert the client and server
7708 * ChangeCipherSpec messages order.
7709 */
Paul Bakker0a597072012-09-25 21:55:46 +00007710 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007713 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007717 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 }
7721 else
7722 ssl->state++;
7723
Paul Bakker48916f92012-09-16 19:57:18 +00007724 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007725 * Switch to our negotiated transform and session parameters for outbound
7726 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007731 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007732 {
7733 unsigned char i;
7734
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007735 /* Remember current epoch settings for resending */
7736 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007737 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007738
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007739 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007740 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007741
7742 /* Increment epoch */
7743 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007744 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007745 break;
7746
7747 /* The loop goes to its end iff the counter is wrapping */
7748 if( i == 0 )
7749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7751 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007752 }
7753 }
7754 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007756 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007757
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007758 ssl->transform_out = ssl->transform_negotiate;
7759 ssl->session_out = ssl->session_negotiate;
7760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7762 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7767 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007768 }
7769 }
7770#endif
7771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007773 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007775#endif
7776
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007777 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007778 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007780 return( ret );
7781 }
7782
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007783#if defined(MBEDTLS_SSL_PROTO_DTLS)
7784 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7785 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7786 {
7787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7788 return( ret );
7789 }
7790#endif
7791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007793
7794 return( 0 );
7795}
7796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007798#define SSL_MAX_HASH_LEN 36
7799#else
7800#define SSL_MAX_HASH_LEN 12
7801#endif
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007804{
Paul Bakker23986e52011-04-24 08:57:21 +00007805 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007806 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007807 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007810
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007811 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007812
Hanno Becker327c93b2018-08-15 13:56:18 +01007813 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007816 return( ret );
7817 }
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007822 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7823 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007825 }
7826
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007827 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828#if defined(MBEDTLS_SSL_PROTO_SSL3)
7829 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007830 hash_len = 36;
7831 else
7832#endif
7833 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7836 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007839 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7840 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007842 }
7843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007845 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7849 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007851 }
7852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007854 ssl->verify_data_len = hash_len;
7855 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007856#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007857
Paul Bakker0a597072012-09-25 21:55:46 +00007858 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007861 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007865 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007867#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007868 }
7869 else
7870 ssl->state++;
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007873 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007875#endif
7876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007878
7879 return( 0 );
7880}
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7887 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7888 mbedtls_md5_init( &handshake->fin_md5 );
7889 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007890 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7891 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007892#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7894#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007895#if defined(MBEDTLS_USE_PSA_CRYPTO)
7896 handshake->fin_sha256_psa = psa_hash_operation_init();
7897 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7898#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007900 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007901#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007904#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007905 handshake->fin_sha384_psa = psa_hash_operation_init();
7906 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007907#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007909 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007910#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007913
7914 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007915
7916#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7917 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7918 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7919#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#if defined(MBEDTLS_DHM_C)
7922 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_ECDH_C)
7925 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007927#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007928 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007929#if defined(MBEDTLS_SSL_CLI_C)
7930 handshake->ecjpake_cache = NULL;
7931 handshake->ecjpake_cache_len = 0;
7932#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007933#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007934
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007935#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007936 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007937#endif
7938
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007939#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7940 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7941#endif
Hanno Becker75173122019-02-06 16:18:31 +00007942
7943#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7944 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7945 mbedtls_pk_init( &handshake->peer_pubkey );
7946#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007947}
7948
Hanno Beckera18d1322018-01-03 14:27:32 +00007949void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7954 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007955
Hanno Beckerd56ed242018-01-03 15:32:51 +00007956#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 mbedtls_md_init( &transform->md_ctx_enc );
7958 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007959#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007960}
7961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007965}
7966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007968{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007969 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007970 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007974 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007975 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007976
7977 /*
7978 * Either the pointers are now NULL or cleared properly and can be freed.
7979 * Now allocate missing structures.
7980 */
7981 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007982 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007983 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007984 }
Paul Bakker48916f92012-09-16 19:57:18 +00007985
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007986 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007987 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007988 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007989 }
Paul Bakker48916f92012-09-16 19:57:18 +00007990
Paul Bakker82788fb2014-10-20 13:59:19 +02007991 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007992 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007993 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007994 }
Paul Bakker48916f92012-09-16 19:57:18 +00007995
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007997 if( ssl->handshake == NULL ||
7998 ssl->transform_negotiate == NULL ||
7999 ssl->session_negotiate == NULL )
8000 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 mbedtls_free( ssl->handshake );
8004 mbedtls_free( ssl->transform_negotiate );
8005 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008006
8007 ssl->handshake = NULL;
8008 ssl->transform_negotiate = NULL;
8009 ssl->session_negotiate = NULL;
8010
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008011 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008012 }
8013
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008014 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008016 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008017 ssl_handshake_params_init( ssl->handshake );
8018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008020 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8021 {
8022 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008023
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008024 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8025 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8026 else
8027 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008028
8029 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008030 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008031#endif
8032
Paul Bakker48916f92012-09-16 19:57:18 +00008033 return( 0 );
8034}
8035
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008036#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008037/* Dummy cookie callbacks for defaults */
8038static int ssl_cookie_write_dummy( void *ctx,
8039 unsigned char **p, unsigned char *end,
8040 const unsigned char *cli_id, size_t cli_id_len )
8041{
8042 ((void) ctx);
8043 ((void) p);
8044 ((void) end);
8045 ((void) cli_id);
8046 ((void) cli_id_len);
8047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008049}
8050
8051static int ssl_cookie_check_dummy( void *ctx,
8052 const unsigned char *cookie, size_t cookie_len,
8053 const unsigned char *cli_id, size_t cli_id_len )
8054{
8055 ((void) ctx);
8056 ((void) cookie);
8057 ((void) cookie_len);
8058 ((void) cli_id);
8059 ((void) cli_id_len);
8060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008062}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008063#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008064
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008065/* Once ssl->out_hdr as the address of the beginning of the
8066 * next outgoing record is set, deduce the other pointers.
8067 *
8068 * Note: For TLS, we save the implicit record sequence number
8069 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8070 * and the caller has to make sure there's space for this.
8071 */
8072
8073static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8074 mbedtls_ssl_transform *transform )
8075{
8076#if defined(MBEDTLS_SSL_PROTO_DTLS)
8077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8078 {
8079 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008080#if defined(MBEDTLS_SSL_CID)
8081 ssl->out_cid = ssl->out_ctr + 8;
8082 ssl->out_len = ssl->out_cid;
8083 if( transform != NULL )
8084 ssl->out_len += transform->out_cid_len;
8085#else /* MBEDTLS_SSL_CID */
8086 ssl->out_len = ssl->out_ctr + 8;
8087#endif /* MBEDTLS_SSL_CID */
8088 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008089 }
8090 else
8091#endif
8092 {
8093 ssl->out_ctr = ssl->out_hdr - 8;
8094 ssl->out_len = ssl->out_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008095#if defined(MBEDTLS_SSL_CID)
8096 ssl->out_cid = ssl->out_len;
8097#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008098 ssl->out_iv = ssl->out_hdr + 5;
8099 }
8100
8101 /* Adjust out_msg to make space for explicit IV, if used. */
8102 if( transform != NULL &&
8103 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8104 {
8105 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8106 }
8107 else
8108 ssl->out_msg = ssl->out_iv;
8109}
8110
8111/* Once ssl->in_hdr as the address of the beginning of the
8112 * next incoming record is set, deduce the other pointers.
8113 *
8114 * Note: For TLS, we save the implicit record sequence number
8115 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8116 * and the caller has to make sure there's space for this.
8117 */
8118
Hanno Becker79594fd2019-05-08 09:38:41 +01008119static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008120{
Hanno Becker79594fd2019-05-08 09:38:41 +01008121 /* This function sets the pointers to match the case
8122 * of unprotected TLS/DTLS records, with both ssl->in_iv
8123 * and ssl->in_msg pointing to the beginning of the record
8124 * content.
8125 *
8126 * When decrypting a protected record, ssl->in_msg
8127 * will be shifted to point to the beginning of the
8128 * record plaintext.
8129 */
8130
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008131#if defined(MBEDTLS_SSL_PROTO_DTLS)
8132 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8133 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008134 /* This sets the header pointers to match records
8135 * without CID. When we receive a record containing
8136 * a CID, the fields are shifted accordingly in
8137 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008138 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008139#if defined(MBEDTLS_SSL_CID)
8140 ssl->in_cid = ssl->in_ctr + 8;
8141 ssl->in_len = ssl->in_cid; /* Default: no CID */
8142#else /* MBEDTLS_SSL_CID */
8143 ssl->in_len = ssl->in_ctr + 8;
8144#endif /* MBEDTLS_SSL_CID */
8145 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008146 }
8147 else
8148#endif
8149 {
8150 ssl->in_ctr = ssl->in_hdr - 8;
8151 ssl->in_len = ssl->in_hdr + 3;
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008152#if defined(MBEDTLS_SSL_CID)
8153 ssl->in_cid = ssl->in_len;
8154#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008155 ssl->in_iv = ssl->in_hdr + 5;
8156 }
8157
Hanno Becker79594fd2019-05-08 09:38:41 +01008158 /* This will be adjusted at record decryption time. */
8159 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008160}
8161
Paul Bakker5121ce52009-01-03 21:22:43 +00008162/*
8163 * Initialize an SSL context
8164 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008165void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8166{
8167 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8168}
8169
8170/*
8171 * Setup an SSL context
8172 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008173
8174static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8175{
8176 /* Set the incoming and outgoing record pointers. */
8177#if defined(MBEDTLS_SSL_PROTO_DTLS)
8178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8179 {
8180 ssl->out_hdr = ssl->out_buf;
8181 ssl->in_hdr = ssl->in_buf;
8182 }
8183 else
8184#endif /* MBEDTLS_SSL_PROTO_DTLS */
8185 {
8186 ssl->out_hdr = ssl->out_buf + 8;
8187 ssl->in_hdr = ssl->in_buf + 8;
8188 }
8189
8190 /* Derive other internal pointers. */
8191 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008192 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008193}
8194
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008195int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008196 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008197{
Paul Bakker48916f92012-09-16 19:57:18 +00008198 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008199
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008200 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008201
8202 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008203 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008204 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008205
8206 /* Set to NULL in case of an error condition */
8207 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008208
Angus Grattond8213d02016-05-25 20:56:48 +10008209 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8210 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008211 {
Angus Grattond8213d02016-05-25 20:56:48 +10008212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008213 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008214 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008215 }
8216
8217 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8218 if( ssl->out_buf == NULL )
8219 {
8220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008221 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008222 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008223 }
8224
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008225 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008226
Paul Bakker48916f92012-09-16 19:57:18 +00008227 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008228 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008229
8230 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008231
8232error:
8233 mbedtls_free( ssl->in_buf );
8234 mbedtls_free( ssl->out_buf );
8235
8236 ssl->conf = NULL;
8237
8238 ssl->in_buf = NULL;
8239 ssl->out_buf = NULL;
8240
8241 ssl->in_hdr = NULL;
8242 ssl->in_ctr = NULL;
8243 ssl->in_len = NULL;
8244 ssl->in_iv = NULL;
8245 ssl->in_msg = NULL;
8246
8247 ssl->out_hdr = NULL;
8248 ssl->out_ctr = NULL;
8249 ssl->out_len = NULL;
8250 ssl->out_iv = NULL;
8251 ssl->out_msg = NULL;
8252
k-stachowiak9f7798e2018-07-31 16:52:32 +02008253 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008254}
8255
8256/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008257 * Reset an initialized and used SSL context for re-use while retaining
8258 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008259 *
8260 * If partial is non-zero, keep data in the input buffer and client ID.
8261 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008262 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008263static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008264{
Paul Bakker48916f92012-09-16 19:57:18 +00008265 int ret;
8266
Hanno Becker7e772132018-08-10 12:38:21 +01008267#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8268 !defined(MBEDTLS_SSL_SRV_C)
8269 ((void) partial);
8270#endif
8271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008273
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008274 /* Cancel any possibly running timer */
8275 ssl_set_timer( ssl, 0 );
8276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277#if defined(MBEDTLS_SSL_RENEGOTIATION)
8278 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008279 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008280
8281 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8283 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008286
Paul Bakker7eb013f2011-10-06 12:37:39 +00008287 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008288 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008289
8290 ssl->in_msgtype = 0;
8291 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008293 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008294 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008295#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008297 ssl_dtls_replay_reset( ssl );
8298#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008299
8300 ssl->in_hslen = 0;
8301 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008302
8303 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008304
8305 ssl->out_msgtype = 0;
8306 ssl->out_msglen = 0;
8307 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8309 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008310 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008311#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008312
Hanno Becker19859472018-08-06 09:40:20 +01008313 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8314
Paul Bakker48916f92012-09-16 19:57:18 +00008315 ssl->transform_in = NULL;
8316 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008317
Hanno Becker78640902018-08-13 16:35:15 +01008318 ssl->session_in = NULL;
8319 ssl->session_out = NULL;
8320
Angus Grattond8213d02016-05-25 20:56:48 +10008321 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008322
8323#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008324 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008325#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8326 {
8327 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008328 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008329 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8332 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8335 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8338 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008339 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008340 }
8341#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008342
Paul Bakker48916f92012-09-16 19:57:18 +00008343 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 mbedtls_ssl_transform_free( ssl->transform );
8346 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008347 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008348 }
Paul Bakker48916f92012-09-16 19:57:18 +00008349
Paul Bakkerc0463502013-02-14 11:19:38 +01008350 if( ssl->session )
8351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 mbedtls_ssl_session_free( ssl->session );
8353 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008354 ssl->session = NULL;
8355 }
8356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008358 ssl->alpn_chosen = NULL;
8359#endif
8360
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008361#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008362#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008363 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008364#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008365 {
8366 mbedtls_free( ssl->cli_id );
8367 ssl->cli_id = NULL;
8368 ssl->cli_id_len = 0;
8369 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008370#endif
8371
Paul Bakker48916f92012-09-16 19:57:18 +00008372 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8373 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008374
8375 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008376}
8377
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008378/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008379 * Reset an initialized and used SSL context for re-use while retaining
8380 * all application-set variables, function pointers and data.
8381 */
8382int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8383{
8384 return( ssl_session_reset_int( ssl, 0 ) );
8385}
8386
8387/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008388 * SSL set accessors
8389 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008390void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008392 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008393}
8394
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008395void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008396{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008397 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008398}
8399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008401void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008403 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008404}
8405#endif
8406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008408void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008410 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008411}
8412#endif
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008415
Hanno Becker1841b0a2018-08-24 11:13:57 +01008416void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8417 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008418{
8419 ssl->disable_datagram_packing = !allow_packing;
8420}
8421
8422void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8423 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008425 conf->hs_timeout_min = min;
8426 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008427}
8428#endif
8429
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008430void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008431{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008432 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008433}
8434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008436void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008437 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008438 void *p_vrfy )
8439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->f_vrfy = f_vrfy;
8441 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008444
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008445void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008446 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008447 void *p_rng )
8448{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008449 conf->f_rng = f_rng;
8450 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008451}
8452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008453void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008454 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008455 void *p_dbg )
8456{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008457 conf->f_dbg = f_dbg;
8458 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008459}
8460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008462 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008463 mbedtls_ssl_send_t *f_send,
8464 mbedtls_ssl_recv_t *f_recv,
8465 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008466{
8467 ssl->p_bio = p_bio;
8468 ssl->f_send = f_send;
8469 ssl->f_recv = f_recv;
8470 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008471}
8472
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008473#if defined(MBEDTLS_SSL_PROTO_DTLS)
8474void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8475{
8476 ssl->mtu = mtu;
8477}
8478#endif
8479
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008480void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008481{
8482 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008483}
8484
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008485void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8486 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008487 mbedtls_ssl_set_timer_t *f_set_timer,
8488 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008489{
8490 ssl->p_timer = p_timer;
8491 ssl->f_set_timer = f_set_timer;
8492 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008493
8494 /* Make sure we start with no timer running */
8495 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008496}
8497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008499void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008500 void *p_cache,
8501 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8502 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008503{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008504 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008505 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008506 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510#if defined(MBEDTLS_SSL_CLI_C)
8511int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008512{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008513 int ret;
8514
8515 if( ssl == NULL ||
8516 session == NULL ||
8517 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008518 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008521 }
8522
Hanno Becker52055ae2019-02-06 14:30:46 +00008523 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8524 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008525 return( ret );
8526
Paul Bakker0a597072012-09-25 21:55:46 +00008527 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008528
8529 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008532
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008533void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008534 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008535{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008536 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8537 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8538 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8539 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008540}
8541
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008542void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008543 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008544 int major, int minor )
8545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008547 return;
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008550 return;
8551
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008552 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008553}
8554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008556void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008557 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008558{
8559 conf->cert_profile = profile;
8560}
8561
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008562/* Append a new keycert entry to a (possibly empty) list */
8563static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8564 mbedtls_x509_crt *cert,
8565 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008566{
niisato8ee24222018-06-25 19:05:48 +09008567 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008568
niisato8ee24222018-06-25 19:05:48 +09008569 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8570 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008571 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008572
niisato8ee24222018-06-25 19:05:48 +09008573 new_cert->cert = cert;
8574 new_cert->key = key;
8575 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008576
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008577 /* Update head is the list was null, else add to the end */
8578 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008579 {
niisato8ee24222018-06-25 19:05:48 +09008580 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008581 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008582 else
8583 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008584 mbedtls_ssl_key_cert *cur = *head;
8585 while( cur->next != NULL )
8586 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008587 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008588 }
8589
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008590 return( 0 );
8591}
8592
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008593int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008594 mbedtls_x509_crt *own_cert,
8595 mbedtls_pk_context *pk_key )
8596{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008597 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008598}
8599
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008600void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008601 mbedtls_x509_crt *ca_chain,
8602 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008603{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008604 conf->ca_chain = ca_chain;
8605 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008606
8607#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8608 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8609 * cannot be used together. */
8610 conf->f_ca_cb = NULL;
8611 conf->p_ca_cb = NULL;
8612#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008613}
Hanno Becker5adaad92019-03-27 16:54:37 +00008614
8615#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8616void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008617 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008618 void *p_ca_cb )
8619{
8620 conf->f_ca_cb = f_ca_cb;
8621 conf->p_ca_cb = p_ca_cb;
8622
8623 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8624 * cannot be used together. */
8625 conf->ca_chain = NULL;
8626 conf->ca_crl = NULL;
8627}
8628#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008630
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008631#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8632int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8633 mbedtls_x509_crt *own_cert,
8634 mbedtls_pk_context *pk_key )
8635{
8636 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8637 own_cert, pk_key ) );
8638}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008639
8640void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8641 mbedtls_x509_crt *ca_chain,
8642 mbedtls_x509_crl *ca_crl )
8643{
8644 ssl->handshake->sni_ca_chain = ca_chain;
8645 ssl->handshake->sni_ca_crl = ca_crl;
8646}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008647
8648void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8649 int authmode )
8650{
8651 ssl->handshake->sni_authmode = authmode;
8652}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008653#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8654
Hanno Becker8927c832019-04-03 12:52:50 +01008655#if defined(MBEDTLS_X509_CRT_PARSE_C)
8656void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8657 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8658 void *p_vrfy )
8659{
8660 ssl->f_vrfy = f_vrfy;
8661 ssl->p_vrfy = p_vrfy;
8662}
8663#endif
8664
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008665#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008666/*
8667 * Set EC J-PAKE password for current handshake
8668 */
8669int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8670 const unsigned char *pw,
8671 size_t pw_len )
8672{
8673 mbedtls_ecjpake_role role;
8674
Janos Follath8eb64132016-06-03 15:40:57 +01008675 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8677
8678 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8679 role = MBEDTLS_ECJPAKE_SERVER;
8680 else
8681 role = MBEDTLS_ECJPAKE_CLIENT;
8682
8683 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8684 role,
8685 MBEDTLS_MD_SHA256,
8686 MBEDTLS_ECP_DP_SECP256R1,
8687 pw, pw_len ) );
8688}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008689#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008692
8693static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8694{
8695 /* Remove reference to existing PSK, if any. */
8696#if defined(MBEDTLS_USE_PSA_CRYPTO)
8697 if( conf->psk_opaque != 0 )
8698 {
8699 /* The maintenance of the PSK key slot is the
8700 * user's responsibility. */
8701 conf->psk_opaque = 0;
8702 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008703 /* This and the following branch should never
8704 * be taken simultaenously as we maintain the
8705 * invariant that raw and opaque PSKs are never
8706 * configured simultaneously. As a safeguard,
8707 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008708#endif /* MBEDTLS_USE_PSA_CRYPTO */
8709 if( conf->psk != NULL )
8710 {
8711 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8712
8713 mbedtls_free( conf->psk );
8714 conf->psk = NULL;
8715 conf->psk_len = 0;
8716 }
8717
8718 /* Remove reference to PSK identity, if any. */
8719 if( conf->psk_identity != NULL )
8720 {
8721 mbedtls_free( conf->psk_identity );
8722 conf->psk_identity = NULL;
8723 conf->psk_identity_len = 0;
8724 }
8725}
8726
Hanno Becker7390c712018-11-15 13:33:04 +00008727/* This function assumes that PSK identity in the SSL config is unset.
8728 * It checks that the provided identity is well-formed and attempts
8729 * to make a copy of it in the SSL config.
8730 * On failure, the PSK identity in the config remains unset. */
8731static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8732 unsigned char const *psk_identity,
8733 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008734{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008735 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008736 if( psk_identity == NULL ||
8737 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008738 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008739 {
8740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8741 }
8742
Hanno Becker7390c712018-11-15 13:33:04 +00008743 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8744 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008746
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008747 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008748 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008749
8750 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008751}
8752
Hanno Becker7390c712018-11-15 13:33:04 +00008753int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8754 const unsigned char *psk, size_t psk_len,
8755 const unsigned char *psk_identity, size_t psk_identity_len )
8756{
8757 int ret;
8758 /* Remove opaque/raw PSK + PSK Identity */
8759 ssl_conf_remove_psk( conf );
8760
8761 /* Check and set raw PSK */
8762 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8764 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8765 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8766 conf->psk_len = psk_len;
8767 memcpy( conf->psk, psk, conf->psk_len );
8768
8769 /* Check and set PSK Identity */
8770 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8771 if( ret != 0 )
8772 ssl_conf_remove_psk( conf );
8773
8774 return( ret );
8775}
8776
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008777static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8778{
8779#if defined(MBEDTLS_USE_PSA_CRYPTO)
8780 if( ssl->handshake->psk_opaque != 0 )
8781 {
8782 ssl->handshake->psk_opaque = 0;
8783 }
8784 else
8785#endif /* MBEDTLS_USE_PSA_CRYPTO */
8786 if( ssl->handshake->psk != NULL )
8787 {
8788 mbedtls_platform_zeroize( ssl->handshake->psk,
8789 ssl->handshake->psk_len );
8790 mbedtls_free( ssl->handshake->psk );
8791 ssl->handshake->psk_len = 0;
8792 }
8793}
8794
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008795int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8796 const unsigned char *psk, size_t psk_len )
8797{
8798 if( psk == NULL || ssl->handshake == NULL )
8799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8800
8801 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8803
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008804 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008805
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008806 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008807 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008808
8809 ssl->handshake->psk_len = psk_len;
8810 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8811
8812 return( 0 );
8813}
8814
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008815#if defined(MBEDTLS_USE_PSA_CRYPTO)
8816int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008817 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008818 const unsigned char *psk_identity,
8819 size_t psk_identity_len )
8820{
Hanno Becker7390c712018-11-15 13:33:04 +00008821 int ret;
8822 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008823 ssl_conf_remove_psk( conf );
8824
Hanno Becker7390c712018-11-15 13:33:04 +00008825 /* Check and set opaque PSK */
8826 if( psk_slot == 0 )
8827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008828 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008829
8830 /* Check and set PSK Identity */
8831 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8832 psk_identity_len );
8833 if( ret != 0 )
8834 ssl_conf_remove_psk( conf );
8835
8836 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008837}
8838
8839int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008840 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008841{
8842 if( psk_slot == 0 || ssl->handshake == NULL )
8843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8844
8845 ssl_remove_psk( ssl );
8846 ssl->handshake->psk_opaque = psk_slot;
8847 return( 0 );
8848}
8849#endif /* MBEDTLS_USE_PSA_CRYPTO */
8850
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008851void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008853 size_t),
8854 void *p_psk )
8855{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008856 conf->f_psk = f_psk;
8857 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008858}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008860
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008861#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008862
8863#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008864int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008865{
8866 int ret;
8867
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008868 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8869 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8870 {
8871 mbedtls_mpi_free( &conf->dhm_P );
8872 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008873 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
8876 return( 0 );
8877}
Hanno Becker470a8c42017-10-04 15:28:46 +01008878#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008879
Hanno Beckera90658f2017-10-04 15:29:08 +01008880int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8881 const unsigned char *dhm_P, size_t P_len,
8882 const unsigned char *dhm_G, size_t G_len )
8883{
8884 int ret;
8885
8886 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8887 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8888 {
8889 mbedtls_mpi_free( &conf->dhm_P );
8890 mbedtls_mpi_free( &conf->dhm_G );
8891 return( ret );
8892 }
8893
8894 return( 0 );
8895}
Paul Bakker5121ce52009-01-03 21:22:43 +00008896
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008897int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008898{
8899 int ret;
8900
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008901 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8902 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8903 {
8904 mbedtls_mpi_free( &conf->dhm_P );
8905 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008906 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008907 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008908
8909 return( 0 );
8910}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008911#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008912
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008913#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8914/*
8915 * Set the minimum length for Diffie-Hellman parameters
8916 */
8917void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8918 unsigned int bitlen )
8919{
8920 conf->dhm_min_bitlen = bitlen;
8921}
8922#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8923
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008924#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008925/*
8926 * Set allowed/preferred hashes for handshake signatures
8927 */
8928void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8929 const int *hashes )
8930{
8931 conf->sig_hashes = hashes;
8932}
Hanno Becker947194e2017-04-07 13:25:49 +01008933#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008934
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008935#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008936/*
8937 * Set the allowed elliptic curves
8938 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008939void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008940 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008942 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008943}
Hanno Becker947194e2017-04-07 13:25:49 +01008944#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008945
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008946#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008948{
Hanno Becker947194e2017-04-07 13:25:49 +01008949 /* Initialize to suppress unnecessary compiler warning */
8950 size_t hostname_len = 0;
8951
8952 /* Check if new hostname is valid before
8953 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008954 if( hostname != NULL )
8955 {
8956 hostname_len = strlen( hostname );
8957
8958 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8960 }
8961
8962 /* Now it's clear that we will overwrite the old hostname,
8963 * so we can free it safely */
8964
8965 if( ssl->hostname != NULL )
8966 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008967 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008968 mbedtls_free( ssl->hostname );
8969 }
8970
8971 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008972
Paul Bakker5121ce52009-01-03 21:22:43 +00008973 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008974 {
8975 ssl->hostname = NULL;
8976 }
8977 else
8978 {
8979 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008980 if( ssl->hostname == NULL )
8981 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008982
Hanno Becker947194e2017-04-07 13:25:49 +01008983 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008984
Hanno Becker947194e2017-04-07 13:25:49 +01008985 ssl->hostname[hostname_len] = '\0';
8986 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008987
8988 return( 0 );
8989}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008990#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008991
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008992#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008993void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008995 const unsigned char *, size_t),
8996 void *p_sni )
8997{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008998 conf->f_sni = f_sni;
8999 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009004int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009005{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009006 size_t cur_len, tot_len;
9007 const char **p;
9008
9009 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009010 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9011 * MUST NOT be truncated."
9012 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009013 */
9014 tot_len = 0;
9015 for( p = protos; *p != NULL; p++ )
9016 {
9017 cur_len = strlen( *p );
9018 tot_len += cur_len;
9019
9020 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009022 }
9023
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009024 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009025
9026 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009027}
9028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009030{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009031 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009032}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009034
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009035void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009036{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009037 conf->max_major_ver = major;
9038 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009039}
9040
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009041void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009042{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009043 conf->min_major_ver = major;
9044 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009045}
9046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009048void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009049{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009050 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009051}
9052#endif
9053
Janos Follath088ce432017-04-10 12:42:31 +01009054#if defined(MBEDTLS_SSL_SRV_C)
9055void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9056 char cert_req_ca_list )
9057{
9058 conf->cert_req_ca_list = cert_req_ca_list;
9059}
9060#endif
9061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009063void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009064{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009065 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009066}
9067#endif
9068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009070void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009071{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009072 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009073}
9074#endif
9075
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009076#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009077void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009079 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009080}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009081#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009084int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009087 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009090 }
9091
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009092 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093
9094 return( 0 );
9095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009099void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009100{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009101 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009106void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009107{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009108 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009109}
9110#endif
9111
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009114 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009115}
9116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009117#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009118void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009120 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009121}
9122
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009123void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009125 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009126}
9127
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009128void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009129 const unsigned char period[8] )
9130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009131 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009136#if defined(MBEDTLS_SSL_CLI_C)
9137void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009138{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009139 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009140}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009141#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009142
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009143#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009144void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9145 mbedtls_ssl_ticket_write_t *f_ticket_write,
9146 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9147 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009148{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009149 conf->f_ticket_write = f_ticket_write;
9150 conf->f_ticket_parse = f_ticket_parse;
9151 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009152}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009155
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009156#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9157void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9158 mbedtls_ssl_export_keys_t *f_export_keys,
9159 void *p_export_keys )
9160{
9161 conf->f_export_keys = f_export_keys;
9162 conf->p_export_keys = p_export_keys;
9163}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009164
9165void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9166 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9167 void *p_export_keys )
9168{
9169 conf->f_export_keys_ext = f_export_keys_ext;
9170 conf->p_export_keys = p_export_keys;
9171}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009172#endif
9173
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009174#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009175void mbedtls_ssl_conf_async_private_cb(
9176 mbedtls_ssl_config *conf,
9177 mbedtls_ssl_async_sign_t *f_async_sign,
9178 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9179 mbedtls_ssl_async_resume_t *f_async_resume,
9180 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009181 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009182{
9183 conf->f_async_sign_start = f_async_sign;
9184 conf->f_async_decrypt_start = f_async_decrypt;
9185 conf->f_async_resume = f_async_resume;
9186 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009187 conf->p_async_config_data = async_config_data;
9188}
9189
Gilles Peskine8f97af72018-04-26 11:46:10 +02009190void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9191{
9192 return( conf->p_async_config_data );
9193}
9194
Gilles Peskine1febfef2018-04-30 11:54:39 +02009195void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009196{
9197 if( ssl->handshake == NULL )
9198 return( NULL );
9199 else
9200 return( ssl->handshake->user_async_ctx );
9201}
9202
Gilles Peskine1febfef2018-04-30 11:54:39 +02009203void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009204 void *ctx )
9205{
9206 if( ssl->handshake != NULL )
9207 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009208}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009209#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009210
Paul Bakker5121ce52009-01-03 21:22:43 +00009211/*
9212 * SSL get accessors
9213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009215{
9216 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9217}
9218
Hanno Becker8b170a02017-10-10 11:51:19 +01009219int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9220{
9221 /*
9222 * Case A: We're currently holding back
9223 * a message for further processing.
9224 */
9225
9226 if( ssl->keep_current_message == 1 )
9227 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009229 return( 1 );
9230 }
9231
9232 /*
9233 * Case B: Further records are pending in the current datagram.
9234 */
9235
9236#if defined(MBEDTLS_SSL_PROTO_DTLS)
9237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9238 ssl->in_left > ssl->next_record_offset )
9239 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009241 return( 1 );
9242 }
9243#endif /* MBEDTLS_SSL_PROTO_DTLS */
9244
9245 /*
9246 * Case C: A handshake message is being processed.
9247 */
9248
Hanno Becker8b170a02017-10-10 11:51:19 +01009249 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9250 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009252 return( 1 );
9253 }
9254
9255 /*
9256 * Case D: An application data message is being processed
9257 */
9258 if( ssl->in_offt != NULL )
9259 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009261 return( 1 );
9262 }
9263
9264 /*
9265 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009266 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009267 * we implement support for multiple alerts in single records.
9268 */
9269
9270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9271 return( 0 );
9272}
9273
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009274uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009275{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009276 if( ssl->session != NULL )
9277 return( ssl->session->verify_result );
9278
9279 if( ssl->session_negotiate != NULL )
9280 return( ssl->session_negotiate->verify_result );
9281
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009282 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009283}
9284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009286{
Paul Bakker926c8e42013-03-06 10:23:34 +01009287 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009288 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009291}
9292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009296 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009297 {
9298 switch( ssl->minor_ver )
9299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009301 return( "DTLSv1.0" );
9302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009304 return( "DTLSv1.2" );
9305
9306 default:
9307 return( "unknown (DTLS)" );
9308 }
9309 }
9310#endif
9311
Paul Bakker43ca69c2011-01-15 17:35:19 +00009312 switch( ssl->minor_ver )
9313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009315 return( "SSLv3.0" );
9316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009318 return( "TLSv1.0" );
9319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321 return( "TLSv1.1" );
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009324 return( "TLSv1.2" );
9325
Paul Bakker43ca69c2011-01-15 17:35:19 +00009326 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009327 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009328 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009329}
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009332{
Hanno Becker3136ede2018-08-17 15:28:19 +01009333 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009335 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009336
Hanno Becker5903de42019-05-03 14:46:38 +01009337 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9338
Hanno Becker78640902018-08-13 16:35:15 +01009339 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009340 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#if defined(MBEDTLS_ZLIB_SUPPORT)
9343 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9344 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009345#endif
9346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 case MBEDTLS_MODE_GCM:
9350 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009351 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009353 transform_expansion = transform->minlen;
9354 break;
9355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009357
9358 block_size = mbedtls_cipher_get_block_size(
9359 &transform->cipher_ctx_enc );
9360
Hanno Becker3136ede2018-08-17 15:28:19 +01009361 /* Expansion due to the addition of the MAC. */
9362 transform_expansion += transform->maclen;
9363
9364 /* Expansion due to the addition of CBC padding;
9365 * Theoretically up to 256 bytes, but we never use
9366 * more than the block size of the underlying cipher. */
9367 transform_expansion += block_size;
9368
9369 /* For TLS 1.1 or higher, an explicit IV is added
9370 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009371#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9372 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009373 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009374#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009375
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009376 break;
9377
9378 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009381 }
9382
Hanno Becker6cbad552019-05-08 15:40:11 +01009383#if defined(MBEDTLS_SSL_CID)
9384 if( transform->out_cid_len != 0 )
9385 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
9386#endif /* MBEDTLS_SSL_CID */
9387
Hanno Becker5903de42019-05-03 14:46:38 +01009388 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009389}
9390
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009391#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9392size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9393{
9394 size_t max_len;
9395
9396 /*
9397 * Assume mfl_code is correct since it was checked when set
9398 */
Angus Grattond8213d02016-05-25 20:56:48 +10009399 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009400
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009401 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009402 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009403 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009404 {
Angus Grattond8213d02016-05-25 20:56:48 +10009405 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009406 }
9407
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009408 /* During a handshake, use the value being negotiated */
9409 if( ssl->session_negotiate != NULL &&
9410 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9411 {
9412 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9413 }
9414
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009415 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009416}
9417#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9418
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009419#if defined(MBEDTLS_SSL_PROTO_DTLS)
9420static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9421{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009422 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9423 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9424 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9425 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9426 return ( 0 );
9427
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009428 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9429 return( ssl->mtu );
9430
9431 if( ssl->mtu == 0 )
9432 return( ssl->handshake->mtu );
9433
9434 return( ssl->mtu < ssl->handshake->mtu ?
9435 ssl->mtu : ssl->handshake->mtu );
9436}
9437#endif /* MBEDTLS_SSL_PROTO_DTLS */
9438
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009439int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9440{
9441 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9442
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009443#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9444 !defined(MBEDTLS_SSL_PROTO_DTLS)
9445 (void) ssl;
9446#endif
9447
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009448#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9449 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9450
9451 if( max_len > mfl )
9452 max_len = mfl;
9453#endif
9454
9455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009456 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009458 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009459 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9460 const size_t overhead = (size_t) ret;
9461
9462 if( ret < 0 )
9463 return( ret );
9464
9465 if( mtu <= overhead )
9466 {
9467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9468 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9469 }
9470
9471 if( max_len > mtu - overhead )
9472 max_len = mtu - overhead;
9473 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009474#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009475
Hanno Becker0defedb2018-08-10 12:35:02 +01009476#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9477 !defined(MBEDTLS_SSL_PROTO_DTLS)
9478 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009479#endif
9480
9481 return( (int) max_len );
9482}
9483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484#if defined(MBEDTLS_X509_CRT_PARSE_C)
9485const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009486{
9487 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009488 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009489
Hanno Beckere6824572019-02-07 13:18:46 +00009490#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009491 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009492#else
9493 return( NULL );
9494#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009499int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9500 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009501{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009502 if( ssl == NULL ||
9503 dst == NULL ||
9504 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009505 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009508 }
9509
Hanno Becker52055ae2019-02-06 14:30:46 +00009510 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009513
Paul Bakker5121ce52009-01-03 21:22:43 +00009514/*
Paul Bakker1961b702013-01-25 14:49:24 +01009515 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009520
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009521 if( ssl == NULL || ssl->conf == NULL )
9522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009525 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009529 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009531#endif
9532
Paul Bakker1961b702013-01-25 14:49:24 +01009533 return( ret );
9534}
9535
9536/*
9537 * Perform the SSL handshake
9538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009540{
9541 int ret = 0;
9542
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009543 if( ssl == NULL || ssl->conf == NULL )
9544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009551
9552 if( ret != 0 )
9553 break;
9554 }
9555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009557
9558 return( ret );
9559}
9560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561#if defined(MBEDTLS_SSL_RENEGOTIATION)
9562#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009563/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009564 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009567{
9568 int ret;
9569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009571
9572 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9574 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009575
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009576 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009577 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009579 return( ret );
9580 }
9581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009583
9584 return( 0 );
9585}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009587
9588/*
9589 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 * - any side: calling mbedtls_ssl_renegotiate(),
9591 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9592 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009593 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009594 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009598{
9599 int ret;
9600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009602
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009603 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9604 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009605
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009606 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9607 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009611 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009613 ssl->handshake->out_msg_seq = 1;
9614 else
9615 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009616 }
9617#endif
9618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9620 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009625 return( ret );
9626 }
9627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009629
9630 return( 0 );
9631}
9632
9633/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009634 * Renegotiate current connection on client,
9635 * or request renegotiation on server
9636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009640
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009641 if( ssl == NULL || ssl->conf == NULL )
9642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009645 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009646 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009652
9653 /* Did we already try/start sending HelloRequest? */
9654 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009656
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009657 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009662 /*
9663 * On client, either start the renegotiation process or,
9664 * if already in progress, continue the handshake
9665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670
9671 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009674 return( ret );
9675 }
9676 }
9677 else
9678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682 return( ret );
9683 }
9684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009686
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009687 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009688}
9689
9690/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009691 * Check record counters and renegotiate if they're above the limit.
9692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009694{
Andres AG2196c7f2016-12-15 17:01:16 +00009695 size_t ep_len = ssl_ep_len( ssl );
9696 int in_ctr_cmp;
9697 int out_ctr_cmp;
9698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9700 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009701 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009702 {
9703 return( 0 );
9704 }
9705
Andres AG2196c7f2016-12-15 17:01:16 +00009706 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9707 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009708 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009709 ssl->conf->renego_period + ep_len, 8 - ep_len );
9710
9711 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009712 {
9713 return( 0 );
9714 }
9715
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009720
9721/*
9722 * Receive application data decrypted from the SSL layer
9723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009725{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009726 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009727 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009728
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009729 if( ssl == NULL || ssl->conf == NULL )
9730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009735 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009738 return( ret );
9739
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009740 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009742 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009743 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009744 return( ret );
9745 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009746 }
9747#endif
9748
Hanno Becker4a810fb2017-05-24 16:27:30 +01009749 /*
9750 * Check if renegotiation is necessary and/or handshake is
9751 * in process. If yes, perform/continue, and fall through
9752 * if an unexpected packet is received while the client
9753 * is waiting for the ServerHello.
9754 *
9755 * (There is no equivalent to the last condition on
9756 * the server-side as it is not treated as within
9757 * a handshake while waiting for the ClientHello
9758 * after a renegotiation request.)
9759 */
9760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009762 ret = ssl_check_ctr_renegotiate( ssl );
9763 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9764 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009767 return( ret );
9768 }
9769#endif
9770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009774 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9775 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009778 return( ret );
9779 }
9780 }
9781
Hanno Beckere41158b2017-10-23 13:30:32 +01009782 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009783 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009784 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009785 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009786 if( ssl->f_get_timer != NULL &&
9787 ssl->f_get_timer( ssl->p_timer ) == -1 )
9788 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009789 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009790 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009791
Hanno Becker327c93b2018-08-15 13:56:18 +01009792 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009793 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009794 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9795 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009796
Hanno Becker4a810fb2017-05-24 16:27:30 +01009797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9798 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009799 }
9800
9801 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009803 {
9804 /*
9805 * OpenSSL sends empty messages to randomize the IV
9806 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009807 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009810 return( 0 );
9811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009813 return( ret );
9814 }
9815 }
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009820
Hanno Becker4a810fb2017-05-24 16:27:30 +01009821 /*
9822 * - For client-side, expect SERVER_HELLO_REQUEST.
9823 * - For server-side, expect CLIENT_HELLO.
9824 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9825 */
9826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009828 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009830 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009833
9834 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009837 {
9838 continue;
9839 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009842 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009843#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009844
Hanno Becker4a810fb2017-05-24 16:27:30 +01009845#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009846 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009850
9851 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009854 {
9855 continue;
9856 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009859 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009860#endif /* MBEDTLS_SSL_SRV_C */
9861
Hanno Becker21df7f92017-10-17 11:03:26 +01009862#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009863 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009864 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9865 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9866 ssl->conf->allow_legacy_renegotiation ==
9867 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9868 {
9869 /*
9870 * Accept renegotiation request
9871 */
Paul Bakker48916f92012-09-16 19:57:18 +00009872
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009873 /* DTLS clients need to know renego is server-initiated */
9874#if defined(MBEDTLS_SSL_PROTO_DTLS)
9875 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9876 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9877 {
9878 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9879 }
9880#endif
9881 ret = ssl_start_renegotiation( ssl );
9882 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9883 ret != 0 )
9884 {
9885 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9886 return( ret );
9887 }
9888 }
9889 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009890#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009891 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009892 /*
9893 * Refuse renegotiation
9894 */
9895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898#if defined(MBEDTLS_SSL_PROTO_SSL3)
9899 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009900 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009901 /* SSLv3 does not have a "no_renegotiation" warning, so
9902 we send a fatal alert and abort the connection. */
9903 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9904 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9905 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009906 }
9907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9909#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9910 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9911 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9914 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9915 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009916 {
9917 return( ret );
9918 }
Paul Bakker48916f92012-09-16 19:57:18 +00009919 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009920 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9922 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009926 }
Paul Bakker48916f92012-09-16 19:57:18 +00009927 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009928
Hanno Becker90333da2017-10-10 11:27:13 +01009929 /* At this point, we don't know whether the renegotiation has been
9930 * completed or not. The cases to consider are the following:
9931 * 1) The renegotiation is complete. In this case, no new record
9932 * has been read yet.
9933 * 2) The renegotiation is incomplete because the client received
9934 * an application data record while awaiting the ServerHello.
9935 * 3) The renegotiation is incomplete because the client received
9936 * a non-handshake, non-application data message while awaiting
9937 * the ServerHello.
9938 * In each of these case, looping will be the proper action:
9939 * - For 1), the next iteration will read a new record and check
9940 * if it's application data.
9941 * - For 2), the loop condition isn't satisfied as application data
9942 * is present, hence continue is the same as break
9943 * - For 3), the loop condition is satisfied and read_record
9944 * will re-deliver the message that was held back by the client
9945 * when expecting the ServerHello.
9946 */
9947 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009948 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009949#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009951 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009952 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009953 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009954 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009957 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009959 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009960 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009961 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9965 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009968 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009969 }
9970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9974 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009975 }
9976
9977 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009978
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009979 /* We're going to return something now, cancel timer,
9980 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009982 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009983
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009985 /* If we requested renego but received AppData, resend HelloRequest.
9986 * Do it now, after setting in_offt, to avoid taking this branch
9987 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009991 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009992 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009995 return( ret );
9996 }
9997 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009999#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010000 }
10001
10002 n = ( len < ssl->in_msglen )
10003 ? len : ssl->in_msglen;
10004
10005 memcpy( buf, ssl->in_offt, n );
10006 ssl->in_msglen -= n;
10007
10008 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010009 {
10010 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010011 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010012 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010013 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010014 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010015 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010016 /* more data available */
10017 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010018 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010021
Paul Bakker23986e52011-04-24 08:57:21 +000010022 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010023}
10024
10025/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010026 * Send application data to be encrypted by the SSL layer, taking care of max
10027 * fragment length and buffer size.
10028 *
10029 * According to RFC 5246 Section 6.2.1:
10030 *
10031 * Zero-length fragments of Application data MAY be sent as they are
10032 * potentially useful as a traffic analysis countermeasure.
10033 *
10034 * Therefore, it is possible that the input message length is 0 and the
10035 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010036 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010037static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010038 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010039{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010040 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10041 const size_t max_len = (size_t) ret;
10042
10043 if( ret < 0 )
10044 {
10045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10046 return( ret );
10047 }
10048
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010049 if( len > max_len )
10050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010052 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010055 "maximum fragment length: %d > %d",
10056 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010058 }
10059 else
10060#endif
10061 len = max_len;
10062 }
Paul Bakker887bd502011-06-08 13:10:54 +000010063
Paul Bakker5121ce52009-01-03 21:22:43 +000010064 if( ssl->out_left != 0 )
10065 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010066 /*
10067 * The user has previously tried to send the data and
10068 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10069 * written. In this case, we expect the high-level write function
10070 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010075 return( ret );
10076 }
10077 }
Paul Bakker887bd502011-06-08 13:10:54 +000010078 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010079 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010080 /*
10081 * The user is trying to send a message the first time, so we need to
10082 * copy the data into the internal buffers and setup the data structure
10083 * to keep track of partial writes
10084 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010085 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010087 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010088
Hanno Becker67bc7c32018-08-06 11:33:50 +010010089 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010092 return( ret );
10093 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010094 }
10095
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010096 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010097}
10098
10099/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010100 * Write application data, doing 1/n-1 splitting if necessary.
10101 *
10102 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010103 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010104 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010107static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010108 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010109{
10110 int ret;
10111
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010112 if( ssl->conf->cbc_record_splitting ==
10113 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010114 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10116 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10117 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010118 {
10119 return( ssl_write_real( ssl, buf, len ) );
10120 }
10121
10122 if( ssl->split_done == 0 )
10123 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010124 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010125 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010126 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010127 }
10128
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010129 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10130 return( ret );
10131 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010132
10133 return( ret + 1 );
10134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010135#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010136
10137/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010138 * Write application data (public-facing wrapper)
10139 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010140int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010141{
10142 int ret;
10143
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010145
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010146 if( ssl == NULL || ssl->conf == NULL )
10147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10148
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010149#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010150 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10151 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010152 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010153 return( ret );
10154 }
10155#endif
10156
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010157 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010158 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010159 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010160 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010162 return( ret );
10163 }
10164 }
10165
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010166#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010167 ret = ssl_write_split( ssl, buf, len );
10168#else
10169 ret = ssl_write_real( ssl, buf, len );
10170#endif
10171
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010173
10174 return( ret );
10175}
10176
10177/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010178 * Notify the peer that the connection is being closed
10179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010181{
10182 int ret;
10183
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010184 if( ssl == NULL || ssl->conf == NULL )
10185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010188
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010189 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10195 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10196 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010199 return( ret );
10200 }
10201 }
10202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010204
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010205 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010206}
10207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010209{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010210 if( transform == NULL )
10211 return;
10212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010214 deflateEnd( &transform->ctx_deflate );
10215 inflateEnd( &transform->ctx_inflate );
10216#endif
10217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10219 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010220
Hanno Beckerd56ed242018-01-03 15:32:51 +000010221#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222 mbedtls_md_free( &transform->md_ctx_enc );
10223 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010224#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010225
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010226 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010227}
10228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229#if defined(MBEDTLS_X509_CRT_PARSE_C)
10230static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010233
10234 while( cur != NULL )
10235 {
10236 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010238 cur = next;
10239 }
10240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010242
Hanno Becker0271f962018-08-16 13:23:47 +010010243#if defined(MBEDTLS_SSL_PROTO_DTLS)
10244
10245static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10246{
10247 unsigned offset;
10248 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10249
10250 if( hs == NULL )
10251 return;
10252
Hanno Becker283f5ef2018-08-24 09:34:47 +010010253 ssl_free_buffered_record( ssl );
10254
Hanno Becker0271f962018-08-16 13:23:47 +010010255 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010256 ssl_buffering_free_slot( ssl, offset );
10257}
10258
10259static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10260 uint8_t slot )
10261{
10262 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10263 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010264
10265 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10266 return;
10267
Hanno Beckere605b192018-08-21 15:59:07 +010010268 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010269 {
Hanno Beckere605b192018-08-21 15:59:07 +010010270 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010271 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010272 mbedtls_free( hs_buf->data );
10273 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010274 }
10275}
10276
10277#endif /* MBEDTLS_SSL_PROTO_DTLS */
10278
Gilles Peskine9b562d52018-04-25 20:32:43 +020010279void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010280{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010281 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10282
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010283 if( handshake == NULL )
10284 return;
10285
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010286#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10287 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10288 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010289 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010290 handshake->async_in_progress = 0;
10291 }
10292#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10293
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010294#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10295 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10296 mbedtls_md5_free( &handshake->fin_md5 );
10297 mbedtls_sha1_free( &handshake->fin_sha1 );
10298#endif
10299#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10300#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010301#if defined(MBEDTLS_USE_PSA_CRYPTO)
10302 psa_hash_abort( &handshake->fin_sha256_psa );
10303#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010304 mbedtls_sha256_free( &handshake->fin_sha256 );
10305#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010306#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010307#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010308#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010309 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010310#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010311 mbedtls_sha512_free( &handshake->fin_sha512 );
10312#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010313#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010314#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316#if defined(MBEDTLS_DHM_C)
10317 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#if defined(MBEDTLS_ECDH_C)
10320 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010321#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010322#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010323 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010324#if defined(MBEDTLS_SSL_CLI_C)
10325 mbedtls_free( handshake->ecjpake_cache );
10326 handshake->ecjpake_cache = NULL;
10327 handshake->ecjpake_cache_len = 0;
10328#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010329#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010330
Janos Follath4ae5c292016-02-10 11:27:43 +000010331#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10332 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010333 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010335#endif
10336
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010337#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10338 if( handshake->psk != NULL )
10339 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010340 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010341 mbedtls_free( handshake->psk );
10342 }
10343#endif
10344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10346 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010347 /*
10348 * Free only the linked list wrapper, not the keys themselves
10349 * since the belong to the SNI callback
10350 */
10351 if( handshake->sni_key_cert != NULL )
10352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010354
10355 while( cur != NULL )
10356 {
10357 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010359 cur = next;
10360 }
10361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010363
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010364#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010365 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010366 if( handshake->ecrs_peer_cert != NULL )
10367 {
10368 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10369 mbedtls_free( handshake->ecrs_peer_cert );
10370 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010371#endif
10372
Hanno Becker75173122019-02-06 16:18:31 +000010373#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10374 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10375 mbedtls_pk_free( &handshake->peer_pubkey );
10376#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378#if defined(MBEDTLS_SSL_PROTO_DTLS)
10379 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010380 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010381 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010382#endif
10383
Hanno Becker4a63ed42019-01-08 11:39:35 +000010384#if defined(MBEDTLS_ECDH_C) && \
10385 defined(MBEDTLS_USE_PSA_CRYPTO)
10386 psa_destroy_key( handshake->ecdh_psa_privkey );
10387#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10388
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010389 mbedtls_platform_zeroize( handshake,
10390 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010391}
10392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010394{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010395 if( session == NULL )
10396 return;
10397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010399 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010400#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010401
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010402#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010404#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010405
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010406 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010407}
10408
Paul Bakker5121ce52009-01-03 21:22:43 +000010409/*
10410 * Free an SSL context
10411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010413{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010414 if( ssl == NULL )
10415 return;
10416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010418
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010419 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010420 {
Angus Grattond8213d02016-05-25 20:56:48 +100010421 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010422 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010423 }
10424
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010425 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010426 {
Angus Grattond8213d02016-05-25 20:56:48 +100010427 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010429 }
10430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010432 if( ssl->compress_buf != NULL )
10433 {
Angus Grattond8213d02016-05-25 20:56:48 +100010434 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010436 }
10437#endif
10438
Paul Bakker48916f92012-09-16 19:57:18 +000010439 if( ssl->transform )
10440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 mbedtls_ssl_transform_free( ssl->transform );
10442 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010443 }
10444
10445 if( ssl->handshake )
10446 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010447 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10449 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 mbedtls_free( ssl->handshake );
10452 mbedtls_free( ssl->transform_negotiate );
10453 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010454 }
10455
Paul Bakkerc0463502013-02-14 11:19:38 +010010456 if( ssl->session )
10457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 mbedtls_ssl_session_free( ssl->session );
10459 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010460 }
10461
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010462#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010463 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010464 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010465 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010467 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010470#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10471 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10474 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010475 }
10476#endif
10477
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010478#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010480#endif
10481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010483
Paul Bakker86f04f42013-02-14 11:20:09 +010010484 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010485 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010486}
10487
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010488/*
10489 * Initialze mbedtls_ssl_config
10490 */
10491void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10492{
10493 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10494}
10495
Simon Butcherc97b6972015-12-27 23:48:17 +000010496#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010497static int ssl_preset_default_hashes[] = {
10498#if defined(MBEDTLS_SHA512_C)
10499 MBEDTLS_MD_SHA512,
10500 MBEDTLS_MD_SHA384,
10501#endif
10502#if defined(MBEDTLS_SHA256_C)
10503 MBEDTLS_MD_SHA256,
10504 MBEDTLS_MD_SHA224,
10505#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010506#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010507 MBEDTLS_MD_SHA1,
10508#endif
10509 MBEDTLS_MD_NONE
10510};
Simon Butcherc97b6972015-12-27 23:48:17 +000010511#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010512
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010513static int ssl_preset_suiteb_ciphersuites[] = {
10514 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10515 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10516 0
10517};
10518
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010519#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010520static int ssl_preset_suiteb_hashes[] = {
10521 MBEDTLS_MD_SHA256,
10522 MBEDTLS_MD_SHA384,
10523 MBEDTLS_MD_NONE
10524};
10525#endif
10526
10527#if defined(MBEDTLS_ECP_C)
10528static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10529 MBEDTLS_ECP_DP_SECP256R1,
10530 MBEDTLS_ECP_DP_SECP384R1,
10531 MBEDTLS_ECP_DP_NONE
10532};
10533#endif
10534
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010535/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010536 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010537 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010538int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010539 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010540{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010541#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010542 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010543#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010544
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010545 /* Use the functions here so that they are covered in tests,
10546 * but otherwise access member directly for efficiency */
10547 mbedtls_ssl_conf_endpoint( conf, endpoint );
10548 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010549
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010550 /*
10551 * Things that are common to all presets
10552 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010553#if defined(MBEDTLS_SSL_CLI_C)
10554 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10555 {
10556 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10557#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10558 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10559#endif
10560 }
10561#endif
10562
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010563#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010564 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010565#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010566
10567#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10568 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10569#endif
10570
10571#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10572 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10573#endif
10574
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010575#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10576 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10577#endif
10578
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010579#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010580 conf->f_cookie_write = ssl_cookie_write_dummy;
10581 conf->f_cookie_check = ssl_cookie_check_dummy;
10582#endif
10583
10584#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10585 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10586#endif
10587
Janos Follath088ce432017-04-10 12:42:31 +010010588#if defined(MBEDTLS_SSL_SRV_C)
10589 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10590#endif
10591
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010592#if defined(MBEDTLS_SSL_PROTO_DTLS)
10593 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10594 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10595#endif
10596
10597#if defined(MBEDTLS_SSL_RENEGOTIATION)
10598 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010599 memset( conf->renego_period, 0x00, 2 );
10600 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010601#endif
10602
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010603#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10604 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10605 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010606 const unsigned char dhm_p[] =
10607 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10608 const unsigned char dhm_g[] =
10609 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10610
Hanno Beckera90658f2017-10-04 15:29:08 +010010611 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10612 dhm_p, sizeof( dhm_p ),
10613 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010614 {
10615 return( ret );
10616 }
10617 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010618#endif
10619
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010620 /*
10621 * Preset-specific defaults
10622 */
10623 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010624 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010625 /*
10626 * NSA Suite B
10627 */
10628 case MBEDTLS_SSL_PRESET_SUITEB:
10629 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10630 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10631 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10632 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10633
10634 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10635 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10636 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10637 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10638 ssl_preset_suiteb_ciphersuites;
10639
10640#if defined(MBEDTLS_X509_CRT_PARSE_C)
10641 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010642#endif
10643
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010644#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010645 conf->sig_hashes = ssl_preset_suiteb_hashes;
10646#endif
10647
10648#if defined(MBEDTLS_ECP_C)
10649 conf->curve_list = ssl_preset_suiteb_curves;
10650#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010651 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010652
10653 /*
10654 * Default
10655 */
10656 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010657 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10658 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10659 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10660 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10661 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10662 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10663 MBEDTLS_SSL_MIN_MINOR_VERSION :
10664 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010665 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10666 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10667
10668#if defined(MBEDTLS_SSL_PROTO_DTLS)
10669 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10670 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10671#endif
10672
10673 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10674 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10675 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10676 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10677 mbedtls_ssl_list_ciphersuites();
10678
10679#if defined(MBEDTLS_X509_CRT_PARSE_C)
10680 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10681#endif
10682
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010683#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010684 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010685#endif
10686
10687#if defined(MBEDTLS_ECP_C)
10688 conf->curve_list = mbedtls_ecp_grp_id_list();
10689#endif
10690
10691#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10692 conf->dhm_min_bitlen = 1024;
10693#endif
10694 }
10695
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010696 return( 0 );
10697}
10698
10699/*
10700 * Free mbedtls_ssl_config
10701 */
10702void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10703{
10704#if defined(MBEDTLS_DHM_C)
10705 mbedtls_mpi_free( &conf->dhm_P );
10706 mbedtls_mpi_free( &conf->dhm_G );
10707#endif
10708
10709#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10710 if( conf->psk != NULL )
10711 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010712 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010713 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010714 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010715 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010716 }
10717
10718 if( conf->psk_identity != NULL )
10719 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010720 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010721 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010722 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723 conf->psk_identity_len = 0;
10724 }
10725#endif
10726
10727#if defined(MBEDTLS_X509_CRT_PARSE_C)
10728 ssl_key_cert_free( conf->key_cert );
10729#endif
10730
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010731 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010732}
10733
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010734#if defined(MBEDTLS_PK_C) && \
10735 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010736/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741#if defined(MBEDTLS_RSA_C)
10742 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10743 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745#if defined(MBEDTLS_ECDSA_C)
10746 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10747 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010750}
10751
Hanno Becker7e5437a2017-04-28 17:15:26 +010010752unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10753{
10754 switch( type ) {
10755 case MBEDTLS_PK_RSA:
10756 return( MBEDTLS_SSL_SIG_RSA );
10757 case MBEDTLS_PK_ECDSA:
10758 case MBEDTLS_PK_ECKEY:
10759 return( MBEDTLS_SSL_SIG_ECDSA );
10760 default:
10761 return( MBEDTLS_SSL_SIG_ANON );
10762 }
10763}
10764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010766{
10767 switch( sig )
10768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769#if defined(MBEDTLS_RSA_C)
10770 case MBEDTLS_SSL_SIG_RSA:
10771 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773#if defined(MBEDTLS_ECDSA_C)
10774 case MBEDTLS_SSL_SIG_ECDSA:
10775 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010776#endif
10777 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010779 }
10780}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010781#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010782
Hanno Becker7e5437a2017-04-28 17:15:26 +010010783#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10784 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10785
10786/* Find an entry in a signature-hash set matching a given hash algorithm. */
10787mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10788 mbedtls_pk_type_t sig_alg )
10789{
10790 switch( sig_alg )
10791 {
10792 case MBEDTLS_PK_RSA:
10793 return( set->rsa );
10794 case MBEDTLS_PK_ECDSA:
10795 return( set->ecdsa );
10796 default:
10797 return( MBEDTLS_MD_NONE );
10798 }
10799}
10800
10801/* Add a signature-hash-pair to a signature-hash set */
10802void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10803 mbedtls_pk_type_t sig_alg,
10804 mbedtls_md_type_t md_alg )
10805{
10806 switch( sig_alg )
10807 {
10808 case MBEDTLS_PK_RSA:
10809 if( set->rsa == MBEDTLS_MD_NONE )
10810 set->rsa = md_alg;
10811 break;
10812
10813 case MBEDTLS_PK_ECDSA:
10814 if( set->ecdsa == MBEDTLS_MD_NONE )
10815 set->ecdsa = md_alg;
10816 break;
10817
10818 default:
10819 break;
10820 }
10821}
10822
10823/* Allow exactly one hash algorithm for each signature. */
10824void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10825 mbedtls_md_type_t md_alg )
10826{
10827 set->rsa = md_alg;
10828 set->ecdsa = md_alg;
10829}
10830
10831#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10832 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10833
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010834/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010835 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010838{
10839 switch( hash )
10840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841#if defined(MBEDTLS_MD5_C)
10842 case MBEDTLS_SSL_HASH_MD5:
10843 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845#if defined(MBEDTLS_SHA1_C)
10846 case MBEDTLS_SSL_HASH_SHA1:
10847 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849#if defined(MBEDTLS_SHA256_C)
10850 case MBEDTLS_SSL_HASH_SHA224:
10851 return( MBEDTLS_MD_SHA224 );
10852 case MBEDTLS_SSL_HASH_SHA256:
10853 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010854#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010855#if defined(MBEDTLS_SHA512_C)
10856 case MBEDTLS_SSL_HASH_SHA384:
10857 return( MBEDTLS_MD_SHA384 );
10858 case MBEDTLS_SSL_HASH_SHA512:
10859 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010860#endif
10861 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010862 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010863 }
10864}
10865
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010866/*
10867 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10868 */
10869unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10870{
10871 switch( md )
10872 {
10873#if defined(MBEDTLS_MD5_C)
10874 case MBEDTLS_MD_MD5:
10875 return( MBEDTLS_SSL_HASH_MD5 );
10876#endif
10877#if defined(MBEDTLS_SHA1_C)
10878 case MBEDTLS_MD_SHA1:
10879 return( MBEDTLS_SSL_HASH_SHA1 );
10880#endif
10881#if defined(MBEDTLS_SHA256_C)
10882 case MBEDTLS_MD_SHA224:
10883 return( MBEDTLS_SSL_HASH_SHA224 );
10884 case MBEDTLS_MD_SHA256:
10885 return( MBEDTLS_SSL_HASH_SHA256 );
10886#endif
10887#if defined(MBEDTLS_SHA512_C)
10888 case MBEDTLS_MD_SHA384:
10889 return( MBEDTLS_SSL_HASH_SHA384 );
10890 case MBEDTLS_MD_SHA512:
10891 return( MBEDTLS_SSL_HASH_SHA512 );
10892#endif
10893 default:
10894 return( MBEDTLS_SSL_HASH_NONE );
10895 }
10896}
10897
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010898#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010899/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010900 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010901 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010902 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010903int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010905 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010906
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010907 if( ssl->conf->curve_list == NULL )
10908 return( -1 );
10909
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010910 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010911 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010912 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010913
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010914 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010915}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010916#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010917
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010918#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010919/*
10920 * Check if a hash proposed by the peer is in our list.
10921 * Return 0 if we're willing to use it, -1 otherwise.
10922 */
10923int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10924 mbedtls_md_type_t md )
10925{
10926 const int *cur;
10927
10928 if( ssl->conf->sig_hashes == NULL )
10929 return( -1 );
10930
10931 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10932 if( *cur == (int) md )
10933 return( 0 );
10934
10935 return( -1 );
10936}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010937#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939#if defined(MBEDTLS_X509_CRT_PARSE_C)
10940int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10941 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010942 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010943 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010944{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010945 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010947 int usage = 0;
10948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010950 const char *ext_oid;
10951 size_t ext_len;
10952#endif
10953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10955 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010956 ((void) cert);
10957 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010958 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010959#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10962 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010963 {
10964 /* Server part of the key exchange */
10965 switch( ciphersuite->key_exchange )
10966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967 case MBEDTLS_KEY_EXCHANGE_RSA:
10968 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010969 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010970 break;
10971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10973 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10974 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10975 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010976 break;
10977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010978 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10979 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010980 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010981 break;
10982
10983 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984 case MBEDTLS_KEY_EXCHANGE_NONE:
10985 case MBEDTLS_KEY_EXCHANGE_PSK:
10986 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10987 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010988 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010989 usage = 0;
10990 }
10991 }
10992 else
10993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10995 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010996 }
10997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010999 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011000 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011001 ret = -1;
11002 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011003#else
11004 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011007#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11008 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11011 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011012 }
11013 else
11014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11016 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011017 }
11018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011020 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011021 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011022 ret = -1;
11023 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011025
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011026 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011027}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011028#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011029
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011030/*
11031 * Convert version numbers to/from wire format
11032 * and, for DTLS, to/from TLS equivalent.
11033 *
11034 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011035 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011036 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11037 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011040 unsigned char ver[2] )
11041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042#if defined(MBEDTLS_SSL_PROTO_DTLS)
11043 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011046 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11047
11048 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11049 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11050 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011051 else
11052#else
11053 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011054#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011055 {
11056 ver[0] = (unsigned char) major;
11057 ver[1] = (unsigned char) minor;
11058 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011059}
11060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011061void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011062 const unsigned char ver[2] )
11063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011064#if defined(MBEDTLS_SSL_PROTO_DTLS)
11065 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011066 {
11067 *major = 255 - ver[0] + 2;
11068 *minor = 255 - ver[1] + 1;
11069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011071 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11072 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011073 else
11074#else
11075 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011076#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011077 {
11078 *major = ver[0];
11079 *minor = ver[1];
11080 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011081}
11082
Simon Butcher99000142016-10-13 17:21:01 +010011083int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11084{
11085#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11086 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11087 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11088
11089 switch( md )
11090 {
11091#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11092#if defined(MBEDTLS_MD5_C)
11093 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011094 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011095#endif
11096#if defined(MBEDTLS_SHA1_C)
11097 case MBEDTLS_SSL_HASH_SHA1:
11098 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11099 break;
11100#endif
11101#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11102#if defined(MBEDTLS_SHA512_C)
11103 case MBEDTLS_SSL_HASH_SHA384:
11104 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11105 break;
11106#endif
11107#if defined(MBEDTLS_SHA256_C)
11108 case MBEDTLS_SSL_HASH_SHA256:
11109 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11110 break;
11111#endif
11112 default:
11113 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11114 }
11115
11116 return 0;
11117#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11118 (void) ssl;
11119 (void) md;
11120
11121 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11122#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11123}
11124
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011125#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11126 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11127int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11128 unsigned char *output,
11129 unsigned char *data, size_t data_len )
11130{
11131 int ret = 0;
11132 mbedtls_md5_context mbedtls_md5;
11133 mbedtls_sha1_context mbedtls_sha1;
11134
11135 mbedtls_md5_init( &mbedtls_md5 );
11136 mbedtls_sha1_init( &mbedtls_sha1 );
11137
11138 /*
11139 * digitally-signed struct {
11140 * opaque md5_hash[16];
11141 * opaque sha_hash[20];
11142 * };
11143 *
11144 * md5_hash
11145 * MD5(ClientHello.random + ServerHello.random
11146 * + ServerParams);
11147 * sha_hash
11148 * SHA(ClientHello.random + ServerHello.random
11149 * + ServerParams);
11150 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011151 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011152 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011154 goto exit;
11155 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011156 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011157 ssl->handshake->randbytes, 64 ) ) != 0 )
11158 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011160 goto exit;
11161 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011162 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011163 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011165 goto exit;
11166 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011167 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011168 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011170 goto exit;
11171 }
11172
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011173 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011174 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011176 goto exit;
11177 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011178 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011179 ssl->handshake->randbytes, 64 ) ) != 0 )
11180 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011182 goto exit;
11183 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011184 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011185 data_len ) ) != 0 )
11186 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011188 goto exit;
11189 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011190 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011191 output + 16 ) ) != 0 )
11192 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011194 goto exit;
11195 }
11196
11197exit:
11198 mbedtls_md5_free( &mbedtls_md5 );
11199 mbedtls_sha1_free( &mbedtls_sha1 );
11200
11201 if( ret != 0 )
11202 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11203 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11204
11205 return( ret );
11206
11207}
11208#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11209 MBEDTLS_SSL_PROTO_TLS1_1 */
11210
11211#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11212 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011213
11214#if defined(MBEDTLS_USE_PSA_CRYPTO)
11215int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11216 unsigned char *hash, size_t *hashlen,
11217 unsigned char *data, size_t data_len,
11218 mbedtls_md_type_t md_alg )
11219{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011220 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011221 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011222 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11223
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011225
11226 if( ( status = psa_hash_setup( &hash_operation,
11227 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011228 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011229 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011230 goto exit;
11231 }
11232
Andrzej Kurek814feff2019-01-14 04:35:19 -050011233 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11234 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011235 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011236 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011237 goto exit;
11238 }
11239
Andrzej Kurek814feff2019-01-14 04:35:19 -050011240 if( ( status = psa_hash_update( &hash_operation,
11241 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011242 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011243 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011244 goto exit;
11245 }
11246
Andrzej Kurek814feff2019-01-14 04:35:19 -050011247 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11248 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011249 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011250 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011251 goto exit;
11252 }
11253
11254exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011255 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 {
11257 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11258 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011259 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011260 {
11261 case PSA_ERROR_NOT_SUPPORTED:
11262 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011263 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011264 case PSA_ERROR_BUFFER_TOO_SMALL:
11265 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11266 case PSA_ERROR_INSUFFICIENT_MEMORY:
11267 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11268 default:
11269 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11270 }
11271 }
11272 return( 0 );
11273}
11274
11275#else
11276
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011277int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011278 unsigned char *hash, size_t *hashlen,
11279 unsigned char *data, size_t data_len,
11280 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011281{
11282 int ret = 0;
11283 mbedtls_md_context_t ctx;
11284 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011285 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011286
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011288
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011289 mbedtls_md_init( &ctx );
11290
11291 /*
11292 * digitally-signed struct {
11293 * opaque client_random[32];
11294 * opaque server_random[32];
11295 * ServerDHParams params;
11296 * };
11297 */
11298 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11299 {
11300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11301 goto exit;
11302 }
11303 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11304 {
11305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11306 goto exit;
11307 }
11308 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11309 {
11310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11311 goto exit;
11312 }
11313 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11314 {
11315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11316 goto exit;
11317 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011318 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011319 {
11320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11321 goto exit;
11322 }
11323
11324exit:
11325 mbedtls_md_free( &ctx );
11326
11327 if( ret != 0 )
11328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11329 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11330
11331 return( ret );
11332}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011333#endif /* MBEDTLS_USE_PSA_CRYPTO */
11334
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011335#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11336 MBEDTLS_SSL_PROTO_TLS1_2 */
11337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011338#endif /* MBEDTLS_SSL_TLS_C */