blob: d7291fec0ec204e93c0e05b21ebc2f2de4df1a79 [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 );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Becker35c36a62019-04-23 12:31:42 +0100121#if defined(MBEDTLS_SSL_CID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100122/* Top-level Connection ID API */
123
Hanno Beckerca092242019-04-25 16:01:49 +0100124/* WARNING: The CID feature isn't fully implemented yet
125 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100126int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
127 int enable,
128 unsigned char const *own_cid,
129 size_t own_cid_len )
130{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100131 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133
Hanno Beckerca092242019-04-25 16:01:49 +0100134 ssl->negotiate_cid = enable;
135 if( enable == MBEDTLS_SSL_CID_DISABLED )
136 {
137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
138 return( 0 );
139 }
140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
141
142 if( own_cid_len > MBEDTLS_SSL_CID_IN_LEN_MAX )
143 {
144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID too large: Maximum %u, actual %u",
145 (unsigned) MBEDTLS_SSL_CID_IN_LEN_MAX,
146 (unsigned) own_cid_len ) );
147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
148 }
149
150 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100151 /* Truncation is not an issue here because
152 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
153 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100154
155 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100156 return( 0 );
157}
158
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100159/* WARNING: The CID feature isn't fully implemented yet
160 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100161int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
162 int *enabled,
163 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
164 size_t *peer_cid_len )
165{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100166 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100167
Hanno Becker76a79ab2019-05-03 14:38:32 +0100168 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
169 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
170 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173
Hanno Beckerc5f24222019-05-03 12:54:52 +0100174 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
175 * were used, but client and server requested the empty CID.
176 * This is indistinguishable from not using the CID extension
177 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100178 if( ssl->transform_in->in_cid_len == 0 &&
179 ssl->transform_in->out_cid_len == 0 )
180 {
181 return( 0 );
182 }
183
184 *peer_cid_len = ssl->transform_in->out_cid_len;
185 memcpy( peer_cid, ssl->transform_in->out_cid,
186 ssl->transform_in->out_cid_len );
187
188 *enabled = MBEDTLS_SSL_CID_ENABLED;
189
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100190 return( 0 );
191}
Hanno Becker35c36a62019-04-23 12:31:42 +0100192#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100193
Hanno Beckerd5847772018-08-28 10:09:23 +0100194/* Forward declarations for functions related to message buffering. */
195static void ssl_buffering_free( mbedtls_ssl_context *ssl );
196static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
197 uint8_t slot );
198static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
199static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
200static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
201static int ssl_buffer_message( mbedtls_ssl_context *ssl );
202static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100203static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100204
Hanno Beckera67dee22018-08-22 10:05:20 +0100205static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100206static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100207{
Hanno Becker11682cc2018-08-22 14:41:02 +0100208 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100209
210 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100211 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100212
213 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
214}
215
Hanno Becker67bc7c32018-08-06 11:33:50 +0100216static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
217{
Hanno Becker11682cc2018-08-22 14:41:02 +0100218 size_t const bytes_written = ssl->out_left;
219 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100220
221 /* Double-check that the write-index hasn't gone
222 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100223 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100224 {
225 /* Should never happen... */
226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
227 }
228
229 return( (int) ( mtu - bytes_written ) );
230}
231
232static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
233{
234 int ret;
235 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400236 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100237
238#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
239 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
240
241 if( max_len > mfl )
242 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100243
244 /* By the standard (RFC 6066 Sect. 4), the MFL extension
245 * only limits the maximum record payload size, so in theory
246 * we would be allowed to pack multiple records of payload size
247 * MFL into a single datagram. However, this would mean that there's
248 * no way to explicitly communicate MTU restrictions to the peer.
249 *
250 * The following reduction of max_len makes sure that we never
251 * write datagrams larger than MFL + Record Expansion Overhead.
252 */
253 if( max_len <= ssl->out_left )
254 return( 0 );
255
256 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100257#endif
258
259 ret = ssl_get_remaining_space_in_datagram( ssl );
260 if( ret < 0 )
261 return( ret );
262 remaining = (size_t) ret;
263
264 ret = mbedtls_ssl_get_record_expansion( ssl );
265 if( ret < 0 )
266 return( ret );
267 expansion = (size_t) ret;
268
269 if( remaining <= expansion )
270 return( 0 );
271
272 remaining -= expansion;
273 if( remaining >= max_len )
274 remaining = max_len;
275
276 return( (int) remaining );
277}
278
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279/*
280 * Double the retransmit timeout value, within the allowed range,
281 * returning -1 if the maximum value has already been reached.
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284{
285 uint32_t new_timeout;
286
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200287 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200288 return( -1 );
289
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200290 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
291 * in the following way: after the initial transmission and a first
292 * retransmission, back off to a temporary estimated MTU of 508 bytes.
293 * This value is guaranteed to be deliverable (if not guaranteed to be
294 * delivered) of any compliant IPv4 (and IPv6) network, and should work
295 * on most non-IP stacks too. */
296 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400297 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200298 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
300 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200301
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200302 new_timeout = 2 * ssl->handshake->retransmit_timeout;
303
304 /* Avoid arithmetic overflow and range overflow */
305 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200306 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200307 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200308 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200309 }
310
311 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313 ssl->handshake->retransmit_timeout ) );
314
315 return( 0 );
316}
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200320 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200322 ssl->handshake->retransmit_timeout ) );
323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200327/*
328 * Convert max_fragment_length codes to length.
329 * RFC 6066 says:
330 * enum{
331 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
332 * } MaxFragmentLength;
333 * and we add 0 -> extension unused
334 */
Angus Grattond8213d02016-05-25 20:56:48 +1000335static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200336{
Angus Grattond8213d02016-05-25 20:56:48 +1000337 switch( mfl )
338 {
339 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
340 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
341 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
342 return 512;
343 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
344 return 1024;
345 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
346 return 2048;
347 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
348 return 4096;
349 default:
350 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
351 }
352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200354
Hanno Becker52055ae2019-02-06 14:30:46 +0000355int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
356 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_ssl_session_free( dst );
359 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000362
363#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200364 if( src->peer_cert != NULL )
365 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200366 int ret;
367
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200368 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200369 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200375 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 dst->peer_cert = NULL;
379 return( ret );
380 }
381 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000382#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000383 if( src->peer_cert_digest != NULL )
384 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000385 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000386 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000387 if( dst->peer_cert_digest == NULL )
388 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
389
390 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
391 src->peer_cert_digest_len );
392 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000393 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000394 }
395#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200398
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200399#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200400 if( src->ticket != NULL )
401 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200402 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200403 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200405
406 memcpy( dst->ticket, src->ticket, src->ticket_len );
407 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200408#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409
410 return( 0 );
411}
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
414int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200415 const unsigned char *key_enc, const unsigned char *key_dec,
416 size_t keylen,
417 const unsigned char *iv_enc, const unsigned char *iv_dec,
418 size_t ivlen,
419 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200420 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
422int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
423int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
424int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
425int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
426#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000427
Paul Bakker5121ce52009-01-03 21:22:43 +0000428/*
429 * Key material generation
430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200432static int ssl3_prf( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char *dstbuf, size_t dlen )
436{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100437 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000438 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_context md5;
440 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000441 unsigned char padding[16];
442 unsigned char sha1sum[20];
443 ((void)label);
444
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200445 mbedtls_md5_init( &md5 );
446 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200447
Paul Bakker5f70b252012-09-13 14:23:06 +0000448 /*
449 * SSLv3:
450 * block =
451 * MD5( secret + SHA1( 'A' + secret + random ) ) +
452 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
453 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
454 * ...
455 */
456 for( i = 0; i < dlen / 16; i++ )
457 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200458 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000459
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100464 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100465 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000470
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100473 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100474 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 }
480
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200482 mbedtls_md5_free( &md5 );
483 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500485 mbedtls_platform_zeroize( padding, sizeof( padding ) );
486 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000487
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200493static int tls1_prf( const unsigned char *secret, size_t slen,
494 const char *label,
495 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000496 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000497{
Paul Bakker23986e52011-04-24 08:57:21 +0000498 size_t nb, hs;
499 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200500 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300501 unsigned char *tmp;
502 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 const mbedtls_md_info_t *md_info;
505 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100506 int ret;
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Ron Eldor3b350852019-05-07 18:31:49 +0300510 tmp_len = 20 + strlen( label ) + rlen;
511 tmp = mbedtls_calloc( 1, tmp_len );
512 if( tmp == NULL )
513 {
514 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
515 goto exit;
516 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 hs = ( slen + 1 ) / 2;
519 S1 = secret;
520 S2 = secret + slen - hs;
521
522 nb = strlen( label );
523 memcpy( tmp + 20, label, nb );
524 memcpy( tmp + 20 + nb, random, rlen );
525 nb += rlen;
526
527 /*
528 * First compute P_md5(secret,label+random)[0..dlen]
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300531 {
532 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
533 goto exit;
534 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300537 {
538 goto exit;
539 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
542 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
543 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
545 for( i = 0; i < dlen; i += 16 )
546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_md_hmac_reset ( &md_ctx );
548 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
549 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_md_hmac_reset ( &md_ctx );
552 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
553 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
556
557 for( j = 0; j < k; j++ )
558 dstbuf[i + j] = h_i[j];
559 }
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100562
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 /*
564 * XOR out with P_sha1(secret,label+random)[0..dlen]
565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300567 {
568 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
569 goto exit;
570 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300573 {
574 goto exit;
575 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
578 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
579 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 for( i = 0; i < dlen; i += 20 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
585 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_hmac_reset ( &md_ctx );
588 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
589 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
592
593 for( j = 0; j < k; j++ )
594 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
595 }
596
Ron Eldor3b350852019-05-07 18:31:49 +0300597exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100599
Ron Eldor3b350852019-05-07 18:31:49 +0300600 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500601 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Ron Eldor3b350852019-05-07 18:31:49 +0300603 mbedtls_free( tmp );
604 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500609#if defined(MBEDTLS_USE_PSA_CRYPTO)
610static int tls_prf_generic( mbedtls_md_type_t md_type,
611 const unsigned char *secret, size_t slen,
612 const char *label,
613 const unsigned char *random, size_t rlen,
614 unsigned char *dstbuf, size_t dlen )
615{
616 psa_status_t status;
617 psa_algorithm_t alg;
618 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500619 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500620 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
621
Andrzej Kurek2f760752019-01-28 08:08:15 -0500622 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500623 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500624
Andrzej Kurekc929a822019-01-14 03:51:11 -0500625 if( md_type == MBEDTLS_MD_SHA384 )
626 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
627 else
628 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
629
Andrzej Kurek2f760752019-01-28 08:08:15 -0500630 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500631 psa_key_policy_set_usage( &policy,
632 PSA_KEY_USAGE_DERIVE,
633 alg );
634 status = psa_set_key_policy( master_slot, &policy );
635 if( status != PSA_SUCCESS )
636 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
637
638 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
639 if( status != PSA_SUCCESS )
640 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
641
642 status = psa_key_derivation( &generator,
643 master_slot, alg,
644 random, rlen,
645 (unsigned char const *) label,
646 (size_t) strlen( label ),
647 dlen );
648 if( status != PSA_SUCCESS )
649 {
650 psa_generator_abort( &generator );
651 psa_destroy_key( master_slot );
652 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
653 }
654
655 status = psa_generator_read( &generator, dstbuf, dlen );
656 if( status != PSA_SUCCESS )
657 {
658 psa_generator_abort( &generator );
659 psa_destroy_key( master_slot );
660 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
661 }
662
663 status = psa_generator_abort( &generator );
664 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500665 {
666 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500667 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500668 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500669
670 status = psa_destroy_key( master_slot );
671 if( status != PSA_SUCCESS )
672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
673
Andrzej Kurek33171262019-01-15 03:25:18 -0500674 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500675}
676
677#else /* MBEDTLS_USE_PSA_CRYPTO */
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100680 const unsigned char *secret, size_t slen,
681 const char *label,
682 const unsigned char *random, size_t rlen,
683 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000684{
685 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100686 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300687 unsigned char *tmp;
688 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
690 const mbedtls_md_info_t *md_info;
691 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100692 int ret;
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
697 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100700
Ron Eldor3b350852019-05-07 18:31:49 +0300701 tmp_len = md_len + strlen( label ) + rlen;
702 tmp = mbedtls_calloc( 1, tmp_len );
703 if( tmp == NULL )
704 {
705 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
706 goto exit;
707 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000708
709 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100710 memcpy( tmp + md_len, label, nb );
711 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000712 nb += rlen;
713
714 /*
715 * Compute P_<hash>(secret, label + random)[0..dlen]
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300718 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
721 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
722 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100723
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100724 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_md_hmac_reset ( &md_ctx );
727 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
728 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_md_hmac_reset ( &md_ctx );
731 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
732 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000733
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100734 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000735
736 for( j = 0; j < k; j++ )
737 dstbuf[i + j] = h_i[j];
738 }
739
Ron Eldor3b350852019-05-07 18:31:49 +0300740exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100742
Ron Eldor3b350852019-05-07 18:31:49 +0300743 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500744 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000745
Ron Eldor3b350852019-05-07 18:31:49 +0300746 mbedtls_free( tmp );
747
748 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000749}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500750#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100752static int tls_prf_sha256( const unsigned char *secret, size_t slen,
753 const char *label,
754 const unsigned char *random, size_t rlen,
755 unsigned char *dstbuf, size_t dlen )
756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100758 label, random, rlen, dstbuf, dlen ) );
759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200763static int tls_prf_sha384( const unsigned char *secret, size_t slen,
764 const char *label,
765 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000766 unsigned char *dstbuf, size_t dlen )
767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100769 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#endif /* MBEDTLS_SHA512_C */
772#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
777 defined(MBEDTLS_SSL_PROTO_TLS1_1)
778static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200779#endif
Paul Bakker380da532012-04-18 16:10:25 +0000780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SSL_PROTO_SSL3)
782static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
783static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784#endif
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
787static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
788static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200789#endif
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
792#if defined(MBEDTLS_SHA256_C)
793static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
794static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
795static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200796#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_SHA512_C)
799static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
800static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
801static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100805#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100806 defined(MBEDTLS_USE_PSA_CRYPTO)
807static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
808{
809 if( ssl->conf->f_psk != NULL )
810 {
811 /* If we've used a callback to select the PSK,
812 * the static configuration is irrelevant. */
813 if( ssl->handshake->psk_opaque != 0 )
814 return( 1 );
815
816 return( 0 );
817 }
818
819 if( ssl->conf->psk_opaque != 0 )
820 return( 1 );
821
822 return( 0 );
823}
824#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100825 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100826
Ron Eldorcf280092019-05-14 20:19:13 +0300827#if defined(MBEDTLS_SSL_EXPORT_KEYS)
828static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
829{
830#if defined(MBEDTLS_SSL_PROTO_SSL3)
831 if( tls_prf == ssl3_prf )
832 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300833 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300834 }
835 else
836#endif
837#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
838 if( tls_prf == tls1_prf )
839 {
840 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
841 }
842 else
843#endif
844#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
845#if defined(MBEDTLS_SHA512_C)
846 if( tls_prf == tls_prf_sha384 )
847 {
848 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
849 }
850 else
851#endif
852#if defined(MBEDTLS_SHA256_C)
853 if( tls_prf == tls_prf_sha256 )
854 {
855 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
856 }
857 else
858#endif
859#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
860 return( MBEDTLS_SSL_TLS_PRF_NONE );
861}
862#endif /* MBEDTLS_SSL_EXPORT_KEYS */
863
Ron Eldor51d3ab52019-05-12 14:54:30 +0300864int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
865 const unsigned char *secret, size_t slen,
866 const char *label,
867 const unsigned char *random, size_t rlen,
868 unsigned char *dstbuf, size_t dlen )
869{
870 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
871
872 switch( prf )
873 {
874#if defined(MBEDTLS_SSL_PROTO_SSL3)
875 case MBEDTLS_SSL_TLS_PRF_SSL3:
876 tls_prf = ssl3_prf;
877 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300878#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300879#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
880 case MBEDTLS_SSL_TLS_PRF_TLS1:
881 tls_prf = tls1_prf;
882 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300883#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
884
885#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300886#if defined(MBEDTLS_SHA512_C)
887 case MBEDTLS_SSL_TLS_PRF_SHA384:
888 tls_prf = tls_prf_sha384;
889 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300890#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300891#if defined(MBEDTLS_SHA256_C)
892 case MBEDTLS_SSL_TLS_PRF_SHA256:
893 tls_prf = tls_prf_sha256;
894 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300895#endif /* MBEDTLS_SHA256_C */
896#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300897 default:
898 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
899 }
900
901 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
902}
903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000905{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200906 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000907#if defined(MBEDTLS_USE_PSA_CRYPTO)
908 int psa_fallthrough;
909#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 unsigned char keyblk[256];
912 unsigned char *key1;
913 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100914 unsigned char *mac_enc;
915 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000916 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200917 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000918 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000919 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 const mbedtls_cipher_info_t *cipher_info;
921 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100922
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000923 /* cf. RFC 5246, Section 8.1:
924 * "The master secret is always exactly 48 bytes in length." */
925 size_t const master_secret_len = 48;
926
Hanno Becker35b23c72018-10-23 12:10:41 +0100927#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
928 unsigned char session_hash[48];
929#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_ssl_session *session = ssl->session_negotiate;
932 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
933 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000937#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
938 transform->encrypt_then_mac = session->encrypt_then_mac;
939#endif
940 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000941
942 ciphersuite_info = handshake->ciphersuite_info;
943 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100944 if( cipher_info == NULL )
945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000947 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100949 }
950
Hanno Beckere694c3e2017-12-27 21:34:08 +0000951 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100952 if( md_info == NULL )
953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000955 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100957 }
958
Hanno Becker4bf74652019-04-26 16:22:27 +0100959#if defined(MBEDTLS_SSL_CID)
960 /* Copy own and peer's CID if the use of the CID
961 * extension has been negotiated. */
962 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100965
966 /* Uncomment this once CID-parsing and support for a change
967 * record content type during record decryption are added. */
968 /* transform->in_cid_len = ssl->own_cid_len; */
969 /* transform->out_cid_len = ssl->handshake->peer_cid_len; */
970 /* memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); */
971 /* memcpy( transform->out_cid, ssl->handshake->peer_cid, */
972 /* ssl->handshake->peer_cid_len ); */
Hanno Becker4bf74652019-04-26 16:22:27 +0100973
974 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
975 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100976 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100977 transform->in_cid_len );
978 }
979#endif /* MBEDTLS_SSL_CID */
980
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000982 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_SSL_PROTO_SSL3)
985 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000986 {
Paul Bakker48916f92012-09-16 19:57:18 +0000987 handshake->tls_prf = ssl3_prf;
988 handshake->calc_verify = ssl_calc_verify_ssl;
989 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000990 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200991 else
992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
994 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000995 {
Paul Bakker48916f92012-09-16 19:57:18 +0000996 handshake->tls_prf = tls1_prf;
997 handshake->calc_verify = ssl_calc_verify_tls;
998 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000999 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001000 else
1001#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1003#if defined(MBEDTLS_SHA512_C)
1004 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001005 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001006 {
Paul Bakker48916f92012-09-16 19:57:18 +00001007 handshake->tls_prf = tls_prf_sha384;
1008 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1009 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001010 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001011 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_SHA256_C)
1014 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001015 {
Paul Bakker48916f92012-09-16 19:57:18 +00001016 handshake->tls_prf = tls_prf_sha256;
1017 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1018 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001019 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001020 else
1021#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1025 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001026 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001027
1028 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 * SSLv3:
1030 * master =
1031 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1032 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1033 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001034 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001035 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 * master = PRF( premaster, "master secret", randbytes )[0..47]
1037 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001038 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1041 }
1042 else
1043 {
1044 /* The label for the KDF used for key expansion.
1045 * This is either "master secret" or "extended master secret"
1046 * depending on whether the Extended Master Secret extension
1047 * is used. */
1048 char const *lbl = "master secret";
1049
1050 /* The salt for the KDF used for key expansion.
1051 * - If the Extended Master Secret extension is not used,
1052 * this is ClientHello.Random + ServerHello.Random
1053 * (see Sect. 8.1 in RFC 5246).
1054 * - If the Extended Master Secret extension is used,
1055 * this is the transcript of the handshake so far.
1056 * (see Sect. 4 in RFC 7627). */
1057 unsigned char const *salt = handshake->randbytes;
1058 size_t salt_len = 64;
1059
1060#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001064
Hanno Becker35b23c72018-10-23 12:10:41 +01001065 lbl = "extended master secret";
1066 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001067 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1069 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001072 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1073 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001074 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001075 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001076 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001077#endif /* MBEDTLS_SHA512_C */
1078 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001079 }
1080 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001082 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001083
Hanno Becker35b23c72018-10-23 12:10:41 +01001084 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001085 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001086#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1087
Hanno Becker7d0a5692018-10-23 15:26:22 +01001088#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1089 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1090 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1091 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1092 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001093 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001094 /* Perform PSK-to-MS expansion in a single step. */
1095 psa_status_t status;
1096 psa_algorithm_t alg;
1097 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001098 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001099
1100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1101
1102 psk = ssl->conf->psk_opaque;
1103 if( ssl->handshake->psk_opaque != 0 )
1104 psk = ssl->handshake->psk_opaque;
1105
Hanno Becker22bf1452019-04-05 11:21:08 +01001106 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001107 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1108 else
1109 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1110
1111 status = psa_key_derivation( &generator, psk, alg,
1112 salt, salt_len,
1113 (unsigned char const *) lbl,
1114 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001115 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001116 if( status != PSA_SUCCESS )
1117 {
1118 psa_generator_abort( &generator );
1119 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1120 }
1121
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001122 status = psa_generator_read( &generator, session->master,
1123 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001124 if( status != PSA_SUCCESS )
1125 {
1126 psa_generator_abort( &generator );
1127 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1128 }
1129
1130 status = psa_generator_abort( &generator );
1131 if( status != PSA_SUCCESS )
1132 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001133 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001134 else
1135#endif
1136 {
1137 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1138 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001139 session->master,
1140 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 if( ret != 0 )
1142 {
1143 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1144 return( ret );
1145 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001146
Hanno Becker7d0a5692018-10-23 15:26:22 +01001147 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1148 handshake->premaster,
1149 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001150
Hanno Becker7d0a5692018-10-23 15:26:22 +01001151 mbedtls_platform_zeroize( handshake->premaster,
1152 sizeof(handshake->premaster) );
1153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
1156 /*
1157 * Swap the client and server random values.
1158 */
Paul Bakker48916f92012-09-16 19:57:18 +00001159 memcpy( tmp, handshake->randbytes, 64 );
1160 memcpy( handshake->randbytes, tmp + 32, 32 );
1161 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001162 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
1164 /*
1165 * SSLv3:
1166 * key block =
1167 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1168 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1169 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1170 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1171 * ...
1172 *
1173 * TLSv1:
1174 * key block = PRF( master, "key expansion", randbytes )
1175 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001176 ret = handshake->tls_prf( session->master, 48, "key expansion",
1177 handshake->randbytes, 64, keyblk, 256 );
1178 if( ret != 0 )
1179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001181 return( ret );
1182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1185 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1186 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1187 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1188 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 /*
1191 * Determine the appropriate key, IV and MAC length.
1192 */
Paul Bakker68884e32013-01-07 18:20:04 +01001193
Hanno Becker88aaf652017-12-27 08:17:40 +00001194 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001195
Hanno Becker8031d062018-01-03 15:32:31 +00001196#if defined(MBEDTLS_GCM_C) || \
1197 defined(MBEDTLS_CCM_C) || \
1198 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001200 cipher_info->mode == MBEDTLS_MODE_CCM ||
1201 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001203 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001204
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001205 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001206 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001207 transform->taglen =
1208 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001209
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001210 /* All modes haves 96-bit IVs;
1211 * GCM and CCM has 4 implicit and 8 explicit bytes
1212 * ChachaPoly has all 12 bytes implicit
1213 */
Paul Bakker68884e32013-01-07 18:20:04 +01001214 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001215 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1216 transform->fixed_ivlen = 12;
1217 else
1218 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001219
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001220 /* Minimum length of encrypted record */
1221 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001222 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001223 }
1224 else
Hanno Becker8031d062018-01-03 15:32:31 +00001225#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1226#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1227 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1228 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001229 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001230 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1232 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001235 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001236 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001238 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001239 mac_key_len = mbedtls_md_get_size( md_info );
1240 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001243 /*
1244 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1245 * (rfc 6066 page 13 or rfc 2104 section 4),
1246 * so we only need to adjust the length here.
1247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001251
1252#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1253 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001254 * HMAC implementation which also truncates the key
1255 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001256 mac_key_len = transform->maclen;
1257#endif
1258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001260
1261 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001262 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001264 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001266 transform->minlen = transform->maclen;
1267 else
Paul Bakker68884e32013-01-07 18:20:04 +01001268 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001269 /*
1270 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001271 * 1. if EtM is in use: one block plus MAC
1272 * otherwise: * first multiple of blocklen greater than maclen
1273 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1276 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001277 {
1278 transform->minlen = transform->maclen
1279 + cipher_info->block_size;
1280 }
1281 else
1282#endif
1283 {
1284 transform->minlen = transform->maclen
1285 + cipher_info->block_size
1286 - transform->maclen % cipher_info->block_size;
1287 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1290 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1291 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001292 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001293 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1296 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1297 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001298 {
1299 transform->minlen += transform->ivlen;
1300 }
1301 else
1302#endif
1303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001305 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1306 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001307 }
Paul Bakker68884e32013-01-07 18:20:04 +01001308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
Hanno Becker8031d062018-01-03 15:32:31 +00001310 else
1311#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1312 {
1313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
Hanno Becker88aaf652017-12-27 08:17:40 +00001317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1318 (unsigned) keylen,
1319 (unsigned) transform->minlen,
1320 (unsigned) transform->ivlen,
1321 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 /*
1324 * Finally setup the cipher contexts, IVs and MAC secrets.
1325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001327 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001329 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001330 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Paul Bakker68884e32013-01-07 18:20:04 +01001332 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001333 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001335 /*
1336 * This is not used in TLS v1.1.
1337 */
Paul Bakker48916f92012-09-16 19:57:18 +00001338 iv_copy_len = ( transform->fixed_ivlen ) ?
1339 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001340 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1341 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001342 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 }
1344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#endif /* MBEDTLS_SSL_CLI_C */
1346#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001347 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001349 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001350 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
Hanno Becker81c7b182017-11-09 18:39:33 +00001352 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001353 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001355 /*
1356 * This is not used in TLS v1.1.
1357 */
Paul Bakker48916f92012-09-16 19:57:18 +00001358 iv_copy_len = ( transform->fixed_ivlen ) ?
1359 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001360 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1361 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001362 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001368 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1369 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Hanno Beckerd56ed242018-01-03 15:32:51 +00001372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_SSL_PROTO_SSL3)
1374 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001375 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001376 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001379 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1380 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001381 }
1382
Hanno Becker81c7b182017-11-09 18:39:33 +00001383 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1384 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001385 }
1386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1389 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1390 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001391 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001392 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1393 For AEAD-based ciphersuites, there is nothing to do here. */
1394 if( mac_key_len != 0 )
1395 {
1396 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1397 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1398 }
Paul Bakker68884e32013-01-07 18:20:04 +01001399 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001400 else
1401#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001404 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1405 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001406 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001407#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1410 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001411 {
1412 int ret = 0;
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001415
Hanno Becker88aaf652017-12-27 08:17:40 +00001416 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001417 transform->iv_enc, transform->iv_dec,
1418 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001419 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001420 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001423 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1424 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001425 }
1426 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001427#else
1428 ((void) mac_dec);
1429 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001431
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001432#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1433 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001434 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001435 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1436 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001437 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001438 iv_copy_len );
1439 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001440
1441 if( ssl->conf->f_export_keys_ext != NULL )
1442 {
1443 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1444 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001445 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001446 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001447 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001448 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001449 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001450 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001451#endif
1452
Hanno Beckerf704bef2018-11-16 15:21:18 +00001453#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001454
1455 /* Only use PSA-based ciphers for TLS-1.2.
1456 * That's relevant at least for TLS-1.0, where
1457 * we assume that mbedtls_cipher_crypt() updates
1458 * the structure field for the IV, which the PSA-based
1459 * implementation currently doesn't. */
1460#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1461 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001462 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001463 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001464 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001465 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1466 {
1467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001468 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001469 }
1470
1471 if( ret == 0 )
1472 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001474 psa_fallthrough = 0;
1475 }
1476 else
1477 {
1478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1479 psa_fallthrough = 1;
1480 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001481 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001482 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001483 psa_fallthrough = 1;
1484#else
1485 psa_fallthrough = 1;
1486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001487
Hanno Beckercb1cc802018-11-17 22:27:38 +00001488 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001489#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001490 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001491 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001494 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001495 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001496
Hanno Beckerf704bef2018-11-16 15:21:18 +00001497#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001498 /* Only use PSA-based ciphers for TLS-1.2.
1499 * That's relevant at least for TLS-1.0, where
1500 * we assume that mbedtls_cipher_crypt() updates
1501 * the structure field for the IV, which the PSA-based
1502 * implementation currently doesn't. */
1503#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1504 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001505 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001506 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001507 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001508 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1509 {
1510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001511 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001512 }
1513
1514 if( ret == 0 )
1515 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001517 psa_fallthrough = 0;
1518 }
1519 else
1520 {
1521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1522 psa_fallthrough = 1;
1523 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001524 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001525 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001526 psa_fallthrough = 1;
1527#else
1528 psa_fallthrough = 1;
1529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001530
Hanno Beckercb1cc802018-11-17 22:27:38 +00001531 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001532#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001533 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001534 cipher_info ) ) != 0 )
1535 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001537 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001541 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001545 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001546 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001549 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001553 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556#if defined(MBEDTLS_CIPHER_MODE_CBC)
1557 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1560 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001563 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001564 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1567 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001570 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001577 // Initialize compression
1578 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001580 {
Paul Bakker16770332013-10-11 09:59:44 +02001581 if( ssl->compress_buf == NULL )
1582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001584 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001585 if( ssl->compress_buf == NULL )
1586 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001588 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001589 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1590 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001591 }
1592 }
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001595
Paul Bakker48916f92012-09-16 19:57:18 +00001596 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1597 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001598
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001599 if( deflateInit( &transform->ctx_deflate,
1600 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001601 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001604 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1605 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606 }
1607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001611end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001612 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1613 mbedtls_platform_zeroize( handshake->randbytes,
1614 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616}
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#if defined(MBEDTLS_SSL_PROTO_SSL3)
1619void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001620{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001621 mbedtls_md5_context md5;
1622 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 unsigned char pad_1[48];
1624 unsigned char pad_2[48];
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001628 mbedtls_md5_init( &md5 );
1629 mbedtls_sha1_init( &sha1 );
1630
1631 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1632 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001633
Paul Bakker380da532012-04-18 16:10:25 +00001634 memset( pad_1, 0x36, 48 );
1635 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001637 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1638 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1639 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001641 mbedtls_md5_starts_ret( &md5 );
1642 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1643 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1644 mbedtls_md5_update_ret( &md5, hash, 16 );
1645 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001647 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1648 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001650
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001651 mbedtls_sha1_starts_ret( &sha1 );
1652 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1653 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1654 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1655 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001659
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001660 mbedtls_md5_free( &md5 );
1661 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001662
Paul Bakker380da532012-04-18 16:10:25 +00001663 return;
1664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1668void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001669{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001670 mbedtls_md5_context md5;
1671 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001674
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001675 mbedtls_md5_init( &md5 );
1676 mbedtls_sha1_init( &sha1 );
1677
1678 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1679 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Andrzej Kurekeb342242019-01-29 09:14:33 -05001681 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001682 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001686
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001687 mbedtls_md5_free( &md5 );
1688 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001689
Paul Bakker380da532012-04-18 16:10:25 +00001690 return;
1691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695#if defined(MBEDTLS_SHA256_C)
1696void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001697{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001698#if defined(MBEDTLS_USE_PSA_CRYPTO)
1699 size_t hash_size;
1700 psa_status_t status;
1701 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1702
1703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1704 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1705 if( status != PSA_SUCCESS )
1706 {
1707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1708 return;
1709 }
1710
1711 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1712 if( status != PSA_SUCCESS )
1713 {
1714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1715 return;
1716 }
1717 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1719#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001720 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001721
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001722 mbedtls_sha256_init( &sha256 );
1723
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001725
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001726 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001727 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001731
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001732 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001733#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001734 return;
1735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#if defined(MBEDTLS_SHA512_C)
1739void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001740{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001741#if defined(MBEDTLS_USE_PSA_CRYPTO)
1742 size_t hash_size;
1743 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001744 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001745
1746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001747 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001748 if( status != PSA_SUCCESS )
1749 {
1750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1751 return;
1752 }
1753
Andrzej Kurek972fba52019-01-30 03:29:12 -05001754 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001755 if( status != PSA_SUCCESS )
1756 {
1757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1758 return;
1759 }
1760 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1762#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001763 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001764
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001765 mbedtls_sha512_init( &sha512 );
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001768
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001769 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001770 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001775 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001776#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 return;
1778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779#endif /* MBEDTLS_SHA512_C */
1780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1783int 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 +02001784{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001785 unsigned char *p = ssl->handshake->premaster;
1786 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001787 const unsigned char *psk = ssl->conf->psk;
1788 size_t psk_len = ssl->conf->psk_len;
1789
1790 /* If the psk callback was called, use its result */
1791 if( ssl->handshake->psk != NULL )
1792 {
1793 psk = ssl->handshake->psk;
1794 psk_len = ssl->handshake->psk_len;
1795 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001796
1797 /*
1798 * PMS = struct {
1799 * opaque other_secret<0..2^16-1>;
1800 * opaque psk<0..2^16-1>;
1801 * };
1802 * with "other_secret" depending on the particular key exchange
1803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1805 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001806 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001807 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001809
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001810 *(p++) = (unsigned char)( psk_len >> 8 );
1811 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001812
1813 if( end < p || (size_t)( end - p ) < psk_len )
1814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1815
1816 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001817 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001818 }
1819 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1821#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1822 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001823 {
1824 /*
1825 * other_secret already set by the ClientKeyExchange message,
1826 * and is 48 bytes long
1827 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001828 if( end - p < 2 )
1829 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1830
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001831 *p++ = 0;
1832 *p++ = 48;
1833 p += 48;
1834 }
1835 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1837#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1838 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001839 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001840 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001841 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001842
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001843 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001845 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001846 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001849 return( ret );
1850 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001851 *(p++) = (unsigned char)( len >> 8 );
1852 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853 p += len;
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001856 }
1857 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1859#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1860 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001861 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001862 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001863 size_t zlen;
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001866 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001867 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 return( ret );
1871 }
1872
1873 *(p++) = (unsigned char)( zlen >> 8 );
1874 *(p++) = (unsigned char)( zlen );
1875 p += zlen;
1876
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001877 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1878 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 }
1880 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001885 }
1886
1887 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001888 if( end - p < 2 )
1889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001890
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001891 *(p++) = (unsigned char)( psk_len >> 8 );
1892 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001893
1894 if( end < p || (size_t)( end - p ) < psk_len )
1895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1896
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001897 memcpy( p, psk, psk_len );
1898 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001899
1900 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1901
1902 return( 0 );
1903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001907/*
1908 * SSLv3.0 MAC functions
1909 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001910#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001911static void ssl_mac( mbedtls_md_context_t *md_ctx,
1912 const unsigned char *secret,
1913 const unsigned char *buf, size_t len,
1914 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001915 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001916{
1917 unsigned char header[11];
1918 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001919 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1921 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001922
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001923 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001925 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001926 else
Paul Bakker68884e32013-01-07 18:20:04 +01001927 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
1929 memcpy( header, ctr, 8 );
1930 header[ 8] = (unsigned char) type;
1931 header[ 9] = (unsigned char)( len >> 8 );
1932 header[10] = (unsigned char)( len );
1933
Paul Bakker68884e32013-01-07 18:20:04 +01001934 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 mbedtls_md_starts( md_ctx );
1936 mbedtls_md_update( md_ctx, secret, md_size );
1937 mbedtls_md_update( md_ctx, padding, padlen );
1938 mbedtls_md_update( md_ctx, header, 11 );
1939 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001940 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Paul Bakker68884e32013-01-07 18:20:04 +01001942 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 mbedtls_md_starts( md_ctx );
1944 mbedtls_md_update( md_ctx, secret, md_size );
1945 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001946 mbedtls_md_update( md_ctx, out, md_size );
1947 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001950
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001951/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001952 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001953#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001954 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1955 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1956 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1957/* This function makes sure every byte in the memory region is accessed
1958 * (in ascending addresses order) */
1959static void ssl_read_memory( unsigned char *p, size_t len )
1960{
1961 unsigned char acc = 0;
1962 volatile unsigned char force;
1963
1964 for( ; len != 0; p++, len-- )
1965 acc ^= *p;
1966
1967 force = acc;
1968 (void) force;
1969}
1970#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1971
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001972/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001974 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001975
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001976#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001977/* This functions transforms a DTLS plaintext fragment and a record content
1978 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001979 *
1980 * struct {
1981 * opaque content[DTLSPlaintext.length];
1982 * ContentType real_type;
1983 * uint8 zeros[length_of_padding];
1984 * } DTLSInnerPlaintext;
1985 *
1986 * Input:
1987 * - `content`: The beginning of the buffer holding the
1988 * plaintext to be wrapped.
1989 * - `*content_size`: The length of the plaintext in Bytes.
1990 * - `max_len`: The number of Bytes available starting from
1991 * `content`. This must be `>= *content_size`.
1992 * - `rec_type`: The desired record content type.
1993 *
1994 * Output:
1995 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1996 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1997 *
1998 * Returns:
1999 * - `0` on success.
2000 * - A negative error code if `max_len` didn't offer enough space
2001 * for the expansion.
2002 */
2003static int ssl_cid_build_inner_plaintext( unsigned char *content,
2004 size_t *content_size,
2005 size_t remaining,
2006 uint8_t rec_type )
2007{
2008 size_t len = *content_size;
2009 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2010
2011 /* Write real content type */
2012 if( remaining == 0 )
2013 return( -1 );
2014 content[ len ] = rec_type;
2015 len++;
2016 remaining--;
2017
2018 if( remaining < pad )
2019 return( -1 );
2020 memset( content + len, 0, pad );
2021 len += pad;
2022 remaining -= pad;
2023
2024 *content_size = len;
2025 return( 0 );
2026}
2027
Hanno Becker07dc97d2019-05-20 15:08:01 +01002028/* This function parses a DTLSInnerPlaintext structure.
2029 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002030static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2031 size_t *content_size,
2032 uint8_t *rec_type )
2033{
2034 size_t remaining = *content_size;
2035
2036 /* Determine length of padding by skipping zeroes from the back. */
2037 do
2038 {
2039 if( remaining == 0 )
2040 return( -1 );
2041 remaining--;
2042 } while( content[ remaining ] == 0 );
2043
2044 *content_size = remaining;
2045 *rec_type = content[ remaining ];
2046
2047 return( 0 );
2048}
2049#endif /* MBEDTLS_SSL_CID */
2050
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002051/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002052 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002053static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002054 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002055 mbedtls_record *rec )
2056{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002057 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002058 *
2059 * additional_data = seq_num + TLSCompressed.type +
2060 * TLSCompressed.version + TLSCompressed.length;
2061 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002062 * For the CID extension, this is extended as follows
2063 * (quoting draft-ietf-tls-dtls-connection-id-05,
2064 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002065 *
2066 * additional_data = seq_num + DTLSPlaintext.type +
2067 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002068 * cid +
2069 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002070 * length_of_DTLSInnerPlaintext;
2071 */
2072
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002073 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2074 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002075 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002076
2077#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002078 if( rec->cid_len != 0 )
2079 {
2080 memcpy( add_data + 11, rec->cid, rec->cid_len );
2081 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2082 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2083 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2084 *add_data_len = 13 + 1 + rec->cid_len;
2085 }
2086 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002087#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002088 {
2089 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2090 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2091 *add_data_len = 13;
2092 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002093}
2094
Hanno Beckera18d1322018-01-03 14:27:32 +00002095int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2096 mbedtls_ssl_transform *transform,
2097 mbedtls_record *rec,
2098 int (*f_rng)(void *, unsigned char *, size_t),
2099 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002102 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002103 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002104 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002105 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002106 size_t post_avail;
2107
2108 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002109#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002110 ((void) ssl);
2111#endif
2112
2113 /* The PRNG is used for dynamic IV generation that's used
2114 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2115#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2116 ( defined(MBEDTLS_AES_C) || \
2117 defined(MBEDTLS_ARIA_C) || \
2118 defined(MBEDTLS_CAMELLIA_C) ) && \
2119 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2120 ((void) f_rng);
2121 ((void) p_rng);
2122#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002126 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002127 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2130 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002131 if( rec == NULL
2132 || rec->buf == NULL
2133 || rec->buf_len < rec->data_offset
2134 || rec->buf_len - rec->data_offset < rec->data_len
2135#if defined(MBEDTLS_SSL_CID)
2136 || rec->cid_len != 0
2137#endif
2138 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139 {
2140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002142 }
2143
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002144 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002145 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002147 data, rec->data_len );
2148
2149 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2150
2151 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2152 {
2153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2154 (unsigned) rec->data_len,
2155 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2157 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002158
Hanno Beckercab87e62019-04-29 13:52:53 +01002159#if defined(MBEDTLS_SSL_CID)
2160 /*
2161 * Add CID information
2162 */
2163 rec->cid_len = transform->out_cid_len;
2164 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2165 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002166
2167 if( rec->cid_len != 0 )
2168 {
2169 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002170 * Wrap plaintext into DTLSInnerPlaintext structure.
2171 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002172 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002173 * Note that this changes `rec->data_len`, and hence
2174 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002175 */
2176 if( ssl_cid_build_inner_plaintext( data,
2177 &rec->data_len,
2178 post_avail,
2179 rec->type ) != 0 )
2180 {
2181 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2182 }
2183
2184 rec->type = MBEDTLS_SSL_MSG_CID;
2185 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002186#endif /* MBEDTLS_SSL_CID */
2187
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002188 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2189
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002191 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 */
Hanno Becker52344c22018-01-03 15:24:20 +00002193#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 if( mode == MBEDTLS_MODE_STREAM ||
2195 ( mode == MBEDTLS_MODE_CBC
2196#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002197 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002198#endif
2199 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002201 if( post_avail < transform->maclen )
2202 {
2203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2204 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2205 }
2206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002208 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002209 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002210 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002211 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2212 data, rec->data_len, rec->ctr, rec->type, mac );
2213 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002214 }
2215 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2218 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002220 {
Hanno Becker992b6872017-11-09 18:57:39 +00002221 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2222
Hanno Beckercab87e62019-04-29 13:52:53 +01002223 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002224
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002225 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002226 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2228 data, rec->data_len );
2229 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2230 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2231
2232 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 }
2234 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002235#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 }
2240
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002241 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2242 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002243
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 rec->data_len += transform->maclen;
2245 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002246 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002247 }
Hanno Becker52344c22018-01-03 15:24:20 +00002248#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002250 /*
2251 * Encrypt
2252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2254 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002256 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002259 "including %d bytes of padding",
2260 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2263 transform->iv_enc, transform->ivlen,
2264 data, rec->data_len,
2265 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002268 return( ret );
2269 }
2270
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002271 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
Paul Bakker68884e32013-01-07 18:20:04 +01002277 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002279
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002280#if defined(MBEDTLS_GCM_C) || \
2281 defined(MBEDTLS_CCM_C) || \
2282 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002284 mode == MBEDTLS_MODE_CCM ||
2285 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002286 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002287 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002288 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002290
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 /* Check that there's space for both the authentication tag
2292 * and the explicit IV before and after the record content. */
2293 if( post_avail < transform->taglen ||
2294 rec->data_offset < explicit_iv_len )
2295 {
2296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2297 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2298 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002299
Paul Bakker68884e32013-01-07 18:20:04 +01002300 /*
2301 * Generate IV
2302 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002303 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2304 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002305 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002306 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002307 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2308 explicit_iv_len );
2309 /* Prefix record content with explicit IV. */
2310 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002311 }
2312 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2313 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002314 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002315 unsigned char i;
2316
2317 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2318
2319 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002320 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002321 }
2322 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002323 {
2324 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002327 }
2328
Hanno Beckercab87e62019-04-29 13:52:53 +01002329 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002330
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2332 iv, transform->ivlen );
2333 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002334 data - explicit_iv_len, explicit_iv_len );
2335 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002336 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002340
Paul Bakker68884e32013-01-07 18:20:04 +01002341 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002342 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002343 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002344
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002345 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002346 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002347 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348 data, rec->data_len, /* source */
2349 data, &rec->data_len, /* destination */
2350 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002353 return( ret );
2354 }
2355
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002356 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2357 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002358
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 rec->data_len += transform->taglen + explicit_iv_len;
2360 rec->data_offset -= explicit_iv_len;
2361 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002362 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2366#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002367 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002370 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 size_t padlen, i;
2372 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002373
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002374 /* Currently we're always using minimal padding
2375 * (up to 255 bytes would be allowed). */
2376 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2377 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 padlen = 0;
2379
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 /* Check there's enough space in the buffer for the padding. */
2381 if( post_avail < padlen + 1 )
2382 {
2383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2384 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2385 }
2386
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002390 rec->data_len += padlen + 1;
2391 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002394 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002395 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2396 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002397 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002399 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002400 if( f_rng == NULL )
2401 {
2402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2404 }
2405
2406 if( rec->data_offset < transform->ivlen )
2407 {
2408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2409 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2410 }
2411
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002412 /*
2413 * Generate IV
2414 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002415 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002416 if( ret != 0 )
2417 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 memcpy( data - transform->ivlen, transform->iv_enc,
2420 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002421
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002427 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002428 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2431 transform->iv_enc,
2432 transform->ivlen,
2433 data, rec->data_len,
2434 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002437 return( ret );
2438 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002439
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002440 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002444 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002448 {
2449 /*
2450 * Save IV in SSL3 and TLS1
2451 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2453 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002455 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002456#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002457 {
2458 data -= transform->ivlen;
2459 rec->data_offset -= transform->ivlen;
2460 rec->data_len += transform->ivlen;
2461 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002464 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002465 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002466 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2467
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002468 /*
2469 * MAC(MAC_write_key, seq_num +
2470 * TLSCipherText.type +
2471 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002472 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473 * IV + // except for TLS 1.0
2474 * ENC(content + padding + padding_length));
2475 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476
2477 if( post_avail < transform->maclen)
2478 {
2479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2480 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2481 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002482
Hanno Beckercab87e62019-04-29 13:52:53 +01002483 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002487 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002488
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002490 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002491 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2492 data, rec->data_len );
2493 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2494 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002495
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002498 rec->data_len += transform->maclen;
2499 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002500 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002506 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002510 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002512 /* Make extra sure authentication was performed, exactly once */
2513 if( auth_done != 1 )
2514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002517 }
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
2521 return( 0 );
2522}
2523
Hanno Beckera18d1322018-01-03 14:27:32 +00002524int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2525 mbedtls_ssl_transform *transform,
2526 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002527{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002528 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002530 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002531#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002532 size_t padlen = 0, correct = 1;
2533#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002534 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002535 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002536 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002537
Hanno Beckera18d1322018-01-03 14:27:32 +00002538#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002539 ((void) ssl);
2540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002543 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2547 }
2548 if( rec == NULL ||
2549 rec->buf == NULL ||
2550 rec->buf_len < rec->data_offset ||
2551 rec->buf_len - rec->data_offset < rec->data_len )
2552 {
2553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002555 }
2556
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002557 data = rec->buf + rec->data_offset;
2558 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Hanno Beckercab87e62019-04-29 13:52:53 +01002560#if defined(MBEDTLS_SSL_CID)
2561 /*
2562 * Match record's CID with incoming CID.
2563 */
2564
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002565 /* Uncomment this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002566 /* if( rec->cid_len != transform->in_cid_len || */
2567 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2568 /* { */
2569 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2570 /* } */
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002571
2572 /* Remove this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002573 rec->cid_len = transform->in_cid_len;
2574 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2575 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2576#endif /* MBEDTLS_SSL_CID */
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2579 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002580 {
2581 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002582 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2583 transform->iv_dec,
2584 transform->ivlen,
2585 data, rec->data_len,
2586 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002589 return( ret );
2590 }
2591
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002592 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 }
Paul Bakker68884e32013-01-07 18:20:04 +01002598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002600#if defined(MBEDTLS_GCM_C) || \
2601 defined(MBEDTLS_CCM_C) || \
2602 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604 mode == MBEDTLS_MODE_CCM ||
2605 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002606 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002607 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002608 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002609
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002610 /*
2611 * Compute and update sizes
2612 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002613 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002616 "+ taglen (%d)", rec->data_len,
2617 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002619 }
Paul Bakker68884e32013-01-07 18:20:04 +01002620
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 /*
2622 * Prepare IV
2623 */
2624 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2625 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002626 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002627 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002628 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002629
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002630 }
2631 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2632 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002633 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002634 unsigned char i;
2635
2636 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2637
2638 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002639 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002640 }
2641 else
2642 {
2643 /* Reminder if we ever add an AEAD mode with a different size */
2644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2645 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2646 }
2647
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002648 data += explicit_iv_len;
2649 rec->data_offset += explicit_iv_len;
2650 rec->data_len -= explicit_iv_len + transform->taglen;
2651
Hanno Beckercab87e62019-04-29 13:52:53 +01002652 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002653 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002654 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002655
2656 memcpy( transform->iv_dec + transform->fixed_ivlen,
2657 data - explicit_iv_len, explicit_iv_len );
2658
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002659 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002661 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002662
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002663
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002664 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002665 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002666 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002667 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2668 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002669 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002670 data, rec->data_len,
2671 data, &olen,
2672 data + rec->data_len,
2673 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2678 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002679
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002680 return( ret );
2681 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002682 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002683
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002684 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2687 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002688 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2692#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002693 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002696 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002697
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 /*
Paul Bakker45829992013-01-03 14:52:21 +01002699 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002702 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2703 {
2704 /* The ciphertext is prefixed with the CBC IV. */
2705 minlen += transform->ivlen;
2706 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002707#endif
Paul Bakker45829992013-01-03 14:52:21 +01002708
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002709 /* Size considerations:
2710 *
2711 * - The CBC cipher text must not be empty and hence
2712 * at least of size transform->ivlen.
2713 *
2714 * Together with the potential IV-prefix, this explains
2715 * the first of the two checks below.
2716 *
2717 * - The record must contain a MAC, either in plain or
2718 * encrypted, depending on whether Encrypt-then-MAC
2719 * is used or not.
2720 * - If it is, the message contains the IV-prefix,
2721 * the CBC ciphertext, and the MAC.
2722 * - If it is not, the padded plaintext, and hence
2723 * the CBC ciphertext, has at least length maclen + 1
2724 * because there is at least the padding length byte.
2725 *
2726 * As the CBC ciphertext is not empty, both cases give the
2727 * lower bound minlen + maclen + 1 on the record size, which
2728 * we test for in the second check below.
2729 */
2730 if( rec->data_len < minlen + transform->ivlen ||
2731 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 "+ 1 ) ( + expl IV )", rec->data_len,
2735 transform->ivlen,
2736 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002738 }
2739
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002740 /*
2741 * Authenticate before decrypt if enabled
2742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002744 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002745 {
Hanno Becker992b6872017-11-09 18:57:39 +00002746 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002749
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002750 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2751 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002752
Hanno Beckercab87e62019-04-29 13:52:53 +01002753 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002754
Hanno Beckercab87e62019-04-29 13:52:53 +01002755 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2756 add_data_len );
2757 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2758 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2760 data, rec->data_len );
2761 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2762 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002763
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002764 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2765 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002766 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2770 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002774 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002775 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778
2779 /*
2780 * Check length sanity
2781 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787 }
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002790 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002791 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002794 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002795 /* This is safe because data_len >= minlen + maclen + 1 initially,
2796 * and at this point we have at most subtracted maclen (note that
2797 * minlen == transform->ivlen here). */
2798 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 data += transform->ivlen;
2801 rec->data_offset += transform->ivlen;
2802 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2807 transform->iv_dec, transform->ivlen,
2808 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002811 return( ret );
2812 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002813
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002814 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2817 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002818 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002822 {
2823 /*
2824 * Save IV in SSL3 and TLS1
2825 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2827 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002829#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 /* Safe since data_len >= minlen + maclen + 1, so after having
2832 * subtracted at most minlen and maclen up to this point,
2833 * data_len > 0. */
2834 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002835
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002836 if( auth_done == 1 )
2837 {
2838 correct *= ( rec->data_len >= padlen + 1 );
2839 padlen *= ( rec->data_len >= padlen + 1 );
2840 }
2841 else
Paul Bakker45829992013-01-03 14:52:21 +01002842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002844 if( rec->data_len < transform->maclen + padlen + 1 )
2845 {
2846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2847 rec->data_len,
2848 transform->maclen,
2849 padlen + 1 ) );
2850 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002851#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002852
2853 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2854 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857 padlen++;
2858
2859 /* Regardless of the validity of the padding,
2860 * we have data_len >= padlen here. */
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002865 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_DEBUG_ALL)
2868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 "should be no more than %d",
2870 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002871#endif
Paul Bakker45829992013-01-03 14:52:21 +01002872 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 }
2874 }
2875 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2877#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2878 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002879 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 /* The padding check involves a series of up to 256
2882 * consecutive memory reads at the end of the record
2883 * plaintext buffer. In order to hide the length and
2884 * validity of the padding, always perform exactly
2885 * `min(256,plaintext_len)` reads (but take into account
2886 * only the last `padlen` bytes for the padding check). */
2887 size_t pad_count = 0;
2888 size_t real_count = 0;
2889 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002890
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002891 /* Index of first padding byte; it has been ensured above
2892 * that the subtraction is safe. */
2893 size_t const padding_idx = rec->data_len - padlen;
2894 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2895 size_t const start_idx = rec->data_len - num_checks;
2896 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002897
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002898 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002899 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002900 real_count |= ( idx >= padding_idx );
2901 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002902 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002906 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002908#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002909 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2913 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002917 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002918
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002919 /* If the padding was found to be invalid, padlen == 0
2920 * and the subtraction is safe. If the padding was found valid,
2921 * padlen hasn't been changed and the previous assertion
2922 * data_len >= padlen still holds. */
2923 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002925 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002927 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002931 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002933#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002935 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002936#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
2938 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002939 * Authenticate if not done yet.
2940 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 */
Hanno Becker52344c22018-01-03 15:24:20 +00002942#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002943 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002944 {
Hanno Becker992b6872017-11-09 18:57:39 +00002945 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002946
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002947 /* If the initial value of padlen was such that
2948 * data_len < maclen + padlen + 1, then padlen
2949 * got reset to 1, and the initial check
2950 * data_len >= minlen + maclen + 1
2951 * guarantees that at this point we still
2952 * have at least data_len >= maclen.
2953 *
2954 * If the initial value of padlen was such that
2955 * data_len >= maclen + padlen + 1, then we have
2956 * subtracted either padlen + 1 (if the padding was correct)
2957 * or 0 (if the padding was incorrect) since then,
2958 * hence data_len >= maclen in any case.
2959 */
2960 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Hanno Beckercab87e62019-04-29 13:52:53 +01002962 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002965 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002966 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 ssl_mac( &transform->md_ctx_dec,
2968 transform->mac_dec,
2969 data, rec->data_len,
2970 rec->ctr, rec->type,
2971 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002972 }
2973 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2976 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002978 {
2979 /*
2980 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002981 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002982 *
2983 * Known timing attacks:
2984 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2985 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002986 * To compensate for different timings for the MAC calculation
2987 * depending on how much padding was removed (which is determined
2988 * by padlen), process extra_run more blocks through the hash
2989 * function.
2990 *
2991 * The formula in the paper is
2992 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2993 * where L1 is the size of the header plus the decrypted message
2994 * plus CBC padding and L2 is the size of the header plus the
2995 * decrypted message. This is for an underlying hash function
2996 * with 64-byte blocks.
2997 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2998 * correctly. We round down instead of up, so -56 is the correct
2999 * value for our calculations instead of -55.
3000 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003001 * Repeat the formula rather than defining a block_size variable.
3002 * This avoids requiring division by a variable at runtime
3003 * (which would be marginally less efficient and would require
3004 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003005 */
3006 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003007 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003008
3009 /*
3010 * The next two sizes are the minimum and maximum values of
3011 * in_msglen over all padlen values.
3012 *
3013 * They're independent of padlen, since we previously did
3014 * in_msglen -= padlen.
3015 *
3016 * Note that max_len + maclen is never more than the buffer
3017 * length, as we previously did in_msglen -= maclen too.
3018 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003019 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003020 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3021
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003022 memset( tmp, 0, sizeof( tmp ) );
3023
3024 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003025 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003026#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3027 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003028 case MBEDTLS_MD_MD5:
3029 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003030 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003031 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003032 extra_run =
3033 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3034 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003035 break;
3036#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003037#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003038 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003039 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003040 extra_run =
3041 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3042 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 break;
3044#endif
3045 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3048 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003049
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003050 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003051
Hanno Beckercab87e62019-04-29 13:52:53 +01003052 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3053 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003054 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3055 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003056 /* Make sure we access everything even when padlen > 0. This
3057 * makes the synchronisation requirements for just-in-time
3058 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 ssl_read_memory( data + rec->data_len, padlen );
3060 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003061
3062 /* Call mbedtls_md_process at least once due to cache attacks
3063 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003064 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003066
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003067 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003068
3069 /* Make sure we access all the memory that could contain the MAC,
3070 * before we check it in the next code block. This makes the
3071 * synchronisation requirements for just-in-time Prime+Probe
3072 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003073 ssl_read_memory( data + min_len,
3074 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003075 }
3076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3078 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003082 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003084#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003085 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3086 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003089 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3090 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#if defined(MBEDTLS_SSL_DEBUG_ALL)
3093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003094#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003095 correct = 0;
3096 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003097 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003098 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003099
3100 /*
3101 * Finally check the correct flag
3102 */
3103 if( correct == 0 )
3104 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003105#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003106
3107 /* Make extra sure authentication was performed, exactly once */
3108 if( auth_done != 1 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003114#if defined(MBEDTLS_SSL_CID)
3115 if( rec->cid_len != 0 )
3116 {
3117 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3118 &rec->type );
3119 if( ret != 0 )
3120 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3121 }
3122#endif /* MBEDTLS_SSL_CID */
3123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
3126 return( 0 );
3127}
3128
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003129#undef MAC_NONE
3130#undef MAC_PLAINTEXT
3131#undef MAC_CIPHERTEXT
3132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003134/*
3135 * Compression/decompression functions
3136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003138{
3139 int ret;
3140 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003141 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003142 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003143 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003146
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003147 if( len_pre == 0 )
3148 return( 0 );
3149
Paul Bakker2770fbd2012-07-03 13:30:23 +00003150 memcpy( msg_pre, ssl->out_msg, len_pre );
3151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003153 ssl->out_msglen ) );
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003156 ssl->out_msg, ssl->out_msglen );
3157
Paul Bakker48916f92012-09-16 19:57:18 +00003158 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3159 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3160 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003161 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162
Paul Bakker48916f92012-09-16 19:57:18 +00003163 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003164 if( ret != Z_OK )
3165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3167 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168 }
3169
Angus Grattond8213d02016-05-25 20:56:48 +10003170 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003171 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174 ssl->out_msglen ) );
3175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003177 ssl->out_msg, ssl->out_msglen );
3178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003180
3181 return( 0 );
3182}
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185{
3186 int ret;
3187 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003188 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003190 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003194 if( len_pre == 0 )
3195 return( 0 );
3196
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197 memcpy( msg_pre, ssl->in_msg, len_pre );
3198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003200 ssl->in_msglen ) );
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003203 ssl->in_msg, ssl->in_msglen );
3204
Paul Bakker48916f92012-09-16 19:57:18 +00003205 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3206 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3207 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003208 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003209 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210
Paul Bakker48916f92012-09-16 19:57:18 +00003211 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003212 if( ret != Z_OK )
3213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3215 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216 }
3217
Angus Grattond8213d02016-05-25 20:56:48 +10003218 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003219 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222 ssl->in_msglen ) );
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225 ssl->in_msg, ssl->in_msglen );
3226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228
3229 return( 0 );
3230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3234static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_SSL_PROTO_DTLS)
3237static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003238{
3239 /* If renegotiation is not enforced, retransmit until we would reach max
3240 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003241 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003242 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003243 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003244 unsigned char doublings = 1;
3245
3246 while( ratio != 0 )
3247 {
3248 ++doublings;
3249 ratio >>= 1;
3250 }
3251
3252 if( ++ssl->renego_records_seen > doublings )
3253 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003255 return( 0 );
3256 }
3257 }
3258
3259 return( ssl_write_hello_request( ssl ) );
3260}
3261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003263
Paul Bakker5121ce52009-01-03 21:22:43 +00003264/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003265 * Fill the input message buffer by appending data to it.
3266 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003267 *
3268 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3269 * available (from this read and/or a previous one). Otherwise, an error code
3270 * is returned (possibly EOF or WANT_READ).
3271 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003272 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3273 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3274 * since we always read a whole datagram at once.
3275 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003276 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003277 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003280{
Paul Bakker23986e52011-04-24 08:57:21 +00003281 int ret;
3282 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003286 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003289 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003291 }
3292
Angus Grattond8213d02016-05-25 20:56:48 +10003293 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003297 }
3298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003300 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003302 uint32_t timeout;
3303
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003304 /* Just to be sure */
3305 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3306 {
3307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3308 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3310 }
3311
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003312 /*
3313 * The point is, we need to always read a full datagram at once, so we
3314 * sometimes read more then requested, and handle the additional data.
3315 * It could be the rest of the current record (while fetching the
3316 * header) and/or some other records in the same datagram.
3317 */
3318
3319 /*
3320 * Move to the next record in the already read datagram if applicable
3321 */
3322 if( ssl->next_record_offset != 0 )
3323 {
3324 if( ssl->in_left < ssl->next_record_offset )
3325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003328 }
3329
3330 ssl->in_left -= ssl->next_record_offset;
3331
3332 if( ssl->in_left != 0 )
3333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003335 ssl->next_record_offset ) );
3336 memmove( ssl->in_hdr,
3337 ssl->in_hdr + ssl->next_record_offset,
3338 ssl->in_left );
3339 }
3340
3341 ssl->next_record_offset = 0;
3342 }
3343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003346
3347 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003348 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003349 */
3350 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003353 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003354 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003355
3356 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003357 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003358 * are not at the beginning of a new record, the caller did something
3359 * wrong.
3360 */
3361 if( ssl->in_left != 0 )
3362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003365 }
3366
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003367 /*
3368 * Don't even try to read if time's out already.
3369 * This avoids by-passing the timer when repeatedly receiving messages
3370 * that will end up being dropped.
3371 */
3372 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003373 {
3374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003375 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003376 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003377 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003378 {
Angus Grattond8213d02016-05-25 20:56:48 +10003379 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003382 timeout = ssl->handshake->retransmit_timeout;
3383 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003384 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003387
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003388 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003389 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3390 timeout );
3391 else
3392 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003395
3396 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398 }
3399
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003400 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003403 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003406 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003407 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003410 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003411 }
3412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003416 return( ret );
3417 }
3418
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003419 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003420 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003422 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003424 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003425 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003428 return( ret );
3429 }
3430
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003431 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003434 }
3435
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 if( ret < 0 )
3437 return( ret );
3438
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003439 ssl->in_left = ret;
3440 }
3441 else
3442#endif
3443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003445 ssl->in_left, nb_want ) );
3446
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003447 while( ssl->in_left < nb_want )
3448 {
3449 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003450
3451 if( ssl_check_timer( ssl ) != 0 )
3452 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3453 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003454 {
3455 if( ssl->f_recv_timeout != NULL )
3456 {
3457 ret = ssl->f_recv_timeout( ssl->p_bio,
3458 ssl->in_hdr + ssl->in_left, len,
3459 ssl->conf->read_timeout );
3460 }
3461 else
3462 {
3463 ret = ssl->f_recv( ssl->p_bio,
3464 ssl->in_hdr + ssl->in_left, len );
3465 }
3466 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003469 ssl->in_left, nb_want ) );
3470 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003471
3472 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003474
3475 if( ret < 0 )
3476 return( ret );
3477
mohammad160352aecb92018-03-28 23:41:40 -07003478 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003479 {
Darryl Green11999bb2018-03-13 15:22:58 +00003480 MBEDTLS_SSL_DEBUG_MSG( 1,
3481 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003482 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3484 }
3485
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003486 ssl->in_left += ret;
3487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 }
3489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491
3492 return( 0 );
3493}
3494
3495/*
3496 * Flush any data not yet written
3497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003499{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003500 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003501 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003505 if( ssl->f_send == NULL )
3506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003508 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003510 }
3511
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003512 /* Avoid incrementing counter if data is flushed */
3513 if( ssl->out_left == 0 )
3514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003516 return( 0 );
3517 }
3518
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 while( ssl->out_left > 0 )
3520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3522 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Hanno Becker2b1e3542018-08-06 11:19:13 +01003524 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003525 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
3529 if( ret <= 0 )
3530 return( ret );
3531
mohammad160352aecb92018-03-28 23:41:40 -07003532 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003533 {
Darryl Green11999bb2018-03-13 15:22:58 +00003534 MBEDTLS_SSL_DEBUG_MSG( 1,
3535 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003536 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3538 }
3539
Paul Bakker5121ce52009-01-03 21:22:43 +00003540 ssl->out_left -= ret;
3541 }
3542
Hanno Becker2b1e3542018-08-06 11:19:13 +01003543#if defined(MBEDTLS_SSL_PROTO_DTLS)
3544 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003545 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003546 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003547 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003548 else
3549#endif
3550 {
3551 ssl->out_hdr = ssl->out_buf + 8;
3552 }
3553 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
3557 return( 0 );
3558}
3559
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003560/*
3561 * Functions to handle the DTLS retransmission state machine
3562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003564/*
3565 * Append current handshake message to current outgoing flight
3566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3571 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3572 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573
3574 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003575 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003576 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003579 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 }
3581
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003582 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003583 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003586 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 }
3588
3589 /* Copy current handshake message with headers */
3590 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3591 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003592 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003593 msg->next = NULL;
3594
3595 /* Append to the current flight */
3596 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003597 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598 else
3599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003601 while( cur->next != NULL )
3602 cur = cur->next;
3603 cur->next = msg;
3604 }
3605
Hanno Becker3b235902018-08-06 09:54:53 +01003606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 return( 0 );
3608}
3609
3610/*
3611 * Free the current flight of handshake messages
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 mbedtls_ssl_flight_item *cur = flight;
3616 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003617
3618 while( cur != NULL )
3619 {
3620 next = cur->next;
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 mbedtls_free( cur->p );
3623 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003624
3625 cur = next;
3626 }
3627}
3628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3630static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003631#endif
3632
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003633/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003634 * Swap transform_out and out_ctr with the alternative ones
3635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003639 unsigned char tmp_out_ctr[8];
3640
3641 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003644 return;
3645 }
3646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003648
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003649 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650 tmp_transform = ssl->transform_out;
3651 ssl->transform_out = ssl->handshake->alt_transform_out;
3652 ssl->handshake->alt_transform_out = tmp_transform;
3653
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003654 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003655 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3656 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003657 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003658
3659 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003660 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3663 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3668 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003669 }
3670 }
3671#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003672}
3673
3674/*
3675 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003676 */
3677int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3678{
3679 int ret = 0;
3680
3681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3682
3683 ret = mbedtls_ssl_flight_transmit( ssl );
3684
3685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3686
3687 return( ret );
3688}
3689
3690/*
3691 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 *
3693 * Need to remember the current message in case flush_output returns
3694 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003695 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003697int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003699 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003703 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003705
3706 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003707 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003708 ssl_swap_epochs( ssl );
3709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003711 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712
3713 while( ssl->handshake->cur_msg != NULL )
3714 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003715 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003716 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003717
Hanno Beckere1dcb032018-08-17 16:47:58 +01003718 int const is_finished =
3719 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3720 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3721
Hanno Becker04da1892018-08-14 13:22:10 +01003722 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3723 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3724
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003725 /* Swap epochs before sending Finished: we can't do it after
3726 * sending ChangeCipherSpec, in case write returns WANT_READ.
3727 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003728 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003729 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003731 ssl_swap_epochs( ssl );
3732 }
3733
Hanno Becker67bc7c32018-08-06 11:33:50 +01003734 ret = ssl_get_remaining_payload_in_datagram( ssl );
3735 if( ret < 0 )
3736 return( ret );
3737 max_frag_len = (size_t) ret;
3738
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003739 /* CCS is copied as is, while HS messages may need fragmentation */
3740 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3741 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003742 if( max_frag_len == 0 )
3743 {
3744 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3745 return( ret );
3746
3747 continue;
3748 }
3749
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003750 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003752 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003753
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003754 /* Update position inside current message */
3755 ssl->handshake->cur_msg_p += cur->len;
3756 }
3757 else
3758 {
3759 const unsigned char * const p = ssl->handshake->cur_msg_p;
3760 const size_t hs_len = cur->len - 12;
3761 const size_t frag_off = p - ( cur->p + 12 );
3762 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003763 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764
Hanno Beckere1dcb032018-08-17 16:47:58 +01003765 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003766 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003767 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003768 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003769
Hanno Becker67bc7c32018-08-06 11:33:50 +01003770 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3771 return( ret );
3772
3773 continue;
3774 }
3775 max_hs_frag_len = max_frag_len - 12;
3776
3777 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3778 max_hs_frag_len : rem_len;
3779
3780 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003781 {
3782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003783 (unsigned) cur_hs_frag_len,
3784 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003785 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003786
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003787 /* Messages are stored with handshake headers as if not fragmented,
3788 * copy beginning of headers then fill fragmentation fields.
3789 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3790 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003791
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003792 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3793 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3794 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3795
Hanno Becker67bc7c32018-08-06 11:33:50 +01003796 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3797 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3798 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003799
3800 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3801
Hanno Becker3f7b9732018-08-28 09:53:25 +01003802 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003803 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3804 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003805 ssl->out_msgtype = cur->type;
3806
3807 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003809 }
3810
3811 /* If done with the current message move to the next one if any */
3812 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3813 {
3814 if( cur->next != NULL )
3815 {
3816 ssl->handshake->cur_msg = cur->next;
3817 ssl->handshake->cur_msg_p = cur->next->p + 12;
3818 }
3819 else
3820 {
3821 ssl->handshake->cur_msg = NULL;
3822 ssl->handshake->cur_msg_p = NULL;
3823 }
3824 }
3825
3826 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003827 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003830 return( ret );
3831 }
3832 }
3833
Hanno Becker67bc7c32018-08-06 11:33:50 +01003834 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3835 return( ret );
3836
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003837 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3839 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003840 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003843 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3844 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003845
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003847
3848 return( 0 );
3849}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003850
3851/*
3852 * To be called when the last message of an incoming flight is received.
3853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003855{
3856 /* We won't need to resend that one any more */
3857 ssl_flight_free( ssl->handshake->flight );
3858 ssl->handshake->flight = NULL;
3859 ssl->handshake->cur_msg = NULL;
3860
3861 /* The next incoming flight will start with this msg_seq */
3862 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3863
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003864 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003865 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003866
Hanno Becker0271f962018-08-16 13:23:47 +01003867 /* Clear future message buffering structure. */
3868 ssl_buffering_free( ssl );
3869
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003870 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003871 ssl_set_timer( ssl, 0 );
3872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3874 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003877 }
3878 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003880}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003881
3882/*
3883 * To be called when the last message of an outgoing flight is send.
3884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003886{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003887 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003888 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3891 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003894 }
3895 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003899
Paul Bakker5121ce52009-01-03 21:22:43 +00003900/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003901 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003903
3904/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003905 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003906 *
3907 * - fill in handshake headers
3908 * - update handshake checksum
3909 * - DTLS: save message for resending
3910 * - then pass to the record layer
3911 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003912 * DTLS: except for HelloRequest, messages are only queued, and will only be
3913 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003914 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003915 * Inputs:
3916 * - ssl->out_msglen: 4 + actual handshake message len
3917 * (4 is the size of handshake headers for TLS)
3918 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3919 * - ssl->out_msg + 4: the handshake message body
3920 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003921 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003922 * - ssl->out_msglen: the length of the record contents
3923 * (including handshake headers but excluding record headers)
3924 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003925 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003927{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003928 int ret;
3929 const size_t hs_len = ssl->out_msglen - 4;
3930 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003931
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3933
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003934 /*
3935 * Sanity checks
3936 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003937 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003938 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3939 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003940 /* In SSLv3, the client might send a NoCertificate alert. */
3941#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3942 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3943 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3944 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3945#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3946 {
3947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3948 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3949 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003952 /* Whenever we send anything different from a
3953 * HelloRequest we should be in a handshake - double check. */
3954 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3955 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003956 ssl->handshake == NULL )
3957 {
3958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003964 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003966 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003971
Hanno Beckerb50a2532018-08-06 11:52:54 +01003972 /* Double-check that we did not exceed the bounds
3973 * of the outgoing record buffer.
3974 * This should never fail as the various message
3975 * writing functions must obey the bounds of the
3976 * outgoing record buffer, but better be safe.
3977 *
3978 * Note: We deliberately do not check for the MTU or MFL here.
3979 */
3980 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3981 {
3982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3983 "size %u, maximum %u",
3984 (unsigned) ssl->out_msglen,
3985 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3987 }
3988
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003989 /*
3990 * Fill handshake headers
3991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003993 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003994 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3995 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3996 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003997
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003998 /*
3999 * DTLS has additional fields in the Handshake layer,
4000 * between the length field and the actual payload:
4001 * uint16 message_seq;
4002 * uint24 fragment_offset;
4003 * uint24 fragment_length;
4004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004006 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004007 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004008 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004009 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004010 {
4011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4012 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004013 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004014 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4016 }
4017
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004018 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004019 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004020
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004021 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004022 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004023 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004024 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4025 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4026 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004027 }
4028 else
4029 {
4030 ssl->out_msg[4] = 0;
4031 ssl->out_msg[5] = 0;
4032 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004033
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004034 /* Handshake hashes are computed without fragmentation,
4035 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004036 memset( ssl->out_msg + 6, 0x00, 3 );
4037 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004040
Hanno Becker0207e532018-08-28 10:28:28 +01004041 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004042 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4043 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 }
4045
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004046 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004049 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4050 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004051 {
4052 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004055 return( ret );
4056 }
4057 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004058 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004060 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004061 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004062 {
4063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4064 return( ret );
4065 }
4066 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004067
4068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4069
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004070 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004071}
4072
4073/*
4074 * Record layer functions
4075 */
4076
4077/*
4078 * Write current record.
4079 *
4080 * Uses:
4081 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4082 * - ssl->out_msglen: length of the record content (excl headers)
4083 * - ssl->out_msg: record content
4084 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004085int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004086{
4087 int ret, done = 0;
4088 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004089 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004090
4091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004094 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004096 {
4097 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004100 return( ret );
4101 }
4102
4103 len = ssl->out_msglen;
4104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4108 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 ret = mbedtls_ssl_hw_record_write( ssl );
4113 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4116 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004117 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004118
4119 if( ret == 0 )
4120 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004121 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004123 if( !done )
4124 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004125 unsigned i;
4126 size_t protected_record_size;
4127
Paul Bakker05ef8352012-05-08 09:17:57 +00004128 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004130 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004131
Hanno Becker19859472018-08-06 09:40:20 +01004132 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004133 ssl->out_len[0] = (unsigned char)( len >> 8 );
4134 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004135
Paul Bakker48916f92012-09-16 19:57:18 +00004136 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004137 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004138 mbedtls_record rec;
4139
4140 rec.buf = ssl->out_iv;
4141 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4142 ( ssl->out_iv - ssl->out_buf );
4143 rec.data_len = ssl->out_msglen;
4144 rec.data_offset = ssl->out_msg - rec.buf;
4145
4146 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4147 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4148 ssl->conf->transport, rec.ver );
4149 rec.type = ssl->out_msgtype;
4150
Hanno Becker43c24b82019-05-01 09:45:57 +01004151#if defined(MBEDTLS_SSL_CID)
4152 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004153 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004154#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004155
Hanno Beckera18d1322018-01-03 14:27:32 +00004156 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004157 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004160 return( ret );
4161 }
4162
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004163 if( rec.data_offset != 0 )
4164 {
4165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4167 }
4168
Hanno Becker78f839d2019-03-14 12:56:23 +00004169 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004170 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4171 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004172 }
4173
Hanno Becker2b1e3542018-08-06 11:19:13 +01004174 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4175
4176#if defined(MBEDTLS_SSL_PROTO_DTLS)
4177 /* In case of DTLS, double-check that we don't exceed
4178 * the remaining space in the datagram. */
4179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4180 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004181 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004182 if( ret < 0 )
4183 return( ret );
4184
4185 if( protected_record_size > (size_t) ret )
4186 {
4187 /* Should never happen */
4188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4189 }
4190 }
4191#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004194 "version = [%d:%d], msglen = %d",
4195 ssl->out_hdr[0], ssl->out_hdr[1],
4196 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004199 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004200
4201 ssl->out_left += protected_record_size;
4202 ssl->out_hdr += protected_record_size;
4203 ssl_update_out_pointers( ssl, ssl->transform_out );
4204
Hanno Becker04484622018-08-06 09:49:38 +01004205 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4206 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4207 break;
4208
4209 /* The loop goes to its end iff the counter is wrapping */
4210 if( i == ssl_ep_len( ssl ) )
4211 {
4212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4213 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4214 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 }
4216
Hanno Becker67bc7c32018-08-06 11:33:50 +01004217#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4219 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004220 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004221 size_t remaining;
4222 ret = ssl_get_remaining_payload_in_datagram( ssl );
4223 if( ret < 0 )
4224 {
4225 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4226 ret );
4227 return( ret );
4228 }
4229
4230 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004231 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004232 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004233 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004234 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004235 else
4236 {
Hanno Becker513815a2018-08-20 11:56:09 +01004237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004238 }
4239 }
4240#endif /* MBEDTLS_SSL_PROTO_DTLS */
4241
4242 if( ( flush == SSL_FORCE_FLUSH ) &&
4243 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004246 return( ret );
4247 }
4248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004250
4251 return( 0 );
4252}
4253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004255
4256static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4257{
4258 if( ssl->in_msglen < ssl->in_hslen ||
4259 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4260 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4261 {
4262 return( 1 );
4263 }
4264 return( 0 );
4265}
Hanno Becker44650b72018-08-16 12:51:11 +01004266
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004267static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004268{
4269 return( ( ssl->in_msg[9] << 16 ) |
4270 ( ssl->in_msg[10] << 8 ) |
4271 ssl->in_msg[11] );
4272}
4273
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004274static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004275{
4276 return( ( ssl->in_msg[6] << 16 ) |
4277 ( ssl->in_msg[7] << 8 ) |
4278 ssl->in_msg[8] );
4279}
4280
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004281static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004282{
4283 uint32_t msg_len, frag_off, frag_len;
4284
4285 msg_len = ssl_get_hs_total_len( ssl );
4286 frag_off = ssl_get_hs_frag_off( ssl );
4287 frag_len = ssl_get_hs_frag_len( ssl );
4288
4289 if( frag_off > msg_len )
4290 return( -1 );
4291
4292 if( frag_len > msg_len - frag_off )
4293 return( -1 );
4294
4295 if( frag_len + 12 > ssl->in_msglen )
4296 return( -1 );
4297
4298 return( 0 );
4299}
4300
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004301/*
4302 * Mark bits in bitmask (used for DTLS HS reassembly)
4303 */
4304static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4305{
4306 unsigned int start_bits, end_bits;
4307
4308 start_bits = 8 - ( offset % 8 );
4309 if( start_bits != 8 )
4310 {
4311 size_t first_byte_idx = offset / 8;
4312
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004313 /* Special case */
4314 if( len <= start_bits )
4315 {
4316 for( ; len != 0; len-- )
4317 mask[first_byte_idx] |= 1 << ( start_bits - len );
4318
4319 /* Avoid potential issues with offset or len becoming invalid */
4320 return;
4321 }
4322
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004323 offset += start_bits; /* Now offset % 8 == 0 */
4324 len -= start_bits;
4325
4326 for( ; start_bits != 0; start_bits-- )
4327 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4328 }
4329
4330 end_bits = len % 8;
4331 if( end_bits != 0 )
4332 {
4333 size_t last_byte_idx = ( offset + len ) / 8;
4334
4335 len -= end_bits; /* Now len % 8 == 0 */
4336
4337 for( ; end_bits != 0; end_bits-- )
4338 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4339 }
4340
4341 memset( mask + offset / 8, 0xFF, len / 8 );
4342}
4343
4344/*
4345 * Check that bitmask is full
4346 */
4347static int ssl_bitmask_check( unsigned char *mask, size_t len )
4348{
4349 size_t i;
4350
4351 for( i = 0; i < len / 8; i++ )
4352 if( mask[i] != 0xFF )
4353 return( -1 );
4354
4355 for( i = 0; i < len % 8; i++ )
4356 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4357 return( -1 );
4358
4359 return( 0 );
4360}
4361
Hanno Becker56e205e2018-08-16 09:06:12 +01004362/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004363static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004364 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004365{
Hanno Becker56e205e2018-08-16 09:06:12 +01004366 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004367
Hanno Becker56e205e2018-08-16 09:06:12 +01004368 alloc_len = 12; /* Handshake header */
4369 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004370
Hanno Beckerd07df862018-08-16 09:14:58 +01004371 if( add_bitmap )
4372 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004373
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004374 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004375}
Hanno Becker56e205e2018-08-16 09:06:12 +01004376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004378
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004379static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004380{
4381 return( ( ssl->in_msg[1] << 16 ) |
4382 ( ssl->in_msg[2] << 8 ) |
4383 ssl->in_msg[3] );
4384}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004385
Simon Butcher99000142016-10-13 17:21:01 +01004386int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004391 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004393 }
4394
Hanno Becker12555c62018-08-16 12:47:53 +01004395 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004398 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004399 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004402 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004403 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004404 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004405 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004406
Hanno Becker44650b72018-08-16 12:51:11 +01004407 if( ssl_check_hs_header( ssl ) != 0 )
4408 {
4409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4410 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4411 }
4412
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004413 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004414 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4415 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4416 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4417 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004418 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004419 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4422 recv_msg_seq,
4423 ssl->handshake->in_msg_seq ) );
4424 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4425 }
4426
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004427 /* Retransmit only on last message from previous flight, to avoid
4428 * too many retransmissions.
4429 * Besides, No sane server ever retransmits HelloVerifyRequest */
4430 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004434 "message_seq = %d, start_of_flight = %d",
4435 recv_msg_seq,
4436 ssl->handshake->in_flight_start_seq ) );
4437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004441 return( ret );
4442 }
4443 }
4444 else
4445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004447 "message_seq = %d, expected = %d",
4448 recv_msg_seq,
4449 ssl->handshake->in_msg_seq ) );
4450 }
4451
Hanno Becker90333da2017-10-10 11:27:13 +01004452 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004453 }
4454 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004455
Hanno Becker6d97ef52018-08-16 13:09:04 +01004456 /* Message reassembly is handled alongside buffering of future
4457 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004458 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004459 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004460 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004463 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004464 }
4465 }
4466 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004468 /* With TLS we don't handle fragmentation (for now) */
4469 if( ssl->in_msglen < ssl->in_hslen )
4470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4472 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004473 }
4474
Simon Butcher99000142016-10-13 17:21:01 +01004475 return( 0 );
4476}
4477
4478void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4479{
Hanno Becker0271f962018-08-16 13:23:47 +01004480 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004481
Hanno Becker0271f962018-08-16 13:23:47 +01004482 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004483 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004484 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004485 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004486
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004487 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004490 ssl->handshake != NULL )
4491 {
Hanno Becker0271f962018-08-16 13:23:47 +01004492 unsigned offset;
4493 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004494
Hanno Becker0271f962018-08-16 13:23:47 +01004495 /* Increment handshake sequence number */
4496 hs->in_msg_seq++;
4497
4498 /*
4499 * Clear up handshake buffering and reassembly structure.
4500 */
4501
4502 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004503 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004504
4505 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004506 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4507 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004508 offset++, hs_buf++ )
4509 {
4510 *hs_buf = *(hs_buf + 1);
4511 }
4512
4513 /* Create a fresh last entry */
4514 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004515 }
4516#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004517}
4518
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004519/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004520 * DTLS anti-replay: RFC 6347 4.1.2.6
4521 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004522 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4523 * Bit n is set iff record number in_window_top - n has been seen.
4524 *
4525 * Usually, in_window_top is the last record number seen and the lsb of
4526 * in_window is set. The only exception is the initial state (record number 0
4527 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4530static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004531{
4532 ssl->in_window_top = 0;
4533 ssl->in_window = 0;
4534}
4535
4536static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4537{
4538 return( ( (uint64_t) buf[0] << 40 ) |
4539 ( (uint64_t) buf[1] << 32 ) |
4540 ( (uint64_t) buf[2] << 24 ) |
4541 ( (uint64_t) buf[3] << 16 ) |
4542 ( (uint64_t) buf[4] << 8 ) |
4543 ( (uint64_t) buf[5] ) );
4544}
4545
4546/*
4547 * Return 0 if sequence number is acceptable, -1 otherwise
4548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004550{
4551 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4552 uint64_t bit;
4553
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004554 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004555 return( 0 );
4556
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004557 if( rec_seqnum > ssl->in_window_top )
4558 return( 0 );
4559
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004560 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004561
4562 if( bit >= 64 )
4563 return( -1 );
4564
4565 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4566 return( -1 );
4567
4568 return( 0 );
4569}
4570
4571/*
4572 * Update replay window on new validated record
4573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004575{
4576 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4577
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004578 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004579 return;
4580
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004581 if( rec_seqnum > ssl->in_window_top )
4582 {
4583 /* Update window_top and the contents of the window */
4584 uint64_t shift = rec_seqnum - ssl->in_window_top;
4585
4586 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004587 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004588 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004589 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004590 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004591 ssl->in_window |= 1;
4592 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004593
4594 ssl->in_window_top = rec_seqnum;
4595 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596 else
4597 {
4598 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004599 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004600
4601 if( bit < 64 ) /* Always true, but be extra sure */
4602 ssl->in_window |= (uint64_t) 1 << bit;
4603 }
4604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004606
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004607#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004608/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004609static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4610
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004611/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004612 * Without any SSL context, check if a datagram looks like a ClientHello with
4613 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004614 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004615 *
4616 * - if cookie is valid, return 0
4617 * - if ClientHello looks superficially valid but cookie is not,
4618 * fill obuf and set olen, then
4619 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4620 * - otherwise return a specific error code
4621 */
4622static int ssl_check_dtls_clihlo_cookie(
4623 mbedtls_ssl_cookie_write_t *f_cookie_write,
4624 mbedtls_ssl_cookie_check_t *f_cookie_check,
4625 void *p_cookie,
4626 const unsigned char *cli_id, size_t cli_id_len,
4627 const unsigned char *in, size_t in_len,
4628 unsigned char *obuf, size_t buf_len, size_t *olen )
4629{
4630 size_t sid_len, cookie_len;
4631 unsigned char *p;
4632
4633 if( f_cookie_write == NULL || f_cookie_check == NULL )
4634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4635
4636 /*
4637 * Structure of ClientHello with record and handshake headers,
4638 * and expected values. We don't need to check a lot, more checks will be
4639 * done when actually parsing the ClientHello - skipping those checks
4640 * avoids code duplication and does not make cookie forging any easier.
4641 *
4642 * 0-0 ContentType type; copied, must be handshake
4643 * 1-2 ProtocolVersion version; copied
4644 * 3-4 uint16 epoch; copied, must be 0
4645 * 5-10 uint48 sequence_number; copied
4646 * 11-12 uint16 length; (ignored)
4647 *
4648 * 13-13 HandshakeType msg_type; (ignored)
4649 * 14-16 uint24 length; (ignored)
4650 * 17-18 uint16 message_seq; copied
4651 * 19-21 uint24 fragment_offset; copied, must be 0
4652 * 22-24 uint24 fragment_length; (ignored)
4653 *
4654 * 25-26 ProtocolVersion client_version; (ignored)
4655 * 27-58 Random random; (ignored)
4656 * 59-xx SessionID session_id; 1 byte len + sid_len content
4657 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4658 * ...
4659 *
4660 * Minimum length is 61 bytes.
4661 */
4662 if( in_len < 61 ||
4663 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4664 in[3] != 0 || in[4] != 0 ||
4665 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4666 {
4667 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4668 }
4669
4670 sid_len = in[59];
4671 if( sid_len > in_len - 61 )
4672 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4673
4674 cookie_len = in[60 + sid_len];
4675 if( cookie_len > in_len - 60 )
4676 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4677
4678 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4679 cli_id, cli_id_len ) == 0 )
4680 {
4681 /* Valid cookie */
4682 return( 0 );
4683 }
4684
4685 /*
4686 * If we get here, we've got an invalid cookie, let's prepare HVR.
4687 *
4688 * 0-0 ContentType type; copied
4689 * 1-2 ProtocolVersion version; copied
4690 * 3-4 uint16 epoch; copied
4691 * 5-10 uint48 sequence_number; copied
4692 * 11-12 uint16 length; olen - 13
4693 *
4694 * 13-13 HandshakeType msg_type; hello_verify_request
4695 * 14-16 uint24 length; olen - 25
4696 * 17-18 uint16 message_seq; copied
4697 * 19-21 uint24 fragment_offset; copied
4698 * 22-24 uint24 fragment_length; olen - 25
4699 *
4700 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4701 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4702 *
4703 * Minimum length is 28.
4704 */
4705 if( buf_len < 28 )
4706 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4707
4708 /* Copy most fields and adapt others */
4709 memcpy( obuf, in, 25 );
4710 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4711 obuf[25] = 0xfe;
4712 obuf[26] = 0xff;
4713
4714 /* Generate and write actual cookie */
4715 p = obuf + 28;
4716 if( f_cookie_write( p_cookie,
4717 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4718 {
4719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4720 }
4721
4722 *olen = p - obuf;
4723
4724 /* Go back and fill length fields */
4725 obuf[27] = (unsigned char)( *olen - 28 );
4726
4727 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4728 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4729 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4730
4731 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4732 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4733
4734 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4735}
4736
4737/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004738 * Handle possible client reconnect with the same UDP quadruplet
4739 * (RFC 6347 Section 4.2.8).
4740 *
4741 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4742 * that looks like a ClientHello.
4743 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004744 * - if the input looks like a ClientHello without cookies,
4745 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004746 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004747 * - if the input looks like a ClientHello with a valid cookie,
4748 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004749 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004750 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004751 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004752 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004753 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4754 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004755 */
4756static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4757{
4758 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004759 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004760
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004761 ret = ssl_check_dtls_clihlo_cookie(
4762 ssl->conf->f_cookie_write,
4763 ssl->conf->f_cookie_check,
4764 ssl->conf->p_cookie,
4765 ssl->cli_id, ssl->cli_id_len,
4766 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004767 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004768
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004769 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4770
4771 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004772 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004773 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004774 * If the error is permanent we'll catch it later,
4775 * if it's not, then hopefully it'll work next time. */
4776 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4777
4778 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004779 }
4780
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004781 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004783 /* Got a valid cookie, partially reset context */
4784 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4785 {
4786 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4787 return( ret );
4788 }
4789
4790 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004791 }
4792
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004793 return( ret );
4794}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004795#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004796
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004797/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004798 * ContentType type;
4799 * ProtocolVersion version;
4800 * uint16 epoch; // DTLS only
4801 * uint48 sequence_number; // DTLS only
4802 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004803 *
4804 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004805 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004806 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4807 *
4808 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004809 * 1. proceed with the record if this function returns 0
4810 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4811 * 3. return CLIENT_RECONNECT if this function return that value
4812 * 4. drop the whole datagram if this function returns anything else.
4813 * Point 2 is needed when the peer is resending, and we have already received
4814 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004817{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004818 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004821
Paul Bakker5121ce52009-01-03 21:22:43 +00004822 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004823 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004824 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004827 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004828 ssl->in_msgtype,
4829 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004830
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004831 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4833 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4834 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4835 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004838
4839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004840 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4841 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004842 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4843#endif /* MBEDTLS_SSL_PROTO_DTLS */
4844 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4845 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004848 }
4849
4850 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004851 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4854 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 }
4856
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004857 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4860 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 }
4862
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004863 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004864 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004865 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004869 }
4870
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004871 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004872 * DTLS-related tests.
4873 * Check epoch before checking length constraint because
4874 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4875 * message gets duplicated before the corresponding Finished message,
4876 * the second ChangeCipherSpec should be discarded because it belongs
4877 * to an old epoch, but not because its length is shorter than
4878 * the minimum record length for packets using the new record transform.
4879 * Note that these two kinds of failures are handled differently,
4880 * as an unexpected record is silently skipped but an invalid
4881 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004882 */
4883#if defined(MBEDTLS_SSL_PROTO_DTLS)
4884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4885 {
4886 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4887
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004888 /* Check epoch (and sequence number) with DTLS */
4889 if( rec_epoch != ssl->in_epoch )
4890 {
4891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4892 "expected %d, received %d",
4893 ssl->in_epoch, rec_epoch ) );
4894
4895#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4896 /*
4897 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4898 * access the first byte of record content (handshake type), as we
4899 * have an active transform (possibly iv_len != 0), so use the
4900 * fact that the record header len is 13 instead.
4901 */
4902 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4903 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4904 rec_epoch == 0 &&
4905 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4906 ssl->in_left > 13 &&
4907 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4908 {
4909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4910 "from the same port" ) );
4911 return( ssl_handle_possible_reconnect( ssl ) );
4912 }
4913 else
4914#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004915 {
4916 /* Consider buffering the record. */
4917 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4918 {
4919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4920 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4921 }
4922
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004923 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004924 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004925 }
4926
4927#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4928 /* Replay detection only works for the current epoch */
4929 if( rec_epoch == ssl->in_epoch &&
4930 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4931 {
4932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4933 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4934 }
4935#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004936
Hanno Becker52c6dc62017-05-26 16:07:36 +01004937 /* Drop unexpected ApplicationData records,
4938 * except at the beginning of renegotiations */
4939 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4940 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4941#if defined(MBEDTLS_SSL_RENEGOTIATION)
4942 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4943 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4944#endif
4945 )
4946 {
4947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4948 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4949 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004950 }
4951#endif /* MBEDTLS_SSL_PROTO_DTLS */
4952
Hanno Becker52c6dc62017-05-26 16:07:36 +01004953
4954 /* Check length against bounds of the current transform and version */
4955 if( ssl->transform_in == NULL )
4956 {
4957 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004958 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004959 {
4960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4961 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4962 }
4963 }
4964 else
4965 {
4966 if( ssl->in_msglen < ssl->transform_in->minlen )
4967 {
4968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4969 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4970 }
4971
4972#if defined(MBEDTLS_SSL_PROTO_SSL3)
4973 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004974 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004975 {
4976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4977 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4978 }
4979#endif
4980#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4981 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4982 /*
4983 * TLS encrypted messages can have up to 256 bytes of padding
4984 */
4985 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4986 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004987 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004988 {
4989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4990 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4991 }
4992#endif
4993 }
4994
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004995 return( 0 );
4996}
Paul Bakker5121ce52009-01-03 21:22:43 +00004997
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004998/*
4999 * If applicable, decrypt (and decompress) record content
5000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005002{
5003 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
5006 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5009 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 ret = mbedtls_ssl_hw_record_read( ssl );
5014 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5017 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005018 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005019
5020 if( ret == 0 )
5021 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005022 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005024 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005025 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005026 mbedtls_record rec;
5027
5028 rec.buf = ssl->in_iv;
5029 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5030 - ( ssl->in_iv - ssl->in_buf );
5031 rec.data_len = ssl->in_msglen;
5032 rec.data_offset = 0;
5033
5034 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5035 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5036 ssl->conf->transport, rec.ver );
5037 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005038 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5039 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005042 return( ret );
5043 }
5044
Hanno Becker29800d22018-08-07 14:30:18 +01005045 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
5046 {
5047 /* Should never happen */
5048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005050
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005051 ssl->in_msglen = rec.data_len;
5052 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5053 ssl->in_len[1] = (unsigned char)( rec.data_len );
5054
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005055 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5056 ssl->in_msg, ssl->in_msglen );
5057
Angus Grattond8213d02016-05-25 20:56:48 +10005058 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5061 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005062 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005063 else if( ssl->in_msglen == 0 )
5064 {
5065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5066 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5067 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5068 {
5069 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5071 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5072 }
5073#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5074
5075 ssl->nb_zero++;
5076
5077 /*
5078 * Three or more empty messages may be a DoS attack
5079 * (excessive CPU consumption).
5080 */
5081 if( ssl->nb_zero > 3 )
5082 {
5083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
5084 "messages, possible DoS attack" ) );
5085 /* Q: Is that the right error code? */
5086 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5087 }
5088 }
5089 else
5090 ssl->nb_zero = 0;
5091
5092#if defined(MBEDTLS_SSL_PROTO_DTLS)
5093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5094 {
5095 ; /* in_ctr read from peer, not maintained internally */
5096 }
5097 else
5098#endif
5099 {
5100 unsigned i;
5101 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5102 if( ++ssl->in_ctr[i - 1] != 0 )
5103 break;
5104
5105 /* The loop goes to its end iff the counter is wrapping */
5106 if( i == ssl_ep_len( ssl ) )
5107 {
5108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5109 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5110 }
5111 }
5112
Paul Bakker5121ce52009-01-03 21:22:43 +00005113 }
5114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005116 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005118 {
5119 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005122 return( ret );
5123 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005124 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005128 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005131 }
5132#endif
5133
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005134 return( 0 );
5135}
5136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005138
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005139/*
5140 * Read a record.
5141 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005142 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5143 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5144 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005145 */
Hanno Becker1097b342018-08-15 14:09:41 +01005146
5147/* Helper functions for mbedtls_ssl_read_record(). */
5148static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005149static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5150static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005151
Hanno Becker327c93b2018-08-15 13:56:18 +01005152int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005153 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005154{
5155 int ret;
5156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005158
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005159 if( ssl->keep_current_message == 0 )
5160 {
5161 do {
Simon Butcher99000142016-10-13 17:21:01 +01005162
Hanno Becker26994592018-08-15 14:14:59 +01005163 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005164 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005165 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005166
Hanno Beckere74d5562018-08-15 14:26:08 +01005167 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005168 {
Hanno Becker40f50842018-08-15 14:48:01 +01005169#if defined(MBEDTLS_SSL_PROTO_DTLS)
5170 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005171
Hanno Becker40f50842018-08-15 14:48:01 +01005172 /* We only check for buffered messages if the
5173 * current datagram is fully consumed. */
5174 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005175 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005176 {
Hanno Becker40f50842018-08-15 14:48:01 +01005177 if( ssl_load_buffered_message( ssl ) == 0 )
5178 have_buffered = 1;
5179 }
5180
5181 if( have_buffered == 0 )
5182#endif /* MBEDTLS_SSL_PROTO_DTLS */
5183 {
5184 ret = ssl_get_next_record( ssl );
5185 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5186 continue;
5187
5188 if( ret != 0 )
5189 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005190 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005191 return( ret );
5192 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005193 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005194 }
5195
5196 ret = mbedtls_ssl_handle_message_type( ssl );
5197
Hanno Becker40f50842018-08-15 14:48:01 +01005198#if defined(MBEDTLS_SSL_PROTO_DTLS)
5199 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5200 {
5201 /* Buffer future message */
5202 ret = ssl_buffer_message( ssl );
5203 if( ret != 0 )
5204 return( ret );
5205
5206 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5207 }
5208#endif /* MBEDTLS_SSL_PROTO_DTLS */
5209
Hanno Becker90333da2017-10-10 11:27:13 +01005210 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5211 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005212
5213 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005214 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005215 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005216 return( ret );
5217 }
5218
Hanno Becker327c93b2018-08-15 13:56:18 +01005219 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005220 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005221 {
5222 mbedtls_ssl_update_handshake_status( ssl );
5223 }
Simon Butcher99000142016-10-13 17:21:01 +01005224 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005225 else
Simon Butcher99000142016-10-13 17:21:01 +01005226 {
Hanno Becker02f59072018-08-15 14:00:24 +01005227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005228 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005229 }
5230
5231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5232
5233 return( 0 );
5234}
5235
Hanno Becker40f50842018-08-15 14:48:01 +01005236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005237static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005238{
Hanno Becker40f50842018-08-15 14:48:01 +01005239 if( ssl->in_left > ssl->next_record_offset )
5240 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005241
Hanno Becker40f50842018-08-15 14:48:01 +01005242 return( 0 );
5243}
5244
5245static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5246{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005247 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005248 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005249 int ret = 0;
5250
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005251 if( hs == NULL )
5252 return( -1 );
5253
Hanno Beckere00ae372018-08-20 09:39:42 +01005254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5255
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005256 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5257 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5258 {
5259 /* Check if we have seen a ChangeCipherSpec before.
5260 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005261 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005262 {
5263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5264 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005265 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005266 }
5267
Hanno Becker39b8bc92018-08-28 17:17:13 +01005268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005269 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5270 ssl->in_msglen = 1;
5271 ssl->in_msg[0] = 1;
5272
5273 /* As long as they are equal, the exact value doesn't matter. */
5274 ssl->in_left = 0;
5275 ssl->next_record_offset = 0;
5276
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005277 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005278 goto exit;
5279 }
Hanno Becker37f95322018-08-16 13:55:32 +01005280
Hanno Beckerb8f50142018-08-28 10:01:34 +01005281#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005282 /* Debug only */
5283 {
5284 unsigned offset;
5285 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5286 {
5287 hs_buf = &hs->buffering.hs[offset];
5288 if( hs_buf->is_valid == 1 )
5289 {
5290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5291 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005292 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005293 }
5294 }
5295 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005296#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005297
5298 /* Check if we have buffered and/or fully reassembled the
5299 * next handshake message. */
5300 hs_buf = &hs->buffering.hs[0];
5301 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5302 {
5303 /* Synthesize a record containing the buffered HS message. */
5304 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5305 ( hs_buf->data[2] << 8 ) |
5306 hs_buf->data[3];
5307
5308 /* Double-check that we haven't accidentally buffered
5309 * a message that doesn't fit into the input buffer. */
5310 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5311 {
5312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5314 }
5315
5316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5317 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5318 hs_buf->data, msg_len + 12 );
5319
5320 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5321 ssl->in_hslen = msg_len + 12;
5322 ssl->in_msglen = msg_len + 12;
5323 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5324
5325 ret = 0;
5326 goto exit;
5327 }
5328 else
5329 {
5330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5331 hs->in_msg_seq ) );
5332 }
5333
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005334 ret = -1;
5335
5336exit:
5337
5338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5339 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005340}
5341
Hanno Beckera02b0b42018-08-21 17:20:27 +01005342static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5343 size_t desired )
5344{
5345 int offset;
5346 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5348 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005349
Hanno Becker01315ea2018-08-21 17:22:17 +01005350 /* Get rid of future records epoch first, if such exist. */
5351 ssl_free_buffered_record( ssl );
5352
5353 /* Check if we have enough space available now. */
5354 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5355 hs->buffering.total_bytes_buffered ) )
5356 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005358 return( 0 );
5359 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005360
Hanno Becker4f432ad2018-08-28 10:02:32 +01005361 /* We don't have enough space to buffer the next expected handshake
5362 * message. Remove buffers used for future messages to gain space,
5363 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005364 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5365 offset >= 0; offset-- )
5366 {
5367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5368 offset ) );
5369
Hanno Beckerb309b922018-08-23 13:18:05 +01005370 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005371
5372 /* Check if we have enough space available now. */
5373 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5374 hs->buffering.total_bytes_buffered ) )
5375 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005377 return( 0 );
5378 }
5379 }
5380
5381 return( -1 );
5382}
5383
Hanno Becker40f50842018-08-15 14:48:01 +01005384static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5385{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005386 int ret = 0;
5387 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5388
5389 if( hs == NULL )
5390 return( 0 );
5391
5392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5393
5394 switch( ssl->in_msgtype )
5395 {
5396 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005398
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005399 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005400 break;
5401
5402 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005403 {
5404 unsigned recv_msg_seq_offset;
5405 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5406 mbedtls_ssl_hs_buffer *hs_buf;
5407 size_t msg_len = ssl->in_hslen - 12;
5408
5409 /* We should never receive an old handshake
5410 * message - double-check nonetheless. */
5411 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5412 {
5413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5415 }
5416
5417 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5418 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5419 {
5420 /* Silently ignore -- message too far in the future */
5421 MBEDTLS_SSL_DEBUG_MSG( 2,
5422 ( "Ignore future HS message with sequence number %u, "
5423 "buffering window %u - %u",
5424 recv_msg_seq, ssl->handshake->in_msg_seq,
5425 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5426
5427 goto exit;
5428 }
5429
5430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5431 recv_msg_seq, recv_msg_seq_offset ) );
5432
5433 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5434
5435 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005436 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005437 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005438 size_t reassembly_buf_sz;
5439
Hanno Becker37f95322018-08-16 13:55:32 +01005440 hs_buf->is_fragmented =
5441 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5442
5443 /* We copy the message back into the input buffer
5444 * after reassembly, so check that it's not too large.
5445 * This is an implementation-specific limitation
5446 * and not one from the standard, hence it is not
5447 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005448 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005449 {
5450 /* Ignore message */
5451 goto exit;
5452 }
5453
Hanno Beckere0b150f2018-08-21 15:51:03 +01005454 /* Check if we have enough space to buffer the message. */
5455 if( hs->buffering.total_bytes_buffered >
5456 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5457 {
5458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5460 }
5461
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005462 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5463 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005464
5465 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5466 hs->buffering.total_bytes_buffered ) )
5467 {
5468 if( recv_msg_seq_offset > 0 )
5469 {
5470 /* If we can't buffer a future message because
5471 * of space limitations -- ignore. */
5472 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",
5473 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5474 (unsigned) hs->buffering.total_bytes_buffered ) );
5475 goto exit;
5476 }
Hanno Beckere1801392018-08-21 16:51:05 +01005477 else
5478 {
5479 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",
5480 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5481 (unsigned) hs->buffering.total_bytes_buffered ) );
5482 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005483
Hanno Beckera02b0b42018-08-21 17:20:27 +01005484 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005485 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005486 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",
5487 (unsigned) msg_len,
5488 (unsigned) reassembly_buf_sz,
5489 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005490 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005491 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5492 goto exit;
5493 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005494 }
5495
5496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5497 msg_len ) );
5498
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005499 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5500 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005501 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005502 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005503 goto exit;
5504 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005505 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005506
5507 /* Prepare final header: copy msg_type, length and message_seq,
5508 * then add standardised fragment_offset and fragment_length */
5509 memcpy( hs_buf->data, ssl->in_msg, 6 );
5510 memset( hs_buf->data + 6, 0, 3 );
5511 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5512
5513 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005514
5515 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005516 }
5517 else
5518 {
5519 /* Make sure msg_type and length are consistent */
5520 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5521 {
5522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5523 /* Ignore */
5524 goto exit;
5525 }
5526 }
5527
Hanno Becker4422bbb2018-08-20 09:40:19 +01005528 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005529 {
5530 size_t frag_len, frag_off;
5531 unsigned char * const msg = hs_buf->data + 12;
5532
5533 /*
5534 * Check and copy current fragment
5535 */
5536
5537 /* Validation of header fields already done in
5538 * mbedtls_ssl_prepare_handshake_record(). */
5539 frag_off = ssl_get_hs_frag_off( ssl );
5540 frag_len = ssl_get_hs_frag_len( ssl );
5541
5542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5543 frag_off, frag_len ) );
5544 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5545
5546 if( hs_buf->is_fragmented )
5547 {
5548 unsigned char * const bitmask = msg + msg_len;
5549 ssl_bitmask_set( bitmask, frag_off, frag_len );
5550 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5551 msg_len ) == 0 );
5552 }
5553 else
5554 {
5555 hs_buf->is_complete = 1;
5556 }
5557
5558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5559 hs_buf->is_complete ? "" : "not yet " ) );
5560 }
5561
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005562 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005563 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005564
5565 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005566 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005567 break;
5568 }
5569
5570exit:
5571
5572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5573 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005574}
5575#endif /* MBEDTLS_SSL_PROTO_DTLS */
5576
Hanno Becker1097b342018-08-15 14:09:41 +01005577static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005578{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005579 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005580 * Consume last content-layer message and potentially
5581 * update in_msglen which keeps track of the contents'
5582 * consumption state.
5583 *
5584 * (1) Handshake messages:
5585 * Remove last handshake message, move content
5586 * and adapt in_msglen.
5587 *
5588 * (2) Alert messages:
5589 * Consume whole record content, in_msglen = 0.
5590 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005591 * (3) Change cipher spec:
5592 * Consume whole record content, in_msglen = 0.
5593 *
5594 * (4) Application data:
5595 * Don't do anything - the record layer provides
5596 * the application data as a stream transport
5597 * and consumes through mbedtls_ssl_read only.
5598 *
5599 */
5600
5601 /* Case (1): Handshake messages */
5602 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005603 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005604 /* Hard assertion to be sure that no application data
5605 * is in flight, as corrupting ssl->in_msglen during
5606 * ssl->in_offt != NULL is fatal. */
5607 if( ssl->in_offt != NULL )
5608 {
5609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5610 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5611 }
5612
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005613 /*
5614 * Get next Handshake message in the current record
5615 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005616
Hanno Becker4a810fb2017-05-24 16:27:30 +01005617 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005618 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005619 * current handshake content: If DTLS handshake
5620 * fragmentation is used, that's the fragment
5621 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005622 * size here is faulty and should be changed at
5623 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005624 * (2) While it doesn't seem to cause problems, one
5625 * has to be very careful not to assume that in_hslen
5626 * is always <= in_msglen in a sensible communication.
5627 * Again, it's wrong for DTLS handshake fragmentation.
5628 * The following check is therefore mandatory, and
5629 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005630 * Additionally, ssl->in_hslen might be arbitrarily out of
5631 * bounds after handling a DTLS message with an unexpected
5632 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005633 */
5634 if( ssl->in_hslen < ssl->in_msglen )
5635 {
5636 ssl->in_msglen -= ssl->in_hslen;
5637 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5638 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005639
Hanno Becker4a810fb2017-05-24 16:27:30 +01005640 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5641 ssl->in_msg, ssl->in_msglen );
5642 }
5643 else
5644 {
5645 ssl->in_msglen = 0;
5646 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005647
Hanno Becker4a810fb2017-05-24 16:27:30 +01005648 ssl->in_hslen = 0;
5649 }
5650 /* Case (4): Application data */
5651 else if( ssl->in_offt != NULL )
5652 {
5653 return( 0 );
5654 }
5655 /* Everything else (CCS & Alerts) */
5656 else
5657 {
5658 ssl->in_msglen = 0;
5659 }
5660
Hanno Becker1097b342018-08-15 14:09:41 +01005661 return( 0 );
5662}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005663
Hanno Beckere74d5562018-08-15 14:26:08 +01005664static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5665{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005666 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005667 return( 1 );
5668
5669 return( 0 );
5670}
5671
Hanno Becker5f066e72018-08-16 14:56:31 +01005672#if defined(MBEDTLS_SSL_PROTO_DTLS)
5673
5674static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5675{
5676 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5677 if( hs == NULL )
5678 return;
5679
Hanno Becker01315ea2018-08-21 17:22:17 +01005680 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005681 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005682 hs->buffering.total_bytes_buffered -=
5683 hs->buffering.future_record.len;
5684
5685 mbedtls_free( hs->buffering.future_record.data );
5686 hs->buffering.future_record.data = NULL;
5687 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005688}
5689
5690static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5691{
5692 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5693 unsigned char * rec;
5694 size_t rec_len;
5695 unsigned rec_epoch;
5696
5697 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5698 return( 0 );
5699
5700 if( hs == NULL )
5701 return( 0 );
5702
Hanno Becker5f066e72018-08-16 14:56:31 +01005703 rec = hs->buffering.future_record.data;
5704 rec_len = hs->buffering.future_record.len;
5705 rec_epoch = hs->buffering.future_record.epoch;
5706
5707 if( rec == NULL )
5708 return( 0 );
5709
Hanno Becker4cb782d2018-08-20 11:19:05 +01005710 /* Only consider loading future records if the
5711 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005712 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005713 return( 0 );
5714
Hanno Becker5f066e72018-08-16 14:56:31 +01005715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5716
5717 if( rec_epoch != ssl->in_epoch )
5718 {
5719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5720 goto exit;
5721 }
5722
5723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5724
5725 /* Double-check that the record is not too large */
5726 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5727 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5728 {
5729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5731 }
5732
5733 memcpy( ssl->in_hdr, rec, rec_len );
5734 ssl->in_left = rec_len;
5735 ssl->next_record_offset = 0;
5736
5737 ssl_free_buffered_record( ssl );
5738
5739exit:
5740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5741 return( 0 );
5742}
5743
5744static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5745{
5746 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5747 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005748 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005749
5750 /* Don't buffer future records outside handshakes. */
5751 if( hs == NULL )
5752 return( 0 );
5753
5754 /* Only buffer handshake records (we are only interested
5755 * in Finished messages). */
5756 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5757 return( 0 );
5758
5759 /* Don't buffer more than one future epoch record. */
5760 if( hs->buffering.future_record.data != NULL )
5761 return( 0 );
5762
Hanno Becker01315ea2018-08-21 17:22:17 +01005763 /* Don't buffer record if there's not enough buffering space remaining. */
5764 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5765 hs->buffering.total_bytes_buffered ) )
5766 {
5767 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",
5768 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5769 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005770 return( 0 );
5771 }
5772
Hanno Becker5f066e72018-08-16 14:56:31 +01005773 /* Buffer record */
5774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5775 ssl->in_epoch + 1 ) );
5776 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5777 rec_hdr_len + ssl->in_msglen );
5778
5779 /* ssl_parse_record_header() only considers records
5780 * of the next epoch as candidates for buffering. */
5781 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005782 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005783
5784 hs->buffering.future_record.data =
5785 mbedtls_calloc( 1, hs->buffering.future_record.len );
5786 if( hs->buffering.future_record.data == NULL )
5787 {
5788 /* If we run out of RAM trying to buffer a
5789 * record from the next epoch, just ignore. */
5790 return( 0 );
5791 }
5792
Hanno Becker01315ea2018-08-21 17:22:17 +01005793 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005794
Hanno Becker01315ea2018-08-21 17:22:17 +01005795 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005796 return( 0 );
5797}
5798
5799#endif /* MBEDTLS_SSL_PROTO_DTLS */
5800
Hanno Beckere74d5562018-08-15 14:26:08 +01005801static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005802{
5803 int ret;
5804
Hanno Becker5f066e72018-08-16 14:56:31 +01005805#if defined(MBEDTLS_SSL_PROTO_DTLS)
5806 /* We might have buffered a future record; if so,
5807 * and if the epoch matches now, load it.
5808 * On success, this call will set ssl->in_left to
5809 * the length of the buffered record, so that
5810 * the calls to ssl_fetch_input() below will
5811 * essentially be no-ops. */
5812 ret = ssl_load_buffered_record( ssl );
5813 if( ret != 0 )
5814 return( ret );
5815#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005820 return( ret );
5821 }
5822
5823 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5827 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005828 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005829 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5830 {
5831 ret = ssl_buffer_future_record( ssl );
5832 if( ret != 0 )
5833 return( ret );
5834
5835 /* Fall through to handling of unexpected records */
5836 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5837 }
5838
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005839 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5840 {
5841 /* Skip unexpected record (but not whole datagram) */
5842 ssl->next_record_offset = ssl->in_msglen
5843 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005844
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5846 "(header)" ) );
5847 }
5848 else
5849 {
5850 /* Skip invalid record and the rest of the datagram */
5851 ssl->next_record_offset = 0;
5852 ssl->in_left = 0;
5853
5854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5855 "(header)" ) );
5856 }
5857
5858 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005859 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005860 }
5861#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005862 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005863 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005864
5865 /*
5866 * Read and optionally decrypt the message contents
5867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5869 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005872 return( ret );
5873 }
5874
5875 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005877 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005880 if( ssl->next_record_offset < ssl->in_left )
5881 {
5882 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5883 }
5884 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005885 else
5886#endif
5887 ssl->in_left = 0;
5888
5889 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005891#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005892 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005893 {
5894 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5896 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005897 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005898 /* Except when waiting for Finished as a bad mac here
5899 * probably means something went wrong in the handshake
5900 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5901 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5902 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5903 {
5904#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5905 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5906 {
5907 mbedtls_ssl_send_alert_message( ssl,
5908 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5909 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5910 }
5911#endif
5912 return( ret );
5913 }
5914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005916 if( ssl->conf->badmac_limit != 0 &&
5917 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5920 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005921 }
5922#endif
5923
Hanno Becker4a810fb2017-05-24 16:27:30 +01005924 /* As above, invalid records cause
5925 * dismissal of the whole datagram. */
5926
5927 ssl->next_record_offset = 0;
5928 ssl->in_left = 0;
5929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005931 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005932 }
5933
5934 return( ret );
5935 }
5936 else
5937#endif
5938 {
5939 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5941 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 mbedtls_ssl_send_alert_message( ssl,
5944 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5945 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005946 }
5947#endif
5948 return( ret );
5949 }
5950 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005951
Simon Butcher99000142016-10-13 17:21:01 +01005952 return( 0 );
5953}
5954
5955int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5956{
5957 int ret;
5958
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005959 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005960 * Handle particular types of records
5961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005963 {
Simon Butcher99000142016-10-13 17:21:01 +01005964 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5965 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005966 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005967 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 }
5969
Hanno Beckere678eaa2018-08-21 14:57:46 +01005970 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005971 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005972 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005973 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5975 ssl->in_msglen ) );
5976 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005977 }
5978
Hanno Beckere678eaa2018-08-21 14:57:46 +01005979 if( ssl->in_msg[0] != 1 )
5980 {
5981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5982 ssl->in_msg[0] ) );
5983 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5984 }
5985
5986#if defined(MBEDTLS_SSL_PROTO_DTLS)
5987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5988 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5989 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5990 {
5991 if( ssl->handshake == NULL )
5992 {
5993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5994 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5995 }
5996
5997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5998 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5999 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006000#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006001 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006004 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006005 if( ssl->in_msglen != 2 )
6006 {
6007 /* Note: Standard allows for more than one 2 byte alert
6008 to be packed in a single message, but Mbed TLS doesn't
6009 currently support this. */
6010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6011 ssl->in_msglen ) );
6012 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6013 }
6014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 ssl->in_msg[0], ssl->in_msg[1] ) );
6017
6018 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006019 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006024 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006026 }
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6029 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6032 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006034
6035#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6036 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6037 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6038 {
Hanno Becker90333da2017-10-10 11:27:13 +01006039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006040 /* Will be handled when trying to parse ServerHello */
6041 return( 0 );
6042 }
6043#endif
6044
6045#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6046 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6047 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6048 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6049 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6050 {
6051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6052 /* Will be handled in mbedtls_ssl_parse_certificate() */
6053 return( 0 );
6054 }
6055#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6056
6057 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006058 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 }
6060
Hanno Beckerc76c6192017-06-06 10:03:17 +01006061#if defined(MBEDTLS_SSL_PROTO_DTLS)
6062 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6063 ssl->handshake != NULL &&
6064 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6065 {
6066 ssl_handshake_wrapup_free_hs_transform( ssl );
6067 }
6068#endif
6069
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 return( 0 );
6071}
6072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006074{
6075 int ret;
6076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6078 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6079 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006080 {
6081 return( ret );
6082 }
6083
6084 return( 0 );
6085}
6086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006088 unsigned char level,
6089 unsigned char message )
6090{
6091 int ret;
6092
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006093 if( ssl == NULL || ssl->conf == NULL )
6094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006100 ssl->out_msglen = 2;
6101 ssl->out_msg[0] = level;
6102 ssl->out_msg[1] = message;
6103
Hanno Becker67bc7c32018-08-06 11:33:50 +01006104 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006107 return( ret );
6108 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006110
6111 return( 0 );
6112}
6113
Hanno Beckerb9d44792019-02-08 07:19:04 +00006114#if defined(MBEDTLS_X509_CRT_PARSE_C)
6115static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6116{
6117#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6118 if( session->peer_cert != NULL )
6119 {
6120 mbedtls_x509_crt_free( session->peer_cert );
6121 mbedtls_free( session->peer_cert );
6122 session->peer_cert = NULL;
6123 }
6124#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6125 if( session->peer_cert_digest != NULL )
6126 {
6127 /* Zeroization is not necessary. */
6128 mbedtls_free( session->peer_cert_digest );
6129 session->peer_cert_digest = NULL;
6130 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6131 session->peer_cert_digest_len = 0;
6132 }
6133#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6134}
6135#endif /* MBEDTLS_X509_CRT_PARSE_C */
6136
Paul Bakker5121ce52009-01-03 21:22:43 +00006137/*
6138 * Handshake functions
6139 */
Hanno Becker21489932019-02-05 13:20:55 +00006140#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006141/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006144 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6145 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006148
Hanno Becker7177a882019-02-05 13:36:46 +00006149 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006152 ssl->state++;
6153 return( 0 );
6154 }
6155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006158}
6159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006161{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006162 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6163 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006166
Hanno Becker7177a882019-02-05 13:36:46 +00006167 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006170 ssl->state++;
6171 return( 0 );
6172 }
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006176}
Gilles Peskinef9828522017-05-03 12:28:43 +02006177
Hanno Becker21489932019-02-05 13:20:55 +00006178#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006179/* Some certificate support -> implement write and parse */
6180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006184 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006186 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6187 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006190
Hanno Becker7177a882019-02-05 13:36:46 +00006191 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006194 ssl->state++;
6195 return( 0 );
6196 }
6197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006199 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006200 {
6201 if( ssl->client_auth == 0 )
6202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006204 ssl->state++;
6205 return( 0 );
6206 }
6207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006209 /*
6210 * If using SSLv3 and got no cert, send an Alert message
6211 * (otherwise an empty Certificate message will be sent).
6212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6214 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006215 {
6216 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6218 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6219 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 goto write_msg;
6223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226#endif /* MBEDTLS_SSL_CLI_C */
6227#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006228 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6233 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006234 }
6235 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006239
6240 /*
6241 * 0 . 0 handshake type
6242 * 1 . 3 handshake length
6243 * 4 . 6 length of all certs
6244 * 7 . 9 length of cert. 1
6245 * 10 . n-1 peer certificate
6246 * n . n+2 length of cert. 2
6247 * n+3 . ... upper level cert, etc.
6248 */
6249 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006251
Paul Bakker29087132010-03-21 21:03:34 +00006252 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 {
6254 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006255 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006258 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 }
6261
6262 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6263 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6264 ssl->out_msg[i + 2] = (unsigned char)( n );
6265
6266 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6267 i += n; crt = crt->next;
6268 }
6269
6270 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6271 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6272 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6273
6274 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6276 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006277
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006278#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006279write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006281
6282 ssl->state++;
6283
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006284 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006285 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006287 return( ret );
6288 }
6289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006291
Paul Bakkered27a042013-04-18 22:46:23 +02006292 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006293}
6294
Hanno Becker84879e32019-01-31 07:44:03 +00006295#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006296
6297#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006298static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6299 unsigned char *crt_buf,
6300 size_t crt_buf_len )
6301{
6302 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6303
6304 if( peer_crt == NULL )
6305 return( -1 );
6306
6307 if( peer_crt->raw.len != crt_buf_len )
6308 return( -1 );
6309
Hanno Becker46f34d02019-02-08 14:00:04 +00006310 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006311}
Hanno Becker177475a2019-02-05 17:02:46 +00006312#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6313static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6314 unsigned char *crt_buf,
6315 size_t crt_buf_len )
6316{
6317 int ret;
6318 unsigned char const * const peer_cert_digest =
6319 ssl->session->peer_cert_digest;
6320 mbedtls_md_type_t const peer_cert_digest_type =
6321 ssl->session->peer_cert_digest_type;
6322 mbedtls_md_info_t const * const digest_info =
6323 mbedtls_md_info_from_type( peer_cert_digest_type );
6324 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6325 size_t digest_len;
6326
6327 if( peer_cert_digest == NULL || digest_info == NULL )
6328 return( -1 );
6329
6330 digest_len = mbedtls_md_get_size( digest_info );
6331 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6332 return( -1 );
6333
6334 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6335 if( ret != 0 )
6336 return( -1 );
6337
6338 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6339}
6340#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006341#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006342
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006343/*
6344 * Once the certificate message is read, parse it into a cert chain and
6345 * perform basic checks, but leave actual verification to the caller
6346 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006347static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6348 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006349{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006350 int ret;
6351#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6352 int crt_cnt=0;
6353#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006354 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006355 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006360 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6361 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 }
6364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6366 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006369 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6370 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 }
6373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006375
Paul Bakker5121ce52009-01-03 21:22:43 +00006376 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006378 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006379 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006380
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006381 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006385 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6386 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006388 }
6389
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006390 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6391 i += 3;
6392
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006393 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006394 while( i < ssl->in_hslen )
6395 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006396 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006397 if ( i + 3 > ssl->in_hslen ) {
6398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006399 mbedtls_ssl_send_alert_message( ssl,
6400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6401 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006402 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6403 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006404 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6405 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006406 if( ssl->in_msg[i] != 0 )
6407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006409 mbedtls_ssl_send_alert_message( ssl,
6410 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6411 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006413 }
6414
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006415 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006416 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6417 | (unsigned int) ssl->in_msg[i + 2];
6418 i += 3;
6419
6420 if( n < 128 || i + n > ssl->in_hslen )
6421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006423 mbedtls_ssl_send_alert_message( ssl,
6424 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6425 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 }
6428
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006429 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006430#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6431 if( crt_cnt++ == 0 &&
6432 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6433 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006434 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006435 /* During client-side renegotiation, check that the server's
6436 * end-CRTs hasn't changed compared to the initial handshake,
6437 * mitigating the triple handshake attack. On success, reuse
6438 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6440 if( ssl_check_peer_crt_unchanged( ssl,
6441 &ssl->in_msg[i],
6442 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006443 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6445 mbedtls_ssl_send_alert_message( ssl,
6446 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6447 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6448 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006449 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006450
6451 /* Now we can safely free the original chain. */
6452 ssl_clear_peer_cert( ssl->session );
6453 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006454#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6455
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006456 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006457#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006458 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006459#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006460 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006461 * it in-place from the input buffer instead of making a copy. */
6462 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6463#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006464 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006465 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006466 case 0: /*ok*/
6467 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6468 /* Ignore certificate with an unknown algorithm: maybe a
6469 prior certificate was already trusted. */
6470 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006471
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006472 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6473 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6474 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006475
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006476 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6477 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6478 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006479
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006480 default:
6481 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6482 crt_parse_der_failed:
6483 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6484 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6485 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 }
6487
6488 i += n;
6489 }
6490
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006491 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006492 return( 0 );
6493}
6494
Hanno Becker4a55f632019-02-05 12:49:06 +00006495#if defined(MBEDTLS_SSL_SRV_C)
6496static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6497{
6498 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6499 return( -1 );
6500
6501#if defined(MBEDTLS_SSL_PROTO_SSL3)
6502 /*
6503 * Check if the client sent an empty certificate
6504 */
6505 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6506 {
6507 if( ssl->in_msglen == 2 &&
6508 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6509 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6510 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6511 {
6512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6513 return( 0 );
6514 }
6515
6516 return( -1 );
6517 }
6518#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6519
6520#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6521 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6522 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6523 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6524 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6525 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6526 {
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6528 return( 0 );
6529 }
6530
6531 return( -1 );
6532#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6533 MBEDTLS_SSL_PROTO_TLS1_2 */
6534}
6535#endif /* MBEDTLS_SSL_SRV_C */
6536
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006537/* Check if a certificate message is expected.
6538 * Return either
6539 * - SSL_CERTIFICATE_EXPECTED, or
6540 * - SSL_CERTIFICATE_SKIP
6541 * indicating whether a Certificate message is expected or not.
6542 */
6543#define SSL_CERTIFICATE_EXPECTED 0
6544#define SSL_CERTIFICATE_SKIP 1
6545static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6546 int authmode )
6547{
6548 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006549 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006550
6551 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6552 return( SSL_CERTIFICATE_SKIP );
6553
6554#if defined(MBEDTLS_SSL_SRV_C)
6555 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6556 {
6557 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6558 return( SSL_CERTIFICATE_SKIP );
6559
6560 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6561 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006562 ssl->session_negotiate->verify_result =
6563 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6564 return( SSL_CERTIFICATE_SKIP );
6565 }
6566 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006567#else
6568 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006569#endif /* MBEDTLS_SSL_SRV_C */
6570
6571 return( SSL_CERTIFICATE_EXPECTED );
6572}
6573
Hanno Becker68636192019-02-05 14:36:34 +00006574static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6575 int authmode,
6576 mbedtls_x509_crt *chain,
6577 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006578{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006579 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006580 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006581 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006582 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006583
Hanno Becker8927c832019-04-03 12:52:50 +01006584 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6585 void *p_vrfy;
6586
Hanno Becker68636192019-02-05 14:36:34 +00006587 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6588 return( 0 );
6589
Hanno Becker8927c832019-04-03 12:52:50 +01006590 if( ssl->f_vrfy != NULL )
6591 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006593 f_vrfy = ssl->f_vrfy;
6594 p_vrfy = ssl->p_vrfy;
6595 }
6596 else
6597 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006599 f_vrfy = ssl->conf->f_vrfy;
6600 p_vrfy = ssl->conf->p_vrfy;
6601 }
6602
Hanno Becker68636192019-02-05 14:36:34 +00006603 /*
6604 * Main check: verify certificate
6605 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006606#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6607 if( ssl->conf->f_ca_cb != NULL )
6608 {
6609 ((void) rs_ctx);
6610 have_ca_chain = 1;
6611
6612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006613 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006614 chain,
6615 ssl->conf->f_ca_cb,
6616 ssl->conf->p_ca_cb,
6617 ssl->conf->cert_profile,
6618 ssl->hostname,
6619 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006620 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006621 }
6622 else
6623#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6624 {
6625 mbedtls_x509_crt *ca_chain;
6626 mbedtls_x509_crl *ca_crl;
6627
6628#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6629 if( ssl->handshake->sni_ca_chain != NULL )
6630 {
6631 ca_chain = ssl->handshake->sni_ca_chain;
6632 ca_crl = ssl->handshake->sni_ca_crl;
6633 }
6634 else
6635#endif
6636 {
6637 ca_chain = ssl->conf->ca_chain;
6638 ca_crl = ssl->conf->ca_crl;
6639 }
6640
6641 if( ca_chain != NULL )
6642 have_ca_chain = 1;
6643
6644 ret = mbedtls_x509_crt_verify_restartable(
6645 chain,
6646 ca_chain, ca_crl,
6647 ssl->conf->cert_profile,
6648 ssl->hostname,
6649 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006650 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006651 }
Hanno Becker68636192019-02-05 14:36:34 +00006652
6653 if( ret != 0 )
6654 {
6655 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6656 }
6657
6658#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6659 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6660 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6661#endif
6662
6663 /*
6664 * Secondary checks: always done, but change 'ret' only if it was 0
6665 */
6666
6667#if defined(MBEDTLS_ECP_C)
6668 {
6669 const mbedtls_pk_context *pk = &chain->pk;
6670
6671 /* If certificate uses an EC key, make sure the curve is OK */
6672 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6673 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6674 {
6675 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6676
6677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6678 if( ret == 0 )
6679 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6680 }
6681 }
6682#endif /* MBEDTLS_ECP_C */
6683
6684 if( mbedtls_ssl_check_cert_usage( chain,
6685 ciphersuite_info,
6686 ! ssl->conf->endpoint,
6687 &ssl->session_negotiate->verify_result ) != 0 )
6688 {
6689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6690 if( ret == 0 )
6691 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6692 }
6693
6694 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6695 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6696 * with details encoded in the verification flags. All other kinds
6697 * of error codes, including those from the user provided f_vrfy
6698 * functions, are treated as fatal and lead to a failure of
6699 * ssl_parse_certificate even if verification was optional. */
6700 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6701 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6702 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6703 {
6704 ret = 0;
6705 }
6706
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006707 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006708 {
6709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6710 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6711 }
6712
6713 if( ret != 0 )
6714 {
6715 uint8_t alert;
6716
6717 /* The certificate may have been rejected for several reasons.
6718 Pick one and send the corresponding alert. Which alert to send
6719 may be a subject of debate in some cases. */
6720 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6721 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6722 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6723 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6724 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6725 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6726 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6727 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6728 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6729 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6730 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6731 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6732 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6733 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6734 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6735 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6736 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6737 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6738 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6739 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6740 else
6741 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6742 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6743 alert );
6744 }
6745
6746#if defined(MBEDTLS_DEBUG_C)
6747 if( ssl->session_negotiate->verify_result != 0 )
6748 {
6749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6750 ssl->session_negotiate->verify_result ) );
6751 }
6752 else
6753 {
6754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6755 }
6756#endif /* MBEDTLS_DEBUG_C */
6757
6758 return( ret );
6759}
6760
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006761#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6762static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6763 unsigned char *start, size_t len )
6764{
6765 int ret;
6766 /* Remember digest of the peer's end-CRT. */
6767 ssl->session_negotiate->peer_cert_digest =
6768 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6769 if( ssl->session_negotiate->peer_cert_digest == NULL )
6770 {
6771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6772 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6773 mbedtls_ssl_send_alert_message( ssl,
6774 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6775 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6776
6777 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6778 }
6779
6780 ret = mbedtls_md( mbedtls_md_info_from_type(
6781 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6782 start, len,
6783 ssl->session_negotiate->peer_cert_digest );
6784
6785 ssl->session_negotiate->peer_cert_digest_type =
6786 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6787 ssl->session_negotiate->peer_cert_digest_len =
6788 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6789
6790 return( ret );
6791}
6792
6793static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6794 unsigned char *start, size_t len )
6795{
6796 unsigned char *end = start + len;
6797 int ret;
6798
6799 /* Make a copy of the peer's raw public key. */
6800 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6801 ret = mbedtls_pk_parse_subpubkey( &start, end,
6802 &ssl->handshake->peer_pubkey );
6803 if( ret != 0 )
6804 {
6805 /* We should have parsed the public key before. */
6806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6807 }
6808
6809 return( 0 );
6810}
6811#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6812
Hanno Becker68636192019-02-05 14:36:34 +00006813int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6814{
6815 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006816 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006817#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6818 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6819 ? ssl->handshake->sni_authmode
6820 : ssl->conf->authmode;
6821#else
6822 const int authmode = ssl->conf->authmode;
6823#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006824 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006825 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006826
6827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6828
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006829 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6830 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006831 {
6832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006833 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006834 }
6835
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006836#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6837 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006838 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006839 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006840 chain = ssl->handshake->ecrs_peer_cert;
6841 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006842 goto crt_verify;
6843 }
6844#endif
6845
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006846 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006847 {
6848 /* mbedtls_ssl_read_record may have sent an alert already. We
6849 let it decide whether to alert. */
6850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006851 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006852 }
6853
Hanno Becker4a55f632019-02-05 12:49:06 +00006854#if defined(MBEDTLS_SSL_SRV_C)
6855 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6856 {
6857 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006858
6859 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006860 ret = 0;
6861 else
6862 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006863
Hanno Becker6bdfab22019-02-05 13:11:17 +00006864 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006865 }
6866#endif /* MBEDTLS_SSL_SRV_C */
6867
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006868 /* Clear existing peer CRT structure in case we tried to
6869 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006870 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006871
6872 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6873 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006874 {
6875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6876 sizeof( mbedtls_x509_crt ) ) );
6877 mbedtls_ssl_send_alert_message( ssl,
6878 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6879 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006880
Hanno Becker3dad3112019-02-05 17:19:52 +00006881 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6882 goto exit;
6883 }
6884 mbedtls_x509_crt_init( chain );
6885
6886 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006887 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006888 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006889
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006890#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6891 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006892 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006893
6894crt_verify:
6895 if( ssl->handshake->ecrs_enabled)
6896 rs_ctx = &ssl->handshake->ecrs_ctx;
6897#endif
6898
Hanno Becker68636192019-02-05 14:36:34 +00006899 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006900 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006901 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006902 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006903
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006904#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006905 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006906 unsigned char *crt_start, *pk_start;
6907 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006908
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006909 /* We parse the CRT chain without copying, so
6910 * these pointers point into the input buffer,
6911 * and are hence still valid after freeing the
6912 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006913
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006914 crt_start = chain->raw.p;
6915 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006916
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006917 pk_start = chain->pk_raw.p;
6918 pk_len = chain->pk_raw.len;
6919
6920 /* Free the CRT structures before computing
6921 * digest and copying the peer's public key. */
6922 mbedtls_x509_crt_free( chain );
6923 mbedtls_free( chain );
6924 chain = NULL;
6925
6926 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006927 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006928 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006929
6930 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6931 if( ret != 0 )
6932 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006933 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006934#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6935 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006936 ssl->session_negotiate->peer_cert = chain;
6937 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006938#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006941
Hanno Becker6bdfab22019-02-05 13:11:17 +00006942exit:
6943
Hanno Becker3dad3112019-02-05 17:19:52 +00006944 if( ret == 0 )
6945 ssl->state++;
6946
6947#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6948 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6949 {
6950 ssl->handshake->ecrs_peer_cert = chain;
6951 chain = NULL;
6952 }
6953#endif
6954
6955 if( chain != NULL )
6956 {
6957 mbedtls_x509_crt_free( chain );
6958 mbedtls_free( chain );
6959 }
6960
Paul Bakker5121ce52009-01-03 21:22:43 +00006961 return( ret );
6962}
Hanno Becker21489932019-02-05 13:20:55 +00006963#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006966{
6967 int ret;
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006972 ssl->out_msglen = 1;
6973 ssl->out_msg[0] = 1;
6974
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 ssl->state++;
6976
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006977 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006978 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 return( ret );
6981 }
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006984
6985 return( 0 );
6986}
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006989{
6990 int ret;
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006993
Hanno Becker327c93b2018-08-15 13:56:18 +01006994 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997 return( ret );
6998 }
6999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007003 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7004 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007006 }
7007
Hanno Beckere678eaa2018-08-21 14:57:46 +01007008 /* CCS records are only accepted if they have length 1 and content '1',
7009 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007010
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007011 /*
7012 * Switch to our negotiated transform and session parameters for inbound
7013 * data.
7014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007016 ssl->transform_in = ssl->transform_negotiate;
7017 ssl->session_in = ssl->session_negotiate;
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007020 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007023 ssl_dtls_replay_reset( ssl );
7024#endif
7025
7026 /* Increment epoch */
7027 if( ++ssl->in_epoch == 0 )
7028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007030 /* This is highly unlikely to happen for legitimate reasons, so
7031 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007033 }
7034 }
7035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007037 memset( ssl->in_ctr, 0, 8 );
7038
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007039 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7042 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007047 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7048 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007050 }
7051 }
7052#endif
7053
Paul Bakker5121ce52009-01-03 21:22:43 +00007054 ssl->state++;
7055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
7058 return( 0 );
7059}
7060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7062 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007063{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007064 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7067 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7068 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007069 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007070 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7073#if defined(MBEDTLS_SHA512_C)
7074 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007075 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7076 else
7077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078#if defined(MBEDTLS_SHA256_C)
7079 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007080 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007081 else
7082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007086 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007087 }
Paul Bakker380da532012-04-18 16:10:25 +00007088}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7093 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007094 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7095 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7098#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007100 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007101 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7102#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007103 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007104#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007107#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007108 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007109 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007110#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007111 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007112#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007115}
7116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007118 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7121 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007122 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7123 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7126#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007127#if defined(MBEDTLS_USE_PSA_CRYPTO)
7128 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7129#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007130 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007131#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007134#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007135 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007136#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007137 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007138#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007141}
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7144 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7145static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007146 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007147{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007148 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7149 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007150}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007151#endif
Paul Bakker380da532012-04-18 16:10:25 +00007152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7154#if defined(MBEDTLS_SHA256_C)
7155static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007156 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007157{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007158#if defined(MBEDTLS_USE_PSA_CRYPTO)
7159 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7160#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007161 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007162#endif
Paul Bakker380da532012-04-18 16:10:25 +00007163}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007164#endif
Paul Bakker380da532012-04-18 16:10:25 +00007165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#if defined(MBEDTLS_SHA512_C)
7167static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007168 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007169{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007170#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007171 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007172#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007173 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007174#endif
Paul Bakker380da532012-04-18 16:10:25 +00007175}
Paul Bakker769075d2012-11-24 11:26:46 +01007176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007180static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007182{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007183 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007184 mbedtls_md5_context md5;
7185 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007186
Paul Bakker5121ce52009-01-03 21:22:43 +00007187 unsigned char padbuf[48];
7188 unsigned char md5sum[16];
7189 unsigned char sha1sum[20];
7190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007192 if( !session )
7193 session = ssl->session;
7194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007197 mbedtls_md5_init( &md5 );
7198 mbedtls_sha1_init( &sha1 );
7199
7200 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7201 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007202
7203 /*
7204 * SSLv3:
7205 * hash =
7206 * MD5( master + pad2 +
7207 * MD5( handshake + sender + master + pad1 ) )
7208 * + SHA1( master + pad2 +
7209 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007210 */
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007213 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7214 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007215#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007218 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7219 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007223 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007224
Paul Bakker1ef83d62012-04-11 12:09:53 +00007225 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007226
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007227 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7228 mbedtls_md5_update_ret( &md5, session->master, 48 );
7229 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7230 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007231
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007232 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7233 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7234 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7235 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007236
Paul Bakker1ef83d62012-04-11 12:09:53 +00007237 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007238
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007239 mbedtls_md5_starts_ret( &md5 );
7240 mbedtls_md5_update_ret( &md5, session->master, 48 );
7241 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7242 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7243 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007244
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007245 mbedtls_sha1_starts_ret( &sha1 );
7246 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7247 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7248 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7249 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007252
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007253 mbedtls_md5_free( &md5 );
7254 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007255
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007256 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7257 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7258 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007265static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007267{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007268 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007269 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007270 mbedtls_md5_context md5;
7271 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007272 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007275 if( !session )
7276 session = ssl->session;
7277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007279
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007280 mbedtls_md5_init( &md5 );
7281 mbedtls_sha1_init( &sha1 );
7282
7283 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7284 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007285
Paul Bakker1ef83d62012-04-11 12:09:53 +00007286 /*
7287 * TLSv1:
7288 * hash = PRF( master, finished_label,
7289 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7290 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007293 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7294 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007295#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007298 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7299 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007300#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007303 ? "client finished"
7304 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007305
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007306 mbedtls_md5_finish_ret( &md5, padbuf );
7307 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007308
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007309 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007310 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007313
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007314 mbedtls_md5_free( &md5 );
7315 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007316
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007317 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007320}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7324#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007325static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007327{
7328 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007329 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007330 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007331#if defined(MBEDTLS_USE_PSA_CRYPTO)
7332 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007333 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007334 psa_status_t status;
7335#else
7336 mbedtls_sha256_context sha256;
7337#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007340 if( !session )
7341 session = ssl->session;
7342
Andrzej Kurekeb342242019-01-29 09:14:33 -05007343 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7344 ? "client finished"
7345 : "server finished";
7346
7347#if defined(MBEDTLS_USE_PSA_CRYPTO)
7348 sha256_psa = psa_hash_operation_init();
7349
7350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7351
7352 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7353 if( status != PSA_SUCCESS )
7354 {
7355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7356 return;
7357 }
7358
7359 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7360 if( status != PSA_SUCCESS )
7361 {
7362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7363 return;
7364 }
7365 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7366#else
7367
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007368 mbedtls_sha256_init( &sha256 );
7369
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007371
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007372 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007373
7374 /*
7375 * TLSv1.2:
7376 * hash = PRF( master, finished_label,
7377 * Hash( handshake ) )[0.11]
7378 */
7379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if !defined(MBEDTLS_SHA256_ALT)
7381 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007382 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007383#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007384
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007385 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007386 mbedtls_sha256_free( &sha256 );
7387#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007388
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007389 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007390 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007393
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007394 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007401static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007403{
7404 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007405 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007406 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007407#if defined(MBEDTLS_USE_PSA_CRYPTO)
7408 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007409 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007410 psa_status_t status;
7411#else
7412 mbedtls_sha512_context sha512;
7413#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007416 if( !session )
7417 session = ssl->session;
7418
Andrzej Kurekeb342242019-01-29 09:14:33 -05007419 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7420 ? "client finished"
7421 : "server finished";
7422
7423#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007424 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007425
7426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7427
Andrzej Kurek972fba52019-01-30 03:29:12 -05007428 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007429 if( status != PSA_SUCCESS )
7430 {
7431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7432 return;
7433 }
7434
Andrzej Kurek972fba52019-01-30 03:29:12 -05007435 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007436 if( status != PSA_SUCCESS )
7437 {
7438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7439 return;
7440 }
7441 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7442#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007443 mbedtls_sha512_init( &sha512 );
7444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007446
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007447 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007448
7449 /*
7450 * TLSv1.2:
7451 * hash = PRF( master, finished_label,
7452 * Hash( handshake ) )[0.11]
7453 */
7454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007456 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7457 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007458#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007459
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007460 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007461 mbedtls_sha512_free( &sha512 );
7462#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007463
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007464 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007465 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007468
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007469 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473#endif /* MBEDTLS_SHA512_C */
7474#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007479
7480 /*
7481 * Free our handshake params
7482 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007483 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007485 ssl->handshake = NULL;
7486
7487 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007488 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007489 */
7490 if( ssl->transform )
7491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 mbedtls_ssl_transform_free( ssl->transform );
7493 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007494 }
7495 ssl->transform = ssl->transform_negotiate;
7496 ssl->transform_negotiate = NULL;
7497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007499}
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007502{
7503 int resume = ssl->handshake->resume;
7504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507#if defined(MBEDTLS_SSL_RENEGOTIATION)
7508 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007511 ssl->renego_records_seen = 0;
7512 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007513#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007514
7515 /*
7516 * Free the previous session and switch in the current one
7517 */
Paul Bakker0a597072012-09-25 21:55:46 +00007518 if( ssl->session )
7519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007521 /* RFC 7366 3.1: keep the EtM state */
7522 ssl->session_negotiate->encrypt_then_mac =
7523 ssl->session->encrypt_then_mac;
7524#endif
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 mbedtls_ssl_session_free( ssl->session );
7527 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007528 }
7529 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007530 ssl->session_negotiate = NULL;
7531
Paul Bakker0a597072012-09-25 21:55:46 +00007532 /*
7533 * Add cache entry
7534 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007535 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007536 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007537 resume == 0 )
7538 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007539 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007541 }
Paul Bakker0a597072012-09-25 21:55:46 +00007542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007544 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007545 ssl->handshake->flight != NULL )
7546 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007547 /* Cancel handshake timer */
7548 ssl_set_timer( ssl, 0 );
7549
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007550 /* Keep last flight around in case we need to resend it:
7551 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007553 }
7554 else
7555#endif
7556 ssl_handshake_wrapup_free_hs_transform( ssl );
7557
Paul Bakker48916f92012-09-16 19:57:18 +00007558 ssl->state++;
7559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007561}
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007564{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007565 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007568
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007569 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007570
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007571 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007572
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007573 /*
7574 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7575 * may define some other value. Currently (early 2016), no defined
7576 * ciphersuite does this (and this is unlikely to change as activity has
7577 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007582 ssl->verify_data_len = hash_len;
7583 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007584#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007585
Paul Bakker5121ce52009-01-03 21:22:43 +00007586 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7588 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007589
7590 /*
7591 * In case of session resuming, invert the client and server
7592 * ChangeCipherSpec messages order.
7593 */
Paul Bakker0a597072012-09-25 21:55:46 +00007594 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007597 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007599#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007601 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007603#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007604 }
7605 else
7606 ssl->state++;
7607
Paul Bakker48916f92012-09-16 19:57:18 +00007608 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007609 * Switch to our negotiated transform and session parameters for outbound
7610 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007616 {
7617 unsigned char i;
7618
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007619 /* Remember current epoch settings for resending */
7620 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007621 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007622
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007623 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007624 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007625
7626 /* Increment epoch */
7627 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007628 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007629 break;
7630
7631 /* The loop goes to its end iff the counter is wrapping */
7632 if( i == 0 )
7633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7635 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007636 }
7637 }
7638 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007640 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007641
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007642 ssl->transform_out = ssl->transform_negotiate;
7643 ssl->session_out = ssl->session_negotiate;
7644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7646 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7651 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007652 }
7653 }
7654#endif
7655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007657 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007659#endif
7660
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007661 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007662 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007664 return( ret );
7665 }
7666
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007667#if defined(MBEDTLS_SSL_PROTO_DTLS)
7668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7669 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7670 {
7671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7672 return( ret );
7673 }
7674#endif
7675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007677
7678 return( 0 );
7679}
7680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007682#define SSL_MAX_HASH_LEN 36
7683#else
7684#define SSL_MAX_HASH_LEN 12
7685#endif
7686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007688{
Paul Bakker23986e52011-04-24 08:57:21 +00007689 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007690 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007691 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007694
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007695 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007696
Hanno Becker327c93b2018-08-15 13:56:18 +01007697 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007700 return( ret );
7701 }
7702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007706 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7707 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007709 }
7710
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007711 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712#if defined(MBEDTLS_SSL_PROTO_SSL3)
7713 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007714 hash_len = 36;
7715 else
7716#endif
7717 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7720 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007723 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7724 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 }
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007729 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7733 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 }
7736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007738 ssl->verify_data_len = hash_len;
7739 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007740#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007741
Paul Bakker0a597072012-09-25 21:55:46 +00007742 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007745 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007749 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007751#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 }
7753 else
7754 ssl->state++;
7755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007757 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007759#endif
7760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007762
7763 return( 0 );
7764}
7765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7771 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7772 mbedtls_md5_init( &handshake->fin_md5 );
7773 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007774 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7775 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7778#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007779#if defined(MBEDTLS_USE_PSA_CRYPTO)
7780 handshake->fin_sha256_psa = psa_hash_operation_init();
7781 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7782#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007784 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007785#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007788#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007789 handshake->fin_sha384_psa = psa_hash_operation_init();
7790 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007791#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007793 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007794#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007797
7798 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007799
7800#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7801 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7802 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7803#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_DHM_C)
7806 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007807#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808#if defined(MBEDTLS_ECDH_C)
7809 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007810#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007811#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007812 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007813#if defined(MBEDTLS_SSL_CLI_C)
7814 handshake->ecjpake_cache = NULL;
7815 handshake->ecjpake_cache_len = 0;
7816#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007817#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007818
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007819#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007820 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007821#endif
7822
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007823#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7824 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7825#endif
Hanno Becker75173122019-02-06 16:18:31 +00007826
7827#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7828 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7829 mbedtls_pk_init( &handshake->peer_pubkey );
7830#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007831}
7832
Hanno Beckera18d1322018-01-03 14:27:32 +00007833void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7838 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007839
Hanno Beckerd56ed242018-01-03 15:32:51 +00007840#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 mbedtls_md_init( &transform->md_ctx_enc );
7842 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007843#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007844}
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007849}
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007852{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007853 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007854 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007856 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007858 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007859 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007860
7861 /*
7862 * Either the pointers are now NULL or cleared properly and can be freed.
7863 * Now allocate missing structures.
7864 */
7865 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007866 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007867 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007868 }
Paul Bakker48916f92012-09-16 19:57:18 +00007869
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007870 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007871 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007872 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007873 }
Paul Bakker48916f92012-09-16 19:57:18 +00007874
Paul Bakker82788fb2014-10-20 13:59:19 +02007875 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007876 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007877 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007878 }
Paul Bakker48916f92012-09-16 19:57:18 +00007879
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007880 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007881 if( ssl->handshake == NULL ||
7882 ssl->transform_negotiate == NULL ||
7883 ssl->session_negotiate == NULL )
7884 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 mbedtls_free( ssl->handshake );
7888 mbedtls_free( ssl->transform_negotiate );
7889 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007890
7891 ssl->handshake = NULL;
7892 ssl->transform_negotiate = NULL;
7893 ssl->session_negotiate = NULL;
7894
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007895 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007896 }
7897
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007898 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007900 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007901 ssl_handshake_params_init( ssl->handshake );
7902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007904 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7905 {
7906 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007907
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007908 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7909 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7910 else
7911 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007912
7913 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007914 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007915#endif
7916
Paul Bakker48916f92012-09-16 19:57:18 +00007917 return( 0 );
7918}
7919
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007920#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007921/* Dummy cookie callbacks for defaults */
7922static int ssl_cookie_write_dummy( void *ctx,
7923 unsigned char **p, unsigned char *end,
7924 const unsigned char *cli_id, size_t cli_id_len )
7925{
7926 ((void) ctx);
7927 ((void) p);
7928 ((void) end);
7929 ((void) cli_id);
7930 ((void) cli_id_len);
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007933}
7934
7935static int ssl_cookie_check_dummy( void *ctx,
7936 const unsigned char *cookie, size_t cookie_len,
7937 const unsigned char *cli_id, size_t cli_id_len )
7938{
7939 ((void) ctx);
7940 ((void) cookie);
7941 ((void) cookie_len);
7942 ((void) cli_id);
7943 ((void) cli_id_len);
7944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007946}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007947#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007948
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007949/* Once ssl->out_hdr as the address of the beginning of the
7950 * next outgoing record is set, deduce the other pointers.
7951 *
7952 * Note: For TLS, we save the implicit record sequence number
7953 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7954 * and the caller has to make sure there's space for this.
7955 */
7956
7957static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7958 mbedtls_ssl_transform *transform )
7959{
7960#if defined(MBEDTLS_SSL_PROTO_DTLS)
7961 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7962 {
7963 ssl->out_ctr = ssl->out_hdr + 3;
7964 ssl->out_len = ssl->out_hdr + 11;
7965 ssl->out_iv = ssl->out_hdr + 13;
7966 }
7967 else
7968#endif
7969 {
7970 ssl->out_ctr = ssl->out_hdr - 8;
7971 ssl->out_len = ssl->out_hdr + 3;
7972 ssl->out_iv = ssl->out_hdr + 5;
7973 }
7974
7975 /* Adjust out_msg to make space for explicit IV, if used. */
7976 if( transform != NULL &&
7977 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7978 {
7979 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7980 }
7981 else
7982 ssl->out_msg = ssl->out_iv;
7983}
7984
7985/* Once ssl->in_hdr as the address of the beginning of the
7986 * next incoming record is set, deduce the other pointers.
7987 *
7988 * Note: For TLS, we save the implicit record sequence number
7989 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7990 * and the caller has to make sure there's space for this.
7991 */
7992
7993static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7994 mbedtls_ssl_transform *transform )
7995{
7996#if defined(MBEDTLS_SSL_PROTO_DTLS)
7997 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7998 {
7999 ssl->in_ctr = ssl->in_hdr + 3;
8000 ssl->in_len = ssl->in_hdr + 11;
8001 ssl->in_iv = ssl->in_hdr + 13;
8002 }
8003 else
8004#endif
8005 {
8006 ssl->in_ctr = ssl->in_hdr - 8;
8007 ssl->in_len = ssl->in_hdr + 3;
8008 ssl->in_iv = ssl->in_hdr + 5;
8009 }
8010
8011 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
8012 if( transform != NULL &&
8013 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8014 {
8015 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
8016 }
8017 else
8018 ssl->in_msg = ssl->in_iv;
8019}
8020
Paul Bakker5121ce52009-01-03 21:22:43 +00008021/*
8022 * Initialize an SSL context
8023 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008024void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8025{
8026 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8027}
8028
8029/*
8030 * Setup an SSL context
8031 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008032
8033static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8034{
8035 /* Set the incoming and outgoing record pointers. */
8036#if defined(MBEDTLS_SSL_PROTO_DTLS)
8037 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8038 {
8039 ssl->out_hdr = ssl->out_buf;
8040 ssl->in_hdr = ssl->in_buf;
8041 }
8042 else
8043#endif /* MBEDTLS_SSL_PROTO_DTLS */
8044 {
8045 ssl->out_hdr = ssl->out_buf + 8;
8046 ssl->in_hdr = ssl->in_buf + 8;
8047 }
8048
8049 /* Derive other internal pointers. */
8050 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
8051 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
8052}
8053
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008054int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008055 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008056{
Paul Bakker48916f92012-09-16 19:57:18 +00008057 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008058
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008059 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008060
8061 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008062 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008063 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008064
8065 /* Set to NULL in case of an error condition */
8066 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008067
Angus Grattond8213d02016-05-25 20:56:48 +10008068 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8069 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008070 {
Angus Grattond8213d02016-05-25 20:56:48 +10008071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008072 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008073 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008074 }
8075
8076 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8077 if( ssl->out_buf == NULL )
8078 {
8079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008080 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008081 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008082 }
8083
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008084 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008085
Paul Bakker48916f92012-09-16 19:57:18 +00008086 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008087 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008088
8089 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008090
8091error:
8092 mbedtls_free( ssl->in_buf );
8093 mbedtls_free( ssl->out_buf );
8094
8095 ssl->conf = NULL;
8096
8097 ssl->in_buf = NULL;
8098 ssl->out_buf = NULL;
8099
8100 ssl->in_hdr = NULL;
8101 ssl->in_ctr = NULL;
8102 ssl->in_len = NULL;
8103 ssl->in_iv = NULL;
8104 ssl->in_msg = NULL;
8105
8106 ssl->out_hdr = NULL;
8107 ssl->out_ctr = NULL;
8108 ssl->out_len = NULL;
8109 ssl->out_iv = NULL;
8110 ssl->out_msg = NULL;
8111
k-stachowiak9f7798e2018-07-31 16:52:32 +02008112 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008113}
8114
8115/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008116 * Reset an initialized and used SSL context for re-use while retaining
8117 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008118 *
8119 * If partial is non-zero, keep data in the input buffer and client ID.
8120 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008121 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008122static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008123{
Paul Bakker48916f92012-09-16 19:57:18 +00008124 int ret;
8125
Hanno Becker7e772132018-08-10 12:38:21 +01008126#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8127 !defined(MBEDTLS_SSL_SRV_C)
8128 ((void) partial);
8129#endif
8130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008132
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008133 /* Cancel any possibly running timer */
8134 ssl_set_timer( ssl, 0 );
8135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136#if defined(MBEDTLS_SSL_RENEGOTIATION)
8137 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008138 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008139
8140 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8142 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008145
Paul Bakker7eb013f2011-10-06 12:37:39 +00008146 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008147 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008148
8149 ssl->in_msgtype = 0;
8150 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008152 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008153 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008156 ssl_dtls_replay_reset( ssl );
8157#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008158
8159 ssl->in_hslen = 0;
8160 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008161
8162 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008163
8164 ssl->out_msgtype = 0;
8165 ssl->out_msglen = 0;
8166 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8168 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008169 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008170#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008171
Hanno Becker19859472018-08-06 09:40:20 +01008172 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8173
Paul Bakker48916f92012-09-16 19:57:18 +00008174 ssl->transform_in = NULL;
8175 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008176
Hanno Becker78640902018-08-13 16:35:15 +01008177 ssl->session_in = NULL;
8178 ssl->session_out = NULL;
8179
Angus Grattond8213d02016-05-25 20:56:48 +10008180 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008181
8182#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008183 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008184#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8185 {
8186 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008187 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008188 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8191 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8194 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8197 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008198 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008199 }
8200#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008201
Paul Bakker48916f92012-09-16 19:57:18 +00008202 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204 mbedtls_ssl_transform_free( ssl->transform );
8205 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008206 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008207 }
Paul Bakker48916f92012-09-16 19:57:18 +00008208
Paul Bakkerc0463502013-02-14 11:19:38 +01008209 if( ssl->session )
8210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211 mbedtls_ssl_session_free( ssl->session );
8212 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008213 ssl->session = NULL;
8214 }
8215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008217 ssl->alpn_chosen = NULL;
8218#endif
8219
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008220#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008221#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008222 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008223#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008224 {
8225 mbedtls_free( ssl->cli_id );
8226 ssl->cli_id = NULL;
8227 ssl->cli_id_len = 0;
8228 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008229#endif
8230
Paul Bakker48916f92012-09-16 19:57:18 +00008231 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8232 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008233
8234 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008235}
8236
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008237/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008238 * Reset an initialized and used SSL context for re-use while retaining
8239 * all application-set variables, function pointers and data.
8240 */
8241int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8242{
8243 return( ssl_session_reset_int( ssl, 0 ) );
8244}
8245
8246/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008247 * SSL set accessors
8248 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008249void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008250{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008251 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008252}
8253
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008254void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008256 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008257}
8258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008260void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008261{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008262 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008263}
8264#endif
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008267void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008268{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008269 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008270}
8271#endif
8272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008274
Hanno Becker1841b0a2018-08-24 11:13:57 +01008275void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8276 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008277{
8278 ssl->disable_datagram_packing = !allow_packing;
8279}
8280
8281void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8282 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008283{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008284 conf->hs_timeout_min = min;
8285 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008286}
8287#endif
8288
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008289void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008290{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008291 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008292}
8293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008295void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008296 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008297 void *p_vrfy )
8298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008299 conf->f_vrfy = f_vrfy;
8300 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008303
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008304void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008305 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008306 void *p_rng )
8307{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008308 conf->f_rng = f_rng;
8309 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008310}
8311
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008312void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008313 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008314 void *p_dbg )
8315{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008316 conf->f_dbg = f_dbg;
8317 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008318}
8319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008321 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008322 mbedtls_ssl_send_t *f_send,
8323 mbedtls_ssl_recv_t *f_recv,
8324 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008325{
8326 ssl->p_bio = p_bio;
8327 ssl->f_send = f_send;
8328 ssl->f_recv = f_recv;
8329 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008330}
8331
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008332#if defined(MBEDTLS_SSL_PROTO_DTLS)
8333void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8334{
8335 ssl->mtu = mtu;
8336}
8337#endif
8338
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008339void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008340{
8341 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008342}
8343
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008344void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8345 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008346 mbedtls_ssl_set_timer_t *f_set_timer,
8347 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008348{
8349 ssl->p_timer = p_timer;
8350 ssl->f_set_timer = f_set_timer;
8351 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008352
8353 /* Make sure we start with no timer running */
8354 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008355}
8356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008358void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008359 void *p_cache,
8360 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8361 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008362{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008363 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008364 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008365 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#if defined(MBEDTLS_SSL_CLI_C)
8370int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008371{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008372 int ret;
8373
8374 if( ssl == NULL ||
8375 session == NULL ||
8376 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008377 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008380 }
8381
Hanno Becker52055ae2019-02-06 14:30:46 +00008382 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8383 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008384 return( ret );
8385
Paul Bakker0a597072012-09-25 21:55:46 +00008386 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008387
8388 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008391
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008392void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008393 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008395 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8396 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8397 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8398 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008399}
8400
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008401void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008402 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008403 int major, int minor )
8404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008406 return;
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008409 return;
8410
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008411 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008412}
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008415void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008416 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008417{
8418 conf->cert_profile = profile;
8419}
8420
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008421/* Append a new keycert entry to a (possibly empty) list */
8422static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8423 mbedtls_x509_crt *cert,
8424 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008425{
niisato8ee24222018-06-25 19:05:48 +09008426 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008427
niisato8ee24222018-06-25 19:05:48 +09008428 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8429 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008430 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008431
niisato8ee24222018-06-25 19:05:48 +09008432 new_cert->cert = cert;
8433 new_cert->key = key;
8434 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008435
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008436 /* Update head is the list was null, else add to the end */
8437 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008438 {
niisato8ee24222018-06-25 19:05:48 +09008439 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008440 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008441 else
8442 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008443 mbedtls_ssl_key_cert *cur = *head;
8444 while( cur->next != NULL )
8445 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008446 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008447 }
8448
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008449 return( 0 );
8450}
8451
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008452int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008453 mbedtls_x509_crt *own_cert,
8454 mbedtls_pk_context *pk_key )
8455{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008456 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008457}
8458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008459void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008460 mbedtls_x509_crt *ca_chain,
8461 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008462{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008463 conf->ca_chain = ca_chain;
8464 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008465
8466#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8467 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8468 * cannot be used together. */
8469 conf->f_ca_cb = NULL;
8470 conf->p_ca_cb = NULL;
8471#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008472}
Hanno Becker5adaad92019-03-27 16:54:37 +00008473
8474#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8475void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008476 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008477 void *p_ca_cb )
8478{
8479 conf->f_ca_cb = f_ca_cb;
8480 conf->p_ca_cb = p_ca_cb;
8481
8482 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8483 * cannot be used together. */
8484 conf->ca_chain = NULL;
8485 conf->ca_crl = NULL;
8486}
8487#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008489
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008490#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8491int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8492 mbedtls_x509_crt *own_cert,
8493 mbedtls_pk_context *pk_key )
8494{
8495 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8496 own_cert, pk_key ) );
8497}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008498
8499void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8500 mbedtls_x509_crt *ca_chain,
8501 mbedtls_x509_crl *ca_crl )
8502{
8503 ssl->handshake->sni_ca_chain = ca_chain;
8504 ssl->handshake->sni_ca_crl = ca_crl;
8505}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008506
8507void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8508 int authmode )
8509{
8510 ssl->handshake->sni_authmode = authmode;
8511}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008512#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8513
Hanno Becker8927c832019-04-03 12:52:50 +01008514#if defined(MBEDTLS_X509_CRT_PARSE_C)
8515void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8516 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8517 void *p_vrfy )
8518{
8519 ssl->f_vrfy = f_vrfy;
8520 ssl->p_vrfy = p_vrfy;
8521}
8522#endif
8523
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008524#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008525/*
8526 * Set EC J-PAKE password for current handshake
8527 */
8528int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8529 const unsigned char *pw,
8530 size_t pw_len )
8531{
8532 mbedtls_ecjpake_role role;
8533
Janos Follath8eb64132016-06-03 15:40:57 +01008534 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8536
8537 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8538 role = MBEDTLS_ECJPAKE_SERVER;
8539 else
8540 role = MBEDTLS_ECJPAKE_CLIENT;
8541
8542 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8543 role,
8544 MBEDTLS_MD_SHA256,
8545 MBEDTLS_ECP_DP_SECP256R1,
8546 pw, pw_len ) );
8547}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008548#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008551
8552static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8553{
8554 /* Remove reference to existing PSK, if any. */
8555#if defined(MBEDTLS_USE_PSA_CRYPTO)
8556 if( conf->psk_opaque != 0 )
8557 {
8558 /* The maintenance of the PSK key slot is the
8559 * user's responsibility. */
8560 conf->psk_opaque = 0;
8561 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008562 /* This and the following branch should never
8563 * be taken simultaenously as we maintain the
8564 * invariant that raw and opaque PSKs are never
8565 * configured simultaneously. As a safeguard,
8566 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008567#endif /* MBEDTLS_USE_PSA_CRYPTO */
8568 if( conf->psk != NULL )
8569 {
8570 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8571
8572 mbedtls_free( conf->psk );
8573 conf->psk = NULL;
8574 conf->psk_len = 0;
8575 }
8576
8577 /* Remove reference to PSK identity, if any. */
8578 if( conf->psk_identity != NULL )
8579 {
8580 mbedtls_free( conf->psk_identity );
8581 conf->psk_identity = NULL;
8582 conf->psk_identity_len = 0;
8583 }
8584}
8585
Hanno Becker7390c712018-11-15 13:33:04 +00008586/* This function assumes that PSK identity in the SSL config is unset.
8587 * It checks that the provided identity is well-formed and attempts
8588 * to make a copy of it in the SSL config.
8589 * On failure, the PSK identity in the config remains unset. */
8590static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8591 unsigned char const *psk_identity,
8592 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008593{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008594 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008595 if( psk_identity == NULL ||
8596 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008597 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008598 {
8599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8600 }
8601
Hanno Becker7390c712018-11-15 13:33:04 +00008602 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8603 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008604 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008605
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008606 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008607 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008608
8609 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008610}
8611
Hanno Becker7390c712018-11-15 13:33:04 +00008612int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8613 const unsigned char *psk, size_t psk_len,
8614 const unsigned char *psk_identity, size_t psk_identity_len )
8615{
8616 int ret;
8617 /* Remove opaque/raw PSK + PSK Identity */
8618 ssl_conf_remove_psk( conf );
8619
8620 /* Check and set raw PSK */
8621 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8623 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8624 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8625 conf->psk_len = psk_len;
8626 memcpy( conf->psk, psk, conf->psk_len );
8627
8628 /* Check and set PSK Identity */
8629 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8630 if( ret != 0 )
8631 ssl_conf_remove_psk( conf );
8632
8633 return( ret );
8634}
8635
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008636static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8637{
8638#if defined(MBEDTLS_USE_PSA_CRYPTO)
8639 if( ssl->handshake->psk_opaque != 0 )
8640 {
8641 ssl->handshake->psk_opaque = 0;
8642 }
8643 else
8644#endif /* MBEDTLS_USE_PSA_CRYPTO */
8645 if( ssl->handshake->psk != NULL )
8646 {
8647 mbedtls_platform_zeroize( ssl->handshake->psk,
8648 ssl->handshake->psk_len );
8649 mbedtls_free( ssl->handshake->psk );
8650 ssl->handshake->psk_len = 0;
8651 }
8652}
8653
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008654int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8655 const unsigned char *psk, size_t psk_len )
8656{
8657 if( psk == NULL || ssl->handshake == NULL )
8658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8659
8660 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8662
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008663 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008664
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008665 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008666 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008667
8668 ssl->handshake->psk_len = psk_len;
8669 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8670
8671 return( 0 );
8672}
8673
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008674#if defined(MBEDTLS_USE_PSA_CRYPTO)
8675int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008676 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008677 const unsigned char *psk_identity,
8678 size_t psk_identity_len )
8679{
Hanno Becker7390c712018-11-15 13:33:04 +00008680 int ret;
8681 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008682 ssl_conf_remove_psk( conf );
8683
Hanno Becker7390c712018-11-15 13:33:04 +00008684 /* Check and set opaque PSK */
8685 if( psk_slot == 0 )
8686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008687 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008688
8689 /* Check and set PSK Identity */
8690 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8691 psk_identity_len );
8692 if( ret != 0 )
8693 ssl_conf_remove_psk( conf );
8694
8695 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008696}
8697
8698int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008699 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008700{
8701 if( psk_slot == 0 || ssl->handshake == NULL )
8702 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8703
8704 ssl_remove_psk( ssl );
8705 ssl->handshake->psk_opaque = psk_slot;
8706 return( 0 );
8707}
8708#endif /* MBEDTLS_USE_PSA_CRYPTO */
8709
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008710void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008712 size_t),
8713 void *p_psk )
8714{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008715 conf->f_psk = f_psk;
8716 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008719
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008720#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008721
8722#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008723int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008724{
8725 int ret;
8726
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008727 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8728 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8729 {
8730 mbedtls_mpi_free( &conf->dhm_P );
8731 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008732 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008733 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008734
8735 return( 0 );
8736}
Hanno Becker470a8c42017-10-04 15:28:46 +01008737#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008738
Hanno Beckera90658f2017-10-04 15:29:08 +01008739int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8740 const unsigned char *dhm_P, size_t P_len,
8741 const unsigned char *dhm_G, size_t G_len )
8742{
8743 int ret;
8744
8745 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8746 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8747 {
8748 mbedtls_mpi_free( &conf->dhm_P );
8749 mbedtls_mpi_free( &conf->dhm_G );
8750 return( ret );
8751 }
8752
8753 return( 0 );
8754}
Paul Bakker5121ce52009-01-03 21:22:43 +00008755
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008756int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008757{
8758 int ret;
8759
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008760 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8761 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8762 {
8763 mbedtls_mpi_free( &conf->dhm_P );
8764 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008765 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008766 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008767
8768 return( 0 );
8769}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008770#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008771
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008772#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8773/*
8774 * Set the minimum length for Diffie-Hellman parameters
8775 */
8776void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8777 unsigned int bitlen )
8778{
8779 conf->dhm_min_bitlen = bitlen;
8780}
8781#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8782
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008783#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008784/*
8785 * Set allowed/preferred hashes for handshake signatures
8786 */
8787void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8788 const int *hashes )
8789{
8790 conf->sig_hashes = hashes;
8791}
Hanno Becker947194e2017-04-07 13:25:49 +01008792#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008793
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008794#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008795/*
8796 * Set the allowed elliptic curves
8797 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008798void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008799 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008800{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008801 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008802}
Hanno Becker947194e2017-04-07 13:25:49 +01008803#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008804
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008805#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008807{
Hanno Becker947194e2017-04-07 13:25:49 +01008808 /* Initialize to suppress unnecessary compiler warning */
8809 size_t hostname_len = 0;
8810
8811 /* Check if new hostname is valid before
8812 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008813 if( hostname != NULL )
8814 {
8815 hostname_len = strlen( hostname );
8816
8817 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8819 }
8820
8821 /* Now it's clear that we will overwrite the old hostname,
8822 * so we can free it safely */
8823
8824 if( ssl->hostname != NULL )
8825 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008826 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008827 mbedtls_free( ssl->hostname );
8828 }
8829
8830 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008831
Paul Bakker5121ce52009-01-03 21:22:43 +00008832 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008833 {
8834 ssl->hostname = NULL;
8835 }
8836 else
8837 {
8838 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008839 if( ssl->hostname == NULL )
8840 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008841
Hanno Becker947194e2017-04-07 13:25:49 +01008842 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008843
Hanno Becker947194e2017-04-07 13:25:49 +01008844 ssl->hostname[hostname_len] = '\0';
8845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008846
8847 return( 0 );
8848}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008849#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008850
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008851#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008852void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008854 const unsigned char *, size_t),
8855 void *p_sni )
8856{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008857 conf->f_sni = f_sni;
8858 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008859}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008863int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008864{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008865 size_t cur_len, tot_len;
8866 const char **p;
8867
8868 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008869 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8870 * MUST NOT be truncated."
8871 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008872 */
8873 tot_len = 0;
8874 for( p = protos; *p != NULL; p++ )
8875 {
8876 cur_len = strlen( *p );
8877 tot_len += cur_len;
8878
8879 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008881 }
8882
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008883 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008884
8885 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008886}
8887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008889{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008890 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008893
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008894void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008895{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008896 conf->max_major_ver = major;
8897 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008898}
8899
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008900void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008902 conf->min_major_ver = major;
8903 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008904}
8905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008907void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008908{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008909 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008910}
8911#endif
8912
Janos Follath088ce432017-04-10 12:42:31 +01008913#if defined(MBEDTLS_SSL_SRV_C)
8914void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8915 char cert_req_ca_list )
8916{
8917 conf->cert_req_ca_list = cert_req_ca_list;
8918}
8919#endif
8920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008922void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008923{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008924 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008925}
8926#endif
8927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008929void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008930{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008931 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008932}
8933#endif
8934
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008935#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008936void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008937{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008938 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008939}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008940#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008943int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008944{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008946 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008949 }
8950
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008951 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008952
8953 return( 0 );
8954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008958void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008959{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008960 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008965void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008966{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008967 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008968}
8969#endif
8970
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008971void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008973 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008974}
8975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008977void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008979 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008980}
8981
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008982void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008984 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008985}
8986
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008987void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008988 const unsigned char period[8] )
8989{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008990 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008995#if defined(MBEDTLS_SSL_CLI_C)
8996void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008997{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008998 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008999}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009000#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009001
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009002#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009003void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9004 mbedtls_ssl_ticket_write_t *f_ticket_write,
9005 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9006 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009007{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009008 conf->f_ticket_write = f_ticket_write;
9009 conf->f_ticket_parse = f_ticket_parse;
9010 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009011}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009014
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009015#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9016void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9017 mbedtls_ssl_export_keys_t *f_export_keys,
9018 void *p_export_keys )
9019{
9020 conf->f_export_keys = f_export_keys;
9021 conf->p_export_keys = p_export_keys;
9022}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009023
9024void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9025 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9026 void *p_export_keys )
9027{
9028 conf->f_export_keys_ext = f_export_keys_ext;
9029 conf->p_export_keys = p_export_keys;
9030}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009031#endif
9032
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009033#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009034void mbedtls_ssl_conf_async_private_cb(
9035 mbedtls_ssl_config *conf,
9036 mbedtls_ssl_async_sign_t *f_async_sign,
9037 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9038 mbedtls_ssl_async_resume_t *f_async_resume,
9039 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009040 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009041{
9042 conf->f_async_sign_start = f_async_sign;
9043 conf->f_async_decrypt_start = f_async_decrypt;
9044 conf->f_async_resume = f_async_resume;
9045 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009046 conf->p_async_config_data = async_config_data;
9047}
9048
Gilles Peskine8f97af72018-04-26 11:46:10 +02009049void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9050{
9051 return( conf->p_async_config_data );
9052}
9053
Gilles Peskine1febfef2018-04-30 11:54:39 +02009054void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009055{
9056 if( ssl->handshake == NULL )
9057 return( NULL );
9058 else
9059 return( ssl->handshake->user_async_ctx );
9060}
9061
Gilles Peskine1febfef2018-04-30 11:54:39 +02009062void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009063 void *ctx )
9064{
9065 if( ssl->handshake != NULL )
9066 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009067}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009068#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009069
Paul Bakker5121ce52009-01-03 21:22:43 +00009070/*
9071 * SSL get accessors
9072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009074{
9075 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9076}
9077
Hanno Becker8b170a02017-10-10 11:51:19 +01009078int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9079{
9080 /*
9081 * Case A: We're currently holding back
9082 * a message for further processing.
9083 */
9084
9085 if( ssl->keep_current_message == 1 )
9086 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009088 return( 1 );
9089 }
9090
9091 /*
9092 * Case B: Further records are pending in the current datagram.
9093 */
9094
9095#if defined(MBEDTLS_SSL_PROTO_DTLS)
9096 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9097 ssl->in_left > ssl->next_record_offset )
9098 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009100 return( 1 );
9101 }
9102#endif /* MBEDTLS_SSL_PROTO_DTLS */
9103
9104 /*
9105 * Case C: A handshake message is being processed.
9106 */
9107
Hanno Becker8b170a02017-10-10 11:51:19 +01009108 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9109 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009111 return( 1 );
9112 }
9113
9114 /*
9115 * Case D: An application data message is being processed
9116 */
9117 if( ssl->in_offt != NULL )
9118 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009120 return( 1 );
9121 }
9122
9123 /*
9124 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009125 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009126 * we implement support for multiple alerts in single records.
9127 */
9128
9129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9130 return( 0 );
9131}
9132
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009133uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009134{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009135 if( ssl->session != NULL )
9136 return( ssl->session->verify_result );
9137
9138 if( ssl->session_negotiate != NULL )
9139 return( ssl->session_negotiate->verify_result );
9140
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009141 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009142}
9143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009145{
Paul Bakker926c8e42013-03-06 10:23:34 +01009146 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009147 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009150}
9151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009152const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009156 {
9157 switch( ssl->minor_ver )
9158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009160 return( "DTLSv1.0" );
9161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009163 return( "DTLSv1.2" );
9164
9165 default:
9166 return( "unknown (DTLS)" );
9167 }
9168 }
9169#endif
9170
Paul Bakker43ca69c2011-01-15 17:35:19 +00009171 switch( ssl->minor_ver )
9172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009174 return( "SSLv3.0" );
9175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009177 return( "TLSv1.0" );
9178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009180 return( "TLSv1.1" );
9181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009183 return( "TLSv1.2" );
9184
Paul Bakker43ca69c2011-01-15 17:35:19 +00009185 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009186 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009187 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009188}
9189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009191{
Hanno Becker3136ede2018-08-17 15:28:19 +01009192 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009194 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009195
Hanno Becker78640902018-08-13 16:35:15 +01009196 if( transform == NULL )
9197 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199#if defined(MBEDTLS_ZLIB_SUPPORT)
9200 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9201 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009202#endif
9203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 case MBEDTLS_MODE_GCM:
9207 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009208 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009210 transform_expansion = transform->minlen;
9211 break;
9212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009214
9215 block_size = mbedtls_cipher_get_block_size(
9216 &transform->cipher_ctx_enc );
9217
Hanno Becker3136ede2018-08-17 15:28:19 +01009218 /* Expansion due to the addition of the MAC. */
9219 transform_expansion += transform->maclen;
9220
9221 /* Expansion due to the addition of CBC padding;
9222 * Theoretically up to 256 bytes, but we never use
9223 * more than the block size of the underlying cipher. */
9224 transform_expansion += block_size;
9225
9226 /* For TLS 1.1 or higher, an explicit IV is added
9227 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009228#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9229 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009230 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009231#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009232
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009233 break;
9234
9235 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009238 }
9239
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009240 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009241}
9242
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009243#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9244size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9245{
9246 size_t max_len;
9247
9248 /*
9249 * Assume mfl_code is correct since it was checked when set
9250 */
Angus Grattond8213d02016-05-25 20:56:48 +10009251 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009252
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009253 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009254 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009255 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009256 {
Angus Grattond8213d02016-05-25 20:56:48 +10009257 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009258 }
9259
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009260 /* During a handshake, use the value being negotiated */
9261 if( ssl->session_negotiate != NULL &&
9262 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9263 {
9264 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9265 }
9266
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009267 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009268}
9269#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9270
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009271#if defined(MBEDTLS_SSL_PROTO_DTLS)
9272static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9273{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009274 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9275 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9276 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9277 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9278 return ( 0 );
9279
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009280 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9281 return( ssl->mtu );
9282
9283 if( ssl->mtu == 0 )
9284 return( ssl->handshake->mtu );
9285
9286 return( ssl->mtu < ssl->handshake->mtu ?
9287 ssl->mtu : ssl->handshake->mtu );
9288}
9289#endif /* MBEDTLS_SSL_PROTO_DTLS */
9290
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009291int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9292{
9293 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9294
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009295#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9296 !defined(MBEDTLS_SSL_PROTO_DTLS)
9297 (void) ssl;
9298#endif
9299
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009300#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9301 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9302
9303 if( max_len > mfl )
9304 max_len = mfl;
9305#endif
9306
9307#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009308 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009309 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009310 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009311 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9312 const size_t overhead = (size_t) ret;
9313
9314 if( ret < 0 )
9315 return( ret );
9316
9317 if( mtu <= overhead )
9318 {
9319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9320 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9321 }
9322
9323 if( max_len > mtu - overhead )
9324 max_len = mtu - overhead;
9325 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009326#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009327
Hanno Becker0defedb2018-08-10 12:35:02 +01009328#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9329 !defined(MBEDTLS_SSL_PROTO_DTLS)
9330 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009331#endif
9332
9333 return( (int) max_len );
9334}
9335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336#if defined(MBEDTLS_X509_CRT_PARSE_C)
9337const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009338{
9339 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009340 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009341
Hanno Beckere6824572019-02-07 13:18:46 +00009342#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009343 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009344#else
9345 return( NULL );
9346#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009351int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9352 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009353{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009354 if( ssl == NULL ||
9355 dst == NULL ||
9356 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009357 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009360 }
9361
Hanno Becker52055ae2019-02-06 14:30:46 +00009362 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009365
Paul Bakker5121ce52009-01-03 21:22:43 +00009366/*
Paul Bakker1961b702013-01-25 14:49:24 +01009367 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009372
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009373 if( ssl == NULL || ssl->conf == NULL )
9374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009379#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009381 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009383#endif
9384
Paul Bakker1961b702013-01-25 14:49:24 +01009385 return( ret );
9386}
9387
9388/*
9389 * Perform the SSL handshake
9390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009392{
9393 int ret = 0;
9394
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009395 if( ssl == NULL || ssl->conf == NULL )
9396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009403
9404 if( ret != 0 )
9405 break;
9406 }
9407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009409
9410 return( ret );
9411}
9412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#if defined(MBEDTLS_SSL_RENEGOTIATION)
9414#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009415/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009416 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009419{
9420 int ret;
9421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009423
9424 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9426 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009427
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009428 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009429 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009431 return( ret );
9432 }
9433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009435
9436 return( 0 );
9437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009439
9440/*
9441 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 * - any side: calling mbedtls_ssl_renegotiate(),
9443 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9444 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009445 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009446 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009450{
9451 int ret;
9452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009454
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009455 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9456 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009457
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009458 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9459 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009461 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009463 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009464 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009465 ssl->handshake->out_msg_seq = 1;
9466 else
9467 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009468 }
9469#endif
9470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9472 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009477 return( ret );
9478 }
9479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009481
9482 return( 0 );
9483}
9484
9485/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009486 * Renegotiate current connection on client,
9487 * or request renegotiation on server
9488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009492
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009493 if( ssl == NULL || ssl->conf == NULL )
9494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009497 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009498 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009504
9505 /* Did we already try/start sending HelloRequest? */
9506 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009508
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009509 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009510 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009514 /*
9515 * On client, either start the renegotiation process or,
9516 * if already in progress, continue the handshake
9517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009522
9523 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009526 return( ret );
9527 }
9528 }
9529 else
9530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009534 return( ret );
9535 }
9536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009538
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009539 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009540}
9541
9542/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009543 * Check record counters and renegotiate if they're above the limit.
9544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009546{
Andres AG2196c7f2016-12-15 17:01:16 +00009547 size_t ep_len = ssl_ep_len( ssl );
9548 int in_ctr_cmp;
9549 int out_ctr_cmp;
9550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9552 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009553 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009554 {
9555 return( 0 );
9556 }
9557
Andres AG2196c7f2016-12-15 17:01:16 +00009558 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9559 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009560 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009561 ssl->conf->renego_period + ep_len, 8 - ep_len );
9562
9563 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009564 {
9565 return( 0 );
9566 }
9567
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009572
9573/*
9574 * Receive application data decrypted from the SSL layer
9575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009577{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009578 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009579 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009580
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009581 if( ssl == NULL || ssl->conf == NULL )
9582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009590 return( ret );
9591
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009592 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009594 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009595 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009596 return( ret );
9597 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009598 }
9599#endif
9600
Hanno Becker4a810fb2017-05-24 16:27:30 +01009601 /*
9602 * Check if renegotiation is necessary and/or handshake is
9603 * in process. If yes, perform/continue, and fall through
9604 * if an unexpected packet is received while the client
9605 * is waiting for the ServerHello.
9606 *
9607 * (There is no equivalent to the last condition on
9608 * the server-side as it is not treated as within
9609 * a handshake while waiting for the ClientHello
9610 * after a renegotiation request.)
9611 */
9612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009614 ret = ssl_check_ctr_renegotiate( ssl );
9615 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9616 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009619 return( ret );
9620 }
9621#endif
9622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009626 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9627 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009630 return( ret );
9631 }
9632 }
9633
Hanno Beckere41158b2017-10-23 13:30:32 +01009634 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009635 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009636 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009637 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009638 if( ssl->f_get_timer != NULL &&
9639 ssl->f_get_timer( ssl->p_timer ) == -1 )
9640 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009641 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009642 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009643
Hanno Becker327c93b2018-08-15 13:56:18 +01009644 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009645 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009646 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9647 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009648
Hanno Becker4a810fb2017-05-24 16:27:30 +01009649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9650 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009651 }
9652
9653 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009655 {
9656 /*
9657 * OpenSSL sends empty messages to randomize the IV
9658 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009659 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009662 return( 0 );
9663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009665 return( ret );
9666 }
9667 }
9668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009672
Hanno Becker4a810fb2017-05-24 16:27:30 +01009673 /*
9674 * - For client-side, expect SERVER_HELLO_REQUEST.
9675 * - For server-side, expect CLIENT_HELLO.
9676 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9677 */
9678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009680 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009682 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009685
9686 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009688 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009689 {
9690 continue;
9691 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009692#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009694 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009695#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009696
Hanno Becker4a810fb2017-05-24 16:27:30 +01009697#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009698 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009702
9703 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009706 {
9707 continue;
9708 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009711 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009712#endif /* MBEDTLS_SSL_SRV_C */
9713
Hanno Becker21df7f92017-10-17 11:03:26 +01009714#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009715 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009716 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9717 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9718 ssl->conf->allow_legacy_renegotiation ==
9719 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9720 {
9721 /*
9722 * Accept renegotiation request
9723 */
Paul Bakker48916f92012-09-16 19:57:18 +00009724
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009725 /* DTLS clients need to know renego is server-initiated */
9726#if defined(MBEDTLS_SSL_PROTO_DTLS)
9727 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9728 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9729 {
9730 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9731 }
9732#endif
9733 ret = ssl_start_renegotiation( ssl );
9734 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9735 ret != 0 )
9736 {
9737 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9738 return( ret );
9739 }
9740 }
9741 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009742#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009743 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009744 /*
9745 * Refuse renegotiation
9746 */
9747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750#if defined(MBEDTLS_SSL_PROTO_SSL3)
9751 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009752 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009753 /* SSLv3 does not have a "no_renegotiation" warning, so
9754 we send a fatal alert and abort the connection. */
9755 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9756 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9757 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009758 }
9759 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9761#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9762 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9763 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9766 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9767 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009768 {
9769 return( ret );
9770 }
Paul Bakker48916f92012-09-16 19:57:18 +00009771 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009772 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9774 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009778 }
Paul Bakker48916f92012-09-16 19:57:18 +00009779 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009780
Hanno Becker90333da2017-10-10 11:27:13 +01009781 /* At this point, we don't know whether the renegotiation has been
9782 * completed or not. The cases to consider are the following:
9783 * 1) The renegotiation is complete. In this case, no new record
9784 * has been read yet.
9785 * 2) The renegotiation is incomplete because the client received
9786 * an application data record while awaiting the ServerHello.
9787 * 3) The renegotiation is incomplete because the client received
9788 * a non-handshake, non-application data message while awaiting
9789 * the ServerHello.
9790 * In each of these case, looping will be the proper action:
9791 * - For 1), the next iteration will read a new record and check
9792 * if it's application data.
9793 * - For 2), the loop condition isn't satisfied as application data
9794 * is present, hence continue is the same as break
9795 * - For 3), the loop condition is satisfied and read_record
9796 * will re-deliver the message that was held back by the client
9797 * when expecting the ServerHello.
9798 */
9799 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009800 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009801#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009803 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009804 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009805 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009806 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009809 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009811 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009812 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009813 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9817 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009820 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009821 }
9822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9826 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009827 }
9828
9829 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009830
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009831 /* We're going to return something now, cancel timer,
9832 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009834 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009835
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009837 /* If we requested renego but received AppData, resend HelloRequest.
9838 * Do it now, after setting in_offt, to avoid taking this branch
9839 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009843 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009844 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009847 return( ret );
9848 }
9849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009851#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009852 }
9853
9854 n = ( len < ssl->in_msglen )
9855 ? len : ssl->in_msglen;
9856
9857 memcpy( buf, ssl->in_offt, n );
9858 ssl->in_msglen -= n;
9859
9860 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009861 {
9862 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009863 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009864 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009865 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009866 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009867 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009868 /* more data available */
9869 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009873
Paul Bakker23986e52011-04-24 08:57:21 +00009874 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009875}
9876
9877/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009878 * Send application data to be encrypted by the SSL layer, taking care of max
9879 * fragment length and buffer size.
9880 *
9881 * According to RFC 5246 Section 6.2.1:
9882 *
9883 * Zero-length fragments of Application data MAY be sent as they are
9884 * potentially useful as a traffic analysis countermeasure.
9885 *
9886 * Therefore, it is possible that the input message length is 0 and the
9887 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009888 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009889static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009890 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009891{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009892 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9893 const size_t max_len = (size_t) ret;
9894
9895 if( ret < 0 )
9896 {
9897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9898 return( ret );
9899 }
9900
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009901 if( len > max_len )
9902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009904 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009907 "maximum fragment length: %d > %d",
9908 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009910 }
9911 else
9912#endif
9913 len = max_len;
9914 }
Paul Bakker887bd502011-06-08 13:10:54 +00009915
Paul Bakker5121ce52009-01-03 21:22:43 +00009916 if( ssl->out_left != 0 )
9917 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009918 /*
9919 * The user has previously tried to send the data and
9920 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9921 * written. In this case, we expect the high-level write function
9922 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009927 return( ret );
9928 }
9929 }
Paul Bakker887bd502011-06-08 13:10:54 +00009930 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009931 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009932 /*
9933 * The user is trying to send a message the first time, so we need to
9934 * copy the data into the internal buffers and setup the data structure
9935 * to keep track of partial writes
9936 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009937 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009939 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009940
Hanno Becker67bc7c32018-08-06 11:33:50 +01009941 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009944 return( ret );
9945 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009946 }
9947
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009948 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009949}
9950
9951/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009952 * Write application data, doing 1/n-1 splitting if necessary.
9953 *
9954 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009955 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009956 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009959static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009960 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009961{
9962 int ret;
9963
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009964 if( ssl->conf->cbc_record_splitting ==
9965 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009966 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9968 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9969 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009970 {
9971 return( ssl_write_real( ssl, buf, len ) );
9972 }
9973
9974 if( ssl->split_done == 0 )
9975 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009976 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009977 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009978 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009979 }
9980
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009981 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9982 return( ret );
9983 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009984
9985 return( ret + 1 );
9986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009988
9989/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009990 * Write application data (public-facing wrapper)
9991 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009992int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009993{
9994 int ret;
9995
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009997
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009998 if( ssl == NULL || ssl->conf == NULL )
9999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10000
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010001#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010002 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10003 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010004 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010005 return( ret );
10006 }
10007#endif
10008
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010009 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010010 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010011 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010012 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010014 return( ret );
10015 }
10016 }
10017
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010018#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010019 ret = ssl_write_split( ssl, buf, len );
10020#else
10021 ret = ssl_write_real( ssl, buf, len );
10022#endif
10023
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010025
10026 return( ret );
10027}
10028
10029/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010030 * Notify the peer that the connection is being closed
10031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010033{
10034 int ret;
10035
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010036 if( ssl == NULL || ssl->conf == NULL )
10037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010040
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010041 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10047 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10048 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010051 return( ret );
10052 }
10053 }
10054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010056
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010057 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010058}
10059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010061{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010062 if( transform == NULL )
10063 return;
10064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010066 deflateEnd( &transform->ctx_deflate );
10067 inflateEnd( &transform->ctx_inflate );
10068#endif
10069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10071 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010072
Hanno Beckerd56ed242018-01-03 15:32:51 +000010073#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 mbedtls_md_free( &transform->md_ctx_enc );
10075 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010076#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010077
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010078 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010079}
10080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081#if defined(MBEDTLS_X509_CRT_PARSE_C)
10082static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010085
10086 while( cur != NULL )
10087 {
10088 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010090 cur = next;
10091 }
10092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010094
Hanno Becker0271f962018-08-16 13:23:47 +010010095#if defined(MBEDTLS_SSL_PROTO_DTLS)
10096
10097static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10098{
10099 unsigned offset;
10100 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10101
10102 if( hs == NULL )
10103 return;
10104
Hanno Becker283f5ef2018-08-24 09:34:47 +010010105 ssl_free_buffered_record( ssl );
10106
Hanno Becker0271f962018-08-16 13:23:47 +010010107 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010108 ssl_buffering_free_slot( ssl, offset );
10109}
10110
10111static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10112 uint8_t slot )
10113{
10114 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10115 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010116
10117 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10118 return;
10119
Hanno Beckere605b192018-08-21 15:59:07 +010010120 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010121 {
Hanno Beckere605b192018-08-21 15:59:07 +010010122 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010123 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010124 mbedtls_free( hs_buf->data );
10125 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010126 }
10127}
10128
10129#endif /* MBEDTLS_SSL_PROTO_DTLS */
10130
Gilles Peskine9b562d52018-04-25 20:32:43 +020010131void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010132{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010133 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10134
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010135 if( handshake == NULL )
10136 return;
10137
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010138#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10139 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10140 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010141 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010142 handshake->async_in_progress = 0;
10143 }
10144#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10145
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010146#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10147 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10148 mbedtls_md5_free( &handshake->fin_md5 );
10149 mbedtls_sha1_free( &handshake->fin_sha1 );
10150#endif
10151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10152#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010153#if defined(MBEDTLS_USE_PSA_CRYPTO)
10154 psa_hash_abort( &handshake->fin_sha256_psa );
10155#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010156 mbedtls_sha256_free( &handshake->fin_sha256 );
10157#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010158#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010159#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010160#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010161 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010162#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010163 mbedtls_sha512_free( &handshake->fin_sha512 );
10164#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010165#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168#if defined(MBEDTLS_DHM_C)
10169 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171#if defined(MBEDTLS_ECDH_C)
10172 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010173#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010174#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010175 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010176#if defined(MBEDTLS_SSL_CLI_C)
10177 mbedtls_free( handshake->ecjpake_cache );
10178 handshake->ecjpake_cache = NULL;
10179 handshake->ecjpake_cache_len = 0;
10180#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010181#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010182
Janos Follath4ae5c292016-02-10 11:27:43 +000010183#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10184 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010185 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010187#endif
10188
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010189#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10190 if( handshake->psk != NULL )
10191 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010192 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010193 mbedtls_free( handshake->psk );
10194 }
10195#endif
10196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10198 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010199 /*
10200 * Free only the linked list wrapper, not the keys themselves
10201 * since the belong to the SNI callback
10202 */
10203 if( handshake->sni_key_cert != NULL )
10204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010206
10207 while( cur != NULL )
10208 {
10209 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010211 cur = next;
10212 }
10213 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010215
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010216#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010217 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010218 if( handshake->ecrs_peer_cert != NULL )
10219 {
10220 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10221 mbedtls_free( handshake->ecrs_peer_cert );
10222 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010223#endif
10224
Hanno Becker75173122019-02-06 16:18:31 +000010225#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10226 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10227 mbedtls_pk_free( &handshake->peer_pubkey );
10228#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230#if defined(MBEDTLS_SSL_PROTO_DTLS)
10231 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010232 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010233 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010234#endif
10235
Hanno Becker4a63ed42019-01-08 11:39:35 +000010236#if defined(MBEDTLS_ECDH_C) && \
10237 defined(MBEDTLS_USE_PSA_CRYPTO)
10238 psa_destroy_key( handshake->ecdh_psa_privkey );
10239#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10240
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010241 mbedtls_platform_zeroize( handshake,
10242 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010243}
10244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010246{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010247 if( session == NULL )
10248 return;
10249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010251 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010252#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010253
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010254#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010256#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010257
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010258 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010259}
10260
Paul Bakker5121ce52009-01-03 21:22:43 +000010261/*
10262 * Free an SSL context
10263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010265{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010266 if( ssl == NULL )
10267 return;
10268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010270
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010271 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 {
Angus Grattond8213d02016-05-25 20:56:48 +100010273 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010275 }
10276
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010277 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010278 {
Angus Grattond8213d02016-05-25 20:56:48 +100010279 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010281 }
10282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010284 if( ssl->compress_buf != NULL )
10285 {
Angus Grattond8213d02016-05-25 20:56:48 +100010286 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010288 }
10289#endif
10290
Paul Bakker48916f92012-09-16 19:57:18 +000010291 if( ssl->transform )
10292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 mbedtls_ssl_transform_free( ssl->transform );
10294 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010295 }
10296
10297 if( ssl->handshake )
10298 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010299 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10301 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 mbedtls_free( ssl->handshake );
10304 mbedtls_free( ssl->transform_negotiate );
10305 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010306 }
10307
Paul Bakkerc0463502013-02-14 11:19:38 +010010308 if( ssl->session )
10309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310 mbedtls_ssl_session_free( ssl->session );
10311 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010312 }
10313
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010314#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010315 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010316 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010317 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010319 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010320#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10323 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10326 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010327 }
10328#endif
10329
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010330#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010332#endif
10333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010335
Paul Bakker86f04f42013-02-14 11:20:09 +010010336 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010337 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010338}
10339
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010340/*
10341 * Initialze mbedtls_ssl_config
10342 */
10343void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10344{
10345 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10346}
10347
Simon Butcherc97b6972015-12-27 23:48:17 +000010348#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010349static int ssl_preset_default_hashes[] = {
10350#if defined(MBEDTLS_SHA512_C)
10351 MBEDTLS_MD_SHA512,
10352 MBEDTLS_MD_SHA384,
10353#endif
10354#if defined(MBEDTLS_SHA256_C)
10355 MBEDTLS_MD_SHA256,
10356 MBEDTLS_MD_SHA224,
10357#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010358#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010359 MBEDTLS_MD_SHA1,
10360#endif
10361 MBEDTLS_MD_NONE
10362};
Simon Butcherc97b6972015-12-27 23:48:17 +000010363#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010364
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010365static int ssl_preset_suiteb_ciphersuites[] = {
10366 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10367 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10368 0
10369};
10370
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010371#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010372static int ssl_preset_suiteb_hashes[] = {
10373 MBEDTLS_MD_SHA256,
10374 MBEDTLS_MD_SHA384,
10375 MBEDTLS_MD_NONE
10376};
10377#endif
10378
10379#if defined(MBEDTLS_ECP_C)
10380static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10381 MBEDTLS_ECP_DP_SECP256R1,
10382 MBEDTLS_ECP_DP_SECP384R1,
10383 MBEDTLS_ECP_DP_NONE
10384};
10385#endif
10386
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010387/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010388 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010389 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010390int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010391 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010392{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010393#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010394 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010395#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010396
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010397 /* Use the functions here so that they are covered in tests,
10398 * but otherwise access member directly for efficiency */
10399 mbedtls_ssl_conf_endpoint( conf, endpoint );
10400 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010401
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010402 /*
10403 * Things that are common to all presets
10404 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010405#if defined(MBEDTLS_SSL_CLI_C)
10406 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10407 {
10408 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10409#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10410 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10411#endif
10412 }
10413#endif
10414
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010415#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010416 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010417#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010418
10419#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10420 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10421#endif
10422
10423#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10424 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10425#endif
10426
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010427#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10428 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10429#endif
10430
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010431#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010432 conf->f_cookie_write = ssl_cookie_write_dummy;
10433 conf->f_cookie_check = ssl_cookie_check_dummy;
10434#endif
10435
10436#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10437 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10438#endif
10439
Janos Follath088ce432017-04-10 12:42:31 +010010440#if defined(MBEDTLS_SSL_SRV_C)
10441 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10442#endif
10443
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010444#if defined(MBEDTLS_SSL_PROTO_DTLS)
10445 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10446 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10447#endif
10448
10449#if defined(MBEDTLS_SSL_RENEGOTIATION)
10450 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010451 memset( conf->renego_period, 0x00, 2 );
10452 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010453#endif
10454
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010455#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10456 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10457 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010458 const unsigned char dhm_p[] =
10459 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10460 const unsigned char dhm_g[] =
10461 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10462
Hanno Beckera90658f2017-10-04 15:29:08 +010010463 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10464 dhm_p, sizeof( dhm_p ),
10465 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010466 {
10467 return( ret );
10468 }
10469 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010470#endif
10471
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010472 /*
10473 * Preset-specific defaults
10474 */
10475 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010476 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010477 /*
10478 * NSA Suite B
10479 */
10480 case MBEDTLS_SSL_PRESET_SUITEB:
10481 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10482 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10483 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10484 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10485
10486 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10487 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10488 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10489 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10490 ssl_preset_suiteb_ciphersuites;
10491
10492#if defined(MBEDTLS_X509_CRT_PARSE_C)
10493 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010494#endif
10495
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010496#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010497 conf->sig_hashes = ssl_preset_suiteb_hashes;
10498#endif
10499
10500#if defined(MBEDTLS_ECP_C)
10501 conf->curve_list = ssl_preset_suiteb_curves;
10502#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010503 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010504
10505 /*
10506 * Default
10507 */
10508 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010509 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10510 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10511 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10512 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10513 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10514 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10515 MBEDTLS_SSL_MIN_MINOR_VERSION :
10516 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010517 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10518 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10519
10520#if defined(MBEDTLS_SSL_PROTO_DTLS)
10521 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10522 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10523#endif
10524
10525 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10526 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10527 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10528 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10529 mbedtls_ssl_list_ciphersuites();
10530
10531#if defined(MBEDTLS_X509_CRT_PARSE_C)
10532 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10533#endif
10534
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010535#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010536 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010537#endif
10538
10539#if defined(MBEDTLS_ECP_C)
10540 conf->curve_list = mbedtls_ecp_grp_id_list();
10541#endif
10542
10543#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10544 conf->dhm_min_bitlen = 1024;
10545#endif
10546 }
10547
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010548 return( 0 );
10549}
10550
10551/*
10552 * Free mbedtls_ssl_config
10553 */
10554void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10555{
10556#if defined(MBEDTLS_DHM_C)
10557 mbedtls_mpi_free( &conf->dhm_P );
10558 mbedtls_mpi_free( &conf->dhm_G );
10559#endif
10560
10561#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10562 if( conf->psk != NULL )
10563 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010564 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010565 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010566 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010567 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010568 }
10569
10570 if( conf->psk_identity != NULL )
10571 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010572 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010573 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010574 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010575 conf->psk_identity_len = 0;
10576 }
10577#endif
10578
10579#if defined(MBEDTLS_X509_CRT_PARSE_C)
10580 ssl_key_cert_free( conf->key_cert );
10581#endif
10582
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010583 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010584}
10585
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010586#if defined(MBEDTLS_PK_C) && \
10587 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010588/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010589 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010592{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593#if defined(MBEDTLS_RSA_C)
10594 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10595 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597#if defined(MBEDTLS_ECDSA_C)
10598 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10599 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010602}
10603
Hanno Becker7e5437a2017-04-28 17:15:26 +010010604unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10605{
10606 switch( type ) {
10607 case MBEDTLS_PK_RSA:
10608 return( MBEDTLS_SSL_SIG_RSA );
10609 case MBEDTLS_PK_ECDSA:
10610 case MBEDTLS_PK_ECKEY:
10611 return( MBEDTLS_SSL_SIG_ECDSA );
10612 default:
10613 return( MBEDTLS_SSL_SIG_ANON );
10614 }
10615}
10616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010618{
10619 switch( sig )
10620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621#if defined(MBEDTLS_RSA_C)
10622 case MBEDTLS_SSL_SIG_RSA:
10623 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010624#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625#if defined(MBEDTLS_ECDSA_C)
10626 case MBEDTLS_SSL_SIG_ECDSA:
10627 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010628#endif
10629 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010631 }
10632}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010633#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010634
Hanno Becker7e5437a2017-04-28 17:15:26 +010010635#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10636 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10637
10638/* Find an entry in a signature-hash set matching a given hash algorithm. */
10639mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10640 mbedtls_pk_type_t sig_alg )
10641{
10642 switch( sig_alg )
10643 {
10644 case MBEDTLS_PK_RSA:
10645 return( set->rsa );
10646 case MBEDTLS_PK_ECDSA:
10647 return( set->ecdsa );
10648 default:
10649 return( MBEDTLS_MD_NONE );
10650 }
10651}
10652
10653/* Add a signature-hash-pair to a signature-hash set */
10654void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10655 mbedtls_pk_type_t sig_alg,
10656 mbedtls_md_type_t md_alg )
10657{
10658 switch( sig_alg )
10659 {
10660 case MBEDTLS_PK_RSA:
10661 if( set->rsa == MBEDTLS_MD_NONE )
10662 set->rsa = md_alg;
10663 break;
10664
10665 case MBEDTLS_PK_ECDSA:
10666 if( set->ecdsa == MBEDTLS_MD_NONE )
10667 set->ecdsa = md_alg;
10668 break;
10669
10670 default:
10671 break;
10672 }
10673}
10674
10675/* Allow exactly one hash algorithm for each signature. */
10676void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10677 mbedtls_md_type_t md_alg )
10678{
10679 set->rsa = md_alg;
10680 set->ecdsa = md_alg;
10681}
10682
10683#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10684 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10685
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010686/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010687 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010690{
10691 switch( hash )
10692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693#if defined(MBEDTLS_MD5_C)
10694 case MBEDTLS_SSL_HASH_MD5:
10695 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697#if defined(MBEDTLS_SHA1_C)
10698 case MBEDTLS_SSL_HASH_SHA1:
10699 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701#if defined(MBEDTLS_SHA256_C)
10702 case MBEDTLS_SSL_HASH_SHA224:
10703 return( MBEDTLS_MD_SHA224 );
10704 case MBEDTLS_SSL_HASH_SHA256:
10705 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707#if defined(MBEDTLS_SHA512_C)
10708 case MBEDTLS_SSL_HASH_SHA384:
10709 return( MBEDTLS_MD_SHA384 );
10710 case MBEDTLS_SSL_HASH_SHA512:
10711 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010712#endif
10713 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010715 }
10716}
10717
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010718/*
10719 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10720 */
10721unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10722{
10723 switch( md )
10724 {
10725#if defined(MBEDTLS_MD5_C)
10726 case MBEDTLS_MD_MD5:
10727 return( MBEDTLS_SSL_HASH_MD5 );
10728#endif
10729#if defined(MBEDTLS_SHA1_C)
10730 case MBEDTLS_MD_SHA1:
10731 return( MBEDTLS_SSL_HASH_SHA1 );
10732#endif
10733#if defined(MBEDTLS_SHA256_C)
10734 case MBEDTLS_MD_SHA224:
10735 return( MBEDTLS_SSL_HASH_SHA224 );
10736 case MBEDTLS_MD_SHA256:
10737 return( MBEDTLS_SSL_HASH_SHA256 );
10738#endif
10739#if defined(MBEDTLS_SHA512_C)
10740 case MBEDTLS_MD_SHA384:
10741 return( MBEDTLS_SSL_HASH_SHA384 );
10742 case MBEDTLS_MD_SHA512:
10743 return( MBEDTLS_SSL_HASH_SHA512 );
10744#endif
10745 default:
10746 return( MBEDTLS_SSL_HASH_NONE );
10747 }
10748}
10749
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010750#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010751/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010752 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010753 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010754 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010755int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010758
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010759 if( ssl->conf->curve_list == NULL )
10760 return( -1 );
10761
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010762 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010763 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010764 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010765
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010766 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010767}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010768#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010769
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010770#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010771/*
10772 * Check if a hash proposed by the peer is in our list.
10773 * Return 0 if we're willing to use it, -1 otherwise.
10774 */
10775int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10776 mbedtls_md_type_t md )
10777{
10778 const int *cur;
10779
10780 if( ssl->conf->sig_hashes == NULL )
10781 return( -1 );
10782
10783 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10784 if( *cur == (int) md )
10785 return( 0 );
10786
10787 return( -1 );
10788}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010789#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791#if defined(MBEDTLS_X509_CRT_PARSE_C)
10792int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10793 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010794 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010795 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010796{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010797 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010799 int usage = 0;
10800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010802 const char *ext_oid;
10803 size_t ext_len;
10804#endif
10805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010806#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10807 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010808 ((void) cert);
10809 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010810 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010811#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10814 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010815 {
10816 /* Server part of the key exchange */
10817 switch( ciphersuite->key_exchange )
10818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010819 case MBEDTLS_KEY_EXCHANGE_RSA:
10820 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010821 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010822 break;
10823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10825 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10826 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10827 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010828 break;
10829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10831 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010832 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010833 break;
10834
10835 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836 case MBEDTLS_KEY_EXCHANGE_NONE:
10837 case MBEDTLS_KEY_EXCHANGE_PSK:
10838 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10839 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010840 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010841 usage = 0;
10842 }
10843 }
10844 else
10845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10847 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010848 }
10849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010850 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010851 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010852 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010853 ret = -1;
10854 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010855#else
10856 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10860 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010862 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10863 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010864 }
10865 else
10866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10868 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010869 }
10870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010872 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010873 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010874 ret = -1;
10875 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010877
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010878 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010880#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010881
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010882/*
10883 * Convert version numbers to/from wire format
10884 * and, for DTLS, to/from TLS equivalent.
10885 *
10886 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010887 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010888 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10889 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010892 unsigned char ver[2] )
10893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894#if defined(MBEDTLS_SSL_PROTO_DTLS)
10895 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010898 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10899
10900 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10901 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10902 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010903 else
10904#else
10905 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010906#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010907 {
10908 ver[0] = (unsigned char) major;
10909 ver[1] = (unsigned char) minor;
10910 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010911}
10912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010914 const unsigned char ver[2] )
10915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916#if defined(MBEDTLS_SSL_PROTO_DTLS)
10917 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010918 {
10919 *major = 255 - ver[0] + 2;
10920 *minor = 255 - ver[1] + 1;
10921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010923 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10924 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010925 else
10926#else
10927 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010928#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010929 {
10930 *major = ver[0];
10931 *minor = ver[1];
10932 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010933}
10934
Simon Butcher99000142016-10-13 17:21:01 +010010935int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10936{
10937#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10938 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10939 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10940
10941 switch( md )
10942 {
10943#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10944#if defined(MBEDTLS_MD5_C)
10945 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010946 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010947#endif
10948#if defined(MBEDTLS_SHA1_C)
10949 case MBEDTLS_SSL_HASH_SHA1:
10950 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10951 break;
10952#endif
10953#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10954#if defined(MBEDTLS_SHA512_C)
10955 case MBEDTLS_SSL_HASH_SHA384:
10956 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10957 break;
10958#endif
10959#if defined(MBEDTLS_SHA256_C)
10960 case MBEDTLS_SSL_HASH_SHA256:
10961 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10962 break;
10963#endif
10964 default:
10965 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10966 }
10967
10968 return 0;
10969#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10970 (void) ssl;
10971 (void) md;
10972
10973 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10974#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10975}
10976
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010977#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10978 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10979int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10980 unsigned char *output,
10981 unsigned char *data, size_t data_len )
10982{
10983 int ret = 0;
10984 mbedtls_md5_context mbedtls_md5;
10985 mbedtls_sha1_context mbedtls_sha1;
10986
10987 mbedtls_md5_init( &mbedtls_md5 );
10988 mbedtls_sha1_init( &mbedtls_sha1 );
10989
10990 /*
10991 * digitally-signed struct {
10992 * opaque md5_hash[16];
10993 * opaque sha_hash[20];
10994 * };
10995 *
10996 * md5_hash
10997 * MD5(ClientHello.random + ServerHello.random
10998 * + ServerParams);
10999 * sha_hash
11000 * SHA(ClientHello.random + ServerHello.random
11001 * + ServerParams);
11002 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011003 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011004 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011006 goto exit;
11007 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011008 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011009 ssl->handshake->randbytes, 64 ) ) != 0 )
11010 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011012 goto exit;
11013 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011014 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011015 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011017 goto exit;
11018 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011019 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011020 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011022 goto exit;
11023 }
11024
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011025 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011026 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011028 goto exit;
11029 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011030 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011031 ssl->handshake->randbytes, 64 ) ) != 0 )
11032 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011034 goto exit;
11035 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011036 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011037 data_len ) ) != 0 )
11038 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011040 goto exit;
11041 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011042 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011043 output + 16 ) ) != 0 )
11044 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011046 goto exit;
11047 }
11048
11049exit:
11050 mbedtls_md5_free( &mbedtls_md5 );
11051 mbedtls_sha1_free( &mbedtls_sha1 );
11052
11053 if( ret != 0 )
11054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11055 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11056
11057 return( ret );
11058
11059}
11060#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11061 MBEDTLS_SSL_PROTO_TLS1_1 */
11062
11063#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11064 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011065
11066#if defined(MBEDTLS_USE_PSA_CRYPTO)
11067int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11068 unsigned char *hash, size_t *hashlen,
11069 unsigned char *data, size_t data_len,
11070 mbedtls_md_type_t md_alg )
11071{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011072 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011073 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011074 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11075
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011077
11078 if( ( status = psa_hash_setup( &hash_operation,
11079 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011080 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011081 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011082 goto exit;
11083 }
11084
Andrzej Kurek814feff2019-01-14 04:35:19 -050011085 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11086 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011087 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011088 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011089 goto exit;
11090 }
11091
Andrzej Kurek814feff2019-01-14 04:35:19 -050011092 if( ( status = psa_hash_update( &hash_operation,
11093 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011094 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011095 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011096 goto exit;
11097 }
11098
Andrzej Kurek814feff2019-01-14 04:35:19 -050011099 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11100 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011101 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011102 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011103 goto exit;
11104 }
11105
11106exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011107 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011108 {
11109 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11110 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011111 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011112 {
11113 case PSA_ERROR_NOT_SUPPORTED:
11114 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011115 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011116 case PSA_ERROR_BUFFER_TOO_SMALL:
11117 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11118 case PSA_ERROR_INSUFFICIENT_MEMORY:
11119 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11120 default:
11121 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11122 }
11123 }
11124 return( 0 );
11125}
11126
11127#else
11128
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011129int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011130 unsigned char *hash, size_t *hashlen,
11131 unsigned char *data, size_t data_len,
11132 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011133{
11134 int ret = 0;
11135 mbedtls_md_context_t ctx;
11136 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011137 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011138
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011140
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011141 mbedtls_md_init( &ctx );
11142
11143 /*
11144 * digitally-signed struct {
11145 * opaque client_random[32];
11146 * opaque server_random[32];
11147 * ServerDHParams params;
11148 * };
11149 */
11150 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11151 {
11152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11153 goto exit;
11154 }
11155 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11156 {
11157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11158 goto exit;
11159 }
11160 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11161 {
11162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11163 goto exit;
11164 }
11165 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11166 {
11167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11168 goto exit;
11169 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011170 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011171 {
11172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11173 goto exit;
11174 }
11175
11176exit:
11177 mbedtls_md_free( &ctx );
11178
11179 if( ret != 0 )
11180 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11181 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11182
11183 return( ret );
11184}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011185#endif /* MBEDTLS_USE_PSA_CRYPTO */
11186
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011187#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11188 MBEDTLS_SSL_PROTO_TLS1_2 */
11189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011190#endif /* MBEDTLS_SSL_TLS_C */