blob: 199c41d0fcd85639dd13beb320173468f171aaa8 [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 "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005084 "messages, possible DoS attack" ) );
5085 /* Treat the records as if they were not properly authenticated,
5086 * thereby failing the connection if we see more than allowed
5087 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005088 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5089 }
5090 }
5091 else
5092 ssl->nb_zero = 0;
5093
5094#if defined(MBEDTLS_SSL_PROTO_DTLS)
5095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5096 {
5097 ; /* in_ctr read from peer, not maintained internally */
5098 }
5099 else
5100#endif
5101 {
5102 unsigned i;
5103 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5104 if( ++ssl->in_ctr[i - 1] != 0 )
5105 break;
5106
5107 /* The loop goes to its end iff the counter is wrapping */
5108 if( i == ssl_ep_len( ssl ) )
5109 {
5110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5111 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5112 }
5113 }
5114
Paul Bakker5121ce52009-01-03 21:22:43 +00005115 }
5116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005118 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005120 {
5121 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005124 return( ret );
5125 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005126 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005130 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005133 }
5134#endif
5135
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005136 return( 0 );
5137}
5138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005140
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005141/*
5142 * Read a record.
5143 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005144 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5145 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5146 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005147 */
Hanno Becker1097b342018-08-15 14:09:41 +01005148
5149/* Helper functions for mbedtls_ssl_read_record(). */
5150static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005151static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5152static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005153
Hanno Becker327c93b2018-08-15 13:56:18 +01005154int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005155 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005156{
5157 int ret;
5158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005160
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005161 if( ssl->keep_current_message == 0 )
5162 {
5163 do {
Simon Butcher99000142016-10-13 17:21:01 +01005164
Hanno Becker26994592018-08-15 14:14:59 +01005165 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005166 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005167 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005168
Hanno Beckere74d5562018-08-15 14:26:08 +01005169 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005170 {
Hanno Becker40f50842018-08-15 14:48:01 +01005171#if defined(MBEDTLS_SSL_PROTO_DTLS)
5172 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005173
Hanno Becker40f50842018-08-15 14:48:01 +01005174 /* We only check for buffered messages if the
5175 * current datagram is fully consumed. */
5176 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005177 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005178 {
Hanno Becker40f50842018-08-15 14:48:01 +01005179 if( ssl_load_buffered_message( ssl ) == 0 )
5180 have_buffered = 1;
5181 }
5182
5183 if( have_buffered == 0 )
5184#endif /* MBEDTLS_SSL_PROTO_DTLS */
5185 {
5186 ret = ssl_get_next_record( ssl );
5187 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5188 continue;
5189
5190 if( ret != 0 )
5191 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005192 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005193 return( ret );
5194 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005195 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005196 }
5197
5198 ret = mbedtls_ssl_handle_message_type( ssl );
5199
Hanno Becker40f50842018-08-15 14:48:01 +01005200#if defined(MBEDTLS_SSL_PROTO_DTLS)
5201 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5202 {
5203 /* Buffer future message */
5204 ret = ssl_buffer_message( ssl );
5205 if( ret != 0 )
5206 return( ret );
5207
5208 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5209 }
5210#endif /* MBEDTLS_SSL_PROTO_DTLS */
5211
Hanno Becker90333da2017-10-10 11:27:13 +01005212 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5213 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005214
5215 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005216 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005217 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005218 return( ret );
5219 }
5220
Hanno Becker327c93b2018-08-15 13:56:18 +01005221 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005222 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005223 {
5224 mbedtls_ssl_update_handshake_status( ssl );
5225 }
Simon Butcher99000142016-10-13 17:21:01 +01005226 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005227 else
Simon Butcher99000142016-10-13 17:21:01 +01005228 {
Hanno Becker02f59072018-08-15 14:00:24 +01005229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005230 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005231 }
5232
5233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5234
5235 return( 0 );
5236}
5237
Hanno Becker40f50842018-08-15 14:48:01 +01005238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005239static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005240{
Hanno Becker40f50842018-08-15 14:48:01 +01005241 if( ssl->in_left > ssl->next_record_offset )
5242 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005243
Hanno Becker40f50842018-08-15 14:48:01 +01005244 return( 0 );
5245}
5246
5247static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5248{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005249 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005250 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005251 int ret = 0;
5252
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005253 if( hs == NULL )
5254 return( -1 );
5255
Hanno Beckere00ae372018-08-20 09:39:42 +01005256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5257
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005258 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5259 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5260 {
5261 /* Check if we have seen a ChangeCipherSpec before.
5262 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005263 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005264 {
5265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5266 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005267 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005268 }
5269
Hanno Becker39b8bc92018-08-28 17:17:13 +01005270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005271 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5272 ssl->in_msglen = 1;
5273 ssl->in_msg[0] = 1;
5274
5275 /* As long as they are equal, the exact value doesn't matter. */
5276 ssl->in_left = 0;
5277 ssl->next_record_offset = 0;
5278
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005279 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005280 goto exit;
5281 }
Hanno Becker37f95322018-08-16 13:55:32 +01005282
Hanno Beckerb8f50142018-08-28 10:01:34 +01005283#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005284 /* Debug only */
5285 {
5286 unsigned offset;
5287 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5288 {
5289 hs_buf = &hs->buffering.hs[offset];
5290 if( hs_buf->is_valid == 1 )
5291 {
5292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5293 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005294 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005295 }
5296 }
5297 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005298#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005299
5300 /* Check if we have buffered and/or fully reassembled the
5301 * next handshake message. */
5302 hs_buf = &hs->buffering.hs[0];
5303 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5304 {
5305 /* Synthesize a record containing the buffered HS message. */
5306 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5307 ( hs_buf->data[2] << 8 ) |
5308 hs_buf->data[3];
5309
5310 /* Double-check that we haven't accidentally buffered
5311 * a message that doesn't fit into the input buffer. */
5312 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5313 {
5314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5315 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5316 }
5317
5318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5319 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5320 hs_buf->data, msg_len + 12 );
5321
5322 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5323 ssl->in_hslen = msg_len + 12;
5324 ssl->in_msglen = msg_len + 12;
5325 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5326
5327 ret = 0;
5328 goto exit;
5329 }
5330 else
5331 {
5332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5333 hs->in_msg_seq ) );
5334 }
5335
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005336 ret = -1;
5337
5338exit:
5339
5340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5341 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005342}
5343
Hanno Beckera02b0b42018-08-21 17:20:27 +01005344static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5345 size_t desired )
5346{
5347 int offset;
5348 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5350 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005351
Hanno Becker01315ea2018-08-21 17:22:17 +01005352 /* Get rid of future records epoch first, if such exist. */
5353 ssl_free_buffered_record( ssl );
5354
5355 /* Check if we have enough space available now. */
5356 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5357 hs->buffering.total_bytes_buffered ) )
5358 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005360 return( 0 );
5361 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005362
Hanno Becker4f432ad2018-08-28 10:02:32 +01005363 /* We don't have enough space to buffer the next expected handshake
5364 * message. Remove buffers used for future messages to gain space,
5365 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005366 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5367 offset >= 0; offset-- )
5368 {
5369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5370 offset ) );
5371
Hanno Beckerb309b922018-08-23 13:18:05 +01005372 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005373
5374 /* Check if we have enough space available now. */
5375 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5376 hs->buffering.total_bytes_buffered ) )
5377 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005379 return( 0 );
5380 }
5381 }
5382
5383 return( -1 );
5384}
5385
Hanno Becker40f50842018-08-15 14:48:01 +01005386static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5387{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005388 int ret = 0;
5389 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5390
5391 if( hs == NULL )
5392 return( 0 );
5393
5394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5395
5396 switch( ssl->in_msgtype )
5397 {
5398 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005400
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005401 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005402 break;
5403
5404 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005405 {
5406 unsigned recv_msg_seq_offset;
5407 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5408 mbedtls_ssl_hs_buffer *hs_buf;
5409 size_t msg_len = ssl->in_hslen - 12;
5410
5411 /* We should never receive an old handshake
5412 * message - double-check nonetheless. */
5413 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5414 {
5415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5417 }
5418
5419 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5420 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5421 {
5422 /* Silently ignore -- message too far in the future */
5423 MBEDTLS_SSL_DEBUG_MSG( 2,
5424 ( "Ignore future HS message with sequence number %u, "
5425 "buffering window %u - %u",
5426 recv_msg_seq, ssl->handshake->in_msg_seq,
5427 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5428
5429 goto exit;
5430 }
5431
5432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5433 recv_msg_seq, recv_msg_seq_offset ) );
5434
5435 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5436
5437 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005438 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005439 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005440 size_t reassembly_buf_sz;
5441
Hanno Becker37f95322018-08-16 13:55:32 +01005442 hs_buf->is_fragmented =
5443 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5444
5445 /* We copy the message back into the input buffer
5446 * after reassembly, so check that it's not too large.
5447 * This is an implementation-specific limitation
5448 * and not one from the standard, hence it is not
5449 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005450 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005451 {
5452 /* Ignore message */
5453 goto exit;
5454 }
5455
Hanno Beckere0b150f2018-08-21 15:51:03 +01005456 /* Check if we have enough space to buffer the message. */
5457 if( hs->buffering.total_bytes_buffered >
5458 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5459 {
5460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5462 }
5463
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005464 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5465 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005466
5467 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5468 hs->buffering.total_bytes_buffered ) )
5469 {
5470 if( recv_msg_seq_offset > 0 )
5471 {
5472 /* If we can't buffer a future message because
5473 * of space limitations -- ignore. */
5474 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",
5475 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5476 (unsigned) hs->buffering.total_bytes_buffered ) );
5477 goto exit;
5478 }
Hanno Beckere1801392018-08-21 16:51:05 +01005479 else
5480 {
5481 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",
5482 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5483 (unsigned) hs->buffering.total_bytes_buffered ) );
5484 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005485
Hanno Beckera02b0b42018-08-21 17:20:27 +01005486 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005487 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005488 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",
5489 (unsigned) msg_len,
5490 (unsigned) reassembly_buf_sz,
5491 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005492 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005493 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5494 goto exit;
5495 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005496 }
5497
5498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5499 msg_len ) );
5500
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005501 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5502 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005503 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005504 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005505 goto exit;
5506 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005507 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005508
5509 /* Prepare final header: copy msg_type, length and message_seq,
5510 * then add standardised fragment_offset and fragment_length */
5511 memcpy( hs_buf->data, ssl->in_msg, 6 );
5512 memset( hs_buf->data + 6, 0, 3 );
5513 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5514
5515 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005516
5517 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005518 }
5519 else
5520 {
5521 /* Make sure msg_type and length are consistent */
5522 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5523 {
5524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5525 /* Ignore */
5526 goto exit;
5527 }
5528 }
5529
Hanno Becker4422bbb2018-08-20 09:40:19 +01005530 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005531 {
5532 size_t frag_len, frag_off;
5533 unsigned char * const msg = hs_buf->data + 12;
5534
5535 /*
5536 * Check and copy current fragment
5537 */
5538
5539 /* Validation of header fields already done in
5540 * mbedtls_ssl_prepare_handshake_record(). */
5541 frag_off = ssl_get_hs_frag_off( ssl );
5542 frag_len = ssl_get_hs_frag_len( ssl );
5543
5544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5545 frag_off, frag_len ) );
5546 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5547
5548 if( hs_buf->is_fragmented )
5549 {
5550 unsigned char * const bitmask = msg + msg_len;
5551 ssl_bitmask_set( bitmask, frag_off, frag_len );
5552 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5553 msg_len ) == 0 );
5554 }
5555 else
5556 {
5557 hs_buf->is_complete = 1;
5558 }
5559
5560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5561 hs_buf->is_complete ? "" : "not yet " ) );
5562 }
5563
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005564 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005565 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005566
5567 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005568 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005569 break;
5570 }
5571
5572exit:
5573
5574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5575 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005576}
5577#endif /* MBEDTLS_SSL_PROTO_DTLS */
5578
Hanno Becker1097b342018-08-15 14:09:41 +01005579static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005580{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005581 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005582 * Consume last content-layer message and potentially
5583 * update in_msglen which keeps track of the contents'
5584 * consumption state.
5585 *
5586 * (1) Handshake messages:
5587 * Remove last handshake message, move content
5588 * and adapt in_msglen.
5589 *
5590 * (2) Alert messages:
5591 * Consume whole record content, in_msglen = 0.
5592 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005593 * (3) Change cipher spec:
5594 * Consume whole record content, in_msglen = 0.
5595 *
5596 * (4) Application data:
5597 * Don't do anything - the record layer provides
5598 * the application data as a stream transport
5599 * and consumes through mbedtls_ssl_read only.
5600 *
5601 */
5602
5603 /* Case (1): Handshake messages */
5604 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005605 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005606 /* Hard assertion to be sure that no application data
5607 * is in flight, as corrupting ssl->in_msglen during
5608 * ssl->in_offt != NULL is fatal. */
5609 if( ssl->in_offt != NULL )
5610 {
5611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5613 }
5614
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005615 /*
5616 * Get next Handshake message in the current record
5617 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005618
Hanno Becker4a810fb2017-05-24 16:27:30 +01005619 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005620 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005621 * current handshake content: If DTLS handshake
5622 * fragmentation is used, that's the fragment
5623 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005624 * size here is faulty and should be changed at
5625 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005626 * (2) While it doesn't seem to cause problems, one
5627 * has to be very careful not to assume that in_hslen
5628 * is always <= in_msglen in a sensible communication.
5629 * Again, it's wrong for DTLS handshake fragmentation.
5630 * The following check is therefore mandatory, and
5631 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005632 * Additionally, ssl->in_hslen might be arbitrarily out of
5633 * bounds after handling a DTLS message with an unexpected
5634 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005635 */
5636 if( ssl->in_hslen < ssl->in_msglen )
5637 {
5638 ssl->in_msglen -= ssl->in_hslen;
5639 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5640 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005641
Hanno Becker4a810fb2017-05-24 16:27:30 +01005642 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5643 ssl->in_msg, ssl->in_msglen );
5644 }
5645 else
5646 {
5647 ssl->in_msglen = 0;
5648 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005649
Hanno Becker4a810fb2017-05-24 16:27:30 +01005650 ssl->in_hslen = 0;
5651 }
5652 /* Case (4): Application data */
5653 else if( ssl->in_offt != NULL )
5654 {
5655 return( 0 );
5656 }
5657 /* Everything else (CCS & Alerts) */
5658 else
5659 {
5660 ssl->in_msglen = 0;
5661 }
5662
Hanno Becker1097b342018-08-15 14:09:41 +01005663 return( 0 );
5664}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005665
Hanno Beckere74d5562018-08-15 14:26:08 +01005666static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5667{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005668 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005669 return( 1 );
5670
5671 return( 0 );
5672}
5673
Hanno Becker5f066e72018-08-16 14:56:31 +01005674#if defined(MBEDTLS_SSL_PROTO_DTLS)
5675
5676static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5677{
5678 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5679 if( hs == NULL )
5680 return;
5681
Hanno Becker01315ea2018-08-21 17:22:17 +01005682 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005683 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005684 hs->buffering.total_bytes_buffered -=
5685 hs->buffering.future_record.len;
5686
5687 mbedtls_free( hs->buffering.future_record.data );
5688 hs->buffering.future_record.data = NULL;
5689 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005690}
5691
5692static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5693{
5694 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5695 unsigned char * rec;
5696 size_t rec_len;
5697 unsigned rec_epoch;
5698
5699 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5700 return( 0 );
5701
5702 if( hs == NULL )
5703 return( 0 );
5704
Hanno Becker5f066e72018-08-16 14:56:31 +01005705 rec = hs->buffering.future_record.data;
5706 rec_len = hs->buffering.future_record.len;
5707 rec_epoch = hs->buffering.future_record.epoch;
5708
5709 if( rec == NULL )
5710 return( 0 );
5711
Hanno Becker4cb782d2018-08-20 11:19:05 +01005712 /* Only consider loading future records if the
5713 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005714 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005715 return( 0 );
5716
Hanno Becker5f066e72018-08-16 14:56:31 +01005717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5718
5719 if( rec_epoch != ssl->in_epoch )
5720 {
5721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5722 goto exit;
5723 }
5724
5725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5726
5727 /* Double-check that the record is not too large */
5728 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5729 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5730 {
5731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5733 }
5734
5735 memcpy( ssl->in_hdr, rec, rec_len );
5736 ssl->in_left = rec_len;
5737 ssl->next_record_offset = 0;
5738
5739 ssl_free_buffered_record( ssl );
5740
5741exit:
5742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5743 return( 0 );
5744}
5745
5746static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5747{
5748 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5749 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005750 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005751
5752 /* Don't buffer future records outside handshakes. */
5753 if( hs == NULL )
5754 return( 0 );
5755
5756 /* Only buffer handshake records (we are only interested
5757 * in Finished messages). */
5758 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5759 return( 0 );
5760
5761 /* Don't buffer more than one future epoch record. */
5762 if( hs->buffering.future_record.data != NULL )
5763 return( 0 );
5764
Hanno Becker01315ea2018-08-21 17:22:17 +01005765 /* Don't buffer record if there's not enough buffering space remaining. */
5766 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5767 hs->buffering.total_bytes_buffered ) )
5768 {
5769 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",
5770 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5771 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005772 return( 0 );
5773 }
5774
Hanno Becker5f066e72018-08-16 14:56:31 +01005775 /* Buffer record */
5776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5777 ssl->in_epoch + 1 ) );
5778 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5779 rec_hdr_len + ssl->in_msglen );
5780
5781 /* ssl_parse_record_header() only considers records
5782 * of the next epoch as candidates for buffering. */
5783 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005784 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005785
5786 hs->buffering.future_record.data =
5787 mbedtls_calloc( 1, hs->buffering.future_record.len );
5788 if( hs->buffering.future_record.data == NULL )
5789 {
5790 /* If we run out of RAM trying to buffer a
5791 * record from the next epoch, just ignore. */
5792 return( 0 );
5793 }
5794
Hanno Becker01315ea2018-08-21 17:22:17 +01005795 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005796
Hanno Becker01315ea2018-08-21 17:22:17 +01005797 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005798 return( 0 );
5799}
5800
5801#endif /* MBEDTLS_SSL_PROTO_DTLS */
5802
Hanno Beckere74d5562018-08-15 14:26:08 +01005803static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005804{
5805 int ret;
5806
Hanno Becker5f066e72018-08-16 14:56:31 +01005807#if defined(MBEDTLS_SSL_PROTO_DTLS)
5808 /* We might have buffered a future record; if so,
5809 * and if the epoch matches now, load it.
5810 * On success, this call will set ssl->in_left to
5811 * the length of the buffered record, so that
5812 * the calls to ssl_fetch_input() below will
5813 * essentially be no-ops. */
5814 ret = ssl_load_buffered_record( ssl );
5815 if( ret != 0 )
5816 return( ret );
5817#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005822 return( ret );
5823 }
5824
5825 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005828 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5829 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005830 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005831 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5832 {
5833 ret = ssl_buffer_future_record( ssl );
5834 if( ret != 0 )
5835 return( ret );
5836
5837 /* Fall through to handling of unexpected records */
5838 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5839 }
5840
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005841 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5842 {
5843 /* Skip unexpected record (but not whole datagram) */
5844 ssl->next_record_offset = ssl->in_msglen
5845 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005846
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5848 "(header)" ) );
5849 }
5850 else
5851 {
5852 /* Skip invalid record and the rest of the datagram */
5853 ssl->next_record_offset = 0;
5854 ssl->in_left = 0;
5855
5856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5857 "(header)" ) );
5858 }
5859
5860 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005861 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005862 }
5863#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005864 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005865 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005866
5867 /*
5868 * Read and optionally decrypt the message contents
5869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5871 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005874 return( ret );
5875 }
5876
5877 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005882 if( ssl->next_record_offset < ssl->in_left )
5883 {
5884 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5885 }
5886 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005887 else
5888#endif
5889 ssl->in_left = 0;
5890
5891 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005895 {
5896 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005897 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005898 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005899 /* Except when waiting for Finished as a bad mac here
5900 * probably means something went wrong in the handshake
5901 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5902 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5903 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5904 {
5905#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5906 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5907 {
5908 mbedtls_ssl_send_alert_message( ssl,
5909 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5910 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5911 }
5912#endif
5913 return( ret );
5914 }
5915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005917 if( ssl->conf->badmac_limit != 0 &&
5918 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5921 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005922 }
5923#endif
5924
Hanno Becker4a810fb2017-05-24 16:27:30 +01005925 /* As above, invalid records cause
5926 * dismissal of the whole datagram. */
5927
5928 ssl->next_record_offset = 0;
5929 ssl->in_left = 0;
5930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005932 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005933 }
5934
5935 return( ret );
5936 }
5937 else
5938#endif
5939 {
5940 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5942 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 mbedtls_ssl_send_alert_message( ssl,
5945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5946 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005947 }
5948#endif
5949 return( ret );
5950 }
5951 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005952
Simon Butcher99000142016-10-13 17:21:01 +01005953 return( 0 );
5954}
5955
5956int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5957{
5958 int ret;
5959
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005960 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005961 * Handle particular types of records
5962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005964 {
Simon Butcher99000142016-10-13 17:21:01 +01005965 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5966 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005967 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005969 }
5970
Hanno Beckere678eaa2018-08-21 14:57:46 +01005971 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005972 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005973 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005974 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5976 ssl->in_msglen ) );
5977 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005978 }
5979
Hanno Beckere678eaa2018-08-21 14:57:46 +01005980 if( ssl->in_msg[0] != 1 )
5981 {
5982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5983 ssl->in_msg[0] ) );
5984 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5985 }
5986
5987#if defined(MBEDTLS_SSL_PROTO_DTLS)
5988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5989 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5990 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5991 {
5992 if( ssl->handshake == NULL )
5993 {
5994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5995 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5996 }
5997
5998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5999 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6000 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006001#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006002 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006005 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006006 if( ssl->in_msglen != 2 )
6007 {
6008 /* Note: Standard allows for more than one 2 byte alert
6009 to be packed in a single message, but Mbed TLS doesn't
6010 currently support this. */
6011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6012 ssl->in_msglen ) );
6013 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6014 }
6015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 ssl->in_msg[0], ssl->in_msg[1] ) );
6018
6019 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006020 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006025 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 }
6028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6030 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6033 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006034 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006035
6036#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6037 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6038 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6039 {
Hanno Becker90333da2017-10-10 11:27:13 +01006040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006041 /* Will be handled when trying to parse ServerHello */
6042 return( 0 );
6043 }
6044#endif
6045
6046#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6047 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6048 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6049 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6050 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6051 {
6052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6053 /* Will be handled in mbedtls_ssl_parse_certificate() */
6054 return( 0 );
6055 }
6056#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6057
6058 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006059 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 }
6061
Hanno Beckerc76c6192017-06-06 10:03:17 +01006062#if defined(MBEDTLS_SSL_PROTO_DTLS)
6063 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6064 ssl->handshake != NULL &&
6065 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6066 {
6067 ssl_handshake_wrapup_free_hs_transform( ssl );
6068 }
6069#endif
6070
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 return( 0 );
6072}
6073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006075{
6076 int ret;
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6079 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6080 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006081 {
6082 return( ret );
6083 }
6084
6085 return( 0 );
6086}
6087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006089 unsigned char level,
6090 unsigned char message )
6091{
6092 int ret;
6093
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006094 if( ssl == NULL || ssl->conf == NULL )
6095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006101 ssl->out_msglen = 2;
6102 ssl->out_msg[0] = level;
6103 ssl->out_msg[1] = message;
6104
Hanno Becker67bc7c32018-08-06 11:33:50 +01006105 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006108 return( ret );
6109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006111
6112 return( 0 );
6113}
6114
Hanno Beckerb9d44792019-02-08 07:19:04 +00006115#if defined(MBEDTLS_X509_CRT_PARSE_C)
6116static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6117{
6118#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6119 if( session->peer_cert != NULL )
6120 {
6121 mbedtls_x509_crt_free( session->peer_cert );
6122 mbedtls_free( session->peer_cert );
6123 session->peer_cert = NULL;
6124 }
6125#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6126 if( session->peer_cert_digest != NULL )
6127 {
6128 /* Zeroization is not necessary. */
6129 mbedtls_free( session->peer_cert_digest );
6130 session->peer_cert_digest = NULL;
6131 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6132 session->peer_cert_digest_len = 0;
6133 }
6134#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6135}
6136#endif /* MBEDTLS_X509_CRT_PARSE_C */
6137
Paul Bakker5121ce52009-01-03 21:22:43 +00006138/*
6139 * Handshake functions
6140 */
Hanno Becker21489932019-02-05 13:20:55 +00006141#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006142/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006144{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006145 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6146 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006149
Hanno Becker7177a882019-02-05 13:36:46 +00006150 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006153 ssl->state++;
6154 return( 0 );
6155 }
6156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006159}
6160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006162{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006163 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6164 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006167
Hanno Becker7177a882019-02-05 13:36:46 +00006168 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006171 ssl->state++;
6172 return( 0 );
6173 }
6174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006177}
Gilles Peskinef9828522017-05-03 12:28:43 +02006178
Hanno Becker21489932019-02-05 13:20:55 +00006179#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006180/* Some certificate support -> implement write and parse */
6181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006185 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006187 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6188 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006191
Hanno Becker7177a882019-02-05 13:36:46 +00006192 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006195 ssl->state++;
6196 return( 0 );
6197 }
6198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006200 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 {
6202 if( ssl->client_auth == 0 )
6203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006205 ssl->state++;
6206 return( 0 );
6207 }
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006210 /*
6211 * If using SSLv3 and got no cert, send an Alert message
6212 * (otherwise an empty Certificate message will be sent).
6213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6215 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006216 {
6217 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6219 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6220 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006223 goto write_msg;
6224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#endif /* MBEDTLS_SSL_CLI_C */
6228#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006229 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6234 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 }
6236 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006237#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240
6241 /*
6242 * 0 . 0 handshake type
6243 * 1 . 3 handshake length
6244 * 4 . 6 length of all certs
6245 * 7 . 9 length of cert. 1
6246 * 10 . n-1 peer certificate
6247 * n . n+2 length of cert. 2
6248 * n+3 . ... upper level cert, etc.
6249 */
6250 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252
Paul Bakker29087132010-03-21 21:03:34 +00006253 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 {
6255 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006256 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006259 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 }
6262
6263 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6264 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6265 ssl->out_msg[i + 2] = (unsigned char)( n );
6266
6267 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6268 i += n; crt = crt->next;
6269 }
6270
6271 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6272 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6273 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6274
6275 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6277 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006278
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006279#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006280write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006281#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006282
6283 ssl->state++;
6284
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006285 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006288 return( ret );
6289 }
6290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006292
Paul Bakkered27a042013-04-18 22:46:23 +02006293 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006294}
6295
Hanno Becker84879e32019-01-31 07:44:03 +00006296#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006297
6298#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006299static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6300 unsigned char *crt_buf,
6301 size_t crt_buf_len )
6302{
6303 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6304
6305 if( peer_crt == NULL )
6306 return( -1 );
6307
6308 if( peer_crt->raw.len != crt_buf_len )
6309 return( -1 );
6310
Hanno Becker46f34d02019-02-08 14:00:04 +00006311 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006312}
Hanno Becker177475a2019-02-05 17:02:46 +00006313#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6314static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6315 unsigned char *crt_buf,
6316 size_t crt_buf_len )
6317{
6318 int ret;
6319 unsigned char const * const peer_cert_digest =
6320 ssl->session->peer_cert_digest;
6321 mbedtls_md_type_t const peer_cert_digest_type =
6322 ssl->session->peer_cert_digest_type;
6323 mbedtls_md_info_t const * const digest_info =
6324 mbedtls_md_info_from_type( peer_cert_digest_type );
6325 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6326 size_t digest_len;
6327
6328 if( peer_cert_digest == NULL || digest_info == NULL )
6329 return( -1 );
6330
6331 digest_len = mbedtls_md_get_size( digest_info );
6332 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6333 return( -1 );
6334
6335 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6336 if( ret != 0 )
6337 return( -1 );
6338
6339 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6340}
6341#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006342#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006343
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006344/*
6345 * Once the certificate message is read, parse it into a cert chain and
6346 * perform basic checks, but leave actual verification to the caller
6347 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006348static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6349 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006350{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006351 int ret;
6352#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6353 int crt_cnt=0;
6354#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006355 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006356 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006361 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6362 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 }
6365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6367 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006370 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6371 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006373 }
6374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006376
Paul Bakker5121ce52009-01-03 21:22:43 +00006377 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006380 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006381
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006382 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006386 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6387 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006389 }
6390
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006391 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6392 i += 3;
6393
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006394 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006395 while( i < ssl->in_hslen )
6396 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006397 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006398 if ( i + 3 > ssl->in_hslen ) {
6399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006400 mbedtls_ssl_send_alert_message( ssl,
6401 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6402 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006403 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6404 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006405 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6406 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006407 if( ssl->in_msg[i] != 0 )
6408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006410 mbedtls_ssl_send_alert_message( ssl,
6411 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6412 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006414 }
6415
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006416 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006417 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6418 | (unsigned int) ssl->in_msg[i + 2];
6419 i += 3;
6420
6421 if( n < 128 || i + n > ssl->in_hslen )
6422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006424 mbedtls_ssl_send_alert_message( ssl,
6425 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6426 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006428 }
6429
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006430 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006431#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6432 if( crt_cnt++ == 0 &&
6433 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6434 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006435 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006436 /* During client-side renegotiation, check that the server's
6437 * end-CRTs hasn't changed compared to the initial handshake,
6438 * mitigating the triple handshake attack. On success, reuse
6439 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6441 if( ssl_check_peer_crt_unchanged( ssl,
6442 &ssl->in_msg[i],
6443 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006444 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6446 mbedtls_ssl_send_alert_message( ssl,
6447 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6448 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6449 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006450 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006451
6452 /* Now we can safely free the original chain. */
6453 ssl_clear_peer_cert( ssl->session );
6454 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006455#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6456
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006457 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006458#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006459 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006460#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006461 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006462 * it in-place from the input buffer instead of making a copy. */
6463 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6464#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006465 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006466 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006467 case 0: /*ok*/
6468 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6469 /* Ignore certificate with an unknown algorithm: maybe a
6470 prior certificate was already trusted. */
6471 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006472
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006473 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6474 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6475 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006476
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006477 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6478 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6479 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006480
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006481 default:
6482 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6483 crt_parse_der_failed:
6484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6485 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 }
6488
6489 i += n;
6490 }
6491
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006492 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006493 return( 0 );
6494}
6495
Hanno Becker4a55f632019-02-05 12:49:06 +00006496#if defined(MBEDTLS_SSL_SRV_C)
6497static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6498{
6499 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6500 return( -1 );
6501
6502#if defined(MBEDTLS_SSL_PROTO_SSL3)
6503 /*
6504 * Check if the client sent an empty certificate
6505 */
6506 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6507 {
6508 if( ssl->in_msglen == 2 &&
6509 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6510 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6511 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6512 {
6513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6514 return( 0 );
6515 }
6516
6517 return( -1 );
6518 }
6519#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6520
6521#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6522 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6523 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6524 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6525 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6526 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6527 {
6528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6529 return( 0 );
6530 }
6531
6532 return( -1 );
6533#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6534 MBEDTLS_SSL_PROTO_TLS1_2 */
6535}
6536#endif /* MBEDTLS_SSL_SRV_C */
6537
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006538/* Check if a certificate message is expected.
6539 * Return either
6540 * - SSL_CERTIFICATE_EXPECTED, or
6541 * - SSL_CERTIFICATE_SKIP
6542 * indicating whether a Certificate message is expected or not.
6543 */
6544#define SSL_CERTIFICATE_EXPECTED 0
6545#define SSL_CERTIFICATE_SKIP 1
6546static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6547 int authmode )
6548{
6549 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006550 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006551
6552 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6553 return( SSL_CERTIFICATE_SKIP );
6554
6555#if defined(MBEDTLS_SSL_SRV_C)
6556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6557 {
6558 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6559 return( SSL_CERTIFICATE_SKIP );
6560
6561 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6562 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006563 ssl->session_negotiate->verify_result =
6564 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6565 return( SSL_CERTIFICATE_SKIP );
6566 }
6567 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006568#else
6569 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006570#endif /* MBEDTLS_SSL_SRV_C */
6571
6572 return( SSL_CERTIFICATE_EXPECTED );
6573}
6574
Hanno Becker68636192019-02-05 14:36:34 +00006575static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6576 int authmode,
6577 mbedtls_x509_crt *chain,
6578 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006579{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006580 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006581 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006582 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006583 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006584
Hanno Becker8927c832019-04-03 12:52:50 +01006585 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6586 void *p_vrfy;
6587
Hanno Becker68636192019-02-05 14:36:34 +00006588 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6589 return( 0 );
6590
Hanno Becker8927c832019-04-03 12:52:50 +01006591 if( ssl->f_vrfy != NULL )
6592 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006594 f_vrfy = ssl->f_vrfy;
6595 p_vrfy = ssl->p_vrfy;
6596 }
6597 else
6598 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006600 f_vrfy = ssl->conf->f_vrfy;
6601 p_vrfy = ssl->conf->p_vrfy;
6602 }
6603
Hanno Becker68636192019-02-05 14:36:34 +00006604 /*
6605 * Main check: verify certificate
6606 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006607#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6608 if( ssl->conf->f_ca_cb != NULL )
6609 {
6610 ((void) rs_ctx);
6611 have_ca_chain = 1;
6612
6613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006614 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006615 chain,
6616 ssl->conf->f_ca_cb,
6617 ssl->conf->p_ca_cb,
6618 ssl->conf->cert_profile,
6619 ssl->hostname,
6620 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006621 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006622 }
6623 else
6624#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6625 {
6626 mbedtls_x509_crt *ca_chain;
6627 mbedtls_x509_crl *ca_crl;
6628
6629#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6630 if( ssl->handshake->sni_ca_chain != NULL )
6631 {
6632 ca_chain = ssl->handshake->sni_ca_chain;
6633 ca_crl = ssl->handshake->sni_ca_crl;
6634 }
6635 else
6636#endif
6637 {
6638 ca_chain = ssl->conf->ca_chain;
6639 ca_crl = ssl->conf->ca_crl;
6640 }
6641
6642 if( ca_chain != NULL )
6643 have_ca_chain = 1;
6644
6645 ret = mbedtls_x509_crt_verify_restartable(
6646 chain,
6647 ca_chain, ca_crl,
6648 ssl->conf->cert_profile,
6649 ssl->hostname,
6650 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006651 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006652 }
Hanno Becker68636192019-02-05 14:36:34 +00006653
6654 if( ret != 0 )
6655 {
6656 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6657 }
6658
6659#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6660 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6661 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6662#endif
6663
6664 /*
6665 * Secondary checks: always done, but change 'ret' only if it was 0
6666 */
6667
6668#if defined(MBEDTLS_ECP_C)
6669 {
6670 const mbedtls_pk_context *pk = &chain->pk;
6671
6672 /* If certificate uses an EC key, make sure the curve is OK */
6673 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6674 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6675 {
6676 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6677
6678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6679 if( ret == 0 )
6680 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6681 }
6682 }
6683#endif /* MBEDTLS_ECP_C */
6684
6685 if( mbedtls_ssl_check_cert_usage( chain,
6686 ciphersuite_info,
6687 ! ssl->conf->endpoint,
6688 &ssl->session_negotiate->verify_result ) != 0 )
6689 {
6690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6691 if( ret == 0 )
6692 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6693 }
6694
6695 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6696 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6697 * with details encoded in the verification flags. All other kinds
6698 * of error codes, including those from the user provided f_vrfy
6699 * functions, are treated as fatal and lead to a failure of
6700 * ssl_parse_certificate even if verification was optional. */
6701 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6702 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6703 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6704 {
6705 ret = 0;
6706 }
6707
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006708 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006709 {
6710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6711 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6712 }
6713
6714 if( ret != 0 )
6715 {
6716 uint8_t alert;
6717
6718 /* The certificate may have been rejected for several reasons.
6719 Pick one and send the corresponding alert. Which alert to send
6720 may be a subject of debate in some cases. */
6721 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6722 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6723 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6724 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6725 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6726 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6727 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6728 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6729 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6730 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6731 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6732 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6733 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6734 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6735 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6736 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6737 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6738 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6739 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6740 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6741 else
6742 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6743 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6744 alert );
6745 }
6746
6747#if defined(MBEDTLS_DEBUG_C)
6748 if( ssl->session_negotiate->verify_result != 0 )
6749 {
6750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6751 ssl->session_negotiate->verify_result ) );
6752 }
6753 else
6754 {
6755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6756 }
6757#endif /* MBEDTLS_DEBUG_C */
6758
6759 return( ret );
6760}
6761
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006762#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6763static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6764 unsigned char *start, size_t len )
6765{
6766 int ret;
6767 /* Remember digest of the peer's end-CRT. */
6768 ssl->session_negotiate->peer_cert_digest =
6769 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6770 if( ssl->session_negotiate->peer_cert_digest == NULL )
6771 {
6772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6773 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6774 mbedtls_ssl_send_alert_message( ssl,
6775 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6776 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6777
6778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6779 }
6780
6781 ret = mbedtls_md( mbedtls_md_info_from_type(
6782 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6783 start, len,
6784 ssl->session_negotiate->peer_cert_digest );
6785
6786 ssl->session_negotiate->peer_cert_digest_type =
6787 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6788 ssl->session_negotiate->peer_cert_digest_len =
6789 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6790
6791 return( ret );
6792}
6793
6794static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6795 unsigned char *start, size_t len )
6796{
6797 unsigned char *end = start + len;
6798 int ret;
6799
6800 /* Make a copy of the peer's raw public key. */
6801 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6802 ret = mbedtls_pk_parse_subpubkey( &start, end,
6803 &ssl->handshake->peer_pubkey );
6804 if( ret != 0 )
6805 {
6806 /* We should have parsed the public key before. */
6807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6808 }
6809
6810 return( 0 );
6811}
6812#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6813
Hanno Becker68636192019-02-05 14:36:34 +00006814int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6815{
6816 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006817 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006818#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6819 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6820 ? ssl->handshake->sni_authmode
6821 : ssl->conf->authmode;
6822#else
6823 const int authmode = ssl->conf->authmode;
6824#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006825 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006826 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006827
6828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6829
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006830 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6831 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006832 {
6833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006834 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006835 }
6836
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006837#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6838 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006839 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006840 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006841 chain = ssl->handshake->ecrs_peer_cert;
6842 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006843 goto crt_verify;
6844 }
6845#endif
6846
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006847 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006848 {
6849 /* mbedtls_ssl_read_record may have sent an alert already. We
6850 let it decide whether to alert. */
6851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006852 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006853 }
6854
Hanno Becker4a55f632019-02-05 12:49:06 +00006855#if defined(MBEDTLS_SSL_SRV_C)
6856 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6857 {
6858 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006859
6860 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006861 ret = 0;
6862 else
6863 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006864
Hanno Becker6bdfab22019-02-05 13:11:17 +00006865 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006866 }
6867#endif /* MBEDTLS_SSL_SRV_C */
6868
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006869 /* Clear existing peer CRT structure in case we tried to
6870 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006871 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006872
6873 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6874 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006875 {
6876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6877 sizeof( mbedtls_x509_crt ) ) );
6878 mbedtls_ssl_send_alert_message( ssl,
6879 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6880 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006881
Hanno Becker3dad3112019-02-05 17:19:52 +00006882 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6883 goto exit;
6884 }
6885 mbedtls_x509_crt_init( chain );
6886
6887 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006888 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006889 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006890
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006891#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6892 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006893 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006894
6895crt_verify:
6896 if( ssl->handshake->ecrs_enabled)
6897 rs_ctx = &ssl->handshake->ecrs_ctx;
6898#endif
6899
Hanno Becker68636192019-02-05 14:36:34 +00006900 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006901 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006902 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006903 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006904
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006905#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006906 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006907 unsigned char *crt_start, *pk_start;
6908 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006909
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006910 /* We parse the CRT chain without copying, so
6911 * these pointers point into the input buffer,
6912 * and are hence still valid after freeing the
6913 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006914
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006915 crt_start = chain->raw.p;
6916 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006917
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006918 pk_start = chain->pk_raw.p;
6919 pk_len = chain->pk_raw.len;
6920
6921 /* Free the CRT structures before computing
6922 * digest and copying the peer's public key. */
6923 mbedtls_x509_crt_free( chain );
6924 mbedtls_free( chain );
6925 chain = NULL;
6926
6927 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006928 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006929 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006930
6931 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6932 if( ret != 0 )
6933 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006934 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006935#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6936 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006937 ssl->session_negotiate->peer_cert = chain;
6938 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006939#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006942
Hanno Becker6bdfab22019-02-05 13:11:17 +00006943exit:
6944
Hanno Becker3dad3112019-02-05 17:19:52 +00006945 if( ret == 0 )
6946 ssl->state++;
6947
6948#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6949 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6950 {
6951 ssl->handshake->ecrs_peer_cert = chain;
6952 chain = NULL;
6953 }
6954#endif
6955
6956 if( chain != NULL )
6957 {
6958 mbedtls_x509_crt_free( chain );
6959 mbedtls_free( chain );
6960 }
6961
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 return( ret );
6963}
Hanno Becker21489932019-02-05 13:20:55 +00006964#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006967{
6968 int ret;
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006973 ssl->out_msglen = 1;
6974 ssl->out_msg[0] = 1;
6975
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 ssl->state++;
6977
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006978 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006979 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006981 return( ret );
6982 }
6983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006985
6986 return( 0 );
6987}
6988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006990{
6991 int ret;
6992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006994
Hanno Becker327c93b2018-08-15 13:56:18 +01006995 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006998 return( ret );
6999 }
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007004 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7005 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 }
7008
Hanno Beckere678eaa2018-08-21 14:57:46 +01007009 /* CCS records are only accepted if they have length 1 and content '1',
7010 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007011
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007012 /*
7013 * Switch to our negotiated transform and session parameters for inbound
7014 * data.
7015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007017 ssl->transform_in = ssl->transform_negotiate;
7018 ssl->session_in = ssl->session_negotiate;
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007021 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007024 ssl_dtls_replay_reset( ssl );
7025#endif
7026
7027 /* Increment epoch */
7028 if( ++ssl->in_epoch == 0 )
7029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007031 /* This is highly unlikely to happen for legitimate reasons, so
7032 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007034 }
7035 }
7036 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007038 memset( ssl->in_ctr, 0, 8 );
7039
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007040 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7043 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007048 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7049 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007051 }
7052 }
7053#endif
7054
Paul Bakker5121ce52009-01-03 21:22:43 +00007055 ssl->state++;
7056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
7059 return( 0 );
7060}
7061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7063 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007064{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007065 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7068 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7069 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007070 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007071 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7074#if defined(MBEDTLS_SHA512_C)
7075 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007076 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7077 else
7078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#if defined(MBEDTLS_SHA256_C)
7080 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007081 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007082 else
7083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007087 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007088 }
Paul Bakker380da532012-04-18 16:10:25 +00007089}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7094 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007095 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7096 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7099#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007100#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007101 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007102 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7103#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007104 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007105#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007108#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007109 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007110 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007111#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007112 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007113#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007116}
7117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007119 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7122 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007123 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7124 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7127#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007128#if defined(MBEDTLS_USE_PSA_CRYPTO)
7129 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7130#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007131 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007132#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007135#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007136 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007137#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007138 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007139#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007142}
7143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7145 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7146static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007147 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007148{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007149 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7150 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007151}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007152#endif
Paul Bakker380da532012-04-18 16:10:25 +00007153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7155#if defined(MBEDTLS_SHA256_C)
7156static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007157 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007158{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007159#if defined(MBEDTLS_USE_PSA_CRYPTO)
7160 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7161#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007162 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007163#endif
Paul Bakker380da532012-04-18 16:10:25 +00007164}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007165#endif
Paul Bakker380da532012-04-18 16:10:25 +00007166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#if defined(MBEDTLS_SHA512_C)
7168static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007169 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007170{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007171#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007172 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007173#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007174 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007175#endif
Paul Bakker380da532012-04-18 16:10:25 +00007176}
Paul Bakker769075d2012-11-24 11:26:46 +01007177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007181static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007183{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007184 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007185 mbedtls_md5_context md5;
7186 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007187
Paul Bakker5121ce52009-01-03 21:22:43 +00007188 unsigned char padbuf[48];
7189 unsigned char md5sum[16];
7190 unsigned char sha1sum[20];
7191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007193 if( !session )
7194 session = ssl->session;
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007197
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007198 mbedtls_md5_init( &md5 );
7199 mbedtls_sha1_init( &sha1 );
7200
7201 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7202 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007203
7204 /*
7205 * SSLv3:
7206 * hash =
7207 * MD5( master + pad2 +
7208 * MD5( handshake + sender + master + pad1 ) )
7209 * + SHA1( master + pad2 +
7210 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007211 */
7212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007214 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7215 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007216#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007219 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7220 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007221#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007224 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007225
Paul Bakker1ef83d62012-04-11 12:09:53 +00007226 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007227
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007228 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7229 mbedtls_md5_update_ret( &md5, session->master, 48 );
7230 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7231 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007232
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007233 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7234 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7235 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7236 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007237
Paul Bakker1ef83d62012-04-11 12:09:53 +00007238 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007239
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007240 mbedtls_md5_starts_ret( &md5 );
7241 mbedtls_md5_update_ret( &md5, session->master, 48 );
7242 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7243 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7244 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007245
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007246 mbedtls_sha1_starts_ret( &sha1 );
7247 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7248 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7249 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7250 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007253
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007254 mbedtls_md5_free( &md5 );
7255 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007256
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007257 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7258 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7259 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007266static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007268{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007269 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007270 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007271 mbedtls_md5_context md5;
7272 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007273 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007276 if( !session )
7277 session = ssl->session;
7278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007280
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007281 mbedtls_md5_init( &md5 );
7282 mbedtls_sha1_init( &sha1 );
7283
7284 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7285 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007286
Paul Bakker1ef83d62012-04-11 12:09:53 +00007287 /*
7288 * TLSv1:
7289 * hash = PRF( master, finished_label,
7290 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7291 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007294 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7295 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007296#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007299 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7300 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007301#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007304 ? "client finished"
7305 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007306
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007307 mbedtls_md5_finish_ret( &md5, padbuf );
7308 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007309
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007310 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007311 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007314
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007315 mbedtls_md5_free( &md5 );
7316 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007317
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007318 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007321}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7325#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007326static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007328{
7329 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007330 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007331 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007332#if defined(MBEDTLS_USE_PSA_CRYPTO)
7333 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007334 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007335 psa_status_t status;
7336#else
7337 mbedtls_sha256_context sha256;
7338#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007341 if( !session )
7342 session = ssl->session;
7343
Andrzej Kurekeb342242019-01-29 09:14:33 -05007344 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7345 ? "client finished"
7346 : "server finished";
7347
7348#if defined(MBEDTLS_USE_PSA_CRYPTO)
7349 sha256_psa = psa_hash_operation_init();
7350
7351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7352
7353 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7354 if( status != PSA_SUCCESS )
7355 {
7356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7357 return;
7358 }
7359
7360 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7361 if( status != PSA_SUCCESS )
7362 {
7363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7364 return;
7365 }
7366 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7367#else
7368
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007369 mbedtls_sha256_init( &sha256 );
7370
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007372
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007373 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007374
7375 /*
7376 * TLSv1.2:
7377 * hash = PRF( master, finished_label,
7378 * Hash( handshake ) )[0.11]
7379 */
7380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if !defined(MBEDTLS_SHA256_ALT)
7382 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007383 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007384#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007385
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007386 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007387 mbedtls_sha256_free( &sha256 );
7388#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007389
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007390 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007391 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007394
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007395 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007402static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007404{
7405 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007406 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007407 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007408#if defined(MBEDTLS_USE_PSA_CRYPTO)
7409 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007410 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007411 psa_status_t status;
7412#else
7413 mbedtls_sha512_context sha512;
7414#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007417 if( !session )
7418 session = ssl->session;
7419
Andrzej Kurekeb342242019-01-29 09:14:33 -05007420 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7421 ? "client finished"
7422 : "server finished";
7423
7424#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007425 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426
7427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7428
Andrzej Kurek972fba52019-01-30 03:29:12 -05007429 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007430 if( status != PSA_SUCCESS )
7431 {
7432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7433 return;
7434 }
7435
Andrzej Kurek972fba52019-01-30 03:29:12 -05007436 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007437 if( status != PSA_SUCCESS )
7438 {
7439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7440 return;
7441 }
7442 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7443#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007444 mbedtls_sha512_init( &sha512 );
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007447
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007448 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007449
7450 /*
7451 * TLSv1.2:
7452 * hash = PRF( master, finished_label,
7453 * Hash( handshake ) )[0.11]
7454 */
7455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007457 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7458 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007459#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007460
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007461 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007462 mbedtls_sha512_free( &sha512 );
7463#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007464
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007465 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007466 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007469
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007470 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#endif /* MBEDTLS_SHA512_C */
7475#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007480
7481 /*
7482 * Free our handshake params
7483 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007484 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007486 ssl->handshake = NULL;
7487
7488 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007489 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007490 */
7491 if( ssl->transform )
7492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 mbedtls_ssl_transform_free( ssl->transform );
7494 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007495 }
7496 ssl->transform = ssl->transform_negotiate;
7497 ssl->transform_negotiate = NULL;
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007500}
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007503{
7504 int resume = ssl->handshake->resume;
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SSL_RENEGOTIATION)
7509 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007512 ssl->renego_records_seen = 0;
7513 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007514#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007515
7516 /*
7517 * Free the previous session and switch in the current one
7518 */
Paul Bakker0a597072012-09-25 21:55:46 +00007519 if( ssl->session )
7520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007522 /* RFC 7366 3.1: keep the EtM state */
7523 ssl->session_negotiate->encrypt_then_mac =
7524 ssl->session->encrypt_then_mac;
7525#endif
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 mbedtls_ssl_session_free( ssl->session );
7528 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007529 }
7530 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007531 ssl->session_negotiate = NULL;
7532
Paul Bakker0a597072012-09-25 21:55:46 +00007533 /*
7534 * Add cache entry
7535 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007536 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007537 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007538 resume == 0 )
7539 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007540 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007542 }
Paul Bakker0a597072012-09-25 21:55:46 +00007543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007545 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007546 ssl->handshake->flight != NULL )
7547 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007548 /* Cancel handshake timer */
7549 ssl_set_timer( ssl, 0 );
7550
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007551 /* Keep last flight around in case we need to resend it:
7552 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007554 }
7555 else
7556#endif
7557 ssl_handshake_wrapup_free_hs_transform( ssl );
7558
Paul Bakker48916f92012-09-16 19:57:18 +00007559 ssl->state++;
7560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007562}
7563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007565{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007566 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007569
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007570 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007571
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007572 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007573
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007574 /*
7575 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7576 * may define some other value. Currently (early 2016), no defined
7577 * ciphersuite does this (and this is unlikely to change as activity has
7578 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007583 ssl->verify_data_len = hash_len;
7584 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007585#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007586
Paul Bakker5121ce52009-01-03 21:22:43 +00007587 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7589 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007590
7591 /*
7592 * In case of session resuming, invert the client and server
7593 * ChangeCipherSpec messages order.
7594 */
Paul Bakker0a597072012-09-25 21:55:46 +00007595 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007598 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007602 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007605 }
7606 else
7607 ssl->state++;
7608
Paul Bakker48916f92012-09-16 19:57:18 +00007609 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007610 * Switch to our negotiated transform and session parameters for outbound
7611 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007617 {
7618 unsigned char i;
7619
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007620 /* Remember current epoch settings for resending */
7621 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007622 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007623
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007624 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007625 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007626
7627 /* Increment epoch */
7628 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007629 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007630 break;
7631
7632 /* The loop goes to its end iff the counter is wrapping */
7633 if( i == 0 )
7634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7636 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007637 }
7638 }
7639 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007641 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007642
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007643 ssl->transform_out = ssl->transform_negotiate;
7644 ssl->session_out = ssl->session_negotiate;
7645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7647 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7652 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007653 }
7654 }
7655#endif
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007660#endif
7661
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007662 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007663 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007665 return( ret );
7666 }
7667
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007668#if defined(MBEDTLS_SSL_PROTO_DTLS)
7669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7670 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7671 {
7672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7673 return( ret );
7674 }
7675#endif
7676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007678
7679 return( 0 );
7680}
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007683#define SSL_MAX_HASH_LEN 36
7684#else
7685#define SSL_MAX_HASH_LEN 12
7686#endif
7687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007689{
Paul Bakker23986e52011-04-24 08:57:21 +00007690 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007691 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007692 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007695
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007696 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007697
Hanno Becker327c93b2018-08-15 13:56:18 +01007698 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007701 return( ret );
7702 }
7703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7708 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007710 }
7711
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007712 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713#if defined(MBEDTLS_SSL_PROTO_SSL3)
7714 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007715 hash_len = 36;
7716 else
7717#endif
7718 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7721 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007724 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7725 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007727 }
7728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007730 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007733 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7734 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007736 }
7737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007739 ssl->verify_data_len = hash_len;
7740 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007741#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007742
Paul Bakker0a597072012-09-25 21:55:46 +00007743 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007746 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007750 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007752#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007753 }
7754 else
7755 ssl->state++;
7756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007758 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007760#endif
7761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007763
7764 return( 0 );
7765}
7766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7772 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7773 mbedtls_md5_init( &handshake->fin_md5 );
7774 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007775 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7776 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7779#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007780#if defined(MBEDTLS_USE_PSA_CRYPTO)
7781 handshake->fin_sha256_psa = psa_hash_operation_init();
7782 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7783#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007785 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007786#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007789#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007790 handshake->fin_sha384_psa = psa_hash_operation_init();
7791 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007792#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007794 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007795#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007798
7799 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007800
7801#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7802 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7803 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7804#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#if defined(MBEDTLS_DHM_C)
7807 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007808#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809#if defined(MBEDTLS_ECDH_C)
7810 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007811#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007812#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007813 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007814#if defined(MBEDTLS_SSL_CLI_C)
7815 handshake->ecjpake_cache = NULL;
7816 handshake->ecjpake_cache_len = 0;
7817#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007818#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007819
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007820#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007821 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007822#endif
7823
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007824#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7825 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7826#endif
Hanno Becker75173122019-02-06 16:18:31 +00007827
7828#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7829 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7830 mbedtls_pk_init( &handshake->peer_pubkey );
7831#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007832}
7833
Hanno Beckera18d1322018-01-03 14:27:32 +00007834void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7839 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007840
Hanno Beckerd56ed242018-01-03 15:32:51 +00007841#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 mbedtls_md_init( &transform->md_ctx_enc );
7843 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007844#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007845}
7846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007850}
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007853{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007854 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007855 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007857 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007859 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007860 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007861
7862 /*
7863 * Either the pointers are now NULL or cleared properly and can be freed.
7864 * Now allocate missing structures.
7865 */
7866 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007867 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007868 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007869 }
Paul Bakker48916f92012-09-16 19:57:18 +00007870
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007871 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007872 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007873 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007874 }
Paul Bakker48916f92012-09-16 19:57:18 +00007875
Paul Bakker82788fb2014-10-20 13:59:19 +02007876 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007877 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007878 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007879 }
Paul Bakker48916f92012-09-16 19:57:18 +00007880
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007881 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007882 if( ssl->handshake == NULL ||
7883 ssl->transform_negotiate == NULL ||
7884 ssl->session_negotiate == NULL )
7885 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 mbedtls_free( ssl->handshake );
7889 mbedtls_free( ssl->transform_negotiate );
7890 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007891
7892 ssl->handshake = NULL;
7893 ssl->transform_negotiate = NULL;
7894 ssl->session_negotiate = NULL;
7895
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007896 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007897 }
7898
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007899 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007901 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007902 ssl_handshake_params_init( ssl->handshake );
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007905 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7906 {
7907 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007908
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007909 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7910 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7911 else
7912 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007913
7914 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007915 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007916#endif
7917
Paul Bakker48916f92012-09-16 19:57:18 +00007918 return( 0 );
7919}
7920
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007921#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007922/* Dummy cookie callbacks for defaults */
7923static int ssl_cookie_write_dummy( void *ctx,
7924 unsigned char **p, unsigned char *end,
7925 const unsigned char *cli_id, size_t cli_id_len )
7926{
7927 ((void) ctx);
7928 ((void) p);
7929 ((void) end);
7930 ((void) cli_id);
7931 ((void) cli_id_len);
7932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007934}
7935
7936static int ssl_cookie_check_dummy( void *ctx,
7937 const unsigned char *cookie, size_t cookie_len,
7938 const unsigned char *cli_id, size_t cli_id_len )
7939{
7940 ((void) ctx);
7941 ((void) cookie);
7942 ((void) cookie_len);
7943 ((void) cli_id);
7944 ((void) cli_id_len);
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007947}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007948#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007949
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007950/* Once ssl->out_hdr as the address of the beginning of the
7951 * next outgoing record is set, deduce the other pointers.
7952 *
7953 * Note: For TLS, we save the implicit record sequence number
7954 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7955 * and the caller has to make sure there's space for this.
7956 */
7957
7958static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7959 mbedtls_ssl_transform *transform )
7960{
7961#if defined(MBEDTLS_SSL_PROTO_DTLS)
7962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7963 {
7964 ssl->out_ctr = ssl->out_hdr + 3;
7965 ssl->out_len = ssl->out_hdr + 11;
7966 ssl->out_iv = ssl->out_hdr + 13;
7967 }
7968 else
7969#endif
7970 {
7971 ssl->out_ctr = ssl->out_hdr - 8;
7972 ssl->out_len = ssl->out_hdr + 3;
7973 ssl->out_iv = ssl->out_hdr + 5;
7974 }
7975
7976 /* Adjust out_msg to make space for explicit IV, if used. */
7977 if( transform != NULL &&
7978 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7979 {
7980 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7981 }
7982 else
7983 ssl->out_msg = ssl->out_iv;
7984}
7985
7986/* Once ssl->in_hdr as the address of the beginning of the
7987 * next incoming record is set, deduce the other pointers.
7988 *
7989 * Note: For TLS, we save the implicit record sequence number
7990 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7991 * and the caller has to make sure there's space for this.
7992 */
7993
7994static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7995 mbedtls_ssl_transform *transform )
7996{
7997#if defined(MBEDTLS_SSL_PROTO_DTLS)
7998 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7999 {
8000 ssl->in_ctr = ssl->in_hdr + 3;
8001 ssl->in_len = ssl->in_hdr + 11;
8002 ssl->in_iv = ssl->in_hdr + 13;
8003 }
8004 else
8005#endif
8006 {
8007 ssl->in_ctr = ssl->in_hdr - 8;
8008 ssl->in_len = ssl->in_hdr + 3;
8009 ssl->in_iv = ssl->in_hdr + 5;
8010 }
8011
8012 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
8013 if( transform != NULL &&
8014 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8015 {
8016 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
8017 }
8018 else
8019 ssl->in_msg = ssl->in_iv;
8020}
8021
Paul Bakker5121ce52009-01-03 21:22:43 +00008022/*
8023 * Initialize an SSL context
8024 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008025void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8026{
8027 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8028}
8029
8030/*
8031 * Setup an SSL context
8032 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008033
8034static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8035{
8036 /* Set the incoming and outgoing record pointers. */
8037#if defined(MBEDTLS_SSL_PROTO_DTLS)
8038 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8039 {
8040 ssl->out_hdr = ssl->out_buf;
8041 ssl->in_hdr = ssl->in_buf;
8042 }
8043 else
8044#endif /* MBEDTLS_SSL_PROTO_DTLS */
8045 {
8046 ssl->out_hdr = ssl->out_buf + 8;
8047 ssl->in_hdr = ssl->in_buf + 8;
8048 }
8049
8050 /* Derive other internal pointers. */
8051 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
8052 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
8053}
8054
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008055int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008056 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008057{
Paul Bakker48916f92012-09-16 19:57:18 +00008058 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008059
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008060 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008061
8062 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008063 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008064 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008065
8066 /* Set to NULL in case of an error condition */
8067 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008068
Angus Grattond8213d02016-05-25 20:56:48 +10008069 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8070 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008071 {
Angus Grattond8213d02016-05-25 20:56:48 +10008072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008073 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008074 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008075 }
8076
8077 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8078 if( ssl->out_buf == NULL )
8079 {
8080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008081 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008082 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008083 }
8084
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008085 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008086
Paul Bakker48916f92012-09-16 19:57:18 +00008087 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008088 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008089
8090 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008091
8092error:
8093 mbedtls_free( ssl->in_buf );
8094 mbedtls_free( ssl->out_buf );
8095
8096 ssl->conf = NULL;
8097
8098 ssl->in_buf = NULL;
8099 ssl->out_buf = NULL;
8100
8101 ssl->in_hdr = NULL;
8102 ssl->in_ctr = NULL;
8103 ssl->in_len = NULL;
8104 ssl->in_iv = NULL;
8105 ssl->in_msg = NULL;
8106
8107 ssl->out_hdr = NULL;
8108 ssl->out_ctr = NULL;
8109 ssl->out_len = NULL;
8110 ssl->out_iv = NULL;
8111 ssl->out_msg = NULL;
8112
k-stachowiak9f7798e2018-07-31 16:52:32 +02008113 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008114}
8115
8116/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008117 * Reset an initialized and used SSL context for re-use while retaining
8118 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008119 *
8120 * If partial is non-zero, keep data in the input buffer and client ID.
8121 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008122 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008123static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008124{
Paul Bakker48916f92012-09-16 19:57:18 +00008125 int ret;
8126
Hanno Becker7e772132018-08-10 12:38:21 +01008127#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8128 !defined(MBEDTLS_SSL_SRV_C)
8129 ((void) partial);
8130#endif
8131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008133
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008134 /* Cancel any possibly running timer */
8135 ssl_set_timer( ssl, 0 );
8136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137#if defined(MBEDTLS_SSL_RENEGOTIATION)
8138 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008139 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008140
8141 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8143 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008146
Paul Bakker7eb013f2011-10-06 12:37:39 +00008147 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008148 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008149
8150 ssl->in_msgtype = 0;
8151 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008153 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008154 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008157 ssl_dtls_replay_reset( ssl );
8158#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008159
8160 ssl->in_hslen = 0;
8161 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008162
8163 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008164
8165 ssl->out_msgtype = 0;
8166 ssl->out_msglen = 0;
8167 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8169 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008170 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008171#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008172
Hanno Becker19859472018-08-06 09:40:20 +01008173 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8174
Paul Bakker48916f92012-09-16 19:57:18 +00008175 ssl->transform_in = NULL;
8176 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008177
Hanno Becker78640902018-08-13 16:35:15 +01008178 ssl->session_in = NULL;
8179 ssl->session_out = NULL;
8180
Angus Grattond8213d02016-05-25 20:56:48 +10008181 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008182
8183#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008184 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008185#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8186 {
8187 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008188 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008189 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8192 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8195 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8198 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008199 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008200 }
8201#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008202
Paul Bakker48916f92012-09-16 19:57:18 +00008203 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 mbedtls_ssl_transform_free( ssl->transform );
8206 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008207 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008208 }
Paul Bakker48916f92012-09-16 19:57:18 +00008209
Paul Bakkerc0463502013-02-14 11:19:38 +01008210 if( ssl->session )
8211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 mbedtls_ssl_session_free( ssl->session );
8213 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008214 ssl->session = NULL;
8215 }
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008218 ssl->alpn_chosen = NULL;
8219#endif
8220
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008221#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008222#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008223 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008224#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008225 {
8226 mbedtls_free( ssl->cli_id );
8227 ssl->cli_id = NULL;
8228 ssl->cli_id_len = 0;
8229 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008230#endif
8231
Paul Bakker48916f92012-09-16 19:57:18 +00008232 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8233 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008234
8235 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008236}
8237
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008238/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008239 * Reset an initialized and used SSL context for re-use while retaining
8240 * all application-set variables, function pointers and data.
8241 */
8242int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8243{
8244 return( ssl_session_reset_int( ssl, 0 ) );
8245}
8246
8247/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008248 * SSL set accessors
8249 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008250void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008251{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008252 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008253}
8254
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008255void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008257 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008258}
8259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008261void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008262{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008263 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008264}
8265#endif
8266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008268void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008270 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008271}
8272#endif
8273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008275
Hanno Becker1841b0a2018-08-24 11:13:57 +01008276void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8277 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008278{
8279 ssl->disable_datagram_packing = !allow_packing;
8280}
8281
8282void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8283 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008284{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008285 conf->hs_timeout_min = min;
8286 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008287}
8288#endif
8289
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008290void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008291{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008292 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008293}
8294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008296void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008297 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008298 void *p_vrfy )
8299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008300 conf->f_vrfy = f_vrfy;
8301 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008304
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008305void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008306 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008307 void *p_rng )
8308{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008309 conf->f_rng = f_rng;
8310 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008311}
8312
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008313void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008314 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008315 void *p_dbg )
8316{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008317 conf->f_dbg = f_dbg;
8318 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008319}
8320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008322 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008323 mbedtls_ssl_send_t *f_send,
8324 mbedtls_ssl_recv_t *f_recv,
8325 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008326{
8327 ssl->p_bio = p_bio;
8328 ssl->f_send = f_send;
8329 ssl->f_recv = f_recv;
8330 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008331}
8332
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008333#if defined(MBEDTLS_SSL_PROTO_DTLS)
8334void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8335{
8336 ssl->mtu = mtu;
8337}
8338#endif
8339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008340void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008341{
8342 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008343}
8344
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008345void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8346 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008347 mbedtls_ssl_set_timer_t *f_set_timer,
8348 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008349{
8350 ssl->p_timer = p_timer;
8351 ssl->f_set_timer = f_set_timer;
8352 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008353
8354 /* Make sure we start with no timer running */
8355 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008356}
8357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008359void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008360 void *p_cache,
8361 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8362 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008363{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008364 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008365 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008366 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_SSL_CLI_C)
8371int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008372{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008373 int ret;
8374
8375 if( ssl == NULL ||
8376 session == NULL ||
8377 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008378 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008381 }
8382
Hanno Becker52055ae2019-02-06 14:30:46 +00008383 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8384 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008385 return( ret );
8386
Paul Bakker0a597072012-09-25 21:55:46 +00008387 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008388
8389 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008390}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008392
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008394 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008396 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8397 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8398 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8399 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008400}
8401
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008402void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008403 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008404 int major, int minor )
8405{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008407 return;
8408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008410 return;
8411
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008412 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008413}
8414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008416void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008417 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008418{
8419 conf->cert_profile = profile;
8420}
8421
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008422/* Append a new keycert entry to a (possibly empty) list */
8423static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8424 mbedtls_x509_crt *cert,
8425 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008426{
niisato8ee24222018-06-25 19:05:48 +09008427 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008428
niisato8ee24222018-06-25 19:05:48 +09008429 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8430 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008431 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008432
niisato8ee24222018-06-25 19:05:48 +09008433 new_cert->cert = cert;
8434 new_cert->key = key;
8435 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008436
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008437 /* Update head is the list was null, else add to the end */
8438 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008439 {
niisato8ee24222018-06-25 19:05:48 +09008440 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008441 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008442 else
8443 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008444 mbedtls_ssl_key_cert *cur = *head;
8445 while( cur->next != NULL )
8446 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008447 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008448 }
8449
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008450 return( 0 );
8451}
8452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008453int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008454 mbedtls_x509_crt *own_cert,
8455 mbedtls_pk_context *pk_key )
8456{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008457 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008458}
8459
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008460void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008461 mbedtls_x509_crt *ca_chain,
8462 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008463{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008464 conf->ca_chain = ca_chain;
8465 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008466
8467#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8468 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8469 * cannot be used together. */
8470 conf->f_ca_cb = NULL;
8471 conf->p_ca_cb = NULL;
8472#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008473}
Hanno Becker5adaad92019-03-27 16:54:37 +00008474
8475#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8476void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008477 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008478 void *p_ca_cb )
8479{
8480 conf->f_ca_cb = f_ca_cb;
8481 conf->p_ca_cb = p_ca_cb;
8482
8483 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8484 * cannot be used together. */
8485 conf->ca_chain = NULL;
8486 conf->ca_crl = NULL;
8487}
8488#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008490
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008491#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8492int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8493 mbedtls_x509_crt *own_cert,
8494 mbedtls_pk_context *pk_key )
8495{
8496 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8497 own_cert, pk_key ) );
8498}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008499
8500void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8501 mbedtls_x509_crt *ca_chain,
8502 mbedtls_x509_crl *ca_crl )
8503{
8504 ssl->handshake->sni_ca_chain = ca_chain;
8505 ssl->handshake->sni_ca_crl = ca_crl;
8506}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008507
8508void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8509 int authmode )
8510{
8511 ssl->handshake->sni_authmode = authmode;
8512}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008513#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8514
Hanno Becker8927c832019-04-03 12:52:50 +01008515#if defined(MBEDTLS_X509_CRT_PARSE_C)
8516void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8517 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8518 void *p_vrfy )
8519{
8520 ssl->f_vrfy = f_vrfy;
8521 ssl->p_vrfy = p_vrfy;
8522}
8523#endif
8524
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008525#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008526/*
8527 * Set EC J-PAKE password for current handshake
8528 */
8529int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8530 const unsigned char *pw,
8531 size_t pw_len )
8532{
8533 mbedtls_ecjpake_role role;
8534
Janos Follath8eb64132016-06-03 15:40:57 +01008535 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8537
8538 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8539 role = MBEDTLS_ECJPAKE_SERVER;
8540 else
8541 role = MBEDTLS_ECJPAKE_CLIENT;
8542
8543 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8544 role,
8545 MBEDTLS_MD_SHA256,
8546 MBEDTLS_ECP_DP_SECP256R1,
8547 pw, pw_len ) );
8548}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008549#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008552
8553static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8554{
8555 /* Remove reference to existing PSK, if any. */
8556#if defined(MBEDTLS_USE_PSA_CRYPTO)
8557 if( conf->psk_opaque != 0 )
8558 {
8559 /* The maintenance of the PSK key slot is the
8560 * user's responsibility. */
8561 conf->psk_opaque = 0;
8562 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008563 /* This and the following branch should never
8564 * be taken simultaenously as we maintain the
8565 * invariant that raw and opaque PSKs are never
8566 * configured simultaneously. As a safeguard,
8567 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008568#endif /* MBEDTLS_USE_PSA_CRYPTO */
8569 if( conf->psk != NULL )
8570 {
8571 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8572
8573 mbedtls_free( conf->psk );
8574 conf->psk = NULL;
8575 conf->psk_len = 0;
8576 }
8577
8578 /* Remove reference to PSK identity, if any. */
8579 if( conf->psk_identity != NULL )
8580 {
8581 mbedtls_free( conf->psk_identity );
8582 conf->psk_identity = NULL;
8583 conf->psk_identity_len = 0;
8584 }
8585}
8586
Hanno Becker7390c712018-11-15 13:33:04 +00008587/* This function assumes that PSK identity in the SSL config is unset.
8588 * It checks that the provided identity is well-formed and attempts
8589 * to make a copy of it in the SSL config.
8590 * On failure, the PSK identity in the config remains unset. */
8591static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8592 unsigned char const *psk_identity,
8593 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008594{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008595 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008596 if( psk_identity == NULL ||
8597 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008598 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008599 {
8600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8601 }
8602
Hanno Becker7390c712018-11-15 13:33:04 +00008603 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8604 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008605 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008606
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008607 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008608 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008609
8610 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008611}
8612
Hanno Becker7390c712018-11-15 13:33:04 +00008613int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8614 const unsigned char *psk, size_t psk_len,
8615 const unsigned char *psk_identity, size_t psk_identity_len )
8616{
8617 int ret;
8618 /* Remove opaque/raw PSK + PSK Identity */
8619 ssl_conf_remove_psk( conf );
8620
8621 /* Check and set raw PSK */
8622 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8624 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8625 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8626 conf->psk_len = psk_len;
8627 memcpy( conf->psk, psk, conf->psk_len );
8628
8629 /* Check and set PSK Identity */
8630 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8631 if( ret != 0 )
8632 ssl_conf_remove_psk( conf );
8633
8634 return( ret );
8635}
8636
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008637static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8638{
8639#if defined(MBEDTLS_USE_PSA_CRYPTO)
8640 if( ssl->handshake->psk_opaque != 0 )
8641 {
8642 ssl->handshake->psk_opaque = 0;
8643 }
8644 else
8645#endif /* MBEDTLS_USE_PSA_CRYPTO */
8646 if( ssl->handshake->psk != NULL )
8647 {
8648 mbedtls_platform_zeroize( ssl->handshake->psk,
8649 ssl->handshake->psk_len );
8650 mbedtls_free( ssl->handshake->psk );
8651 ssl->handshake->psk_len = 0;
8652 }
8653}
8654
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008655int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8656 const unsigned char *psk, size_t psk_len )
8657{
8658 if( psk == NULL || ssl->handshake == NULL )
8659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8660
8661 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8663
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008664 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008665
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008666 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008667 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008668
8669 ssl->handshake->psk_len = psk_len;
8670 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8671
8672 return( 0 );
8673}
8674
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008675#if defined(MBEDTLS_USE_PSA_CRYPTO)
8676int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008677 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008678 const unsigned char *psk_identity,
8679 size_t psk_identity_len )
8680{
Hanno Becker7390c712018-11-15 13:33:04 +00008681 int ret;
8682 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008683 ssl_conf_remove_psk( conf );
8684
Hanno Becker7390c712018-11-15 13:33:04 +00008685 /* Check and set opaque PSK */
8686 if( psk_slot == 0 )
8687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008688 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008689
8690 /* Check and set PSK Identity */
8691 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8692 psk_identity_len );
8693 if( ret != 0 )
8694 ssl_conf_remove_psk( conf );
8695
8696 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008697}
8698
8699int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008700 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008701{
8702 if( psk_slot == 0 || ssl->handshake == NULL )
8703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8704
8705 ssl_remove_psk( ssl );
8706 ssl->handshake->psk_opaque = psk_slot;
8707 return( 0 );
8708}
8709#endif /* MBEDTLS_USE_PSA_CRYPTO */
8710
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008711void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008713 size_t),
8714 void *p_psk )
8715{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008716 conf->f_psk = f_psk;
8717 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008720
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008721#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008722
8723#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008724int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008725{
8726 int ret;
8727
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008728 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8729 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8730 {
8731 mbedtls_mpi_free( &conf->dhm_P );
8732 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008733 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008734 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008735
8736 return( 0 );
8737}
Hanno Becker470a8c42017-10-04 15:28:46 +01008738#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008739
Hanno Beckera90658f2017-10-04 15:29:08 +01008740int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8741 const unsigned char *dhm_P, size_t P_len,
8742 const unsigned char *dhm_G, size_t G_len )
8743{
8744 int ret;
8745
8746 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8747 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8748 {
8749 mbedtls_mpi_free( &conf->dhm_P );
8750 mbedtls_mpi_free( &conf->dhm_G );
8751 return( ret );
8752 }
8753
8754 return( 0 );
8755}
Paul Bakker5121ce52009-01-03 21:22:43 +00008756
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008757int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008758{
8759 int ret;
8760
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008761 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8762 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8763 {
8764 mbedtls_mpi_free( &conf->dhm_P );
8765 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008766 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008767 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008768
8769 return( 0 );
8770}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008771#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008772
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008773#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8774/*
8775 * Set the minimum length for Diffie-Hellman parameters
8776 */
8777void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8778 unsigned int bitlen )
8779{
8780 conf->dhm_min_bitlen = bitlen;
8781}
8782#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8783
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008784#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008785/*
8786 * Set allowed/preferred hashes for handshake signatures
8787 */
8788void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8789 const int *hashes )
8790{
8791 conf->sig_hashes = hashes;
8792}
Hanno Becker947194e2017-04-07 13:25:49 +01008793#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008794
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008795#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008796/*
8797 * Set the allowed elliptic curves
8798 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008799void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008800 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008801{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008802 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008803}
Hanno Becker947194e2017-04-07 13:25:49 +01008804#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008805
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008806#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008808{
Hanno Becker947194e2017-04-07 13:25:49 +01008809 /* Initialize to suppress unnecessary compiler warning */
8810 size_t hostname_len = 0;
8811
8812 /* Check if new hostname is valid before
8813 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008814 if( hostname != NULL )
8815 {
8816 hostname_len = strlen( hostname );
8817
8818 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8820 }
8821
8822 /* Now it's clear that we will overwrite the old hostname,
8823 * so we can free it safely */
8824
8825 if( ssl->hostname != NULL )
8826 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008827 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008828 mbedtls_free( ssl->hostname );
8829 }
8830
8831 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008832
Paul Bakker5121ce52009-01-03 21:22:43 +00008833 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008834 {
8835 ssl->hostname = NULL;
8836 }
8837 else
8838 {
8839 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008840 if( ssl->hostname == NULL )
8841 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008842
Hanno Becker947194e2017-04-07 13:25:49 +01008843 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008844
Hanno Becker947194e2017-04-07 13:25:49 +01008845 ssl->hostname[hostname_len] = '\0';
8846 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008847
8848 return( 0 );
8849}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008850#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008851
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008852#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008853void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008855 const unsigned char *, size_t),
8856 void *p_sni )
8857{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008858 conf->f_sni = f_sni;
8859 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008864int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008865{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008866 size_t cur_len, tot_len;
8867 const char **p;
8868
8869 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008870 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8871 * MUST NOT be truncated."
8872 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008873 */
8874 tot_len = 0;
8875 for( p = protos; *p != NULL; p++ )
8876 {
8877 cur_len = strlen( *p );
8878 tot_len += cur_len;
8879
8880 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008882 }
8883
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008884 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008885
8886 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008887}
8888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008890{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008891 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008892}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008894
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008895void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008896{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008897 conf->max_major_ver = major;
8898 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008899}
8900
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008901void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008902{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008903 conf->min_major_ver = major;
8904 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008905}
8906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008907#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008908void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008909{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008910 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008911}
8912#endif
8913
Janos Follath088ce432017-04-10 12:42:31 +01008914#if defined(MBEDTLS_SSL_SRV_C)
8915void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8916 char cert_req_ca_list )
8917{
8918 conf->cert_req_ca_list = cert_req_ca_list;
8919}
8920#endif
8921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008923void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008925 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008926}
8927#endif
8928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008930void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008932 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008933}
8934#endif
8935
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008936#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008937void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008938{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008939 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008940}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008941#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008944int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008947 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008950 }
8951
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008952 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008953
8954 return( 0 );
8955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008959void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008960{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008961 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008966void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008967{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008968 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008969}
8970#endif
8971
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008972void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008973{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008974 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008975}
8976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008978void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008980 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008981}
8982
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008983void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008984{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008985 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008986}
8987
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008988void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008989 const unsigned char period[8] )
8990{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008991 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008996#if defined(MBEDTLS_SSL_CLI_C)
8997void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008998{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008999 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009000}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009001#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009002
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009003#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009004void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9005 mbedtls_ssl_ticket_write_t *f_ticket_write,
9006 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9007 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009008{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009009 conf->f_ticket_write = f_ticket_write;
9010 conf->f_ticket_parse = f_ticket_parse;
9011 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009012}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009015
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009016#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9017void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9018 mbedtls_ssl_export_keys_t *f_export_keys,
9019 void *p_export_keys )
9020{
9021 conf->f_export_keys = f_export_keys;
9022 conf->p_export_keys = p_export_keys;
9023}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009024
9025void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9026 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9027 void *p_export_keys )
9028{
9029 conf->f_export_keys_ext = f_export_keys_ext;
9030 conf->p_export_keys = p_export_keys;
9031}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009032#endif
9033
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009034#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009035void mbedtls_ssl_conf_async_private_cb(
9036 mbedtls_ssl_config *conf,
9037 mbedtls_ssl_async_sign_t *f_async_sign,
9038 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9039 mbedtls_ssl_async_resume_t *f_async_resume,
9040 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009041 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009042{
9043 conf->f_async_sign_start = f_async_sign;
9044 conf->f_async_decrypt_start = f_async_decrypt;
9045 conf->f_async_resume = f_async_resume;
9046 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009047 conf->p_async_config_data = async_config_data;
9048}
9049
Gilles Peskine8f97af72018-04-26 11:46:10 +02009050void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9051{
9052 return( conf->p_async_config_data );
9053}
9054
Gilles Peskine1febfef2018-04-30 11:54:39 +02009055void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009056{
9057 if( ssl->handshake == NULL )
9058 return( NULL );
9059 else
9060 return( ssl->handshake->user_async_ctx );
9061}
9062
Gilles Peskine1febfef2018-04-30 11:54:39 +02009063void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009064 void *ctx )
9065{
9066 if( ssl->handshake != NULL )
9067 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009068}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009069#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009070
Paul Bakker5121ce52009-01-03 21:22:43 +00009071/*
9072 * SSL get accessors
9073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009075{
9076 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9077}
9078
Hanno Becker8b170a02017-10-10 11:51:19 +01009079int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9080{
9081 /*
9082 * Case A: We're currently holding back
9083 * a message for further processing.
9084 */
9085
9086 if( ssl->keep_current_message == 1 )
9087 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009089 return( 1 );
9090 }
9091
9092 /*
9093 * Case B: Further records are pending in the current datagram.
9094 */
9095
9096#if defined(MBEDTLS_SSL_PROTO_DTLS)
9097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9098 ssl->in_left > ssl->next_record_offset )
9099 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009101 return( 1 );
9102 }
9103#endif /* MBEDTLS_SSL_PROTO_DTLS */
9104
9105 /*
9106 * Case C: A handshake message is being processed.
9107 */
9108
Hanno Becker8b170a02017-10-10 11:51:19 +01009109 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9110 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009112 return( 1 );
9113 }
9114
9115 /*
9116 * Case D: An application data message is being processed
9117 */
9118 if( ssl->in_offt != NULL )
9119 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009121 return( 1 );
9122 }
9123
9124 /*
9125 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009126 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009127 * we implement support for multiple alerts in single records.
9128 */
9129
9130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9131 return( 0 );
9132}
9133
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009134uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009135{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009136 if( ssl->session != NULL )
9137 return( ssl->session->verify_result );
9138
9139 if( ssl->session_negotiate != NULL )
9140 return( ssl->session_negotiate->verify_result );
9141
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009142 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009143}
9144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009146{
Paul Bakker926c8e42013-03-06 10:23:34 +01009147 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009148 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009151}
9152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009156 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009157 {
9158 switch( ssl->minor_ver )
9159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009161 return( "DTLSv1.0" );
9162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009164 return( "DTLSv1.2" );
9165
9166 default:
9167 return( "unknown (DTLS)" );
9168 }
9169 }
9170#endif
9171
Paul Bakker43ca69c2011-01-15 17:35:19 +00009172 switch( ssl->minor_ver )
9173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009175 return( "SSLv3.0" );
9176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009178 return( "TLSv1.0" );
9179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009181 return( "TLSv1.1" );
9182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009184 return( "TLSv1.2" );
9185
Paul Bakker43ca69c2011-01-15 17:35:19 +00009186 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009187 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009188 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009189}
9190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009192{
Hanno Becker3136ede2018-08-17 15:28:19 +01009193 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009195 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009196
Hanno Becker78640902018-08-13 16:35:15 +01009197 if( transform == NULL )
9198 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200#if defined(MBEDTLS_ZLIB_SUPPORT)
9201 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9202 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009203#endif
9204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207 case MBEDTLS_MODE_GCM:
9208 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009209 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009211 transform_expansion = transform->minlen;
9212 break;
9213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009215
9216 block_size = mbedtls_cipher_get_block_size(
9217 &transform->cipher_ctx_enc );
9218
Hanno Becker3136ede2018-08-17 15:28:19 +01009219 /* Expansion due to the addition of the MAC. */
9220 transform_expansion += transform->maclen;
9221
9222 /* Expansion due to the addition of CBC padding;
9223 * Theoretically up to 256 bytes, but we never use
9224 * more than the block size of the underlying cipher. */
9225 transform_expansion += block_size;
9226
9227 /* For TLS 1.1 or higher, an explicit IV is added
9228 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009229#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9230 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009231 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009232#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009233
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009234 break;
9235
9236 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009239 }
9240
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009241 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009242}
9243
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009244#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9245size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9246{
9247 size_t max_len;
9248
9249 /*
9250 * Assume mfl_code is correct since it was checked when set
9251 */
Angus Grattond8213d02016-05-25 20:56:48 +10009252 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009253
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009254 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009255 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009256 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009257 {
Angus Grattond8213d02016-05-25 20:56:48 +10009258 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009259 }
9260
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009261 /* During a handshake, use the value being negotiated */
9262 if( ssl->session_negotiate != NULL &&
9263 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9264 {
9265 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9266 }
9267
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009268 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009269}
9270#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9271
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009272#if defined(MBEDTLS_SSL_PROTO_DTLS)
9273static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9274{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009275 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9276 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9277 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9278 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9279 return ( 0 );
9280
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009281 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9282 return( ssl->mtu );
9283
9284 if( ssl->mtu == 0 )
9285 return( ssl->handshake->mtu );
9286
9287 return( ssl->mtu < ssl->handshake->mtu ?
9288 ssl->mtu : ssl->handshake->mtu );
9289}
9290#endif /* MBEDTLS_SSL_PROTO_DTLS */
9291
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009292int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9293{
9294 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9295
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009296#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9297 !defined(MBEDTLS_SSL_PROTO_DTLS)
9298 (void) ssl;
9299#endif
9300
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009301#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9302 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9303
9304 if( max_len > mfl )
9305 max_len = mfl;
9306#endif
9307
9308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009309 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009310 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009311 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009312 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9313 const size_t overhead = (size_t) ret;
9314
9315 if( ret < 0 )
9316 return( ret );
9317
9318 if( mtu <= overhead )
9319 {
9320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9321 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9322 }
9323
9324 if( max_len > mtu - overhead )
9325 max_len = mtu - overhead;
9326 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009327#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009328
Hanno Becker0defedb2018-08-10 12:35:02 +01009329#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9330 !defined(MBEDTLS_SSL_PROTO_DTLS)
9331 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009332#endif
9333
9334 return( (int) max_len );
9335}
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337#if defined(MBEDTLS_X509_CRT_PARSE_C)
9338const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009339{
9340 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009341 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009342
Hanno Beckere6824572019-02-07 13:18:46 +00009343#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009344 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009345#else
9346 return( NULL );
9347#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009352int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9353 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009354{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009355 if( ssl == NULL ||
9356 dst == NULL ||
9357 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009358 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009361 }
9362
Hanno Becker52055ae2019-02-06 14:30:46 +00009363 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009366
Paul Bakker5121ce52009-01-03 21:22:43 +00009367/*
Paul Bakker1961b702013-01-25 14:49:24 +01009368 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009373
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009374 if( ssl == NULL || ssl->conf == NULL )
9375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009384#endif
9385
Paul Bakker1961b702013-01-25 14:49:24 +01009386 return( ret );
9387}
9388
9389/*
9390 * Perform the SSL handshake
9391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009393{
9394 int ret = 0;
9395
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009396 if( ssl == NULL || ssl->conf == NULL )
9397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009404
9405 if( ret != 0 )
9406 break;
9407 }
9408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009410
9411 return( ret );
9412}
9413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#if defined(MBEDTLS_SSL_RENEGOTIATION)
9415#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009416/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009417 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009420{
9421 int ret;
9422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009424
9425 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9427 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009428
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009429 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009430 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009432 return( ret );
9433 }
9434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009436
9437 return( 0 );
9438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009440
9441/*
9442 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443 * - any side: calling mbedtls_ssl_renegotiate(),
9444 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9445 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009446 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009447 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009451{
9452 int ret;
9453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009455
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009456 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9457 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009458
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009459 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9460 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009462 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009464 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009465 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009466 ssl->handshake->out_msg_seq = 1;
9467 else
9468 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009469 }
9470#endif
9471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9473 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009478 return( ret );
9479 }
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009482
9483 return( 0 );
9484}
9485
9486/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009487 * Renegotiate current connection on client,
9488 * or request renegotiation on server
9489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009491{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009493
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009494 if( ssl == NULL || ssl->conf == NULL )
9495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009498 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009499 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009505
9506 /* Did we already try/start sending HelloRequest? */
9507 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009509
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009510 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009511 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009515 /*
9516 * On client, either start the renegotiation process or,
9517 * if already in progress, continue the handshake
9518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009523
9524 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009527 return( ret );
9528 }
9529 }
9530 else
9531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009535 return( ret );
9536 }
9537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009539
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009540 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009541}
9542
9543/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009544 * Check record counters and renegotiate if they're above the limit.
9545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009547{
Andres AG2196c7f2016-12-15 17:01:16 +00009548 size_t ep_len = ssl_ep_len( ssl );
9549 int in_ctr_cmp;
9550 int out_ctr_cmp;
9551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9553 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009554 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009555 {
9556 return( 0 );
9557 }
9558
Andres AG2196c7f2016-12-15 17:01:16 +00009559 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9560 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009561 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009562 ssl->conf->renego_period + ep_len, 8 - ep_len );
9563
9564 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009565 {
9566 return( 0 );
9567 }
9568
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009573
9574/*
9575 * Receive application data decrypted from the SSL layer
9576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009578{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009579 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009580 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009581
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009582 if( ssl == NULL || ssl->conf == NULL )
9583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009588 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009591 return( ret );
9592
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009593 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009595 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009596 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009597 return( ret );
9598 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009599 }
9600#endif
9601
Hanno Becker4a810fb2017-05-24 16:27:30 +01009602 /*
9603 * Check if renegotiation is necessary and/or handshake is
9604 * in process. If yes, perform/continue, and fall through
9605 * if an unexpected packet is received while the client
9606 * is waiting for the ServerHello.
9607 *
9608 * (There is no equivalent to the last condition on
9609 * the server-side as it is not treated as within
9610 * a handshake while waiting for the ClientHello
9611 * after a renegotiation request.)
9612 */
9613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009615 ret = ssl_check_ctr_renegotiate( ssl );
9616 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9617 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009620 return( ret );
9621 }
9622#endif
9623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009627 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9628 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009631 return( ret );
9632 }
9633 }
9634
Hanno Beckere41158b2017-10-23 13:30:32 +01009635 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009636 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009637 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009638 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009639 if( ssl->f_get_timer != NULL &&
9640 ssl->f_get_timer( ssl->p_timer ) == -1 )
9641 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009642 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009643 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009644
Hanno Becker327c93b2018-08-15 13:56:18 +01009645 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009646 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009647 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9648 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009649
Hanno Becker4a810fb2017-05-24 16:27:30 +01009650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9651 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009652 }
9653
9654 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009656 {
9657 /*
9658 * OpenSSL sends empty messages to randomize the IV
9659 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009660 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009663 return( 0 );
9664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009666 return( ret );
9667 }
9668 }
9669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009673
Hanno Becker4a810fb2017-05-24 16:27:30 +01009674 /*
9675 * - For client-side, expect SERVER_HELLO_REQUEST.
9676 * - For server-side, expect CLIENT_HELLO.
9677 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9678 */
9679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009683 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009686
9687 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009689 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009690 {
9691 continue;
9692 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009695 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009696#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009697
Hanno Becker4a810fb2017-05-24 16:27:30 +01009698#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009699 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009703
9704 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009706 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009707 {
9708 continue;
9709 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009712 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009713#endif /* MBEDTLS_SSL_SRV_C */
9714
Hanno Becker21df7f92017-10-17 11:03:26 +01009715#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009716 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009717 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9718 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9719 ssl->conf->allow_legacy_renegotiation ==
9720 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9721 {
9722 /*
9723 * Accept renegotiation request
9724 */
Paul Bakker48916f92012-09-16 19:57:18 +00009725
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009726 /* DTLS clients need to know renego is server-initiated */
9727#if defined(MBEDTLS_SSL_PROTO_DTLS)
9728 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9729 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9730 {
9731 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9732 }
9733#endif
9734 ret = ssl_start_renegotiation( ssl );
9735 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9736 ret != 0 )
9737 {
9738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9739 return( ret );
9740 }
9741 }
9742 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009743#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009744 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009745 /*
9746 * Refuse renegotiation
9747 */
9748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751#if defined(MBEDTLS_SSL_PROTO_SSL3)
9752 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009753 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009754 /* SSLv3 does not have a "no_renegotiation" warning, so
9755 we send a fatal alert and abort the connection. */
9756 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9757 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9758 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009759 }
9760 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9762#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9763 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9764 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9767 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9768 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009769 {
9770 return( ret );
9771 }
Paul Bakker48916f92012-09-16 19:57:18 +00009772 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009773 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9775 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009779 }
Paul Bakker48916f92012-09-16 19:57:18 +00009780 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009781
Hanno Becker90333da2017-10-10 11:27:13 +01009782 /* At this point, we don't know whether the renegotiation has been
9783 * completed or not. The cases to consider are the following:
9784 * 1) The renegotiation is complete. In this case, no new record
9785 * has been read yet.
9786 * 2) The renegotiation is incomplete because the client received
9787 * an application data record while awaiting the ServerHello.
9788 * 3) The renegotiation is incomplete because the client received
9789 * a non-handshake, non-application data message while awaiting
9790 * the ServerHello.
9791 * In each of these case, looping will be the proper action:
9792 * - For 1), the next iteration will read a new record and check
9793 * if it's application data.
9794 * - For 2), the loop condition isn't satisfied as application data
9795 * is present, hence continue is the same as break
9796 * - For 3), the loop condition is satisfied and read_record
9797 * will re-deliver the message that was held back by the client
9798 * when expecting the ServerHello.
9799 */
9800 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009801 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009802#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009804 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009805 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009806 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009807 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009810 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009812 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009813 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9818 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009821 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009822 }
9823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9827 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009828 }
9829
9830 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009831
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009832 /* We're going to return something now, cancel timer,
9833 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009835 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009836
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009838 /* If we requested renego but received AppData, resend HelloRequest.
9839 * Do it now, after setting in_offt, to avoid taking this branch
9840 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009842 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009844 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009845 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009848 return( ret );
9849 }
9850 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009853 }
9854
9855 n = ( len < ssl->in_msglen )
9856 ? len : ssl->in_msglen;
9857
9858 memcpy( buf, ssl->in_offt, n );
9859 ssl->in_msglen -= n;
9860
9861 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009862 {
9863 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009864 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009865 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009866 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009867 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009868 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009869 /* more data available */
9870 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009871 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009874
Paul Bakker23986e52011-04-24 08:57:21 +00009875 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009876}
9877
9878/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009879 * Send application data to be encrypted by the SSL layer, taking care of max
9880 * fragment length and buffer size.
9881 *
9882 * According to RFC 5246 Section 6.2.1:
9883 *
9884 * Zero-length fragments of Application data MAY be sent as they are
9885 * potentially useful as a traffic analysis countermeasure.
9886 *
9887 * Therefore, it is possible that the input message length is 0 and the
9888 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009889 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009890static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009891 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009892{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009893 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9894 const size_t max_len = (size_t) ret;
9895
9896 if( ret < 0 )
9897 {
9898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9899 return( ret );
9900 }
9901
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009902 if( len > max_len )
9903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009905 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009908 "maximum fragment length: %d > %d",
9909 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009911 }
9912 else
9913#endif
9914 len = max_len;
9915 }
Paul Bakker887bd502011-06-08 13:10:54 +00009916
Paul Bakker5121ce52009-01-03 21:22:43 +00009917 if( ssl->out_left != 0 )
9918 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009919 /*
9920 * The user has previously tried to send the data and
9921 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9922 * written. In this case, we expect the high-level write function
9923 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009928 return( ret );
9929 }
9930 }
Paul Bakker887bd502011-06-08 13:10:54 +00009931 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009932 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009933 /*
9934 * The user is trying to send a message the first time, so we need to
9935 * copy the data into the internal buffers and setup the data structure
9936 * to keep track of partial writes
9937 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009938 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009940 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009941
Hanno Becker67bc7c32018-08-06 11:33:50 +01009942 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009945 return( ret );
9946 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009947 }
9948
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009949 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009950}
9951
9952/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009953 * Write application data, doing 1/n-1 splitting if necessary.
9954 *
9955 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009956 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009957 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009960static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009961 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009962{
9963 int ret;
9964
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009965 if( ssl->conf->cbc_record_splitting ==
9966 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009967 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9969 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9970 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009971 {
9972 return( ssl_write_real( ssl, buf, len ) );
9973 }
9974
9975 if( ssl->split_done == 0 )
9976 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009977 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009978 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009979 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009980 }
9981
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009982 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9983 return( ret );
9984 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009985
9986 return( ret + 1 );
9987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009989
9990/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009991 * Write application data (public-facing wrapper)
9992 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009993int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009994{
9995 int ret;
9996
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009998
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009999 if( ssl == NULL || ssl->conf == NULL )
10000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10001
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010002#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010003 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10004 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010005 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010006 return( ret );
10007 }
10008#endif
10009
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010010 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010011 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010012 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010013 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010015 return( ret );
10016 }
10017 }
10018
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010019#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010020 ret = ssl_write_split( ssl, buf, len );
10021#else
10022 ret = ssl_write_real( ssl, buf, len );
10023#endif
10024
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010026
10027 return( ret );
10028}
10029
10030/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010031 * Notify the peer that the connection is being closed
10032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010034{
10035 int ret;
10036
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010037 if( ssl == NULL || ssl->conf == NULL )
10038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010041
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010042 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10048 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10049 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010052 return( ret );
10053 }
10054 }
10055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010057
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010058 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010059}
10060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010062{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010063 if( transform == NULL )
10064 return;
10065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010067 deflateEnd( &transform->ctx_deflate );
10068 inflateEnd( &transform->ctx_inflate );
10069#endif
10070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10072 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010073
Hanno Beckerd56ed242018-01-03 15:32:51 +000010074#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 mbedtls_md_free( &transform->md_ctx_enc );
10076 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010077#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010078
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010079 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010080}
10081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082#if defined(MBEDTLS_X509_CRT_PARSE_C)
10083static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010086
10087 while( cur != NULL )
10088 {
10089 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010091 cur = next;
10092 }
10093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010095
Hanno Becker0271f962018-08-16 13:23:47 +010010096#if defined(MBEDTLS_SSL_PROTO_DTLS)
10097
10098static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10099{
10100 unsigned offset;
10101 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10102
10103 if( hs == NULL )
10104 return;
10105
Hanno Becker283f5ef2018-08-24 09:34:47 +010010106 ssl_free_buffered_record( ssl );
10107
Hanno Becker0271f962018-08-16 13:23:47 +010010108 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010109 ssl_buffering_free_slot( ssl, offset );
10110}
10111
10112static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10113 uint8_t slot )
10114{
10115 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10116 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010117
10118 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10119 return;
10120
Hanno Beckere605b192018-08-21 15:59:07 +010010121 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010122 {
Hanno Beckere605b192018-08-21 15:59:07 +010010123 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010124 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010125 mbedtls_free( hs_buf->data );
10126 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010127 }
10128}
10129
10130#endif /* MBEDTLS_SSL_PROTO_DTLS */
10131
Gilles Peskine9b562d52018-04-25 20:32:43 +020010132void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010133{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010134 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10135
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010136 if( handshake == NULL )
10137 return;
10138
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010139#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10140 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10141 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010142 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010143 handshake->async_in_progress = 0;
10144 }
10145#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10146
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010147#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10148 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10149 mbedtls_md5_free( &handshake->fin_md5 );
10150 mbedtls_sha1_free( &handshake->fin_sha1 );
10151#endif
10152#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10153#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010154#if defined(MBEDTLS_USE_PSA_CRYPTO)
10155 psa_hash_abort( &handshake->fin_sha256_psa );
10156#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010157 mbedtls_sha256_free( &handshake->fin_sha256 );
10158#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010159#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010160#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010161#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010162 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010163#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010164 mbedtls_sha512_free( &handshake->fin_sha512 );
10165#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010166#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169#if defined(MBEDTLS_DHM_C)
10170 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172#if defined(MBEDTLS_ECDH_C)
10173 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010174#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010175#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010176 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010177#if defined(MBEDTLS_SSL_CLI_C)
10178 mbedtls_free( handshake->ecjpake_cache );
10179 handshake->ecjpake_cache = NULL;
10180 handshake->ecjpake_cache_len = 0;
10181#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010182#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010183
Janos Follath4ae5c292016-02-10 11:27:43 +000010184#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10185 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010186 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010188#endif
10189
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010190#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10191 if( handshake->psk != NULL )
10192 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010193 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010194 mbedtls_free( handshake->psk );
10195 }
10196#endif
10197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10199 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010200 /*
10201 * Free only the linked list wrapper, not the keys themselves
10202 * since the belong to the SNI callback
10203 */
10204 if( handshake->sni_key_cert != NULL )
10205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010207
10208 while( cur != NULL )
10209 {
10210 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010212 cur = next;
10213 }
10214 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010216
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010217#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010218 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010219 if( handshake->ecrs_peer_cert != NULL )
10220 {
10221 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10222 mbedtls_free( handshake->ecrs_peer_cert );
10223 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010224#endif
10225
Hanno Becker75173122019-02-06 16:18:31 +000010226#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10227 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10228 mbedtls_pk_free( &handshake->peer_pubkey );
10229#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231#if defined(MBEDTLS_SSL_PROTO_DTLS)
10232 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010233 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010234 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010235#endif
10236
Hanno Becker4a63ed42019-01-08 11:39:35 +000010237#if defined(MBEDTLS_ECDH_C) && \
10238 defined(MBEDTLS_USE_PSA_CRYPTO)
10239 psa_destroy_key( handshake->ecdh_psa_privkey );
10240#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10241
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010242 mbedtls_platform_zeroize( handshake,
10243 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010244}
10245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010247{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010248 if( session == NULL )
10249 return;
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010252 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010253#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010254
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010255#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010257#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010258
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010259 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010260}
10261
Paul Bakker5121ce52009-01-03 21:22:43 +000010262/*
10263 * Free an SSL context
10264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010266{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010267 if( ssl == NULL )
10268 return;
10269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010271
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010272 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010273 {
Angus Grattond8213d02016-05-25 20:56:48 +100010274 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010276 }
10277
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010278 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010279 {
Angus Grattond8213d02016-05-25 20:56:48 +100010280 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010282 }
10283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010285 if( ssl->compress_buf != NULL )
10286 {
Angus Grattond8213d02016-05-25 20:56:48 +100010287 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010289 }
10290#endif
10291
Paul Bakker48916f92012-09-16 19:57:18 +000010292 if( ssl->transform )
10293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294 mbedtls_ssl_transform_free( ssl->transform );
10295 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010296 }
10297
10298 if( ssl->handshake )
10299 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010300 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010301 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10302 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 mbedtls_free( ssl->handshake );
10305 mbedtls_free( ssl->transform_negotiate );
10306 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010307 }
10308
Paul Bakkerc0463502013-02-14 11:19:38 +010010309 if( ssl->session )
10310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311 mbedtls_ssl_session_free( ssl->session );
10312 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010313 }
10314
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010315#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010316 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010317 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010318 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010320 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10324 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10327 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010328 }
10329#endif
10330
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010331#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010333#endif
10334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010336
Paul Bakker86f04f42013-02-14 11:20:09 +010010337 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010338 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010339}
10340
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010341/*
10342 * Initialze mbedtls_ssl_config
10343 */
10344void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10345{
10346 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10347}
10348
Simon Butcherc97b6972015-12-27 23:48:17 +000010349#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010350static int ssl_preset_default_hashes[] = {
10351#if defined(MBEDTLS_SHA512_C)
10352 MBEDTLS_MD_SHA512,
10353 MBEDTLS_MD_SHA384,
10354#endif
10355#if defined(MBEDTLS_SHA256_C)
10356 MBEDTLS_MD_SHA256,
10357 MBEDTLS_MD_SHA224,
10358#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010359#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010360 MBEDTLS_MD_SHA1,
10361#endif
10362 MBEDTLS_MD_NONE
10363};
Simon Butcherc97b6972015-12-27 23:48:17 +000010364#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010365
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010366static int ssl_preset_suiteb_ciphersuites[] = {
10367 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10368 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10369 0
10370};
10371
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010372#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010373static int ssl_preset_suiteb_hashes[] = {
10374 MBEDTLS_MD_SHA256,
10375 MBEDTLS_MD_SHA384,
10376 MBEDTLS_MD_NONE
10377};
10378#endif
10379
10380#if defined(MBEDTLS_ECP_C)
10381static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10382 MBEDTLS_ECP_DP_SECP256R1,
10383 MBEDTLS_ECP_DP_SECP384R1,
10384 MBEDTLS_ECP_DP_NONE
10385};
10386#endif
10387
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010388/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010389 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010390 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010391int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010392 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010393{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010394#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010395 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010396#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010397
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010398 /* Use the functions here so that they are covered in tests,
10399 * but otherwise access member directly for efficiency */
10400 mbedtls_ssl_conf_endpoint( conf, endpoint );
10401 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010402
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010403 /*
10404 * Things that are common to all presets
10405 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010406#if defined(MBEDTLS_SSL_CLI_C)
10407 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10408 {
10409 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10410#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10411 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10412#endif
10413 }
10414#endif
10415
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010416#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010417 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010418#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010419
10420#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10421 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10422#endif
10423
10424#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10425 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10426#endif
10427
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010428#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10429 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10430#endif
10431
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010432#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010433 conf->f_cookie_write = ssl_cookie_write_dummy;
10434 conf->f_cookie_check = ssl_cookie_check_dummy;
10435#endif
10436
10437#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10438 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10439#endif
10440
Janos Follath088ce432017-04-10 12:42:31 +010010441#if defined(MBEDTLS_SSL_SRV_C)
10442 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10443#endif
10444
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010445#if defined(MBEDTLS_SSL_PROTO_DTLS)
10446 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10447 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10448#endif
10449
10450#if defined(MBEDTLS_SSL_RENEGOTIATION)
10451 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010452 memset( conf->renego_period, 0x00, 2 );
10453 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010454#endif
10455
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010456#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10457 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10458 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010459 const unsigned char dhm_p[] =
10460 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10461 const unsigned char dhm_g[] =
10462 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10463
Hanno Beckera90658f2017-10-04 15:29:08 +010010464 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10465 dhm_p, sizeof( dhm_p ),
10466 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010467 {
10468 return( ret );
10469 }
10470 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010471#endif
10472
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010473 /*
10474 * Preset-specific defaults
10475 */
10476 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010477 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010478 /*
10479 * NSA Suite B
10480 */
10481 case MBEDTLS_SSL_PRESET_SUITEB:
10482 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10483 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10484 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10485 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10486
10487 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10488 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10489 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10490 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10491 ssl_preset_suiteb_ciphersuites;
10492
10493#if defined(MBEDTLS_X509_CRT_PARSE_C)
10494 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010495#endif
10496
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010497#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010498 conf->sig_hashes = ssl_preset_suiteb_hashes;
10499#endif
10500
10501#if defined(MBEDTLS_ECP_C)
10502 conf->curve_list = ssl_preset_suiteb_curves;
10503#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010504 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010505
10506 /*
10507 * Default
10508 */
10509 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010510 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10511 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10512 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10513 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10514 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10515 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10516 MBEDTLS_SSL_MIN_MINOR_VERSION :
10517 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010518 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10519 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10520
10521#if defined(MBEDTLS_SSL_PROTO_DTLS)
10522 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10523 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10524#endif
10525
10526 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10527 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10528 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10529 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10530 mbedtls_ssl_list_ciphersuites();
10531
10532#if defined(MBEDTLS_X509_CRT_PARSE_C)
10533 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10534#endif
10535
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010536#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010537 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010538#endif
10539
10540#if defined(MBEDTLS_ECP_C)
10541 conf->curve_list = mbedtls_ecp_grp_id_list();
10542#endif
10543
10544#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10545 conf->dhm_min_bitlen = 1024;
10546#endif
10547 }
10548
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010549 return( 0 );
10550}
10551
10552/*
10553 * Free mbedtls_ssl_config
10554 */
10555void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10556{
10557#if defined(MBEDTLS_DHM_C)
10558 mbedtls_mpi_free( &conf->dhm_P );
10559 mbedtls_mpi_free( &conf->dhm_G );
10560#endif
10561
10562#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10563 if( conf->psk != NULL )
10564 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010565 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010566 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010567 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010568 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010569 }
10570
10571 if( conf->psk_identity != NULL )
10572 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010573 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010574 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010575 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010576 conf->psk_identity_len = 0;
10577 }
10578#endif
10579
10580#if defined(MBEDTLS_X509_CRT_PARSE_C)
10581 ssl_key_cert_free( conf->key_cert );
10582#endif
10583
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010584 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010585}
10586
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010587#if defined(MBEDTLS_PK_C) && \
10588 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010589/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594#if defined(MBEDTLS_RSA_C)
10595 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10596 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598#if defined(MBEDTLS_ECDSA_C)
10599 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10600 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010603}
10604
Hanno Becker7e5437a2017-04-28 17:15:26 +010010605unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10606{
10607 switch( type ) {
10608 case MBEDTLS_PK_RSA:
10609 return( MBEDTLS_SSL_SIG_RSA );
10610 case MBEDTLS_PK_ECDSA:
10611 case MBEDTLS_PK_ECKEY:
10612 return( MBEDTLS_SSL_SIG_ECDSA );
10613 default:
10614 return( MBEDTLS_SSL_SIG_ANON );
10615 }
10616}
10617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010618mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010619{
10620 switch( sig )
10621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622#if defined(MBEDTLS_RSA_C)
10623 case MBEDTLS_SSL_SIG_RSA:
10624 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626#if defined(MBEDTLS_ECDSA_C)
10627 case MBEDTLS_SSL_SIG_ECDSA:
10628 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010629#endif
10630 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010632 }
10633}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010634#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010635
Hanno Becker7e5437a2017-04-28 17:15:26 +010010636#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10637 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10638
10639/* Find an entry in a signature-hash set matching a given hash algorithm. */
10640mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10641 mbedtls_pk_type_t sig_alg )
10642{
10643 switch( sig_alg )
10644 {
10645 case MBEDTLS_PK_RSA:
10646 return( set->rsa );
10647 case MBEDTLS_PK_ECDSA:
10648 return( set->ecdsa );
10649 default:
10650 return( MBEDTLS_MD_NONE );
10651 }
10652}
10653
10654/* Add a signature-hash-pair to a signature-hash set */
10655void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10656 mbedtls_pk_type_t sig_alg,
10657 mbedtls_md_type_t md_alg )
10658{
10659 switch( sig_alg )
10660 {
10661 case MBEDTLS_PK_RSA:
10662 if( set->rsa == MBEDTLS_MD_NONE )
10663 set->rsa = md_alg;
10664 break;
10665
10666 case MBEDTLS_PK_ECDSA:
10667 if( set->ecdsa == MBEDTLS_MD_NONE )
10668 set->ecdsa = md_alg;
10669 break;
10670
10671 default:
10672 break;
10673 }
10674}
10675
10676/* Allow exactly one hash algorithm for each signature. */
10677void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10678 mbedtls_md_type_t md_alg )
10679{
10680 set->rsa = md_alg;
10681 set->ecdsa = md_alg;
10682}
10683
10684#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10685 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10686
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010687/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010688 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010691{
10692 switch( hash )
10693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694#if defined(MBEDTLS_MD5_C)
10695 case MBEDTLS_SSL_HASH_MD5:
10696 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698#if defined(MBEDTLS_SHA1_C)
10699 case MBEDTLS_SSL_HASH_SHA1:
10700 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702#if defined(MBEDTLS_SHA256_C)
10703 case MBEDTLS_SSL_HASH_SHA224:
10704 return( MBEDTLS_MD_SHA224 );
10705 case MBEDTLS_SSL_HASH_SHA256:
10706 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010707#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010708#if defined(MBEDTLS_SHA512_C)
10709 case MBEDTLS_SSL_HASH_SHA384:
10710 return( MBEDTLS_MD_SHA384 );
10711 case MBEDTLS_SSL_HASH_SHA512:
10712 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010713#endif
10714 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010715 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010716 }
10717}
10718
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010719/*
10720 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10721 */
10722unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10723{
10724 switch( md )
10725 {
10726#if defined(MBEDTLS_MD5_C)
10727 case MBEDTLS_MD_MD5:
10728 return( MBEDTLS_SSL_HASH_MD5 );
10729#endif
10730#if defined(MBEDTLS_SHA1_C)
10731 case MBEDTLS_MD_SHA1:
10732 return( MBEDTLS_SSL_HASH_SHA1 );
10733#endif
10734#if defined(MBEDTLS_SHA256_C)
10735 case MBEDTLS_MD_SHA224:
10736 return( MBEDTLS_SSL_HASH_SHA224 );
10737 case MBEDTLS_MD_SHA256:
10738 return( MBEDTLS_SSL_HASH_SHA256 );
10739#endif
10740#if defined(MBEDTLS_SHA512_C)
10741 case MBEDTLS_MD_SHA384:
10742 return( MBEDTLS_SSL_HASH_SHA384 );
10743 case MBEDTLS_MD_SHA512:
10744 return( MBEDTLS_SSL_HASH_SHA512 );
10745#endif
10746 default:
10747 return( MBEDTLS_SSL_HASH_NONE );
10748 }
10749}
10750
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010751#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010752/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010753 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010754 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010755 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010756int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010759
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010760 if( ssl->conf->curve_list == NULL )
10761 return( -1 );
10762
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010763 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010764 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010765 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010766
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010767 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010768}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010769#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010770
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010771#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010772/*
10773 * Check if a hash proposed by the peer is in our list.
10774 * Return 0 if we're willing to use it, -1 otherwise.
10775 */
10776int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10777 mbedtls_md_type_t md )
10778{
10779 const int *cur;
10780
10781 if( ssl->conf->sig_hashes == NULL )
10782 return( -1 );
10783
10784 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10785 if( *cur == (int) md )
10786 return( 0 );
10787
10788 return( -1 );
10789}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010790#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792#if defined(MBEDTLS_X509_CRT_PARSE_C)
10793int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10794 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010795 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010796 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010797{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010798 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010800 int usage = 0;
10801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010803 const char *ext_oid;
10804 size_t ext_len;
10805#endif
10806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10808 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010809 ((void) cert);
10810 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010811 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010812#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10815 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010816 {
10817 /* Server part of the key exchange */
10818 switch( ciphersuite->key_exchange )
10819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820 case MBEDTLS_KEY_EXCHANGE_RSA:
10821 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010822 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010823 break;
10824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010825 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10826 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10827 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10828 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010829 break;
10830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10832 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010833 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010834 break;
10835
10836 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837 case MBEDTLS_KEY_EXCHANGE_NONE:
10838 case MBEDTLS_KEY_EXCHANGE_PSK:
10839 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10840 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010841 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010842 usage = 0;
10843 }
10844 }
10845 else
10846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10848 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010849 }
10850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010852 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010853 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010854 ret = -1;
10855 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010856#else
10857 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10861 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10864 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010865 }
10866 else
10867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10869 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010870 }
10871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010873 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010874 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010875 ret = -1;
10876 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010877#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010878
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010879 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010882
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010883/*
10884 * Convert version numbers to/from wire format
10885 * and, for DTLS, to/from TLS equivalent.
10886 *
10887 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010888 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010889 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10890 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010892void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010893 unsigned char ver[2] )
10894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010895#if defined(MBEDTLS_SSL_PROTO_DTLS)
10896 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010899 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10900
10901 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10902 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10903 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010904 else
10905#else
10906 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010907#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010908 {
10909 ver[0] = (unsigned char) major;
10910 ver[1] = (unsigned char) minor;
10911 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010912}
10913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010915 const unsigned char ver[2] )
10916{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#if defined(MBEDTLS_SSL_PROTO_DTLS)
10918 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010919 {
10920 *major = 255 - ver[0] + 2;
10921 *minor = 255 - ver[1] + 1;
10922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010923 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010924 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10925 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010926 else
10927#else
10928 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010929#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010930 {
10931 *major = ver[0];
10932 *minor = ver[1];
10933 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010934}
10935
Simon Butcher99000142016-10-13 17:21:01 +010010936int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10937{
10938#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10939 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10940 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10941
10942 switch( md )
10943 {
10944#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10945#if defined(MBEDTLS_MD5_C)
10946 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010947 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010948#endif
10949#if defined(MBEDTLS_SHA1_C)
10950 case MBEDTLS_SSL_HASH_SHA1:
10951 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10952 break;
10953#endif
10954#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10955#if defined(MBEDTLS_SHA512_C)
10956 case MBEDTLS_SSL_HASH_SHA384:
10957 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10958 break;
10959#endif
10960#if defined(MBEDTLS_SHA256_C)
10961 case MBEDTLS_SSL_HASH_SHA256:
10962 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10963 break;
10964#endif
10965 default:
10966 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10967 }
10968
10969 return 0;
10970#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10971 (void) ssl;
10972 (void) md;
10973
10974 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10975#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10976}
10977
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010978#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10979 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10980int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10981 unsigned char *output,
10982 unsigned char *data, size_t data_len )
10983{
10984 int ret = 0;
10985 mbedtls_md5_context mbedtls_md5;
10986 mbedtls_sha1_context mbedtls_sha1;
10987
10988 mbedtls_md5_init( &mbedtls_md5 );
10989 mbedtls_sha1_init( &mbedtls_sha1 );
10990
10991 /*
10992 * digitally-signed struct {
10993 * opaque md5_hash[16];
10994 * opaque sha_hash[20];
10995 * };
10996 *
10997 * md5_hash
10998 * MD5(ClientHello.random + ServerHello.random
10999 * + ServerParams);
11000 * sha_hash
11001 * SHA(ClientHello.random + ServerHello.random
11002 * + ServerParams);
11003 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011004 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011005 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011007 goto exit;
11008 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011009 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011010 ssl->handshake->randbytes, 64 ) ) != 0 )
11011 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011013 goto exit;
11014 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011015 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011016 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011018 goto exit;
11019 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011020 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011021 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011023 goto exit;
11024 }
11025
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011026 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011027 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011029 goto exit;
11030 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011031 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011032 ssl->handshake->randbytes, 64 ) ) != 0 )
11033 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011035 goto exit;
11036 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011037 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011038 data_len ) ) != 0 )
11039 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011041 goto exit;
11042 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011043 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011044 output + 16 ) ) != 0 )
11045 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011047 goto exit;
11048 }
11049
11050exit:
11051 mbedtls_md5_free( &mbedtls_md5 );
11052 mbedtls_sha1_free( &mbedtls_sha1 );
11053
11054 if( ret != 0 )
11055 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11056 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11057
11058 return( ret );
11059
11060}
11061#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11062 MBEDTLS_SSL_PROTO_TLS1_1 */
11063
11064#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11065 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011066
11067#if defined(MBEDTLS_USE_PSA_CRYPTO)
11068int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11069 unsigned char *hash, size_t *hashlen,
11070 unsigned char *data, size_t data_len,
11071 mbedtls_md_type_t md_alg )
11072{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011073 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011074 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011075 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11076
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011078
11079 if( ( status = psa_hash_setup( &hash_operation,
11080 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011081 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011082 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011083 goto exit;
11084 }
11085
Andrzej Kurek814feff2019-01-14 04:35:19 -050011086 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11087 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011088 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011089 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011090 goto exit;
11091 }
11092
Andrzej Kurek814feff2019-01-14 04:35:19 -050011093 if( ( status = psa_hash_update( &hash_operation,
11094 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011095 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011096 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011097 goto exit;
11098 }
11099
Andrzej Kurek814feff2019-01-14 04:35:19 -050011100 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11101 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011102 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011103 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011104 goto exit;
11105 }
11106
11107exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011108 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011109 {
11110 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11111 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011112 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011113 {
11114 case PSA_ERROR_NOT_SUPPORTED:
11115 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011116 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011117 case PSA_ERROR_BUFFER_TOO_SMALL:
11118 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11119 case PSA_ERROR_INSUFFICIENT_MEMORY:
11120 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11121 default:
11122 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11123 }
11124 }
11125 return( 0 );
11126}
11127
11128#else
11129
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011130int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011131 unsigned char *hash, size_t *hashlen,
11132 unsigned char *data, size_t data_len,
11133 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011134{
11135 int ret = 0;
11136 mbedtls_md_context_t ctx;
11137 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011138 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011139
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011141
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011142 mbedtls_md_init( &ctx );
11143
11144 /*
11145 * digitally-signed struct {
11146 * opaque client_random[32];
11147 * opaque server_random[32];
11148 * ServerDHParams params;
11149 * };
11150 */
11151 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11152 {
11153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11154 goto exit;
11155 }
11156 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11157 {
11158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11159 goto exit;
11160 }
11161 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11162 {
11163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11164 goto exit;
11165 }
11166 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11167 {
11168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11169 goto exit;
11170 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011171 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 {
11173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11174 goto exit;
11175 }
11176
11177exit:
11178 mbedtls_md_free( &ctx );
11179
11180 if( ret != 0 )
11181 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11182 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11183
11184 return( ret );
11185}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011186#endif /* MBEDTLS_USE_PSA_CRYPTO */
11187
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011188#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11189 MBEDTLS_SSL_PROTO_TLS1_2 */
11190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191#endif /* MBEDTLS_SSL_TLS_C */