blob: a0f9584a7600fa1cdf27c098c2bdf16a01995a0e [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 Beckerca092242019-04-25 16:01:49 +0100131 ssl->negotiate_cid = enable;
132 if( enable == MBEDTLS_SSL_CID_DISABLED )
133 {
134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
135 return( 0 );
136 }
137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
138
139 if( own_cid_len > MBEDTLS_SSL_CID_IN_LEN_MAX )
140 {
141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID too large: Maximum %u, actual %u",
142 (unsigned) MBEDTLS_SSL_CID_IN_LEN_MAX,
143 (unsigned) own_cid_len ) );
144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145 }
146
147 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100148 /* Truncation is not an issue here because
149 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
150 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100151
152 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153 return( 0 );
154}
155
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100156/* WARNING: The CID feature isn't fully implemented yet
157 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100158int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
159 int *enabled,
160 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
161 size_t *peer_cid_len )
162{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100164
165 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
167
168 /* What shall we report if we have exchanged if both client
169 * and server have used the CID extension, but negotiated
170 * empty CIDs? This is indistinguishable from not using the
171 * CID extension in the first place, and we're reporting
172 * MBEDTLS_SSL_CID_DISABLED in this case. */
173 if( ssl->transform_in->in_cid_len == 0 &&
174 ssl->transform_in->out_cid_len == 0 )
175 {
176 return( 0 );
177 }
178
179 *peer_cid_len = ssl->transform_in->out_cid_len;
180 memcpy( peer_cid, ssl->transform_in->out_cid,
181 ssl->transform_in->out_cid_len );
182
183 *enabled = MBEDTLS_SSL_CID_ENABLED;
184
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100185 return( 0 );
186}
Hanno Becker35c36a62019-04-23 12:31:42 +0100187#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100188
Hanno Beckerd5847772018-08-28 10:09:23 +0100189/* Forward declarations for functions related to message buffering. */
190static void ssl_buffering_free( mbedtls_ssl_context *ssl );
191static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
192 uint8_t slot );
193static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
194static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
195static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
196static int ssl_buffer_message( mbedtls_ssl_context *ssl );
197static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100198static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100199
Hanno Beckera67dee22018-08-22 10:05:20 +0100200static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100201static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100202{
Hanno Becker11682cc2018-08-22 14:41:02 +0100203 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100204
205 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100206 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100207
208 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
209}
210
Hanno Becker67bc7c32018-08-06 11:33:50 +0100211static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
212{
Hanno Becker11682cc2018-08-22 14:41:02 +0100213 size_t const bytes_written = ssl->out_left;
214 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100215
216 /* Double-check that the write-index hasn't gone
217 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100218 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100219 {
220 /* Should never happen... */
221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
222 }
223
224 return( (int) ( mtu - bytes_written ) );
225}
226
227static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
228{
229 int ret;
230 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400231 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232
233#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
234 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
235
236 if( max_len > mfl )
237 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100238
239 /* By the standard (RFC 6066 Sect. 4), the MFL extension
240 * only limits the maximum record payload size, so in theory
241 * we would be allowed to pack multiple records of payload size
242 * MFL into a single datagram. However, this would mean that there's
243 * no way to explicitly communicate MTU restrictions to the peer.
244 *
245 * The following reduction of max_len makes sure that we never
246 * write datagrams larger than MFL + Record Expansion Overhead.
247 */
248 if( max_len <= ssl->out_left )
249 return( 0 );
250
251 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100252#endif
253
254 ret = ssl_get_remaining_space_in_datagram( ssl );
255 if( ret < 0 )
256 return( ret );
257 remaining = (size_t) ret;
258
259 ret = mbedtls_ssl_get_record_expansion( ssl );
260 if( ret < 0 )
261 return( ret );
262 expansion = (size_t) ret;
263
264 if( remaining <= expansion )
265 return( 0 );
266
267 remaining -= expansion;
268 if( remaining >= max_len )
269 remaining = max_len;
270
271 return( (int) remaining );
272}
273
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200274/*
275 * Double the retransmit timeout value, within the allowed range,
276 * returning -1 if the maximum value has already been reached.
277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279{
280 uint32_t new_timeout;
281
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200282 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200283 return( -1 );
284
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200285 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
286 * in the following way: after the initial transmission and a first
287 * retransmission, back off to a temporary estimated MTU of 508 bytes.
288 * This value is guaranteed to be deliverable (if not guaranteed to be
289 * delivered) of any compliant IPv4 (and IPv6) network, and should work
290 * on most non-IP stacks too. */
291 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400292 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200293 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
295 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200296
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200297 new_timeout = 2 * ssl->handshake->retransmit_timeout;
298
299 /* Avoid arithmetic overflow and range overflow */
300 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200301 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200302 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 }
305
306 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200308 ssl->handshake->retransmit_timeout ) );
309
310 return( 0 );
311}
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200314{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200315 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200317 ssl->handshake->retransmit_timeout ) );
318}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200322/*
323 * Convert max_fragment_length codes to length.
324 * RFC 6066 says:
325 * enum{
326 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
327 * } MaxFragmentLength;
328 * and we add 0 -> extension unused
329 */
Angus Grattond8213d02016-05-25 20:56:48 +1000330static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200331{
Angus Grattond8213d02016-05-25 20:56:48 +1000332 switch( mfl )
333 {
334 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
335 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
336 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
337 return 512;
338 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
339 return 1024;
340 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
341 return 2048;
342 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
343 return 4096;
344 default:
345 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
346 }
347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200349
Hanno Becker52055ae2019-02-06 14:30:46 +0000350int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
351 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_ssl_session_free( dst );
354 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000357
358#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200359 if( src->peer_cert != NULL )
360 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200361 int ret;
362
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200363 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200364 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200365 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200370 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373 dst->peer_cert = NULL;
374 return( ret );
375 }
376 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000377#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000378 if( src->peer_cert_digest != NULL )
379 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000380 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000381 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000382 if( dst->peer_cert_digest == NULL )
383 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
384
385 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
386 src->peer_cert_digest_len );
387 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000388 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000389 }
390#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200393
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200394#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200395 if( src->ticket != NULL )
396 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200397 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200398 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200399 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200400
401 memcpy( dst->ticket, src->ticket, src->ticket_len );
402 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200403#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
405 return( 0 );
406}
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
409int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200410 const unsigned char *key_enc, const unsigned char *key_dec,
411 size_t keylen,
412 const unsigned char *iv_enc, const unsigned char *iv_dec,
413 size_t ivlen,
414 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200415 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
417int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
420int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
421#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423/*
424 * Key material generation
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427static int ssl3_prf( const unsigned char *secret, size_t slen,
428 const char *label,
429 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000430 unsigned char *dstbuf, size_t dlen )
431{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100432 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000433 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200434 mbedtls_md5_context md5;
435 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000436 unsigned char padding[16];
437 unsigned char sha1sum[20];
438 ((void)label);
439
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200440 mbedtls_md5_init( &md5 );
441 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200442
Paul Bakker5f70b252012-09-13 14:23:06 +0000443 /*
444 * SSLv3:
445 * block =
446 * MD5( secret + SHA1( 'A' + secret + random ) ) +
447 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
448 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
449 * ...
450 */
451 for( i = 0; i < dlen / 16; i++ )
452 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200453 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100455 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100456 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000465
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000474 }
475
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200477 mbedtls_md5_free( &md5 );
478 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000479
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500480 mbedtls_platform_zeroize( padding, sizeof( padding ) );
481 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000482
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200488static int tls1_prf( const unsigned char *secret, size_t slen,
489 const char *label,
490 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000491 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000492{
Paul Bakker23986e52011-04-24 08:57:21 +0000493 size_t nb, hs;
494 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200495 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300496 unsigned char *tmp;
497 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 const mbedtls_md_info_t *md_info;
500 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100501 int ret;
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Ron Eldor3b350852019-05-07 18:31:49 +0300505 tmp_len = 20 + strlen( label ) + rlen;
506 tmp = mbedtls_calloc( 1, tmp_len );
507 if( tmp == NULL )
508 {
509 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
510 goto exit;
511 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 hs = ( slen + 1 ) / 2;
514 S1 = secret;
515 S2 = secret + slen - hs;
516
517 nb = strlen( label );
518 memcpy( tmp + 20, label, nb );
519 memcpy( tmp + 20 + nb, random, rlen );
520 nb += rlen;
521
522 /*
523 * First compute P_md5(secret,label+random)[0..dlen]
524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300526 {
527 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
528 goto exit;
529 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300532 {
533 goto exit;
534 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
537 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
538 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540 for( i = 0; i < dlen; i += 16 )
541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_md_hmac_reset ( &md_ctx );
543 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
544 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_reset ( &md_ctx );
547 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
548 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
551
552 for( j = 0; j < k; j++ )
553 dstbuf[i + j] = h_i[j];
554 }
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100557
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 /*
559 * XOR out with P_sha1(secret,label+random)[0..dlen]
560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300562 {
563 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
564 goto exit;
565 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300568 {
569 goto exit;
570 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
573 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
574 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 for( i = 0; i < dlen; i += 20 )
577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_hmac_reset ( &md_ctx );
579 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
580 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
584 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
587
588 for( j = 0; j < k; j++ )
589 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
590 }
591
Ron Eldor3b350852019-05-07 18:31:49 +0300592exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100594
Ron Eldor3b350852019-05-07 18:31:49 +0300595 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500596 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Ron Eldor3b350852019-05-07 18:31:49 +0300598 mbedtls_free( tmp );
599 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500604#if defined(MBEDTLS_USE_PSA_CRYPTO)
605static int tls_prf_generic( mbedtls_md_type_t md_type,
606 const unsigned char *secret, size_t slen,
607 const char *label,
608 const unsigned char *random, size_t rlen,
609 unsigned char *dstbuf, size_t dlen )
610{
611 psa_status_t status;
612 psa_algorithm_t alg;
613 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500614 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500615 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
616
Andrzej Kurek2f760752019-01-28 08:08:15 -0500617 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500618 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500619
Andrzej Kurekc929a822019-01-14 03:51:11 -0500620 if( md_type == MBEDTLS_MD_SHA384 )
621 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
622 else
623 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
624
Andrzej Kurek2f760752019-01-28 08:08:15 -0500625 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500626 psa_key_policy_set_usage( &policy,
627 PSA_KEY_USAGE_DERIVE,
628 alg );
629 status = psa_set_key_policy( master_slot, &policy );
630 if( status != PSA_SUCCESS )
631 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
632
633 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
634 if( status != PSA_SUCCESS )
635 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
636
637 status = psa_key_derivation( &generator,
638 master_slot, alg,
639 random, rlen,
640 (unsigned char const *) label,
641 (size_t) strlen( label ),
642 dlen );
643 if( status != PSA_SUCCESS )
644 {
645 psa_generator_abort( &generator );
646 psa_destroy_key( master_slot );
647 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
648 }
649
650 status = psa_generator_read( &generator, dstbuf, dlen );
651 if( status != PSA_SUCCESS )
652 {
653 psa_generator_abort( &generator );
654 psa_destroy_key( master_slot );
655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
656 }
657
658 status = psa_generator_abort( &generator );
659 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500660 {
661 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500662 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500663 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500664
665 status = psa_destroy_key( master_slot );
666 if( status != PSA_SUCCESS )
667 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
668
Andrzej Kurek33171262019-01-15 03:25:18 -0500669 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500670}
671
672#else /* MBEDTLS_USE_PSA_CRYPTO */
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100675 const unsigned char *secret, size_t slen,
676 const char *label,
677 const unsigned char *random, size_t rlen,
678 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000679{
680 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100681 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300682 unsigned char *tmp;
683 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
685 const mbedtls_md_info_t *md_info;
686 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100687 int ret;
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100695
Ron Eldor3b350852019-05-07 18:31:49 +0300696 tmp_len = md_len + strlen( label ) + rlen;
697 tmp = mbedtls_calloc( 1, tmp_len );
698 if( tmp == NULL )
699 {
700 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
701 goto exit;
702 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703
704 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100705 memcpy( tmp + md_len, label, nb );
706 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000707 nb += rlen;
708
709 /*
710 * Compute P_<hash>(secret, label + random)[0..dlen]
711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300713 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
716 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
717 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100718
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100719 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_md_hmac_reset ( &md_ctx );
722 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
723 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_md_hmac_reset ( &md_ctx );
726 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
727 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000728
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100729 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000730
731 for( j = 0; j < k; j++ )
732 dstbuf[i + j] = h_i[j];
733 }
734
Ron Eldor3b350852019-05-07 18:31:49 +0300735exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100737
Ron Eldor3b350852019-05-07 18:31:49 +0300738 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500739 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000740
Ron Eldor3b350852019-05-07 18:31:49 +0300741 mbedtls_free( tmp );
742
743 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100747static int tls_prf_sha256( const unsigned char *secret, size_t slen,
748 const char *label,
749 const unsigned char *random, size_t rlen,
750 unsigned char *dstbuf, size_t dlen )
751{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100753 label, random, rlen, dstbuf, dlen ) );
754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200758static int tls_prf_sha384( const unsigned char *secret, size_t slen,
759 const char *label,
760 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000761 unsigned char *dstbuf, size_t dlen )
762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100764 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#endif /* MBEDTLS_SHA512_C */
767#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
772 defined(MBEDTLS_SSL_PROTO_TLS1_1)
773static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200774#endif
Paul Bakker380da532012-04-18 16:10:25 +0000775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_SSL_PROTO_SSL3)
777static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
778static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200779#endif
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
782static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
783static void ssl_calc_finished_tls( 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_2)
787#if defined(MBEDTLS_SHA256_C)
788static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
789static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
790static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200791#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_SHA512_C)
794static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
795static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
796static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000799
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100800#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100801 defined(MBEDTLS_USE_PSA_CRYPTO)
802static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
803{
804 if( ssl->conf->f_psk != NULL )
805 {
806 /* If we've used a callback to select the PSK,
807 * the static configuration is irrelevant. */
808 if( ssl->handshake->psk_opaque != 0 )
809 return( 1 );
810
811 return( 0 );
812 }
813
814 if( ssl->conf->psk_opaque != 0 )
815 return( 1 );
816
817 return( 0 );
818}
819#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100820 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100821
Ron Eldorcf280092019-05-14 20:19:13 +0300822#if defined(MBEDTLS_SSL_EXPORT_KEYS)
823static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
824{
825#if defined(MBEDTLS_SSL_PROTO_SSL3)
826 if( tls_prf == ssl3_prf )
827 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300828 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300829 }
830 else
831#endif
832#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
833 if( tls_prf == tls1_prf )
834 {
835 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
836 }
837 else
838#endif
839#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
840#if defined(MBEDTLS_SHA512_C)
841 if( tls_prf == tls_prf_sha384 )
842 {
843 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
844 }
845 else
846#endif
847#if defined(MBEDTLS_SHA256_C)
848 if( tls_prf == tls_prf_sha256 )
849 {
850 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
851 }
852 else
853#endif
854#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
855 return( MBEDTLS_SSL_TLS_PRF_NONE );
856}
857#endif /* MBEDTLS_SSL_EXPORT_KEYS */
858
Ron Eldor51d3ab52019-05-12 14:54:30 +0300859int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
860 const unsigned char *secret, size_t slen,
861 const char *label,
862 const unsigned char *random, size_t rlen,
863 unsigned char *dstbuf, size_t dlen )
864{
865 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
866
867 switch( prf )
868 {
869#if defined(MBEDTLS_SSL_PROTO_SSL3)
870 case MBEDTLS_SSL_TLS_PRF_SSL3:
871 tls_prf = ssl3_prf;
872 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300873#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300874#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
875 case MBEDTLS_SSL_TLS_PRF_TLS1:
876 tls_prf = tls1_prf;
877 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300878#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
879
880#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300881#if defined(MBEDTLS_SHA512_C)
882 case MBEDTLS_SSL_TLS_PRF_SHA384:
883 tls_prf = tls_prf_sha384;
884 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300885#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300886#if defined(MBEDTLS_SHA256_C)
887 case MBEDTLS_SSL_TLS_PRF_SHA256:
888 tls_prf = tls_prf_sha256;
889 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300890#endif /* MBEDTLS_SHA256_C */
891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300892 default:
893 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
894 }
895
896 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
897}
898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000900{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200901 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000902#if defined(MBEDTLS_USE_PSA_CRYPTO)
903 int psa_fallthrough;
904#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 unsigned char keyblk[256];
907 unsigned char *key1;
908 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100909 unsigned char *mac_enc;
910 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000911 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200912 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000913 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000914 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 const mbedtls_cipher_info_t *cipher_info;
916 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100917
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000918 /* cf. RFC 5246, Section 8.1:
919 * "The master secret is always exactly 48 bytes in length." */
920 size_t const master_secret_len = 48;
921
Hanno Becker35b23c72018-10-23 12:10:41 +0100922#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
923 unsigned char session_hash[48];
924#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_ssl_session *session = ssl->session_negotiate;
927 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
928 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000932#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
933 transform->encrypt_then_mac = session->encrypt_then_mac;
934#endif
935 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000936
937 ciphersuite_info = handshake->ciphersuite_info;
938 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100939 if( cipher_info == NULL )
940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000942 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100944 }
945
Hanno Beckere694c3e2017-12-27 21:34:08 +0000946 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100947 if( md_info == NULL )
948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000950 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100952 }
953
Hanno Becker4bf74652019-04-26 16:22:27 +0100954#if defined(MBEDTLS_SSL_CID)
955 /* Copy own and peer's CID if the use of the CID
956 * extension has been negotiated. */
957 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
958 {
959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
960 transform->in_cid_len = ssl->own_cid_len;
961 transform->out_cid_len = ssl->handshake->peer_cid_len;
962 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
963 memcpy( transform->out_cid, ssl->handshake->peer_cid,
964 ssl->handshake->peer_cid_len );
965
966 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
967 transform->out_cid_len );
968 MBEDTLS_SSL_DEBUG_BUF( 3, "Ingoing CID", transform->in_cid,
969 transform->in_cid_len );
970 }
971#endif /* MBEDTLS_SSL_CID */
972
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000974 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_SSL_PROTO_SSL3)
977 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000978 {
Paul Bakker48916f92012-09-16 19:57:18 +0000979 handshake->tls_prf = ssl3_prf;
980 handshake->calc_verify = ssl_calc_verify_ssl;
981 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000982 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200983 else
984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
986 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000987 {
Paul Bakker48916f92012-09-16 19:57:18 +0000988 handshake->tls_prf = tls1_prf;
989 handshake->calc_verify = ssl_calc_verify_tls;
990 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000991 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200992 else
993#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
995#if defined(MBEDTLS_SHA512_C)
996 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000997 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000998 {
Paul Bakker48916f92012-09-16 19:57:18 +0000999 handshake->tls_prf = tls_prf_sha384;
1000 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1001 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001002 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001003 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#if defined(MBEDTLS_SHA256_C)
1006 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001007 {
Paul Bakker48916f92012-09-16 19:57:18 +00001008 handshake->tls_prf = tls_prf_sha256;
1009 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1010 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001011 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001012 else
1013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001018 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001019
1020 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 * SSLv3:
1022 * master =
1023 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1024 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1025 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001026 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001027 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 * master = PRF( premaster, "master secret", randbytes )[0..47]
1029 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001030 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1033 }
1034 else
1035 {
1036 /* The label for the KDF used for key expansion.
1037 * This is either "master secret" or "extended master secret"
1038 * depending on whether the Extended Master Secret extension
1039 * is used. */
1040 char const *lbl = "master secret";
1041
1042 /* The salt for the KDF used for key expansion.
1043 * - If the Extended Master Secret extension is not used,
1044 * this is ClientHello.Random + ServerHello.Random
1045 * (see Sect. 8.1 in RFC 5246).
1046 * - If the Extended Master Secret extension is used,
1047 * this is the transcript of the handshake so far.
1048 * (see Sect. 4 in RFC 7627). */
1049 unsigned char const *salt = handshake->randbytes;
1050 size_t salt_len = 64;
1051
1052#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001056
Hanno Becker35b23c72018-10-23 12:10:41 +01001057 lbl = "extended master secret";
1058 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001059 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1061 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001064 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1065 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001066 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001067 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001068 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001069#endif /* MBEDTLS_SHA512_C */
1070 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001071 }
1072 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001074 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001075
Hanno Becker35b23c72018-10-23 12:10:41 +01001076 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001077 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001078#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1079
Hanno Becker7d0a5692018-10-23 15:26:22 +01001080#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1081 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1082 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1083 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1084 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001085 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001086 /* Perform PSK-to-MS expansion in a single step. */
1087 psa_status_t status;
1088 psa_algorithm_t alg;
1089 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001090 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001091
1092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1093
1094 psk = ssl->conf->psk_opaque;
1095 if( ssl->handshake->psk_opaque != 0 )
1096 psk = ssl->handshake->psk_opaque;
1097
Hanno Becker22bf1452019-04-05 11:21:08 +01001098 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001099 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1100 else
1101 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1102
1103 status = psa_key_derivation( &generator, psk, alg,
1104 salt, salt_len,
1105 (unsigned char const *) lbl,
1106 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001107 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001108 if( status != PSA_SUCCESS )
1109 {
1110 psa_generator_abort( &generator );
1111 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1112 }
1113
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001114 status = psa_generator_read( &generator, session->master,
1115 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
1122 status = psa_generator_abort( &generator );
1123 if( status != PSA_SUCCESS )
1124 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001125 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001126 else
1127#endif
1128 {
1129 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1130 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001131 session->master,
1132 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001133 if( ret != 0 )
1134 {
1135 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1136 return( ret );
1137 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001138
Hanno Becker7d0a5692018-10-23 15:26:22 +01001139 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1140 handshake->premaster,
1141 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001142
Hanno Becker7d0a5692018-10-23 15:26:22 +01001143 mbedtls_platform_zeroize( handshake->premaster,
1144 sizeof(handshake->premaster) );
1145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001147
1148 /*
1149 * Swap the client and server random values.
1150 */
Paul Bakker48916f92012-09-16 19:57:18 +00001151 memcpy( tmp, handshake->randbytes, 64 );
1152 memcpy( handshake->randbytes, tmp + 32, 32 );
1153 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001154 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
1156 /*
1157 * SSLv3:
1158 * key block =
1159 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1160 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1161 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1162 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1163 * ...
1164 *
1165 * TLSv1:
1166 * key block = PRF( master, "key expansion", randbytes )
1167 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001168 ret = handshake->tls_prf( session->master, 48, "key expansion",
1169 handshake->randbytes, 64, keyblk, 256 );
1170 if( ret != 0 )
1171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001173 return( ret );
1174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1177 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1178 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1179 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1180 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001181
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 /*
1183 * Determine the appropriate key, IV and MAC length.
1184 */
Paul Bakker68884e32013-01-07 18:20:04 +01001185
Hanno Becker88aaf652017-12-27 08:17:40 +00001186 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001187
Hanno Becker8031d062018-01-03 15:32:31 +00001188#if defined(MBEDTLS_GCM_C) || \
1189 defined(MBEDTLS_CCM_C) || \
1190 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001192 cipher_info->mode == MBEDTLS_MODE_CCM ||
1193 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001195 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001196
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001197 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001198 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001199 transform->taglen =
1200 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001201
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001202 /* All modes haves 96-bit IVs;
1203 * GCM and CCM has 4 implicit and 8 explicit bytes
1204 * ChachaPoly has all 12 bytes implicit
1205 */
Paul Bakker68884e32013-01-07 18:20:04 +01001206 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001207 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1208 transform->fixed_ivlen = 12;
1209 else
1210 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001211
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001212 /* Minimum length of encrypted record */
1213 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001214 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001215 }
1216 else
Hanno Becker8031d062018-01-03 15:32:31 +00001217#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1218#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1219 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1220 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001221 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001222 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1224 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001227 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001230 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001231 mac_key_len = mbedtls_md_get_size( md_info );
1232 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001235 /*
1236 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1237 * (rfc 6066 page 13 or rfc 2104 section 4),
1238 * so we only need to adjust the length here.
1239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001243
1244#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1245 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001246 * HMAC implementation which also truncates the key
1247 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001248 mac_key_len = transform->maclen;
1249#endif
1250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001252
1253 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001254 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001256 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001258 transform->minlen = transform->maclen;
1259 else
Paul Bakker68884e32013-01-07 18:20:04 +01001260 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001261 /*
1262 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001263 * 1. if EtM is in use: one block plus MAC
1264 * otherwise: * first multiple of blocklen greater than maclen
1265 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1268 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001269 {
1270 transform->minlen = transform->maclen
1271 + cipher_info->block_size;
1272 }
1273 else
1274#endif
1275 {
1276 transform->minlen = transform->maclen
1277 + cipher_info->block_size
1278 - transform->maclen % cipher_info->block_size;
1279 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1282 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1283 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001284 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001285 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1288 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1289 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001290 {
1291 transform->minlen += transform->ivlen;
1292 }
1293 else
1294#endif
1295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001297 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1298 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001299 }
Paul Bakker68884e32013-01-07 18:20:04 +01001300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 }
Hanno Becker8031d062018-01-03 15:32:31 +00001302 else
1303#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1304 {
1305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Hanno Becker88aaf652017-12-27 08:17:40 +00001309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1310 (unsigned) keylen,
1311 (unsigned) transform->minlen,
1312 (unsigned) transform->ivlen,
1313 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
1315 /*
1316 * Finally setup the cipher contexts, IVs and MAC secrets.
1317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001319 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001321 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001322 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Paul Bakker68884e32013-01-07 18:20:04 +01001324 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001325 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001327 /*
1328 * This is not used in TLS v1.1.
1329 */
Paul Bakker48916f92012-09-16 19:57:18 +00001330 iv_copy_len = ( transform->fixed_ivlen ) ?
1331 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001332 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1333 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001334 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
1336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_SSL_CLI_C */
1338#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001339 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001341 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001342 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Hanno Becker81c7b182017-11-09 18:39:33 +00001344 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001345 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001347 /*
1348 * This is not used in TLS v1.1.
1349 */
Paul Bakker48916f92012-09-16 19:57:18 +00001350 iv_copy_len = ( transform->fixed_ivlen ) ?
1351 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001352 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1353 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001354 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001356 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001360 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1361 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Hanno Beckerd56ed242018-01-03 15:32:51 +00001364#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_SSL_PROTO_SSL3)
1366 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001367 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001368 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001371 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1372 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001373 }
1374
Hanno Becker81c7b182017-11-09 18:39:33 +00001375 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1376 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001377 }
1378 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1380#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1381 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1382 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001383 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001384 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1385 For AEAD-based ciphersuites, there is nothing to do here. */
1386 if( mac_key_len != 0 )
1387 {
1388 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1389 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1390 }
Paul Bakker68884e32013-01-07 18:20:04 +01001391 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001392 else
1393#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001396 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1397 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001398 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001399#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1402 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001403 {
1404 int ret = 0;
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001407
Hanno Becker88aaf652017-12-27 08:17:40 +00001408 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001409 transform->iv_enc, transform->iv_dec,
1410 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001411 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001412 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001415 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1416 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001417 }
1418 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001419#else
1420 ((void) mac_dec);
1421 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001423
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001424#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1425 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001426 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001427 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1428 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001429 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001430 iv_copy_len );
1431 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001432
1433 if( ssl->conf->f_export_keys_ext != NULL )
1434 {
1435 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1436 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001437 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001438 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001439 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001440 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001441 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001442 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001443#endif
1444
Hanno Beckerf704bef2018-11-16 15:21:18 +00001445#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001446
1447 /* Only use PSA-based ciphers for TLS-1.2.
1448 * That's relevant at least for TLS-1.0, where
1449 * we assume that mbedtls_cipher_crypt() updates
1450 * the structure field for the IV, which the PSA-based
1451 * implementation currently doesn't. */
1452#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1453 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001454 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001455 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001456 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001457 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1458 {
1459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001460 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001461 }
1462
1463 if( ret == 0 )
1464 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001466 psa_fallthrough = 0;
1467 }
1468 else
1469 {
1470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1471 psa_fallthrough = 1;
1472 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001473 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001474 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001475 psa_fallthrough = 1;
1476#else
1477 psa_fallthrough = 1;
1478#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001479
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001481#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001482 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001483 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001486 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001487 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001488
Hanno Beckerf704bef2018-11-16 15:21:18 +00001489#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001490 /* Only use PSA-based ciphers for TLS-1.2.
1491 * That's relevant at least for TLS-1.0, where
1492 * we assume that mbedtls_cipher_crypt() updates
1493 * the structure field for the IV, which the PSA-based
1494 * implementation currently doesn't. */
1495#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1496 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001497 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001498 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001499 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1501 {
1502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001503 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001504 }
1505
1506 if( ret == 0 )
1507 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001508 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001509 psa_fallthrough = 0;
1510 }
1511 else
1512 {
1513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1514 psa_fallthrough = 1;
1515 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001516 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001517 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001518 psa_fallthrough = 1;
1519#else
1520 psa_fallthrough = 1;
1521#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001522
Hanno Beckercb1cc802018-11-17 22:27:38 +00001523 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001524#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001525 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001526 cipher_info ) ) != 0 )
1527 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001529 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001530 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001533 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001537 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
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_DECRYPT ) ) != 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 Bakkerda02a7f2013-08-31 17:25:14 +02001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_CIPHER_MODE_CBC)
1549 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1552 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001555 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001556 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1559 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001562 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001566
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001569 // Initialize compression
1570 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001572 {
Paul Bakker16770332013-10-11 09:59:44 +02001573 if( ssl->compress_buf == NULL )
1574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001576 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001577 if( ssl->compress_buf == NULL )
1578 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001580 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001581 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1582 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001583 }
1584 }
1585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001587
Paul Bakker48916f92012-09-16 19:57:18 +00001588 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1589 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001590
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001591 if( deflateInit( &transform->ctx_deflate,
1592 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001593 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001596 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1597 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001598 }
1599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001603end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001604 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1605 mbedtls_platform_zeroize( handshake->randbytes,
1606 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001607 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001608}
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_SSL_PROTO_SSL3)
1611void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001612{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001613 mbedtls_md5_context md5;
1614 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 unsigned char pad_1[48];
1616 unsigned char pad_2[48];
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001620 mbedtls_md5_init( &md5 );
1621 mbedtls_sha1_init( &sha1 );
1622
1623 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1624 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
Paul Bakker380da532012-04-18 16:10:25 +00001626 memset( pad_1, 0x36, 48 );
1627 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001629 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1630 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1631 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001633 mbedtls_md5_starts_ret( &md5 );
1634 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1635 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1636 mbedtls_md5_update_ret( &md5, hash, 16 );
1637 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001639 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1640 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1641 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001642
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001643 mbedtls_sha1_starts_ret( &sha1 );
1644 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1645 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1646 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1647 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001651
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001652 mbedtls_md5_free( &md5 );
1653 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001654
Paul Bakker380da532012-04-18 16:10:25 +00001655 return;
1656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1660void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001661{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001662 mbedtls_md5_context md5;
1663 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001666
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001667 mbedtls_md5_init( &md5 );
1668 mbedtls_sha1_init( &sha1 );
1669
1670 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1671 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001672
Andrzej Kurekeb342242019-01-29 09:14:33 -05001673 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001674 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001678
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001679 mbedtls_md5_free( &md5 );
1680 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001681
Paul Bakker380da532012-04-18 16:10:25 +00001682 return;
1683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1687#if defined(MBEDTLS_SHA256_C)
1688void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001689{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001690#if defined(MBEDTLS_USE_PSA_CRYPTO)
1691 size_t hash_size;
1692 psa_status_t status;
1693 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1694
1695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1696 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1697 if( status != PSA_SUCCESS )
1698 {
1699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1700 return;
1701 }
1702
1703 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1704 if( status != PSA_SUCCESS )
1705 {
1706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1707 return;
1708 }
1709 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1711#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001712 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001713
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001714 mbedtls_sha256_init( &sha256 );
1715
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001717
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001718 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001719 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001723
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001724 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001725#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001726 return;
1727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_SHA512_C)
1731void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001732{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001733#if defined(MBEDTLS_USE_PSA_CRYPTO)
1734 size_t hash_size;
1735 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001736 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001737
1738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001739 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001740 if( status != PSA_SUCCESS )
1741 {
1742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1743 return;
1744 }
1745
Andrzej Kurek972fba52019-01-30 03:29:12 -05001746 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001747 if( status != PSA_SUCCESS )
1748 {
1749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1750 return;
1751 }
1752 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1754#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001755 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001756
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001757 mbedtls_sha512_init( &sha512 );
1758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001760
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001761 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001762 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001766
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001767 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001768#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 return;
1770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#endif /* MBEDTLS_SHA512_C */
1772#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1775int 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 +02001776{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001777 unsigned char *p = ssl->handshake->premaster;
1778 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001779 const unsigned char *psk = ssl->conf->psk;
1780 size_t psk_len = ssl->conf->psk_len;
1781
1782 /* If the psk callback was called, use its result */
1783 if( ssl->handshake->psk != NULL )
1784 {
1785 psk = ssl->handshake->psk;
1786 psk_len = ssl->handshake->psk_len;
1787 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001788
1789 /*
1790 * PMS = struct {
1791 * opaque other_secret<0..2^16-1>;
1792 * opaque psk<0..2^16-1>;
1793 * };
1794 * with "other_secret" depending on the particular key exchange
1795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1797 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001798 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001799 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001801
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001802 *(p++) = (unsigned char)( psk_len >> 8 );
1803 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001804
1805 if( end < p || (size_t)( end - p ) < psk_len )
1806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1807
1808 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001809 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001810 }
1811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1813#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1814 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001815 {
1816 /*
1817 * other_secret already set by the ClientKeyExchange message,
1818 * and is 48 bytes long
1819 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001820 if( end - p < 2 )
1821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1822
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001823 *p++ = 0;
1824 *p++ = 48;
1825 p += 48;
1826 }
1827 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1829#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1830 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001831 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001832 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001833 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001834
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001835 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001837 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001838 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001841 return( ret );
1842 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001843 *(p++) = (unsigned char)( len >> 8 );
1844 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001845 p += len;
1846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001848 }
1849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1851#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1852 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001854 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001855 size_t zlen;
1856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001858 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001859 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001862 return( ret );
1863 }
1864
1865 *(p++) = (unsigned char)( zlen >> 8 );
1866 *(p++) = (unsigned char)( zlen );
1867 p += zlen;
1868
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001869 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1870 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001871 }
1872 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001877 }
1878
1879 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001880 if( end - p < 2 )
1881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001882
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001883 *(p++) = (unsigned char)( psk_len >> 8 );
1884 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001885
1886 if( end < p || (size_t)( end - p ) < psk_len )
1887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1888
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001889 memcpy( p, psk, psk_len );
1890 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001891
1892 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1893
1894 return( 0 );
1895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001899/*
1900 * SSLv3.0 MAC functions
1901 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001902#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001903static void ssl_mac( mbedtls_md_context_t *md_ctx,
1904 const unsigned char *secret,
1905 const unsigned char *buf, size_t len,
1906 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001907 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001908{
1909 unsigned char header[11];
1910 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001911 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1913 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001914
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001915 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001917 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001918 else
Paul Bakker68884e32013-01-07 18:20:04 +01001919 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
1921 memcpy( header, ctr, 8 );
1922 header[ 8] = (unsigned char) type;
1923 header[ 9] = (unsigned char)( len >> 8 );
1924 header[10] = (unsigned char)( len );
1925
Paul Bakker68884e32013-01-07 18:20:04 +01001926 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 mbedtls_md_starts( md_ctx );
1928 mbedtls_md_update( md_ctx, secret, md_size );
1929 mbedtls_md_update( md_ctx, padding, padlen );
1930 mbedtls_md_update( md_ctx, header, 11 );
1931 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001932 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Paul Bakker68884e32013-01-07 18:20:04 +01001934 memset( padding, 0x5C, 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 );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001938 mbedtls_md_update( md_ctx, out, md_size );
1939 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001942
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001943/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001944 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001945#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001946 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1947 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1948 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1949/* This function makes sure every byte in the memory region is accessed
1950 * (in ascending addresses order) */
1951static void ssl_read_memory( unsigned char *p, size_t len )
1952{
1953 unsigned char acc = 0;
1954 volatile unsigned char force;
1955
1956 for( ; len != 0; p++, len-- )
1957 acc ^= *p;
1958
1959 force = acc;
1960 (void) force;
1961}
1962#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1963
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001964/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001966 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001967
1968static void ssl_extract_add_data_from_record( unsigned char* add_data,
1969 mbedtls_record *rec )
1970{
1971 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1972 add_data[8] = rec->type;
1973 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1974 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1975 add_data[12] = rec->data_len & 0xFF;
1976}
1977
Hanno Beckera18d1322018-01-03 14:27:32 +00001978int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1979 mbedtls_ssl_transform *transform,
1980 mbedtls_record *rec,
1981 int (*f_rng)(void *, unsigned char *, size_t),
1982 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001983{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001985 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001986 unsigned char * data;
1987 unsigned char add_data[13];
1988 size_t post_avail;
1989
1990 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001991#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001992 ((void) ssl);
1993#endif
1994
1995 /* The PRNG is used for dynamic IV generation that's used
1996 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1997#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1998 ( defined(MBEDTLS_AES_C) || \
1999 defined(MBEDTLS_ARIA_C) || \
2000 defined(MBEDTLS_CAMELLIA_C) ) && \
2001 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2002 ((void) f_rng);
2003 ((void) p_rng);
2004#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002008 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002009 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2012 }
2013 if( rec == NULL ||
2014 rec->buf == NULL ||
2015 rec->buf_len < rec->data_offset ||
2016 rec->buf_len - rec->data_offset < rec->data_len )
2017 {
2018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002020 }
2021
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002022 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2023 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002025 data, rec->data_len );
2026
2027 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2028
2029 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2030 {
2031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2032 (unsigned) rec->data_len,
2033 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2035 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002036
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002038 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 */
Hanno Becker52344c22018-01-03 15:24:20 +00002040#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 if( mode == MBEDTLS_MODE_STREAM ||
2042 ( mode == MBEDTLS_MODE_CBC
2043#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002044 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002045#endif
2046 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002048 if( post_avail < transform->maclen )
2049 {
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2051 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2052 }
2053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002055 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002056 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002057 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002058 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2059 data, rec->data_len, rec->ctr, rec->type, mac );
2060 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002061 }
2062 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2065 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002066 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002067 {
Hanno Becker992b6872017-11-09 18:57:39 +00002068 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2069
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002070 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002071
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2073 sizeof( add_data ) );
2074 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2075 data, rec->data_len );
2076 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2077 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2078
2079 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002080 }
2081 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002082#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002086 }
2087
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002088 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2089 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002090
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002091 rec->data_len += transform->maclen;
2092 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002093 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002094 }
Hanno Becker52344c22018-01-03 15:24:20 +00002095#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002097 /*
2098 * Encrypt
2099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2101 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002103 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002104 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002106 "including %d bytes of padding",
2107 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002109 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2110 transform->iv_enc, transform->ivlen,
2111 data, rec->data_len,
2112 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002115 return( ret );
2116 }
2117
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002118 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
Paul Bakker68884e32013-01-07 18:20:04 +01002124 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002126
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002127#if defined(MBEDTLS_GCM_C) || \
2128 defined(MBEDTLS_CCM_C) || \
2129 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002131 mode == MBEDTLS_MODE_CCM ||
2132 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002133 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002134 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002135 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002137
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 /* Check that there's space for both the authentication tag
2139 * and the explicit IV before and after the record content. */
2140 if( post_avail < transform->taglen ||
2141 rec->data_offset < explicit_iv_len )
2142 {
2143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2144 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2145 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002146
Paul Bakker68884e32013-01-07 18:20:04 +01002147 /*
2148 * Generate IV
2149 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002150 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2151 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002152 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002153 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002154 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2155 explicit_iv_len );
2156 /* Prefix record content with explicit IV. */
2157 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002158 }
2159 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2160 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002161 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002162 unsigned char i;
2163
2164 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2165
2166 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002167 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002168 }
2169 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002170 {
2171 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002174 }
2175
Hanno Becker1f10d762019-04-26 13:34:37 +01002176 ssl_extract_add_data_from_record( add_data, rec );
2177
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002178 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2179 iv, transform->ivlen );
2180 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002181 data - explicit_iv_len, explicit_iv_len );
2182 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2183 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002185 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002186 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002187
Paul Bakker68884e32013-01-07 18:20:04 +01002188 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002189 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002190 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002191
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002192 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002193 iv, transform->ivlen,
2194 add_data, 13, /* add data */
2195 data, rec->data_len, /* source */
2196 data, &rec->data_len, /* destination */
2197 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002200 return( ret );
2201 }
2202
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002203 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2204 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002205
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002206 rec->data_len += transform->taglen + explicit_iv_len;
2207 rec->data_offset -= explicit_iv_len;
2208 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002209 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2213#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002214 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002217 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002218 size_t padlen, i;
2219 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002220
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002221 /* Currently we're always using minimal padding
2222 * (up to 255 bytes would be allowed). */
2223 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2224 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 padlen = 0;
2226
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 /* Check there's enough space in the buffer for the padding. */
2228 if( post_avail < padlen + 1 )
2229 {
2230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2231 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2232 }
2233
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002235 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002237 rec->data_len += padlen + 1;
2238 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002241 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002242 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2243 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002244 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002245 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002246 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002247 if( f_rng == NULL )
2248 {
2249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2251 }
2252
2253 if( rec->data_offset < transform->ivlen )
2254 {
2255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2256 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2257 }
2258
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002259 /*
2260 * Generate IV
2261 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002263 if( ret != 0 )
2264 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002265
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002266 memcpy( data - transform->ivlen, transform->iv_enc,
2267 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002268
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002273 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002274 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002275 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002277 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2278 transform->iv_enc,
2279 transform->ivlen,
2280 data, rec->data_len,
2281 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002284 return( ret );
2285 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002286
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002291 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002294 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002295 {
2296 /*
2297 * Save IV in SSL3 and TLS1
2298 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002299 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2300 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002302 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002303#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002304 {
2305 data -= transform->ivlen;
2306 rec->data_offset -= transform->ivlen;
2307 rec->data_len += transform->ivlen;
2308 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002312 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002313 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2314
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315 /*
2316 * MAC(MAC_write_key, seq_num +
2317 * TLSCipherText.type +
2318 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002319 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002320 * IV + // except for TLS 1.0
2321 * ENC(content + padding + padding_length));
2322 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323
2324 if( post_avail < transform->maclen)
2325 {
2326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2327 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2328 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002329
Hanno Becker1f10d762019-04-26 13:34:37 +01002330 ssl_extract_add_data_from_record( add_data, rec );
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2334 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002335
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002336 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2337 sizeof( add_data ) );
2338 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2339 data, rec->data_len );
2340 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2341 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002342
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002343 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002344
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 rec->data_len += transform->maclen;
2346 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002347 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002351 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002353 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2356 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002357 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002359 /* Make extra sure authentication was performed, exactly once */
2360 if( auth_done != 1 )
2361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002364 }
2365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
2368 return( 0 );
2369}
2370
Hanno Beckera18d1322018-01-03 14:27:32 +00002371int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2372 mbedtls_ssl_transform *transform,
2373 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002374{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002375 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002377 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002378#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002379 size_t padlen = 0, correct = 1;
2380#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002381 unsigned char* data;
2382 unsigned char add_data[13];
2383
Hanno Beckera18d1322018-01-03 14:27:32 +00002384#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002385 ((void) ssl);
2386#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002389 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002390 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2393 }
2394 if( rec == NULL ||
2395 rec->buf == NULL ||
2396 rec->buf_len < rec->data_offset ||
2397 rec->buf_len - rec->data_offset < rec->data_len )
2398 {
2399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002401 }
2402
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002403 data = rec->buf + rec->data_offset;
2404 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2407 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002408 {
2409 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002410 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2411 transform->iv_dec,
2412 transform->ivlen,
2413 data, rec->data_len,
2414 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002417 return( ret );
2418 }
2419
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002420 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 }
Paul Bakker68884e32013-01-07 18:20:04 +01002426 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002428#if defined(MBEDTLS_GCM_C) || \
2429 defined(MBEDTLS_CCM_C) || \
2430 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002432 mode == MBEDTLS_MODE_CCM ||
2433 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002434 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002435 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002436 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002437
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002438 /*
2439 * Compute and update sizes
2440 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002441 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002444 "+ taglen (%d)", rec->data_len,
2445 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002447 }
Paul Bakker68884e32013-01-07 18:20:04 +01002448
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002449 /*
2450 * Prepare IV
2451 */
2452 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2453 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002454 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002455 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002456 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002457
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002458 }
2459 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2460 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002461 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002462 unsigned char i;
2463
2464 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2465
2466 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002467 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002468 }
2469 else
2470 {
2471 /* Reminder if we ever add an AEAD mode with a different size */
2472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2474 }
2475
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002476 data += explicit_iv_len;
2477 rec->data_offset += explicit_iv_len;
2478 rec->data_len -= explicit_iv_len + transform->taglen;
2479
2480 ssl_extract_add_data_from_record( add_data, rec );
2481 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2482 add_data, 13 );
2483
2484 memcpy( transform->iv_dec + transform->fixed_ivlen,
2485 data - explicit_iv_len, explicit_iv_len );
2486
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002487 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002488 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002489 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002490
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002491
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002492 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002493 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002494 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002495 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2496 iv, transform->ivlen,
2497 add_data, 13,
2498 data, rec->data_len,
2499 data, &olen,
2500 data + rec->data_len,
2501 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2506 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002507
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002508 return( ret );
2509 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002510 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002511
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002512 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002516 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2520#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002521 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002524 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 /*
Paul Bakker45829992013-01-03 14:52:21 +01002527 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002530 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2531 {
2532 /* The ciphertext is prefixed with the CBC IV. */
2533 minlen += transform->ivlen;
2534 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#endif
Paul Bakker45829992013-01-03 14:52:21 +01002536
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002537 /* Size considerations:
2538 *
2539 * - The CBC cipher text must not be empty and hence
2540 * at least of size transform->ivlen.
2541 *
2542 * Together with the potential IV-prefix, this explains
2543 * the first of the two checks below.
2544 *
2545 * - The record must contain a MAC, either in plain or
2546 * encrypted, depending on whether Encrypt-then-MAC
2547 * is used or not.
2548 * - If it is, the message contains the IV-prefix,
2549 * the CBC ciphertext, and the MAC.
2550 * - If it is not, the padded plaintext, and hence
2551 * the CBC ciphertext, has at least length maclen + 1
2552 * because there is at least the padding length byte.
2553 *
2554 * As the CBC ciphertext is not empty, both cases give the
2555 * lower bound minlen + maclen + 1 on the record size, which
2556 * we test for in the second check below.
2557 */
2558 if( rec->data_len < minlen + transform->ivlen ||
2559 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 "+ 1 ) ( + expl IV )", rec->data_len,
2563 transform->ivlen,
2564 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002566 }
2567
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002568 /*
2569 * Authenticate before decrypt if enabled
2570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002572 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002573 {
Hanno Becker992b6872017-11-09 18:57:39 +00002574 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002577
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002578 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2579 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002580
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002581 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002582
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002583 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2584 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2585 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2586 data, rec->data_len );
2587 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2588 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002589
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002590 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2591 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002592 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002594
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002595 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2596 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002600 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002601 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002604
2605 /*
2606 * Check length sanity
2607 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002608 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002611 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002613 }
2614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002616 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002617 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002618 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002619 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002620 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002621 /* This is safe because data_len >= minlen + maclen + 1 initially,
2622 * and at this point we have at most subtracted maclen (note that
2623 * minlen == transform->ivlen here). */
2624 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002625
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002626 data += transform->ivlen;
2627 rec->data_offset += transform->ivlen;
2628 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002629 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002631
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002632 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2633 transform->iv_dec, transform->ivlen,
2634 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002637 return( ret );
2638 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002639
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002640 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2643 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002644 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002647 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002648 {
2649 /*
2650 * Save IV in SSL3 and TLS1
2651 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2653 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002655#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657 /* Safe since data_len >= minlen + maclen + 1, so after having
2658 * subtracted at most minlen and maclen up to this point,
2659 * data_len > 0. */
2660 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002661
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002662 if( auth_done == 1 )
2663 {
2664 correct *= ( rec->data_len >= padlen + 1 );
2665 padlen *= ( rec->data_len >= padlen + 1 );
2666 }
2667 else
Paul Bakker45829992013-01-03 14:52:21 +01002668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002670 if( rec->data_len < transform->maclen + padlen + 1 )
2671 {
2672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2673 rec->data_len,
2674 transform->maclen,
2675 padlen + 1 ) );
2676 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002677#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678
2679 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2680 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 padlen++;
2684
2685 /* Regardless of the validity of the padding,
2686 * we have data_len >= padlen here. */
2687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002689 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002691 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#if defined(MBEDTLS_SSL_DEBUG_ALL)
2694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002695 "should be no more than %d",
2696 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002697#endif
Paul Bakker45829992013-01-03 14:52:21 +01002698 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700 }
2701 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2703#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2704 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002705 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002707 /* The padding check involves a series of up to 256
2708 * consecutive memory reads at the end of the record
2709 * plaintext buffer. In order to hide the length and
2710 * validity of the padding, always perform exactly
2711 * `min(256,plaintext_len)` reads (but take into account
2712 * only the last `padlen` bytes for the padding check). */
2713 size_t pad_count = 0;
2714 size_t real_count = 0;
2715 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002716
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002717 /* Index of first padding byte; it has been ensured above
2718 * that the subtraction is safe. */
2719 size_t const padding_idx = rec->data_len - padlen;
2720 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2721 size_t const start_idx = rec->data_len - num_checks;
2722 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002723
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002724 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002725 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002726 real_count |= ( idx >= padding_idx );
2727 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002728 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002729 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002732 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002734#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002735 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002737 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2739 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2742 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002743 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002744
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002745 /* If the padding was found to be invalid, padlen == 0
2746 * and the subtraction is safe. If the padding was found valid,
2747 * padlen hasn't been changed and the previous assertion
2748 * data_len >= padlen still holds. */
2749 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002751 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002753 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2756 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002757 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002759#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002762#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
2764 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765 * Authenticate if not done yet.
2766 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 */
Hanno Becker52344c22018-01-03 15:24:20 +00002768#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002769 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002770 {
Hanno Becker992b6872017-11-09 18:57:39 +00002771 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002772
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002773 /* If the initial value of padlen was such that
2774 * data_len < maclen + padlen + 1, then padlen
2775 * got reset to 1, and the initial check
2776 * data_len >= minlen + maclen + 1
2777 * guarantees that at this point we still
2778 * have at least data_len >= maclen.
2779 *
2780 * If the initial value of padlen was such that
2781 * data_len >= maclen + padlen + 1, then we have
2782 * subtracted either padlen + 1 (if the padding was correct)
2783 * or 0 (if the padding was incorrect) since then,
2784 * hence data_len >= maclen in any case.
2785 */
2786 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002788 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002791 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002792 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793 ssl_mac( &transform->md_ctx_dec,
2794 transform->mac_dec,
2795 data, rec->data_len,
2796 rec->ctr, rec->type,
2797 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002798 }
2799 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2801#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2802 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002803 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002804 {
2805 /*
2806 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002807 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002808 *
2809 * Known timing attacks:
2810 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2811 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002812 * To compensate for different timings for the MAC calculation
2813 * depending on how much padding was removed (which is determined
2814 * by padlen), process extra_run more blocks through the hash
2815 * function.
2816 *
2817 * The formula in the paper is
2818 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2819 * where L1 is the size of the header plus the decrypted message
2820 * plus CBC padding and L2 is the size of the header plus the
2821 * decrypted message. This is for an underlying hash function
2822 * with 64-byte blocks.
2823 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2824 * correctly. We round down instead of up, so -56 is the correct
2825 * value for our calculations instead of -55.
2826 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002827 * Repeat the formula rather than defining a block_size variable.
2828 * This avoids requiring division by a variable at runtime
2829 * (which would be marginally less efficient and would require
2830 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002831 */
2832 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002833 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002834
2835 /*
2836 * The next two sizes are the minimum and maximum values of
2837 * in_msglen over all padlen values.
2838 *
2839 * They're independent of padlen, since we previously did
2840 * in_msglen -= padlen.
2841 *
2842 * Note that max_len + maclen is never more than the buffer
2843 * length, as we previously did in_msglen -= maclen too.
2844 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002845 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002846 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2847
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002848 memset( tmp, 0, sizeof( tmp ) );
2849
2850 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002851 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002852#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2853 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002854 case MBEDTLS_MD_MD5:
2855 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002856 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002857 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002858 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2859 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002860 break;
2861#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002862#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002863 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002864 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002865 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2866 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002867 break;
2868#endif
2869 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2872 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002873
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002874 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002875
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002876 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2877 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2878 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002879 /* Make sure we access everything even when padlen > 0. This
2880 * makes the synchronisation requirements for just-in-time
2881 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 ssl_read_memory( data + rec->data_len, padlen );
2883 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002884
2885 /* Call mbedtls_md_process at least once due to cache attacks
2886 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002887 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002888 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002889
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002891
2892 /* Make sure we access all the memory that could contain the MAC,
2893 * before we check it in the next code block. This makes the
2894 * synchronisation requirements for just-in-time Prime+Probe
2895 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002896 ssl_read_memory( data + min_len,
2897 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002898 }
2899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2901 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002907#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002908 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2909 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002910#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002912 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2913 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_DEBUG_ALL)
2916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002917#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002918 correct = 0;
2919 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002920 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002921 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002922
2923 /*
2924 * Finally check the correct flag
2925 */
2926 if( correct == 0 )
2927 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002928#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002929
2930 /* Make extra sure authentication was performed, exactly once */
2931 if( auth_done != 1 )
2932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
2939 return( 0 );
2940}
2941
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002942#undef MAC_NONE
2943#undef MAC_PLAINTEXT
2944#undef MAC_CIPHERTEXT
2945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947/*
2948 * Compression/decompression functions
2949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951{
2952 int ret;
2953 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002954 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002956 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002960 if( len_pre == 0 )
2961 return( 0 );
2962
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963 memcpy( msg_pre, ssl->out_msg, len_pre );
2964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002966 ssl->out_msglen ) );
2967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969 ssl->out_msg, ssl->out_msglen );
2970
Paul Bakker48916f92012-09-16 19:57:18 +00002971 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2972 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2973 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002974 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975
Paul Bakker48916f92012-09-16 19:57:18 +00002976 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002977 if( ret != Z_OK )
2978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2980 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002981 }
2982
Angus Grattond8213d02016-05-25 20:56:48 +10002983 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002984 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002987 ssl->out_msglen ) );
2988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002990 ssl->out_msg, ssl->out_msglen );
2991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002993
2994 return( 0 );
2995}
2996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002998{
2999 int ret;
3000 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003001 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003002 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003003 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003006
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003007 if( len_pre == 0 )
3008 return( 0 );
3009
Paul Bakker2770fbd2012-07-03 13:30:23 +00003010 memcpy( msg_pre, ssl->in_msg, len_pre );
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003013 ssl->in_msglen ) );
3014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003016 ssl->in_msg, ssl->in_msglen );
3017
Paul Bakker48916f92012-09-16 19:57:18 +00003018 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3019 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3020 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003021 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003022 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003023
Paul Bakker48916f92012-09-16 19:57:18 +00003024 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003025 if( ret != Z_OK )
3026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3028 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003029 }
3030
Angus Grattond8213d02016-05-25 20:56:48 +10003031 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003032 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003035 ssl->in_msglen ) );
3036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003038 ssl->in_msg, ssl->in_msglen );
3039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003041
3042 return( 0 );
3043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3047static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#if defined(MBEDTLS_SSL_PROTO_DTLS)
3050static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003051{
3052 /* If renegotiation is not enforced, retransmit until we would reach max
3053 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003054 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003055 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003056 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003057 unsigned char doublings = 1;
3058
3059 while( ratio != 0 )
3060 {
3061 ++doublings;
3062 ratio >>= 1;
3063 }
3064
3065 if( ++ssl->renego_records_seen > doublings )
3066 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003068 return( 0 );
3069 }
3070 }
3071
3072 return( ssl_write_hello_request( ssl ) );
3073}
3074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003076
Paul Bakker5121ce52009-01-03 21:22:43 +00003077/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003078 * Fill the input message buffer by appending data to it.
3079 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080 *
3081 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3082 * available (from this read and/or a previous one). Otherwise, an error code
3083 * is returned (possibly EOF or WANT_READ).
3084 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003085 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3086 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3087 * since we always read a whole datagram at once.
3088 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003089 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003090 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003093{
Paul Bakker23986e52011-04-24 08:57:21 +00003094 int ret;
3095 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003098
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003099 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003102 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003104 }
3105
Angus Grattond8213d02016-05-25 20:56:48 +10003106 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003110 }
3111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003113 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003115 uint32_t timeout;
3116
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003117 /* Just to be sure */
3118 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3119 {
3120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3121 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3123 }
3124
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003125 /*
3126 * The point is, we need to always read a full datagram at once, so we
3127 * sometimes read more then requested, and handle the additional data.
3128 * It could be the rest of the current record (while fetching the
3129 * header) and/or some other records in the same datagram.
3130 */
3131
3132 /*
3133 * Move to the next record in the already read datagram if applicable
3134 */
3135 if( ssl->next_record_offset != 0 )
3136 {
3137 if( ssl->in_left < ssl->next_record_offset )
3138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003141 }
3142
3143 ssl->in_left -= ssl->next_record_offset;
3144
3145 if( ssl->in_left != 0 )
3146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003148 ssl->next_record_offset ) );
3149 memmove( ssl->in_hdr,
3150 ssl->in_hdr + ssl->next_record_offset,
3151 ssl->in_left );
3152 }
3153
3154 ssl->next_record_offset = 0;
3155 }
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003159
3160 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003161 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003162 */
3163 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003166 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003167 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003168
3169 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003170 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003171 * are not at the beginning of a new record, the caller did something
3172 * wrong.
3173 */
3174 if( ssl->in_left != 0 )
3175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003178 }
3179
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003180 /*
3181 * Don't even try to read if time's out already.
3182 * This avoids by-passing the timer when repeatedly receiving messages
3183 * that will end up being dropped.
3184 */
3185 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003186 {
3187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003188 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003189 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003190 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003191 {
Angus Grattond8213d02016-05-25 20:56:48 +10003192 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003195 timeout = ssl->handshake->retransmit_timeout;
3196 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003197 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003200
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003202 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3203 timeout );
3204 else
3205 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003208
3209 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003211 }
3212
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003213 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003216 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003219 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003220 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003223 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003224 }
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003229 return( ret );
3230 }
3231
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003232 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003235 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003237 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003238 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003241 return( ret );
3242 }
3243
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003244 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003245 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003247 }
3248
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 if( ret < 0 )
3250 return( ret );
3251
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003252 ssl->in_left = ret;
3253 }
3254 else
3255#endif
3256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003258 ssl->in_left, nb_want ) );
3259
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003260 while( ssl->in_left < nb_want )
3261 {
3262 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003263
3264 if( ssl_check_timer( ssl ) != 0 )
3265 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3266 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003267 {
3268 if( ssl->f_recv_timeout != NULL )
3269 {
3270 ret = ssl->f_recv_timeout( ssl->p_bio,
3271 ssl->in_hdr + ssl->in_left, len,
3272 ssl->conf->read_timeout );
3273 }
3274 else
3275 {
3276 ret = ssl->f_recv( ssl->p_bio,
3277 ssl->in_hdr + ssl->in_left, len );
3278 }
3279 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003282 ssl->in_left, nb_want ) );
3283 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003284
3285 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003287
3288 if( ret < 0 )
3289 return( ret );
3290
mohammad160352aecb92018-03-28 23:41:40 -07003291 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003292 {
Darryl Green11999bb2018-03-13 15:22:58 +00003293 MBEDTLS_SSL_DEBUG_MSG( 1,
3294 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003295 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3297 }
3298
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003299 ssl->in_left += ret;
3300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 }
3302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
3305 return( 0 );
3306}
3307
3308/*
3309 * Flush any data not yet written
3310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003313 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003314 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003318 if( ssl->f_send == NULL )
3319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003321 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003323 }
3324
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003325 /* Avoid incrementing counter if data is flushed */
3326 if( ssl->out_left == 0 )
3327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003329 return( 0 );
3330 }
3331
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 while( ssl->out_left > 0 )
3333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3335 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336
Hanno Becker2b1e3542018-08-06 11:19:13 +01003337 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003338 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003341
3342 if( ret <= 0 )
3343 return( ret );
3344
mohammad160352aecb92018-03-28 23:41:40 -07003345 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003346 {
Darryl Green11999bb2018-03-13 15:22:58 +00003347 MBEDTLS_SSL_DEBUG_MSG( 1,
3348 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003349 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3351 }
3352
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 ssl->out_left -= ret;
3354 }
3355
Hanno Becker2b1e3542018-08-06 11:19:13 +01003356#if defined(MBEDTLS_SSL_PROTO_DTLS)
3357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003358 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003359 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003360 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003361 else
3362#endif
3363 {
3364 ssl->out_hdr = ssl->out_buf + 8;
3365 }
3366 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
3370 return( 0 );
3371}
3372
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003373/*
3374 * Functions to handle the DTLS retransmission state machine
3375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003377/*
3378 * Append current handshake message to current outgoing flight
3379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3384 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3385 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003386
3387 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003388 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003389 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003392 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003393 }
3394
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003395 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003396 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003399 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003400 }
3401
3402 /* Copy current handshake message with headers */
3403 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3404 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003405 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003406 msg->next = NULL;
3407
3408 /* Append to the current flight */
3409 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003410 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003411 else
3412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003414 while( cur->next != NULL )
3415 cur = cur->next;
3416 cur->next = msg;
3417 }
3418
Hanno Becker3b235902018-08-06 09:54:53 +01003419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003420 return( 0 );
3421}
3422
3423/*
3424 * Free the current flight of handshake messages
3425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 mbedtls_ssl_flight_item *cur = flight;
3429 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003430
3431 while( cur != NULL )
3432 {
3433 next = cur->next;
3434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 mbedtls_free( cur->p );
3436 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003437
3438 cur = next;
3439 }
3440}
3441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3443static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003444#endif
3445
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003447 * Swap transform_out and out_ctr with the alternative ones
3448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003450{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003452 unsigned char tmp_out_ctr[8];
3453
3454 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003457 return;
3458 }
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003462 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003463 tmp_transform = ssl->transform_out;
3464 ssl->transform_out = ssl->handshake->alt_transform_out;
3465 ssl->handshake->alt_transform_out = tmp_transform;
3466
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003467 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003468 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3469 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003470 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003471
3472 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003473 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3476 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3481 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003482 }
3483 }
3484#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003485}
3486
3487/*
3488 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003489 */
3490int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3491{
3492 int ret = 0;
3493
3494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3495
3496 ret = mbedtls_ssl_flight_transmit( ssl );
3497
3498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3499
3500 return( ret );
3501}
3502
3503/*
3504 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003505 *
3506 * Need to remember the current message in case flush_output returns
3507 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003508 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003509 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003510int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003511{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003512 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003516 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003518
3519 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003520 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003521 ssl_swap_epochs( ssl );
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003524 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525
3526 while( ssl->handshake->cur_msg != NULL )
3527 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003528 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003529 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003530
Hanno Beckere1dcb032018-08-17 16:47:58 +01003531 int const is_finished =
3532 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3533 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3534
Hanno Becker04da1892018-08-14 13:22:10 +01003535 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3536 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3537
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003538 /* Swap epochs before sending Finished: we can't do it after
3539 * sending ChangeCipherSpec, in case write returns WANT_READ.
3540 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003541 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003542 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003544 ssl_swap_epochs( ssl );
3545 }
3546
Hanno Becker67bc7c32018-08-06 11:33:50 +01003547 ret = ssl_get_remaining_payload_in_datagram( ssl );
3548 if( ret < 0 )
3549 return( ret );
3550 max_frag_len = (size_t) ret;
3551
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003552 /* CCS is copied as is, while HS messages may need fragmentation */
3553 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3554 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003555 if( max_frag_len == 0 )
3556 {
3557 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3558 return( ret );
3559
3560 continue;
3561 }
3562
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003563 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003564 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003565 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003566
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003567 /* Update position inside current message */
3568 ssl->handshake->cur_msg_p += cur->len;
3569 }
3570 else
3571 {
3572 const unsigned char * const p = ssl->handshake->cur_msg_p;
3573 const size_t hs_len = cur->len - 12;
3574 const size_t frag_off = p - ( cur->p + 12 );
3575 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003576 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577
Hanno Beckere1dcb032018-08-17 16:47:58 +01003578 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003579 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003580 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003581 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582
Hanno Becker67bc7c32018-08-06 11:33:50 +01003583 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3584 return( ret );
3585
3586 continue;
3587 }
3588 max_hs_frag_len = max_frag_len - 12;
3589
3590 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3591 max_hs_frag_len : rem_len;
3592
3593 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003594 {
3595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003596 (unsigned) cur_hs_frag_len,
3597 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003598 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003599
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003600 /* Messages are stored with handshake headers as if not fragmented,
3601 * copy beginning of headers then fill fragmentation fields.
3602 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3603 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003605 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3606 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3607 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3608
Hanno Becker67bc7c32018-08-06 11:33:50 +01003609 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3610 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3611 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003612
3613 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3614
Hanno Becker3f7b9732018-08-28 09:53:25 +01003615 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003616 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3617 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003618 ssl->out_msgtype = cur->type;
3619
3620 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003621 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003622 }
3623
3624 /* If done with the current message move to the next one if any */
3625 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3626 {
3627 if( cur->next != NULL )
3628 {
3629 ssl->handshake->cur_msg = cur->next;
3630 ssl->handshake->cur_msg_p = cur->next->p + 12;
3631 }
3632 else
3633 {
3634 ssl->handshake->cur_msg = NULL;
3635 ssl->handshake->cur_msg_p = NULL;
3636 }
3637 }
3638
3639 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003640 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003643 return( ret );
3644 }
3645 }
3646
Hanno Becker67bc7c32018-08-06 11:33:50 +01003647 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3648 return( ret );
3649
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003650 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3652 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003653 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003656 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3657 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003658
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003660
3661 return( 0 );
3662}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003663
3664/*
3665 * To be called when the last message of an incoming flight is received.
3666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003668{
3669 /* We won't need to resend that one any more */
3670 ssl_flight_free( ssl->handshake->flight );
3671 ssl->handshake->flight = NULL;
3672 ssl->handshake->cur_msg = NULL;
3673
3674 /* The next incoming flight will start with this msg_seq */
3675 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3676
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003677 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003678 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003679
Hanno Becker0271f962018-08-16 13:23:47 +01003680 /* Clear future message buffering structure. */
3681 ssl_buffering_free( ssl );
3682
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003683 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003684 ssl_set_timer( ssl, 0 );
3685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3687 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003690 }
3691 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003693}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003694
3695/*
3696 * To be called when the last message of an outgoing flight is send.
3697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003699{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003700 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003701 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3704 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003707 }
3708 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712
Paul Bakker5121ce52009-01-03 21:22:43 +00003713/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003714 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716
3717/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003718 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003719 *
3720 * - fill in handshake headers
3721 * - update handshake checksum
3722 * - DTLS: save message for resending
3723 * - then pass to the record layer
3724 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003725 * DTLS: except for HelloRequest, messages are only queued, and will only be
3726 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003727 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003728 * Inputs:
3729 * - ssl->out_msglen: 4 + actual handshake message len
3730 * (4 is the size of handshake headers for TLS)
3731 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3732 * - ssl->out_msg + 4: the handshake message body
3733 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003734 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003735 * - ssl->out_msglen: the length of the record contents
3736 * (including handshake headers but excluding record headers)
3737 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003738 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003739int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003740{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003741 int ret;
3742 const size_t hs_len = ssl->out_msglen - 4;
3743 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003744
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3746
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003747 /*
3748 * Sanity checks
3749 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003750 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003751 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3752 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003753 /* In SSLv3, the client might send a NoCertificate alert. */
3754#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3755 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3756 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3757 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3758#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3759 {
3760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3762 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003765 /* Whenever we send anything different from a
3766 * HelloRequest we should be in a handshake - double check. */
3767 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3768 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 ssl->handshake == NULL )
3770 {
3771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003779 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003783#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003784
Hanno Beckerb50a2532018-08-06 11:52:54 +01003785 /* Double-check that we did not exceed the bounds
3786 * of the outgoing record buffer.
3787 * This should never fail as the various message
3788 * writing functions must obey the bounds of the
3789 * outgoing record buffer, but better be safe.
3790 *
3791 * Note: We deliberately do not check for the MTU or MFL here.
3792 */
3793 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3794 {
3795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3796 "size %u, maximum %u",
3797 (unsigned) ssl->out_msglen,
3798 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3800 }
3801
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003802 /*
3803 * Fill handshake headers
3804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003806 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003807 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3808 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3809 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003810
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003811 /*
3812 * DTLS has additional fields in the Handshake layer,
3813 * between the length field and the actual payload:
3814 * uint16 message_seq;
3815 * uint24 fragment_offset;
3816 * uint24 fragment_length;
3817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003819 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003820 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003821 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003822 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003823 {
3824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3825 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003826 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003827 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3829 }
3830
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003831 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003832 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003833
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003834 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003835 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003836 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003837 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3838 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3839 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003840 }
3841 else
3842 {
3843 ssl->out_msg[4] = 0;
3844 ssl->out_msg[5] = 0;
3845 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003846
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003847 /* Handshake hashes are computed without fragmentation,
3848 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003849 memset( ssl->out_msg + 6, 0x00, 3 );
3850 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003851 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003853
Hanno Becker0207e532018-08-28 10:28:28 +01003854 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003855 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3856 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 }
3858
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003859 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003862 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3863 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003864 {
3865 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003868 return( ret );
3869 }
3870 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003871 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003872#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003873 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003874 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003875 {
3876 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3877 return( ret );
3878 }
3879 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003880
3881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3882
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003883 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003884}
3885
3886/*
3887 * Record layer functions
3888 */
3889
3890/*
3891 * Write current record.
3892 *
3893 * Uses:
3894 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3895 * - ssl->out_msglen: length of the record content (excl headers)
3896 * - ssl->out_msg: record content
3897 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003898int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003899{
3900 int ret, done = 0;
3901 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003902 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003903
3904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003907 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003909 {
3910 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003913 return( ret );
3914 }
3915
3916 len = ssl->out_msglen;
3917 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3921 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 ret = mbedtls_ssl_hw_record_write( ssl );
3926 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3929 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003930 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003931
3932 if( ret == 0 )
3933 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003934 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003936 if( !done )
3937 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003938 unsigned i;
3939 size_t protected_record_size;
3940
Paul Bakker05ef8352012-05-08 09:17:57 +00003941 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003943 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003944
Hanno Becker19859472018-08-06 09:40:20 +01003945 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003946 ssl->out_len[0] = (unsigned char)( len >> 8 );
3947 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003948
Paul Bakker48916f92012-09-16 19:57:18 +00003949 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003950 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003951 mbedtls_record rec;
3952
3953 rec.buf = ssl->out_iv;
3954 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3955 ( ssl->out_iv - ssl->out_buf );
3956 rec.data_len = ssl->out_msglen;
3957 rec.data_offset = ssl->out_msg - rec.buf;
3958
3959 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3960 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3961 ssl->conf->transport, rec.ver );
3962 rec.type = ssl->out_msgtype;
3963
Hanno Beckera18d1322018-01-03 14:27:32 +00003964 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003965 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003968 return( ret );
3969 }
3970
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003971 if( rec.data_offset != 0 )
3972 {
3973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3975 }
3976
Hanno Becker78f839d2019-03-14 12:56:23 +00003977 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003978 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3979 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003980 }
3981
Hanno Becker2b1e3542018-08-06 11:19:13 +01003982 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3983
3984#if defined(MBEDTLS_SSL_PROTO_DTLS)
3985 /* In case of DTLS, double-check that we don't exceed
3986 * the remaining space in the datagram. */
3987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3988 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003989 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003990 if( ret < 0 )
3991 return( ret );
3992
3993 if( protected_record_size > (size_t) ret )
3994 {
3995 /* Should never happen */
3996 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3997 }
3998 }
3999#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004002 "version = [%d:%d], msglen = %d",
4003 ssl->out_hdr[0], ssl->out_hdr[1],
4004 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004007 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004008
4009 ssl->out_left += protected_record_size;
4010 ssl->out_hdr += protected_record_size;
4011 ssl_update_out_pointers( ssl, ssl->transform_out );
4012
Hanno Becker04484622018-08-06 09:49:38 +01004013 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4014 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4015 break;
4016
4017 /* The loop goes to its end iff the counter is wrapping */
4018 if( i == ssl_ep_len( ssl ) )
4019 {
4020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4021 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004023 }
4024
Hanno Becker67bc7c32018-08-06 11:33:50 +01004025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4027 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004028 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004029 size_t remaining;
4030 ret = ssl_get_remaining_payload_in_datagram( ssl );
4031 if( ret < 0 )
4032 {
4033 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4034 ret );
4035 return( ret );
4036 }
4037
4038 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004039 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004040 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004041 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004042 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004043 else
4044 {
Hanno Becker513815a2018-08-20 11:56:09 +01004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004046 }
4047 }
4048#endif /* MBEDTLS_SSL_PROTO_DTLS */
4049
4050 if( ( flush == SSL_FORCE_FLUSH ) &&
4051 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054 return( ret );
4055 }
4056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004058
4059 return( 0 );
4060}
4061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004063
4064static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4065{
4066 if( ssl->in_msglen < ssl->in_hslen ||
4067 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4068 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4069 {
4070 return( 1 );
4071 }
4072 return( 0 );
4073}
Hanno Becker44650b72018-08-16 12:51:11 +01004074
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004075static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004076{
4077 return( ( ssl->in_msg[9] << 16 ) |
4078 ( ssl->in_msg[10] << 8 ) |
4079 ssl->in_msg[11] );
4080}
4081
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004082static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004083{
4084 return( ( ssl->in_msg[6] << 16 ) |
4085 ( ssl->in_msg[7] << 8 ) |
4086 ssl->in_msg[8] );
4087}
4088
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004089static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004090{
4091 uint32_t msg_len, frag_off, frag_len;
4092
4093 msg_len = ssl_get_hs_total_len( ssl );
4094 frag_off = ssl_get_hs_frag_off( ssl );
4095 frag_len = ssl_get_hs_frag_len( ssl );
4096
4097 if( frag_off > msg_len )
4098 return( -1 );
4099
4100 if( frag_len > msg_len - frag_off )
4101 return( -1 );
4102
4103 if( frag_len + 12 > ssl->in_msglen )
4104 return( -1 );
4105
4106 return( 0 );
4107}
4108
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004109/*
4110 * Mark bits in bitmask (used for DTLS HS reassembly)
4111 */
4112static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4113{
4114 unsigned int start_bits, end_bits;
4115
4116 start_bits = 8 - ( offset % 8 );
4117 if( start_bits != 8 )
4118 {
4119 size_t first_byte_idx = offset / 8;
4120
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004121 /* Special case */
4122 if( len <= start_bits )
4123 {
4124 for( ; len != 0; len-- )
4125 mask[first_byte_idx] |= 1 << ( start_bits - len );
4126
4127 /* Avoid potential issues with offset or len becoming invalid */
4128 return;
4129 }
4130
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004131 offset += start_bits; /* Now offset % 8 == 0 */
4132 len -= start_bits;
4133
4134 for( ; start_bits != 0; start_bits-- )
4135 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4136 }
4137
4138 end_bits = len % 8;
4139 if( end_bits != 0 )
4140 {
4141 size_t last_byte_idx = ( offset + len ) / 8;
4142
4143 len -= end_bits; /* Now len % 8 == 0 */
4144
4145 for( ; end_bits != 0; end_bits-- )
4146 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4147 }
4148
4149 memset( mask + offset / 8, 0xFF, len / 8 );
4150}
4151
4152/*
4153 * Check that bitmask is full
4154 */
4155static int ssl_bitmask_check( unsigned char *mask, size_t len )
4156{
4157 size_t i;
4158
4159 for( i = 0; i < len / 8; i++ )
4160 if( mask[i] != 0xFF )
4161 return( -1 );
4162
4163 for( i = 0; i < len % 8; i++ )
4164 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4165 return( -1 );
4166
4167 return( 0 );
4168}
4169
Hanno Becker56e205e2018-08-16 09:06:12 +01004170/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004171static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004172 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004173{
Hanno Becker56e205e2018-08-16 09:06:12 +01004174 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004175
Hanno Becker56e205e2018-08-16 09:06:12 +01004176 alloc_len = 12; /* Handshake header */
4177 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004178
Hanno Beckerd07df862018-08-16 09:14:58 +01004179 if( add_bitmap )
4180 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004181
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004182 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004183}
Hanno Becker56e205e2018-08-16 09:06:12 +01004184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004186
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004187static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004188{
4189 return( ( ssl->in_msg[1] << 16 ) |
4190 ( ssl->in_msg[2] << 8 ) |
4191 ssl->in_msg[3] );
4192}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004193
Simon Butcher99000142016-10-13 17:21:01 +01004194int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004199 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004201 }
4202
Hanno Becker12555c62018-08-16 12:47:53 +01004203 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004206 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004207 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004211 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004212 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004213 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004214
Hanno Becker44650b72018-08-16 12:51:11 +01004215 if( ssl_check_hs_header( ssl ) != 0 )
4216 {
4217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4218 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4219 }
4220
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004221 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004222 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4223 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4224 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4225 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004226 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004227 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4228 {
4229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4230 recv_msg_seq,
4231 ssl->handshake->in_msg_seq ) );
4232 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4233 }
4234
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004235 /* Retransmit only on last message from previous flight, to avoid
4236 * too many retransmissions.
4237 * Besides, No sane server ever retransmits HelloVerifyRequest */
4238 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004242 "message_seq = %d, start_of_flight = %d",
4243 recv_msg_seq,
4244 ssl->handshake->in_flight_start_seq ) );
4245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004249 return( ret );
4250 }
4251 }
4252 else
4253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004255 "message_seq = %d, expected = %d",
4256 recv_msg_seq,
4257 ssl->handshake->in_msg_seq ) );
4258 }
4259
Hanno Becker90333da2017-10-10 11:27:13 +01004260 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004261 }
4262 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004263
Hanno Becker6d97ef52018-08-16 13:09:04 +01004264 /* Message reassembly is handled alongside buffering of future
4265 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004266 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004267 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004268 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004271 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004272 }
4273 }
4274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004276 /* With TLS we don't handle fragmentation (for now) */
4277 if( ssl->in_msglen < ssl->in_hslen )
4278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4280 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004281 }
4282
Simon Butcher99000142016-10-13 17:21:01 +01004283 return( 0 );
4284}
4285
4286void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4287{
Hanno Becker0271f962018-08-16 13:23:47 +01004288 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004289
Hanno Becker0271f962018-08-16 13:23:47 +01004290 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004291 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004292 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004293 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004294
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004295 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004298 ssl->handshake != NULL )
4299 {
Hanno Becker0271f962018-08-16 13:23:47 +01004300 unsigned offset;
4301 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004302
Hanno Becker0271f962018-08-16 13:23:47 +01004303 /* Increment handshake sequence number */
4304 hs->in_msg_seq++;
4305
4306 /*
4307 * Clear up handshake buffering and reassembly structure.
4308 */
4309
4310 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004311 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004312
4313 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004314 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4315 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004316 offset++, hs_buf++ )
4317 {
4318 *hs_buf = *(hs_buf + 1);
4319 }
4320
4321 /* Create a fresh last entry */
4322 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004323 }
4324#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004325}
4326
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004327/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328 * DTLS anti-replay: RFC 6347 4.1.2.6
4329 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004330 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4331 * Bit n is set iff record number in_window_top - n has been seen.
4332 *
4333 * Usually, in_window_top is the last record number seen and the lsb of
4334 * in_window is set. The only exception is the initial state (record number 0
4335 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4338static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339{
4340 ssl->in_window_top = 0;
4341 ssl->in_window = 0;
4342}
4343
4344static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4345{
4346 return( ( (uint64_t) buf[0] << 40 ) |
4347 ( (uint64_t) buf[1] << 32 ) |
4348 ( (uint64_t) buf[2] << 24 ) |
4349 ( (uint64_t) buf[3] << 16 ) |
4350 ( (uint64_t) buf[4] << 8 ) |
4351 ( (uint64_t) buf[5] ) );
4352}
4353
4354/*
4355 * Return 0 if sequence number is acceptable, -1 otherwise
4356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004358{
4359 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4360 uint64_t bit;
4361
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004362 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004363 return( 0 );
4364
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004365 if( rec_seqnum > ssl->in_window_top )
4366 return( 0 );
4367
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004368 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004369
4370 if( bit >= 64 )
4371 return( -1 );
4372
4373 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4374 return( -1 );
4375
4376 return( 0 );
4377}
4378
4379/*
4380 * Update replay window on new validated record
4381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004383{
4384 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4385
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004386 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004387 return;
4388
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004389 if( rec_seqnum > ssl->in_window_top )
4390 {
4391 /* Update window_top and the contents of the window */
4392 uint64_t shift = rec_seqnum - ssl->in_window_top;
4393
4394 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004395 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004396 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004397 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004398 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004399 ssl->in_window |= 1;
4400 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004401
4402 ssl->in_window_top = rec_seqnum;
4403 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004404 else
4405 {
4406 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004407 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004408
4409 if( bit < 64 ) /* Always true, but be extra sure */
4410 ssl->in_window |= (uint64_t) 1 << bit;
4411 }
4412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004414
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004415#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004416/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004417static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4418
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004419/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004420 * Without any SSL context, check if a datagram looks like a ClientHello with
4421 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004422 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004423 *
4424 * - if cookie is valid, return 0
4425 * - if ClientHello looks superficially valid but cookie is not,
4426 * fill obuf and set olen, then
4427 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4428 * - otherwise return a specific error code
4429 */
4430static int ssl_check_dtls_clihlo_cookie(
4431 mbedtls_ssl_cookie_write_t *f_cookie_write,
4432 mbedtls_ssl_cookie_check_t *f_cookie_check,
4433 void *p_cookie,
4434 const unsigned char *cli_id, size_t cli_id_len,
4435 const unsigned char *in, size_t in_len,
4436 unsigned char *obuf, size_t buf_len, size_t *olen )
4437{
4438 size_t sid_len, cookie_len;
4439 unsigned char *p;
4440
4441 if( f_cookie_write == NULL || f_cookie_check == NULL )
4442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4443
4444 /*
4445 * Structure of ClientHello with record and handshake headers,
4446 * and expected values. We don't need to check a lot, more checks will be
4447 * done when actually parsing the ClientHello - skipping those checks
4448 * avoids code duplication and does not make cookie forging any easier.
4449 *
4450 * 0-0 ContentType type; copied, must be handshake
4451 * 1-2 ProtocolVersion version; copied
4452 * 3-4 uint16 epoch; copied, must be 0
4453 * 5-10 uint48 sequence_number; copied
4454 * 11-12 uint16 length; (ignored)
4455 *
4456 * 13-13 HandshakeType msg_type; (ignored)
4457 * 14-16 uint24 length; (ignored)
4458 * 17-18 uint16 message_seq; copied
4459 * 19-21 uint24 fragment_offset; copied, must be 0
4460 * 22-24 uint24 fragment_length; (ignored)
4461 *
4462 * 25-26 ProtocolVersion client_version; (ignored)
4463 * 27-58 Random random; (ignored)
4464 * 59-xx SessionID session_id; 1 byte len + sid_len content
4465 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4466 * ...
4467 *
4468 * Minimum length is 61 bytes.
4469 */
4470 if( in_len < 61 ||
4471 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4472 in[3] != 0 || in[4] != 0 ||
4473 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4474 {
4475 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4476 }
4477
4478 sid_len = in[59];
4479 if( sid_len > in_len - 61 )
4480 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4481
4482 cookie_len = in[60 + sid_len];
4483 if( cookie_len > in_len - 60 )
4484 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4485
4486 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4487 cli_id, cli_id_len ) == 0 )
4488 {
4489 /* Valid cookie */
4490 return( 0 );
4491 }
4492
4493 /*
4494 * If we get here, we've got an invalid cookie, let's prepare HVR.
4495 *
4496 * 0-0 ContentType type; copied
4497 * 1-2 ProtocolVersion version; copied
4498 * 3-4 uint16 epoch; copied
4499 * 5-10 uint48 sequence_number; copied
4500 * 11-12 uint16 length; olen - 13
4501 *
4502 * 13-13 HandshakeType msg_type; hello_verify_request
4503 * 14-16 uint24 length; olen - 25
4504 * 17-18 uint16 message_seq; copied
4505 * 19-21 uint24 fragment_offset; copied
4506 * 22-24 uint24 fragment_length; olen - 25
4507 *
4508 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4509 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4510 *
4511 * Minimum length is 28.
4512 */
4513 if( buf_len < 28 )
4514 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4515
4516 /* Copy most fields and adapt others */
4517 memcpy( obuf, in, 25 );
4518 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4519 obuf[25] = 0xfe;
4520 obuf[26] = 0xff;
4521
4522 /* Generate and write actual cookie */
4523 p = obuf + 28;
4524 if( f_cookie_write( p_cookie,
4525 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4526 {
4527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4528 }
4529
4530 *olen = p - obuf;
4531
4532 /* Go back and fill length fields */
4533 obuf[27] = (unsigned char)( *olen - 28 );
4534
4535 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4536 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4537 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4538
4539 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4540 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4541
4542 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4543}
4544
4545/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004546 * Handle possible client reconnect with the same UDP quadruplet
4547 * (RFC 6347 Section 4.2.8).
4548 *
4549 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4550 * that looks like a ClientHello.
4551 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004552 * - if the input looks like a ClientHello without cookies,
4553 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004554 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555 * - if the input looks like a ClientHello with a valid cookie,
4556 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004557 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004558 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004559 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004560 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004561 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4562 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004563 */
4564static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4565{
4566 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004567 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004568
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004569 ret = ssl_check_dtls_clihlo_cookie(
4570 ssl->conf->f_cookie_write,
4571 ssl->conf->f_cookie_check,
4572 ssl->conf->p_cookie,
4573 ssl->cli_id, ssl->cli_id_len,
4574 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004575 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004576
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004577 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4578
4579 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004580 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004581 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004582 * If the error is permanent we'll catch it later,
4583 * if it's not, then hopefully it'll work next time. */
4584 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4585
4586 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004587 }
4588
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004589 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004590 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004591 /* Got a valid cookie, partially reset context */
4592 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4593 {
4594 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4595 return( ret );
4596 }
4597
4598 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004599 }
4600
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004601 return( ret );
4602}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004603#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004604
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004605/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004606 * ContentType type;
4607 * ProtocolVersion version;
4608 * uint16 epoch; // DTLS only
4609 * uint48 sequence_number; // DTLS only
4610 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004611 *
4612 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004613 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004614 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4615 *
4616 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004617 * 1. proceed with the record if this function returns 0
4618 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4619 * 3. return CLIENT_RECONNECT if this function return that value
4620 * 4. drop the whole datagram if this function returns anything else.
4621 * Point 2 is needed when the peer is resending, and we have already received
4622 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004625{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004626 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 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 +02004629
Paul Bakker5121ce52009-01-03 21:22:43 +00004630 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004631 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004632 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004635 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004636 ssl->in_msgtype,
4637 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004638
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004639 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4641 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4642 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4643 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004646
4647#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004648 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4649 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004650 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4651#endif /* MBEDTLS_SSL_PROTO_DTLS */
4652 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4653 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004656 }
4657
4658 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004659 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 }
4664
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004665 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4668 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 }
4670
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004671 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004672 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004673 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4676 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004677 }
4678
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004679 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004680 * DTLS-related tests.
4681 * Check epoch before checking length constraint because
4682 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4683 * message gets duplicated before the corresponding Finished message,
4684 * the second ChangeCipherSpec should be discarded because it belongs
4685 * to an old epoch, but not because its length is shorter than
4686 * the minimum record length for packets using the new record transform.
4687 * Note that these two kinds of failures are handled differently,
4688 * as an unexpected record is silently skipped but an invalid
4689 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004690 */
4691#if defined(MBEDTLS_SSL_PROTO_DTLS)
4692 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4693 {
4694 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004696 /* Check epoch (and sequence number) with DTLS */
4697 if( rec_epoch != ssl->in_epoch )
4698 {
4699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4700 "expected %d, received %d",
4701 ssl->in_epoch, rec_epoch ) );
4702
4703#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4704 /*
4705 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4706 * access the first byte of record content (handshake type), as we
4707 * have an active transform (possibly iv_len != 0), so use the
4708 * fact that the record header len is 13 instead.
4709 */
4710 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4711 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4712 rec_epoch == 0 &&
4713 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4714 ssl->in_left > 13 &&
4715 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4716 {
4717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4718 "from the same port" ) );
4719 return( ssl_handle_possible_reconnect( ssl ) );
4720 }
4721 else
4722#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004723 {
4724 /* Consider buffering the record. */
4725 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4728 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4729 }
4730
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004731 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004732 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004733 }
4734
4735#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4736 /* Replay detection only works for the current epoch */
4737 if( rec_epoch == ssl->in_epoch &&
4738 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4739 {
4740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4741 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4742 }
4743#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004744
Hanno Becker52c6dc62017-05-26 16:07:36 +01004745 /* Drop unexpected ApplicationData records,
4746 * except at the beginning of renegotiations */
4747 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4748 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4749#if defined(MBEDTLS_SSL_RENEGOTIATION)
4750 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4751 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4752#endif
4753 )
4754 {
4755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4756 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4757 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004758 }
4759#endif /* MBEDTLS_SSL_PROTO_DTLS */
4760
Hanno Becker52c6dc62017-05-26 16:07:36 +01004761
4762 /* Check length against bounds of the current transform and version */
4763 if( ssl->transform_in == NULL )
4764 {
4765 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004766 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004767 {
4768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4769 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4770 }
4771 }
4772 else
4773 {
4774 if( ssl->in_msglen < ssl->transform_in->minlen )
4775 {
4776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4777 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4778 }
4779
4780#if defined(MBEDTLS_SSL_PROTO_SSL3)
4781 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004782 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004783 {
4784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4785 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4786 }
4787#endif
4788#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4789 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4790 /*
4791 * TLS encrypted messages can have up to 256 bytes of padding
4792 */
4793 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4794 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004795 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004796 {
4797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4798 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4799 }
4800#endif
4801 }
4802
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004803 return( 0 );
4804}
Paul Bakker5121ce52009-01-03 21:22:43 +00004805
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806/*
4807 * If applicable, decrypt (and decompress) record content
4808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004810{
4811 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4814 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4817 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 ret = mbedtls_ssl_hw_record_read( ssl );
4822 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4825 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004826 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004827
4828 if( ret == 0 )
4829 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004832 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004834 mbedtls_record rec;
4835
4836 rec.buf = ssl->in_iv;
4837 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4838 - ( ssl->in_iv - ssl->in_buf );
4839 rec.data_len = ssl->in_msglen;
4840 rec.data_offset = 0;
4841
4842 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4843 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4844 ssl->conf->transport, rec.ver );
4845 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004846 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4847 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004850 return( ret );
4851 }
4852
Hanno Becker29800d22018-08-07 14:30:18 +01004853 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4854 {
4855 /* Should never happen */
4856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004858
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004859 ssl->in_msglen = rec.data_len;
4860 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4861 ssl->in_len[1] = (unsigned char)( rec.data_len );
4862
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004863 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4864 ssl->in_msg, ssl->in_msglen );
4865
Angus Grattond8213d02016-05-25 20:56:48 +10004866 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4869 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004870 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004871 else if( ssl->in_msglen == 0 )
4872 {
4873#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4874 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4875 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4876 {
4877 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4879 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4880 }
4881#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4882
4883 ssl->nb_zero++;
4884
4885 /*
4886 * Three or more empty messages may be a DoS attack
4887 * (excessive CPU consumption).
4888 */
4889 if( ssl->nb_zero > 3 )
4890 {
4891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4892 "messages, possible DoS attack" ) );
4893 /* Q: Is that the right error code? */
4894 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4895 }
4896 }
4897 else
4898 ssl->nb_zero = 0;
4899
4900#if defined(MBEDTLS_SSL_PROTO_DTLS)
4901 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4902 {
4903 ; /* in_ctr read from peer, not maintained internally */
4904 }
4905 else
4906#endif
4907 {
4908 unsigned i;
4909 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4910 if( ++ssl->in_ctr[i - 1] != 0 )
4911 break;
4912
4913 /* The loop goes to its end iff the counter is wrapping */
4914 if( i == ssl_ep_len( ssl ) )
4915 {
4916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4917 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4918 }
4919 }
4920
Paul Bakker5121ce52009-01-03 21:22:43 +00004921 }
4922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004924 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004926 {
4927 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004930 return( ret );
4931 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004932 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004936 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004939 }
4940#endif
4941
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004942 return( 0 );
4943}
4944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004946
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004947/*
4948 * Read a record.
4949 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004950 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4951 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4952 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004953 */
Hanno Becker1097b342018-08-15 14:09:41 +01004954
4955/* Helper functions for mbedtls_ssl_read_record(). */
4956static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004957static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4958static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004959
Hanno Becker327c93b2018-08-15 13:56:18 +01004960int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004961 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004962{
4963 int ret;
4964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004966
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004967 if( ssl->keep_current_message == 0 )
4968 {
4969 do {
Simon Butcher99000142016-10-13 17:21:01 +01004970
Hanno Becker26994592018-08-15 14:14:59 +01004971 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004972 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004973 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004974
Hanno Beckere74d5562018-08-15 14:26:08 +01004975 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004976 {
Hanno Becker40f50842018-08-15 14:48:01 +01004977#if defined(MBEDTLS_SSL_PROTO_DTLS)
4978 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004979
Hanno Becker40f50842018-08-15 14:48:01 +01004980 /* We only check for buffered messages if the
4981 * current datagram is fully consumed. */
4982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004983 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004984 {
Hanno Becker40f50842018-08-15 14:48:01 +01004985 if( ssl_load_buffered_message( ssl ) == 0 )
4986 have_buffered = 1;
4987 }
4988
4989 if( have_buffered == 0 )
4990#endif /* MBEDTLS_SSL_PROTO_DTLS */
4991 {
4992 ret = ssl_get_next_record( ssl );
4993 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4994 continue;
4995
4996 if( ret != 0 )
4997 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004998 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004999 return( ret );
5000 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005001 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005002 }
5003
5004 ret = mbedtls_ssl_handle_message_type( ssl );
5005
Hanno Becker40f50842018-08-15 14:48:01 +01005006#if defined(MBEDTLS_SSL_PROTO_DTLS)
5007 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5008 {
5009 /* Buffer future message */
5010 ret = ssl_buffer_message( ssl );
5011 if( ret != 0 )
5012 return( ret );
5013
5014 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5015 }
5016#endif /* MBEDTLS_SSL_PROTO_DTLS */
5017
Hanno Becker90333da2017-10-10 11:27:13 +01005018 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5019 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005020
5021 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005022 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005023 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005024 return( ret );
5025 }
5026
Hanno Becker327c93b2018-08-15 13:56:18 +01005027 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005028 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005029 {
5030 mbedtls_ssl_update_handshake_status( ssl );
5031 }
Simon Butcher99000142016-10-13 17:21:01 +01005032 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005033 else
Simon Butcher99000142016-10-13 17:21:01 +01005034 {
Hanno Becker02f59072018-08-15 14:00:24 +01005035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005037 }
5038
5039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5040
5041 return( 0 );
5042}
5043
Hanno Becker40f50842018-08-15 14:48:01 +01005044#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005045static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005046{
Hanno Becker40f50842018-08-15 14:48:01 +01005047 if( ssl->in_left > ssl->next_record_offset )
5048 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005049
Hanno Becker40f50842018-08-15 14:48:01 +01005050 return( 0 );
5051}
5052
5053static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5054{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005055 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005056 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005057 int ret = 0;
5058
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005059 if( hs == NULL )
5060 return( -1 );
5061
Hanno Beckere00ae372018-08-20 09:39:42 +01005062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5063
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005064 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5065 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5066 {
5067 /* Check if we have seen a ChangeCipherSpec before.
5068 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005069 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005070 {
5071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5072 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005073 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005074 }
5075
Hanno Becker39b8bc92018-08-28 17:17:13 +01005076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005077 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5078 ssl->in_msglen = 1;
5079 ssl->in_msg[0] = 1;
5080
5081 /* As long as they are equal, the exact value doesn't matter. */
5082 ssl->in_left = 0;
5083 ssl->next_record_offset = 0;
5084
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005085 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005086 goto exit;
5087 }
Hanno Becker37f95322018-08-16 13:55:32 +01005088
Hanno Beckerb8f50142018-08-28 10:01:34 +01005089#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005090 /* Debug only */
5091 {
5092 unsigned offset;
5093 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5094 {
5095 hs_buf = &hs->buffering.hs[offset];
5096 if( hs_buf->is_valid == 1 )
5097 {
5098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5099 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005100 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005101 }
5102 }
5103 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005104#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005105
5106 /* Check if we have buffered and/or fully reassembled the
5107 * next handshake message. */
5108 hs_buf = &hs->buffering.hs[0];
5109 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5110 {
5111 /* Synthesize a record containing the buffered HS message. */
5112 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5113 ( hs_buf->data[2] << 8 ) |
5114 hs_buf->data[3];
5115
5116 /* Double-check that we haven't accidentally buffered
5117 * a message that doesn't fit into the input buffer. */
5118 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5119 {
5120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5122 }
5123
5124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5125 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5126 hs_buf->data, msg_len + 12 );
5127
5128 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5129 ssl->in_hslen = msg_len + 12;
5130 ssl->in_msglen = msg_len + 12;
5131 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5132
5133 ret = 0;
5134 goto exit;
5135 }
5136 else
5137 {
5138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5139 hs->in_msg_seq ) );
5140 }
5141
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005142 ret = -1;
5143
5144exit:
5145
5146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5147 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005148}
5149
Hanno Beckera02b0b42018-08-21 17:20:27 +01005150static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5151 size_t desired )
5152{
5153 int offset;
5154 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5156 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005157
Hanno Becker01315ea2018-08-21 17:22:17 +01005158 /* Get rid of future records epoch first, if such exist. */
5159 ssl_free_buffered_record( ssl );
5160
5161 /* Check if we have enough space available now. */
5162 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5163 hs->buffering.total_bytes_buffered ) )
5164 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005166 return( 0 );
5167 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005168
Hanno Becker4f432ad2018-08-28 10:02:32 +01005169 /* We don't have enough space to buffer the next expected handshake
5170 * message. Remove buffers used for future messages to gain space,
5171 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005172 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5173 offset >= 0; offset-- )
5174 {
5175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5176 offset ) );
5177
Hanno Beckerb309b922018-08-23 13:18:05 +01005178 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005179
5180 /* Check if we have enough space available now. */
5181 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5182 hs->buffering.total_bytes_buffered ) )
5183 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005185 return( 0 );
5186 }
5187 }
5188
5189 return( -1 );
5190}
5191
Hanno Becker40f50842018-08-15 14:48:01 +01005192static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5193{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005194 int ret = 0;
5195 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5196
5197 if( hs == NULL )
5198 return( 0 );
5199
5200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5201
5202 switch( ssl->in_msgtype )
5203 {
5204 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005206
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005207 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005208 break;
5209
5210 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005211 {
5212 unsigned recv_msg_seq_offset;
5213 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5214 mbedtls_ssl_hs_buffer *hs_buf;
5215 size_t msg_len = ssl->in_hslen - 12;
5216
5217 /* We should never receive an old handshake
5218 * message - double-check nonetheless. */
5219 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5220 {
5221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5223 }
5224
5225 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5226 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5227 {
5228 /* Silently ignore -- message too far in the future */
5229 MBEDTLS_SSL_DEBUG_MSG( 2,
5230 ( "Ignore future HS message with sequence number %u, "
5231 "buffering window %u - %u",
5232 recv_msg_seq, ssl->handshake->in_msg_seq,
5233 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5234
5235 goto exit;
5236 }
5237
5238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5239 recv_msg_seq, recv_msg_seq_offset ) );
5240
5241 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5242
5243 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005244 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005245 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005246 size_t reassembly_buf_sz;
5247
Hanno Becker37f95322018-08-16 13:55:32 +01005248 hs_buf->is_fragmented =
5249 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5250
5251 /* We copy the message back into the input buffer
5252 * after reassembly, so check that it's not too large.
5253 * This is an implementation-specific limitation
5254 * and not one from the standard, hence it is not
5255 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005256 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005257 {
5258 /* Ignore message */
5259 goto exit;
5260 }
5261
Hanno Beckere0b150f2018-08-21 15:51:03 +01005262 /* Check if we have enough space to buffer the message. */
5263 if( hs->buffering.total_bytes_buffered >
5264 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5265 {
5266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5268 }
5269
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005270 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5271 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005272
5273 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5274 hs->buffering.total_bytes_buffered ) )
5275 {
5276 if( recv_msg_seq_offset > 0 )
5277 {
5278 /* If we can't buffer a future message because
5279 * of space limitations -- ignore. */
5280 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",
5281 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5282 (unsigned) hs->buffering.total_bytes_buffered ) );
5283 goto exit;
5284 }
Hanno Beckere1801392018-08-21 16:51:05 +01005285 else
5286 {
5287 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",
5288 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5289 (unsigned) hs->buffering.total_bytes_buffered ) );
5290 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005291
Hanno Beckera02b0b42018-08-21 17:20:27 +01005292 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005293 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005294 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",
5295 (unsigned) msg_len,
5296 (unsigned) reassembly_buf_sz,
5297 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005298 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005299 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5300 goto exit;
5301 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005302 }
5303
5304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5305 msg_len ) );
5306
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005307 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5308 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005309 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005310 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005311 goto exit;
5312 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005313 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005314
5315 /* Prepare final header: copy msg_type, length and message_seq,
5316 * then add standardised fragment_offset and fragment_length */
5317 memcpy( hs_buf->data, ssl->in_msg, 6 );
5318 memset( hs_buf->data + 6, 0, 3 );
5319 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5320
5321 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005322
5323 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005324 }
5325 else
5326 {
5327 /* Make sure msg_type and length are consistent */
5328 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5329 {
5330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5331 /* Ignore */
5332 goto exit;
5333 }
5334 }
5335
Hanno Becker4422bbb2018-08-20 09:40:19 +01005336 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005337 {
5338 size_t frag_len, frag_off;
5339 unsigned char * const msg = hs_buf->data + 12;
5340
5341 /*
5342 * Check and copy current fragment
5343 */
5344
5345 /* Validation of header fields already done in
5346 * mbedtls_ssl_prepare_handshake_record(). */
5347 frag_off = ssl_get_hs_frag_off( ssl );
5348 frag_len = ssl_get_hs_frag_len( ssl );
5349
5350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5351 frag_off, frag_len ) );
5352 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5353
5354 if( hs_buf->is_fragmented )
5355 {
5356 unsigned char * const bitmask = msg + msg_len;
5357 ssl_bitmask_set( bitmask, frag_off, frag_len );
5358 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5359 msg_len ) == 0 );
5360 }
5361 else
5362 {
5363 hs_buf->is_complete = 1;
5364 }
5365
5366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5367 hs_buf->is_complete ? "" : "not yet " ) );
5368 }
5369
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005370 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005371 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005372
5373 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005374 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005375 break;
5376 }
5377
5378exit:
5379
5380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5381 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005382}
5383#endif /* MBEDTLS_SSL_PROTO_DTLS */
5384
Hanno Becker1097b342018-08-15 14:09:41 +01005385static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005386{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005387 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005388 * Consume last content-layer message and potentially
5389 * update in_msglen which keeps track of the contents'
5390 * consumption state.
5391 *
5392 * (1) Handshake messages:
5393 * Remove last handshake message, move content
5394 * and adapt in_msglen.
5395 *
5396 * (2) Alert messages:
5397 * Consume whole record content, in_msglen = 0.
5398 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005399 * (3) Change cipher spec:
5400 * Consume whole record content, in_msglen = 0.
5401 *
5402 * (4) Application data:
5403 * Don't do anything - the record layer provides
5404 * the application data as a stream transport
5405 * and consumes through mbedtls_ssl_read only.
5406 *
5407 */
5408
5409 /* Case (1): Handshake messages */
5410 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005411 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005412 /* Hard assertion to be sure that no application data
5413 * is in flight, as corrupting ssl->in_msglen during
5414 * ssl->in_offt != NULL is fatal. */
5415 if( ssl->in_offt != NULL )
5416 {
5417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5419 }
5420
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005421 /*
5422 * Get next Handshake message in the current record
5423 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005424
Hanno Becker4a810fb2017-05-24 16:27:30 +01005425 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005426 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005427 * current handshake content: If DTLS handshake
5428 * fragmentation is used, that's the fragment
5429 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005430 * size here is faulty and should be changed at
5431 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005432 * (2) While it doesn't seem to cause problems, one
5433 * has to be very careful not to assume that in_hslen
5434 * is always <= in_msglen in a sensible communication.
5435 * Again, it's wrong for DTLS handshake fragmentation.
5436 * The following check is therefore mandatory, and
5437 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005438 * Additionally, ssl->in_hslen might be arbitrarily out of
5439 * bounds after handling a DTLS message with an unexpected
5440 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005441 */
5442 if( ssl->in_hslen < ssl->in_msglen )
5443 {
5444 ssl->in_msglen -= ssl->in_hslen;
5445 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5446 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005447
Hanno Becker4a810fb2017-05-24 16:27:30 +01005448 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5449 ssl->in_msg, ssl->in_msglen );
5450 }
5451 else
5452 {
5453 ssl->in_msglen = 0;
5454 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005455
Hanno Becker4a810fb2017-05-24 16:27:30 +01005456 ssl->in_hslen = 0;
5457 }
5458 /* Case (4): Application data */
5459 else if( ssl->in_offt != NULL )
5460 {
5461 return( 0 );
5462 }
5463 /* Everything else (CCS & Alerts) */
5464 else
5465 {
5466 ssl->in_msglen = 0;
5467 }
5468
Hanno Becker1097b342018-08-15 14:09:41 +01005469 return( 0 );
5470}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005471
Hanno Beckere74d5562018-08-15 14:26:08 +01005472static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5473{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005474 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005475 return( 1 );
5476
5477 return( 0 );
5478}
5479
Hanno Becker5f066e72018-08-16 14:56:31 +01005480#if defined(MBEDTLS_SSL_PROTO_DTLS)
5481
5482static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5483{
5484 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5485 if( hs == NULL )
5486 return;
5487
Hanno Becker01315ea2018-08-21 17:22:17 +01005488 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005489 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005490 hs->buffering.total_bytes_buffered -=
5491 hs->buffering.future_record.len;
5492
5493 mbedtls_free( hs->buffering.future_record.data );
5494 hs->buffering.future_record.data = NULL;
5495 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005496}
5497
5498static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5499{
5500 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5501 unsigned char * rec;
5502 size_t rec_len;
5503 unsigned rec_epoch;
5504
5505 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5506 return( 0 );
5507
5508 if( hs == NULL )
5509 return( 0 );
5510
Hanno Becker5f066e72018-08-16 14:56:31 +01005511 rec = hs->buffering.future_record.data;
5512 rec_len = hs->buffering.future_record.len;
5513 rec_epoch = hs->buffering.future_record.epoch;
5514
5515 if( rec == NULL )
5516 return( 0 );
5517
Hanno Becker4cb782d2018-08-20 11:19:05 +01005518 /* Only consider loading future records if the
5519 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005520 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005521 return( 0 );
5522
Hanno Becker5f066e72018-08-16 14:56:31 +01005523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5524
5525 if( rec_epoch != ssl->in_epoch )
5526 {
5527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5528 goto exit;
5529 }
5530
5531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5532
5533 /* Double-check that the record is not too large */
5534 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5535 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5536 {
5537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5539 }
5540
5541 memcpy( ssl->in_hdr, rec, rec_len );
5542 ssl->in_left = rec_len;
5543 ssl->next_record_offset = 0;
5544
5545 ssl_free_buffered_record( ssl );
5546
5547exit:
5548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5549 return( 0 );
5550}
5551
5552static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5553{
5554 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5555 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005556 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005557
5558 /* Don't buffer future records outside handshakes. */
5559 if( hs == NULL )
5560 return( 0 );
5561
5562 /* Only buffer handshake records (we are only interested
5563 * in Finished messages). */
5564 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5565 return( 0 );
5566
5567 /* Don't buffer more than one future epoch record. */
5568 if( hs->buffering.future_record.data != NULL )
5569 return( 0 );
5570
Hanno Becker01315ea2018-08-21 17:22:17 +01005571 /* Don't buffer record if there's not enough buffering space remaining. */
5572 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5573 hs->buffering.total_bytes_buffered ) )
5574 {
5575 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",
5576 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5577 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005578 return( 0 );
5579 }
5580
Hanno Becker5f066e72018-08-16 14:56:31 +01005581 /* Buffer record */
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5583 ssl->in_epoch + 1 ) );
5584 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5585 rec_hdr_len + ssl->in_msglen );
5586
5587 /* ssl_parse_record_header() only considers records
5588 * of the next epoch as candidates for buffering. */
5589 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005591
5592 hs->buffering.future_record.data =
5593 mbedtls_calloc( 1, hs->buffering.future_record.len );
5594 if( hs->buffering.future_record.data == NULL )
5595 {
5596 /* If we run out of RAM trying to buffer a
5597 * record from the next epoch, just ignore. */
5598 return( 0 );
5599 }
5600
Hanno Becker01315ea2018-08-21 17:22:17 +01005601 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005602
Hanno Becker01315ea2018-08-21 17:22:17 +01005603 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005604 return( 0 );
5605}
5606
5607#endif /* MBEDTLS_SSL_PROTO_DTLS */
5608
Hanno Beckere74d5562018-08-15 14:26:08 +01005609static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005610{
5611 int ret;
5612
Hanno Becker5f066e72018-08-16 14:56:31 +01005613#if defined(MBEDTLS_SSL_PROTO_DTLS)
5614 /* We might have buffered a future record; if so,
5615 * and if the epoch matches now, load it.
5616 * On success, this call will set ssl->in_left to
5617 * the length of the buffered record, so that
5618 * the calls to ssl_fetch_input() below will
5619 * essentially be no-ops. */
5620 ret = ssl_load_buffered_record( ssl );
5621 if( ret != 0 )
5622 return( ret );
5623#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005628 return( ret );
5629 }
5630
5631 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5635 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005636 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005637 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5638 {
5639 ret = ssl_buffer_future_record( ssl );
5640 if( ret != 0 )
5641 return( ret );
5642
5643 /* Fall through to handling of unexpected records */
5644 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5645 }
5646
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005647 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5648 {
5649 /* Skip unexpected record (but not whole datagram) */
5650 ssl->next_record_offset = ssl->in_msglen
5651 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005652
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5654 "(header)" ) );
5655 }
5656 else
5657 {
5658 /* Skip invalid record and the rest of the datagram */
5659 ssl->next_record_offset = 0;
5660 ssl->in_left = 0;
5661
5662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5663 "(header)" ) );
5664 }
5665
5666 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005667 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005668 }
5669#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005670 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005671 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005672
5673 /*
5674 * Read and optionally decrypt the message contents
5675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5677 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005680 return( ret );
5681 }
5682
5683 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005685 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005688 if( ssl->next_record_offset < ssl->in_left )
5689 {
5690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5691 }
5692 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005693 else
5694#endif
5695 ssl->in_left = 0;
5696
5697 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005700 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005701 {
5702 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5704 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005705 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005706 /* Except when waiting for Finished as a bad mac here
5707 * probably means something went wrong in the handshake
5708 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5709 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5710 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5711 {
5712#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5713 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5714 {
5715 mbedtls_ssl_send_alert_message( ssl,
5716 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5717 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5718 }
5719#endif
5720 return( ret );
5721 }
5722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005724 if( ssl->conf->badmac_limit != 0 &&
5725 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5728 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005729 }
5730#endif
5731
Hanno Becker4a810fb2017-05-24 16:27:30 +01005732 /* As above, invalid records cause
5733 * dismissal of the whole datagram. */
5734
5735 ssl->next_record_offset = 0;
5736 ssl->in_left = 0;
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005739 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005740 }
5741
5742 return( ret );
5743 }
5744 else
5745#endif
5746 {
5747 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5749 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 mbedtls_ssl_send_alert_message( ssl,
5752 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5753 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005754 }
5755#endif
5756 return( ret );
5757 }
5758 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005759
Simon Butcher99000142016-10-13 17:21:01 +01005760 return( 0 );
5761}
5762
5763int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5764{
5765 int ret;
5766
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005767 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005768 * Handle particular types of records
5769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005771 {
Simon Butcher99000142016-10-13 17:21:01 +01005772 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5773 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005774 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005775 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005776 }
5777
Hanno Beckere678eaa2018-08-21 14:57:46 +01005778 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005779 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005780 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005781 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5783 ssl->in_msglen ) );
5784 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005785 }
5786
Hanno Beckere678eaa2018-08-21 14:57:46 +01005787 if( ssl->in_msg[0] != 1 )
5788 {
5789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5790 ssl->in_msg[0] ) );
5791 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5792 }
5793
5794#if defined(MBEDTLS_SSL_PROTO_DTLS)
5795 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5796 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5797 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5798 {
5799 if( ssl->handshake == NULL )
5800 {
5801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5802 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5803 }
5804
5805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5806 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5807 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005808#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005809 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005812 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005813 if( ssl->in_msglen != 2 )
5814 {
5815 /* Note: Standard allows for more than one 2 byte alert
5816 to be packed in a single message, but Mbed TLS doesn't
5817 currently support this. */
5818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5819 ssl->in_msglen ) );
5820 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5821 }
5822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 ssl->in_msg[0], ssl->in_msg[1] ) );
5825
5826 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005827 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005832 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 }
5835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5837 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5840 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005842
5843#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5844 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5845 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5846 {
Hanno Becker90333da2017-10-10 11:27:13 +01005847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005848 /* Will be handled when trying to parse ServerHello */
5849 return( 0 );
5850 }
5851#endif
5852
5853#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5854 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5855 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5856 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5857 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5858 {
5859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5860 /* Will be handled in mbedtls_ssl_parse_certificate() */
5861 return( 0 );
5862 }
5863#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5864
5865 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005866 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 }
5868
Hanno Beckerc76c6192017-06-06 10:03:17 +01005869#if defined(MBEDTLS_SSL_PROTO_DTLS)
5870 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5871 ssl->handshake != NULL &&
5872 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5873 {
5874 ssl_handshake_wrapup_free_hs_transform( ssl );
5875 }
5876#endif
5877
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 return( 0 );
5879}
5880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005882{
5883 int ret;
5884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5886 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5887 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005888 {
5889 return( ret );
5890 }
5891
5892 return( 0 );
5893}
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005896 unsigned char level,
5897 unsigned char message )
5898{
5899 int ret;
5900
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005901 if( ssl == NULL || ssl->conf == NULL )
5902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005908 ssl->out_msglen = 2;
5909 ssl->out_msg[0] = level;
5910 ssl->out_msg[1] = message;
5911
Hanno Becker67bc7c32018-08-06 11:33:50 +01005912 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005915 return( ret );
5916 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005918
5919 return( 0 );
5920}
5921
Hanno Beckerb9d44792019-02-08 07:19:04 +00005922#if defined(MBEDTLS_X509_CRT_PARSE_C)
5923static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5924{
5925#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5926 if( session->peer_cert != NULL )
5927 {
5928 mbedtls_x509_crt_free( session->peer_cert );
5929 mbedtls_free( session->peer_cert );
5930 session->peer_cert = NULL;
5931 }
5932#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5933 if( session->peer_cert_digest != NULL )
5934 {
5935 /* Zeroization is not necessary. */
5936 mbedtls_free( session->peer_cert_digest );
5937 session->peer_cert_digest = NULL;
5938 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5939 session->peer_cert_digest_len = 0;
5940 }
5941#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5942}
5943#endif /* MBEDTLS_X509_CRT_PARSE_C */
5944
Paul Bakker5121ce52009-01-03 21:22:43 +00005945/*
5946 * Handshake functions
5947 */
Hanno Becker21489932019-02-05 13:20:55 +00005948#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005949/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005951{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005952 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5953 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005956
Hanno Becker7177a882019-02-05 13:36:46 +00005957 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005960 ssl->state++;
5961 return( 0 );
5962 }
5963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005966}
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005969{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005970 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5971 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005974
Hanno Becker7177a882019-02-05 13:36:46 +00005975 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005978 ssl->state++;
5979 return( 0 );
5980 }
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005984}
Gilles Peskinef9828522017-05-03 12:28:43 +02005985
Hanno Becker21489932019-02-05 13:20:55 +00005986#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005987/* Some certificate support -> implement write and parse */
5988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005992 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005994 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5995 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005998
Hanno Becker7177a882019-02-05 13:36:46 +00005999 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006002 ssl->state++;
6003 return( 0 );
6004 }
6005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006007 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006008 {
6009 if( ssl->client_auth == 0 )
6010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 ssl->state++;
6013 return( 0 );
6014 }
6015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 /*
6018 * If using SSLv3 and got no cert, send an Alert message
6019 * (otherwise an empty Certificate message will be sent).
6020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6022 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006023 {
6024 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6026 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6027 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 goto write_msg;
6031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#endif /* MBEDTLS_SSL_CLI_C */
6035#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006036 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6041 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006042 }
6043 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006047
6048 /*
6049 * 0 . 0 handshake type
6050 * 1 . 3 handshake length
6051 * 4 . 6 length of all certs
6052 * 7 . 9 length of cert. 1
6053 * 10 . n-1 peer certificate
6054 * n . n+2 length of cert. 2
6055 * n+3 . ... upper level cert, etc.
6056 */
6057 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006059
Paul Bakker29087132010-03-21 21:03:34 +00006060 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
6062 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006063 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006066 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 }
6069
6070 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6071 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6072 ssl->out_msg[i + 2] = (unsigned char)( n );
6073
6074 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6075 i += n; crt = crt->next;
6076 }
6077
6078 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6079 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6080 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6081
6082 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6084 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006086#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006087write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006089
6090 ssl->state++;
6091
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006092 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 return( ret );
6096 }
6097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006099
Paul Bakkered27a042013-04-18 22:46:23 +02006100 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006101}
6102
Hanno Becker84879e32019-01-31 07:44:03 +00006103#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006104
6105#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006106static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6107 unsigned char *crt_buf,
6108 size_t crt_buf_len )
6109{
6110 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6111
6112 if( peer_crt == NULL )
6113 return( -1 );
6114
6115 if( peer_crt->raw.len != crt_buf_len )
6116 return( -1 );
6117
Hanno Becker46f34d02019-02-08 14:00:04 +00006118 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006119}
Hanno Becker177475a2019-02-05 17:02:46 +00006120#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6121static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6122 unsigned char *crt_buf,
6123 size_t crt_buf_len )
6124{
6125 int ret;
6126 unsigned char const * const peer_cert_digest =
6127 ssl->session->peer_cert_digest;
6128 mbedtls_md_type_t const peer_cert_digest_type =
6129 ssl->session->peer_cert_digest_type;
6130 mbedtls_md_info_t const * const digest_info =
6131 mbedtls_md_info_from_type( peer_cert_digest_type );
6132 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6133 size_t digest_len;
6134
6135 if( peer_cert_digest == NULL || digest_info == NULL )
6136 return( -1 );
6137
6138 digest_len = mbedtls_md_get_size( digest_info );
6139 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6140 return( -1 );
6141
6142 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6143 if( ret != 0 )
6144 return( -1 );
6145
6146 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6147}
6148#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006149#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006150
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006151/*
6152 * Once the certificate message is read, parse it into a cert chain and
6153 * perform basic checks, but leave actual verification to the caller
6154 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006155static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6156 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006157{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006158 int ret;
6159#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6160 int crt_cnt=0;
6161#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006162 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006163 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006168 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6169 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171 }
6172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6174 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006177 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6178 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006180 }
6181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006183
Paul Bakker5121ce52009-01-03 21:22:43 +00006184 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006186 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006187 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006188
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006189 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006193 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6194 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006196 }
6197
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006198 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6199 i += 3;
6200
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006201 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006202 while( i < ssl->in_hslen )
6203 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006204 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006205 if ( i + 3 > ssl->in_hslen ) {
6206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006207 mbedtls_ssl_send_alert_message( ssl,
6208 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6209 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006210 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6211 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006212 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6213 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006214 if( ssl->in_msg[i] != 0 )
6215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006217 mbedtls_ssl_send_alert_message( ssl,
6218 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6219 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 }
6222
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006223 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006224 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6225 | (unsigned int) ssl->in_msg[i + 2];
6226 i += 3;
6227
6228 if( n < 128 || i + n > ssl->in_hslen )
6229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006231 mbedtls_ssl_send_alert_message( ssl,
6232 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6233 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 }
6236
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006237 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006238#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6239 if( crt_cnt++ == 0 &&
6240 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6241 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006242 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006243 /* During client-side renegotiation, check that the server's
6244 * end-CRTs hasn't changed compared to the initial handshake,
6245 * mitigating the triple handshake attack. On success, reuse
6246 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6248 if( ssl_check_peer_crt_unchanged( ssl,
6249 &ssl->in_msg[i],
6250 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006251 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6253 mbedtls_ssl_send_alert_message( ssl,
6254 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6255 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6256 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006257 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006258
6259 /* Now we can safely free the original chain. */
6260 ssl_clear_peer_cert( ssl->session );
6261 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006262#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6263
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006264 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006265#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006266 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006267#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006268 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006269 * it in-place from the input buffer instead of making a copy. */
6270 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6271#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006272 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006273 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006274 case 0: /*ok*/
6275 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6276 /* Ignore certificate with an unknown algorithm: maybe a
6277 prior certificate was already trusted. */
6278 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006279
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006280 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6281 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6282 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006283
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006284 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6285 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6286 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006287
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006288 default:
6289 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6290 crt_parse_der_failed:
6291 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6292 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6293 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006294 }
6295
6296 i += n;
6297 }
6298
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006299 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006300 return( 0 );
6301}
6302
Hanno Becker4a55f632019-02-05 12:49:06 +00006303#if defined(MBEDTLS_SSL_SRV_C)
6304static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6305{
6306 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6307 return( -1 );
6308
6309#if defined(MBEDTLS_SSL_PROTO_SSL3)
6310 /*
6311 * Check if the client sent an empty certificate
6312 */
6313 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6314 {
6315 if( ssl->in_msglen == 2 &&
6316 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6317 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6318 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6319 {
6320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6321 return( 0 );
6322 }
6323
6324 return( -1 );
6325 }
6326#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6327
6328#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6329 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6330 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6331 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6332 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6333 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6334 {
6335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6336 return( 0 );
6337 }
6338
6339 return( -1 );
6340#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6341 MBEDTLS_SSL_PROTO_TLS1_2 */
6342}
6343#endif /* MBEDTLS_SSL_SRV_C */
6344
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006345/* Check if a certificate message is expected.
6346 * Return either
6347 * - SSL_CERTIFICATE_EXPECTED, or
6348 * - SSL_CERTIFICATE_SKIP
6349 * indicating whether a Certificate message is expected or not.
6350 */
6351#define SSL_CERTIFICATE_EXPECTED 0
6352#define SSL_CERTIFICATE_SKIP 1
6353static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6354 int authmode )
6355{
6356 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006357 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006358
6359 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6360 return( SSL_CERTIFICATE_SKIP );
6361
6362#if defined(MBEDTLS_SSL_SRV_C)
6363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6364 {
6365 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6366 return( SSL_CERTIFICATE_SKIP );
6367
6368 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6369 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006370 ssl->session_negotiate->verify_result =
6371 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6372 return( SSL_CERTIFICATE_SKIP );
6373 }
6374 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006375#else
6376 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006377#endif /* MBEDTLS_SSL_SRV_C */
6378
6379 return( SSL_CERTIFICATE_EXPECTED );
6380}
6381
Hanno Becker68636192019-02-05 14:36:34 +00006382static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6383 int authmode,
6384 mbedtls_x509_crt *chain,
6385 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006386{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006387 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006388 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006389 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006390 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006391
Hanno Becker8927c832019-04-03 12:52:50 +01006392 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6393 void *p_vrfy;
6394
Hanno Becker68636192019-02-05 14:36:34 +00006395 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6396 return( 0 );
6397
Hanno Becker8927c832019-04-03 12:52:50 +01006398 if( ssl->f_vrfy != NULL )
6399 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006401 f_vrfy = ssl->f_vrfy;
6402 p_vrfy = ssl->p_vrfy;
6403 }
6404 else
6405 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006407 f_vrfy = ssl->conf->f_vrfy;
6408 p_vrfy = ssl->conf->p_vrfy;
6409 }
6410
Hanno Becker68636192019-02-05 14:36:34 +00006411 /*
6412 * Main check: verify certificate
6413 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006414#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6415 if( ssl->conf->f_ca_cb != NULL )
6416 {
6417 ((void) rs_ctx);
6418 have_ca_chain = 1;
6419
6420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006421 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006422 chain,
6423 ssl->conf->f_ca_cb,
6424 ssl->conf->p_ca_cb,
6425 ssl->conf->cert_profile,
6426 ssl->hostname,
6427 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006428 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006429 }
6430 else
6431#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6432 {
6433 mbedtls_x509_crt *ca_chain;
6434 mbedtls_x509_crl *ca_crl;
6435
6436#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6437 if( ssl->handshake->sni_ca_chain != NULL )
6438 {
6439 ca_chain = ssl->handshake->sni_ca_chain;
6440 ca_crl = ssl->handshake->sni_ca_crl;
6441 }
6442 else
6443#endif
6444 {
6445 ca_chain = ssl->conf->ca_chain;
6446 ca_crl = ssl->conf->ca_crl;
6447 }
6448
6449 if( ca_chain != NULL )
6450 have_ca_chain = 1;
6451
6452 ret = mbedtls_x509_crt_verify_restartable(
6453 chain,
6454 ca_chain, ca_crl,
6455 ssl->conf->cert_profile,
6456 ssl->hostname,
6457 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006458 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006459 }
Hanno Becker68636192019-02-05 14:36:34 +00006460
6461 if( ret != 0 )
6462 {
6463 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6464 }
6465
6466#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6467 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6468 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6469#endif
6470
6471 /*
6472 * Secondary checks: always done, but change 'ret' only if it was 0
6473 */
6474
6475#if defined(MBEDTLS_ECP_C)
6476 {
6477 const mbedtls_pk_context *pk = &chain->pk;
6478
6479 /* If certificate uses an EC key, make sure the curve is OK */
6480 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6481 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6482 {
6483 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6484
6485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6486 if( ret == 0 )
6487 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6488 }
6489 }
6490#endif /* MBEDTLS_ECP_C */
6491
6492 if( mbedtls_ssl_check_cert_usage( chain,
6493 ciphersuite_info,
6494 ! ssl->conf->endpoint,
6495 &ssl->session_negotiate->verify_result ) != 0 )
6496 {
6497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6498 if( ret == 0 )
6499 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6500 }
6501
6502 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6503 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6504 * with details encoded in the verification flags. All other kinds
6505 * of error codes, including those from the user provided f_vrfy
6506 * functions, are treated as fatal and lead to a failure of
6507 * ssl_parse_certificate even if verification was optional. */
6508 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6509 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6510 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6511 {
6512 ret = 0;
6513 }
6514
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006515 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006516 {
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6518 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6519 }
6520
6521 if( ret != 0 )
6522 {
6523 uint8_t alert;
6524
6525 /* The certificate may have been rejected for several reasons.
6526 Pick one and send the corresponding alert. Which alert to send
6527 may be a subject of debate in some cases. */
6528 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6529 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6530 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6531 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6532 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6533 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6534 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6535 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6536 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6537 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6538 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6539 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6540 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6541 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6542 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6543 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6544 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6545 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6546 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6547 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6548 else
6549 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6550 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6551 alert );
6552 }
6553
6554#if defined(MBEDTLS_DEBUG_C)
6555 if( ssl->session_negotiate->verify_result != 0 )
6556 {
6557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6558 ssl->session_negotiate->verify_result ) );
6559 }
6560 else
6561 {
6562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6563 }
6564#endif /* MBEDTLS_DEBUG_C */
6565
6566 return( ret );
6567}
6568
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006569#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6570static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6571 unsigned char *start, size_t len )
6572{
6573 int ret;
6574 /* Remember digest of the peer's end-CRT. */
6575 ssl->session_negotiate->peer_cert_digest =
6576 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6577 if( ssl->session_negotiate->peer_cert_digest == NULL )
6578 {
6579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6580 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6581 mbedtls_ssl_send_alert_message( ssl,
6582 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6583 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6584
6585 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6586 }
6587
6588 ret = mbedtls_md( mbedtls_md_info_from_type(
6589 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6590 start, len,
6591 ssl->session_negotiate->peer_cert_digest );
6592
6593 ssl->session_negotiate->peer_cert_digest_type =
6594 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6595 ssl->session_negotiate->peer_cert_digest_len =
6596 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6597
6598 return( ret );
6599}
6600
6601static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6602 unsigned char *start, size_t len )
6603{
6604 unsigned char *end = start + len;
6605 int ret;
6606
6607 /* Make a copy of the peer's raw public key. */
6608 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6609 ret = mbedtls_pk_parse_subpubkey( &start, end,
6610 &ssl->handshake->peer_pubkey );
6611 if( ret != 0 )
6612 {
6613 /* We should have parsed the public key before. */
6614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6615 }
6616
6617 return( 0 );
6618}
6619#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6620
Hanno Becker68636192019-02-05 14:36:34 +00006621int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6622{
6623 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006624 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006625#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6626 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6627 ? ssl->handshake->sni_authmode
6628 : ssl->conf->authmode;
6629#else
6630 const int authmode = ssl->conf->authmode;
6631#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006632 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006633 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006634
6635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6636
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006637 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6638 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006639 {
6640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006641 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006642 }
6643
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006644#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6645 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006646 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006647 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006648 chain = ssl->handshake->ecrs_peer_cert;
6649 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006650 goto crt_verify;
6651 }
6652#endif
6653
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006654 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006655 {
6656 /* mbedtls_ssl_read_record may have sent an alert already. We
6657 let it decide whether to alert. */
6658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006659 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006660 }
6661
Hanno Becker4a55f632019-02-05 12:49:06 +00006662#if defined(MBEDTLS_SSL_SRV_C)
6663 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6664 {
6665 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006666
6667 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006668 ret = 0;
6669 else
6670 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006671
Hanno Becker6bdfab22019-02-05 13:11:17 +00006672 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006673 }
6674#endif /* MBEDTLS_SSL_SRV_C */
6675
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006676 /* Clear existing peer CRT structure in case we tried to
6677 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006678 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006679
6680 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6681 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006682 {
6683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6684 sizeof( mbedtls_x509_crt ) ) );
6685 mbedtls_ssl_send_alert_message( ssl,
6686 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6687 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006688
Hanno Becker3dad3112019-02-05 17:19:52 +00006689 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6690 goto exit;
6691 }
6692 mbedtls_x509_crt_init( chain );
6693
6694 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006695 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006696 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006697
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006698#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6699 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006700 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006701
6702crt_verify:
6703 if( ssl->handshake->ecrs_enabled)
6704 rs_ctx = &ssl->handshake->ecrs_ctx;
6705#endif
6706
Hanno Becker68636192019-02-05 14:36:34 +00006707 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006708 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006709 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006710 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006711
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006712#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006713 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006714 unsigned char *crt_start, *pk_start;
6715 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006716
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006717 /* We parse the CRT chain without copying, so
6718 * these pointers point into the input buffer,
6719 * and are hence still valid after freeing the
6720 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006721
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006722 crt_start = chain->raw.p;
6723 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006724
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006725 pk_start = chain->pk_raw.p;
6726 pk_len = chain->pk_raw.len;
6727
6728 /* Free the CRT structures before computing
6729 * digest and copying the peer's public key. */
6730 mbedtls_x509_crt_free( chain );
6731 mbedtls_free( chain );
6732 chain = NULL;
6733
6734 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006735 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006736 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006737
6738 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6739 if( ret != 0 )
6740 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006741 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006742#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6743 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006744 ssl->session_negotiate->peer_cert = chain;
6745 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006746#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006749
Hanno Becker6bdfab22019-02-05 13:11:17 +00006750exit:
6751
Hanno Becker3dad3112019-02-05 17:19:52 +00006752 if( ret == 0 )
6753 ssl->state++;
6754
6755#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6756 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6757 {
6758 ssl->handshake->ecrs_peer_cert = chain;
6759 chain = NULL;
6760 }
6761#endif
6762
6763 if( chain != NULL )
6764 {
6765 mbedtls_x509_crt_free( chain );
6766 mbedtls_free( chain );
6767 }
6768
Paul Bakker5121ce52009-01-03 21:22:43 +00006769 return( ret );
6770}
Hanno Becker21489932019-02-05 13:20:55 +00006771#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006774{
6775 int ret;
6776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 ssl->out_msglen = 1;
6781 ssl->out_msg[0] = 1;
6782
Paul Bakker5121ce52009-01-03 21:22:43 +00006783 ssl->state++;
6784
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006785 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006788 return( ret );
6789 }
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006792
6793 return( 0 );
6794}
6795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006797{
6798 int ret;
6799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006801
Hanno Becker327c93b2018-08-15 13:56:18 +01006802 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 return( ret );
6806 }
6807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006811 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6812 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006814 }
6815
Hanno Beckere678eaa2018-08-21 14:57:46 +01006816 /* CCS records are only accepted if they have length 1 and content '1',
6817 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006819 /*
6820 * Switch to our negotiated transform and session parameters for inbound
6821 * data.
6822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006824 ssl->transform_in = ssl->transform_negotiate;
6825 ssl->session_in = ssl->session_negotiate;
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006828 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006831 ssl_dtls_replay_reset( ssl );
6832#endif
6833
6834 /* Increment epoch */
6835 if( ++ssl->in_epoch == 0 )
6836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006838 /* This is highly unlikely to happen for legitimate reasons, so
6839 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006841 }
6842 }
6843 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006845 memset( ssl->in_ctr, 0, 8 );
6846
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006847 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6850 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006855 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6856 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006858 }
6859 }
6860#endif
6861
Paul Bakker5121ce52009-01-03 21:22:43 +00006862 ssl->state++;
6863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865
6866 return( 0 );
6867}
6868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6870 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006871{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006872 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6875 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6876 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006877 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006878 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6881#if defined(MBEDTLS_SHA512_C)
6882 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006883 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6884 else
6885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886#if defined(MBEDTLS_SHA256_C)
6887 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006888 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006889 else
6890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006894 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006895 }
Paul Bakker380da532012-04-18 16:10:25 +00006896}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6901 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006902 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6903 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6906#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006907#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006908 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006909 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6910#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006911 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006912#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006915#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006916 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006917 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006918#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006919 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006920#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006921#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006923}
6924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006926 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6929 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006930 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6931 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006932#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6934#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006935#if defined(MBEDTLS_USE_PSA_CRYPTO)
6936 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6937#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006938 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006939#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006942#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006943 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006944#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006945 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006946#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006949}
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6952 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6953static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006954 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006955{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006956 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6957 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006958}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006959#endif
Paul Bakker380da532012-04-18 16:10:25 +00006960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6962#if defined(MBEDTLS_SHA256_C)
6963static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006964 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006965{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006966#if defined(MBEDTLS_USE_PSA_CRYPTO)
6967 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6968#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006969 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006970#endif
Paul Bakker380da532012-04-18 16:10:25 +00006971}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006972#endif
Paul Bakker380da532012-04-18 16:10:25 +00006973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974#if defined(MBEDTLS_SHA512_C)
6975static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006976 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006977{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006978#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006979 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006980#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006981 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006982#endif
Paul Bakker380da532012-04-18 16:10:25 +00006983}
Paul Bakker769075d2012-11-24 11:26:46 +01006984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006988static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006990{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006991 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006992 mbedtls_md5_context md5;
6993 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006994
Paul Bakker5121ce52009-01-03 21:22:43 +00006995 unsigned char padbuf[48];
6996 unsigned char md5sum[16];
6997 unsigned char sha1sum[20];
6998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007000 if( !session )
7001 session = ssl->session;
7002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007004
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007005 mbedtls_md5_init( &md5 );
7006 mbedtls_sha1_init( &sha1 );
7007
7008 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7009 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007010
7011 /*
7012 * SSLv3:
7013 * hash =
7014 * MD5( master + pad2 +
7015 * MD5( handshake + sender + master + pad1 ) )
7016 * + SHA1( master + pad2 +
7017 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007018 */
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007021 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7022 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007026 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7027 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007031 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007032
Paul Bakker1ef83d62012-04-11 12:09:53 +00007033 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007034
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007035 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7036 mbedtls_md5_update_ret( &md5, session->master, 48 );
7037 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7038 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007039
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007040 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7041 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7042 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7043 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Paul Bakker1ef83d62012-04-11 12:09:53 +00007045 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007046
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007047 mbedtls_md5_starts_ret( &md5 );
7048 mbedtls_md5_update_ret( &md5, session->master, 48 );
7049 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7050 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7051 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007052
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007053 mbedtls_sha1_starts_ret( &sha1 );
7054 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7055 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7056 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7057 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007060
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007061 mbedtls_md5_free( &md5 );
7062 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007064 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7065 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7066 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007073static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007075{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007076 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007077 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007078 mbedtls_md5_context md5;
7079 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007080 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007083 if( !session )
7084 session = ssl->session;
7085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007088 mbedtls_md5_init( &md5 );
7089 mbedtls_sha1_init( &sha1 );
7090
7091 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7092 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007093
Paul Bakker1ef83d62012-04-11 12:09:53 +00007094 /*
7095 * TLSv1:
7096 * hash = PRF( master, finished_label,
7097 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7098 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007101 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7102 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007103#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007106 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7107 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007108#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007111 ? "client finished"
7112 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007113
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007114 mbedtls_md5_finish_ret( &md5, padbuf );
7115 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007116
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007117 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007118 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007121
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007122 mbedtls_md5_free( &md5 );
7123 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007124
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007125 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7132#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007133static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007135{
7136 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007137 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007138 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007139#if defined(MBEDTLS_USE_PSA_CRYPTO)
7140 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007141 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007142 psa_status_t status;
7143#else
7144 mbedtls_sha256_context sha256;
7145#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007148 if( !session )
7149 session = ssl->session;
7150
Andrzej Kurekeb342242019-01-29 09:14:33 -05007151 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7152 ? "client finished"
7153 : "server finished";
7154
7155#if defined(MBEDTLS_USE_PSA_CRYPTO)
7156 sha256_psa = psa_hash_operation_init();
7157
7158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7159
7160 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7161 if( status != PSA_SUCCESS )
7162 {
7163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7164 return;
7165 }
7166
7167 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7168 if( status != PSA_SUCCESS )
7169 {
7170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7171 return;
7172 }
7173 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7174#else
7175
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007176 mbedtls_sha256_init( &sha256 );
7177
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007179
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007180 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007181
7182 /*
7183 * TLSv1.2:
7184 * hash = PRF( master, finished_label,
7185 * Hash( handshake ) )[0.11]
7186 */
7187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if !defined(MBEDTLS_SHA256_ALT)
7189 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007190 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007191#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007193 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007194 mbedtls_sha256_free( &sha256 );
7195#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007197 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007198 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007201
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007202 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007209static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007211{
7212 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007213 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007214 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007215#if defined(MBEDTLS_USE_PSA_CRYPTO)
7216 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007217 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007218 psa_status_t status;
7219#else
7220 mbedtls_sha512_context sha512;
7221#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007224 if( !session )
7225 session = ssl->session;
7226
Andrzej Kurekeb342242019-01-29 09:14:33 -05007227 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7228 ? "client finished"
7229 : "server finished";
7230
7231#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007232 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007233
7234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7235
Andrzej Kurek972fba52019-01-30 03:29:12 -05007236 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007237 if( status != PSA_SUCCESS )
7238 {
7239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7240 return;
7241 }
7242
Andrzej Kurek972fba52019-01-30 03:29:12 -05007243 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007244 if( status != PSA_SUCCESS )
7245 {
7246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7247 return;
7248 }
7249 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7250#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007251 mbedtls_sha512_init( &sha512 );
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007254
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007255 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007256
7257 /*
7258 * TLSv1.2:
7259 * hash = PRF( master, finished_label,
7260 * Hash( handshake ) )[0.11]
7261 */
7262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007264 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7265 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007266#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007267
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007268 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007269 mbedtls_sha512_free( &sha512 );
7270#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007271
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007272 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007273 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007276
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007277 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281#endif /* MBEDTLS_SHA512_C */
7282#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007287
7288 /*
7289 * Free our handshake params
7290 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007291 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007293 ssl->handshake = NULL;
7294
7295 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007296 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007297 */
7298 if( ssl->transform )
7299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 mbedtls_ssl_transform_free( ssl->transform );
7301 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007302 }
7303 ssl->transform = ssl->transform_negotiate;
7304 ssl->transform_negotiate = NULL;
7305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007307}
7308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007310{
7311 int resume = ssl->handshake->resume;
7312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315#if defined(MBEDTLS_SSL_RENEGOTIATION)
7316 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007319 ssl->renego_records_seen = 0;
7320 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007321#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007322
7323 /*
7324 * Free the previous session and switch in the current one
7325 */
Paul Bakker0a597072012-09-25 21:55:46 +00007326 if( ssl->session )
7327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007329 /* RFC 7366 3.1: keep the EtM state */
7330 ssl->session_negotiate->encrypt_then_mac =
7331 ssl->session->encrypt_then_mac;
7332#endif
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 mbedtls_ssl_session_free( ssl->session );
7335 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007336 }
7337 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007338 ssl->session_negotiate = NULL;
7339
Paul Bakker0a597072012-09-25 21:55:46 +00007340 /*
7341 * Add cache entry
7342 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007343 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007344 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007345 resume == 0 )
7346 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007347 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007349 }
Paul Bakker0a597072012-09-25 21:55:46 +00007350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007352 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007353 ssl->handshake->flight != NULL )
7354 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007355 /* Cancel handshake timer */
7356 ssl_set_timer( ssl, 0 );
7357
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007358 /* Keep last flight around in case we need to resend it:
7359 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007361 }
7362 else
7363#endif
7364 ssl_handshake_wrapup_free_hs_transform( ssl );
7365
Paul Bakker48916f92012-09-16 19:57:18 +00007366 ssl->state++;
7367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007369}
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007372{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007373 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007376
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007377 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007378
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007379 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007380
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007381 /*
7382 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7383 * may define some other value. Currently (early 2016), no defined
7384 * ciphersuite does this (and this is unlikely to change as activity has
7385 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007390 ssl->verify_data_len = hash_len;
7391 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007392#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007393
Paul Bakker5121ce52009-01-03 21:22:43 +00007394 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7396 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
7398 /*
7399 * In case of session resuming, invert the client and server
7400 * ChangeCipherSpec messages order.
7401 */
Paul Bakker0a597072012-09-25 21:55:46 +00007402 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007405 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007409 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007411#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007412 }
7413 else
7414 ssl->state++;
7415
Paul Bakker48916f92012-09-16 19:57:18 +00007416 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007417 * Switch to our negotiated transform and session parameters for outbound
7418 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007424 {
7425 unsigned char i;
7426
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007427 /* Remember current epoch settings for resending */
7428 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007429 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007430
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007431 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007432 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007433
7434 /* Increment epoch */
7435 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007436 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007437 break;
7438
7439 /* The loop goes to its end iff the counter is wrapping */
7440 if( i == 0 )
7441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7443 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007444 }
7445 }
7446 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007448 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007449
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007450 ssl->transform_out = ssl->transform_negotiate;
7451 ssl->session_out = ssl->session_negotiate;
7452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7454 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7459 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007460 }
7461 }
7462#endif
7463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007467#endif
7468
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007469 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007470 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007472 return( ret );
7473 }
7474
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007475#if defined(MBEDTLS_SSL_PROTO_DTLS)
7476 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7477 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7478 {
7479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7480 return( ret );
7481 }
7482#endif
7483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007485
7486 return( 0 );
7487}
7488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007490#define SSL_MAX_HASH_LEN 36
7491#else
7492#define SSL_MAX_HASH_LEN 12
7493#endif
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007496{
Paul Bakker23986e52011-04-24 08:57:21 +00007497 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007498 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007499 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007503 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007504
Hanno Becker327c93b2018-08-15 13:56:18 +01007505 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007508 return( ret );
7509 }
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007514 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7515 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007517 }
7518
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007519 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_SSL_PROTO_SSL3)
7521 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007522 hash_len = 36;
7523 else
7524#endif
7525 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7528 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007531 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7532 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007534 }
7535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007537 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007540 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7541 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007543 }
7544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007546 ssl->verify_data_len = hash_len;
7547 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007548#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007549
Paul Bakker0a597072012-09-25 21:55:46 +00007550 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007553 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007559#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007560 }
7561 else
7562 ssl->state++;
7563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007567#endif
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007570
7571 return( 0 );
7572}
7573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007575{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7579 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7580 mbedtls_md5_init( &handshake->fin_md5 );
7581 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007582 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7583 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7586#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007587#if defined(MBEDTLS_USE_PSA_CRYPTO)
7588 handshake->fin_sha256_psa = psa_hash_operation_init();
7589 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7590#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007592 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007593#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007596#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007597 handshake->fin_sha384_psa = psa_hash_operation_init();
7598 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007599#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007601 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007602#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007605
7606 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007607
7608#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7609 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7610 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7611#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613#if defined(MBEDTLS_DHM_C)
7614 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616#if defined(MBEDTLS_ECDH_C)
7617 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007618#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007619#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007620 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007621#if defined(MBEDTLS_SSL_CLI_C)
7622 handshake->ecjpake_cache = NULL;
7623 handshake->ecjpake_cache_len = 0;
7624#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007625#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007626
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007627#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007628 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007629#endif
7630
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007631#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7632 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7633#endif
Hanno Becker75173122019-02-06 16:18:31 +00007634
7635#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7636 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7637 mbedtls_pk_init( &handshake->peer_pubkey );
7638#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007639}
7640
Hanno Beckera18d1322018-01-03 14:27:32 +00007641void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007642{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7646 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007647
Hanno Beckerd56ed242018-01-03 15:32:51 +00007648#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 mbedtls_md_init( &transform->md_ctx_enc );
7650 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007651#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007652}
7653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007657}
7658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007660{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007661 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007662 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007664 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007666 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007667 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007668
7669 /*
7670 * Either the pointers are now NULL or cleared properly and can be freed.
7671 * Now allocate missing structures.
7672 */
7673 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007674 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007675 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007676 }
Paul Bakker48916f92012-09-16 19:57:18 +00007677
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007678 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007679 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007680 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007681 }
Paul Bakker48916f92012-09-16 19:57:18 +00007682
Paul Bakker82788fb2014-10-20 13:59:19 +02007683 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007684 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007685 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007686 }
Paul Bakker48916f92012-09-16 19:57:18 +00007687
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007688 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007689 if( ssl->handshake == NULL ||
7690 ssl->transform_negotiate == NULL ||
7691 ssl->session_negotiate == NULL )
7692 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 mbedtls_free( ssl->handshake );
7696 mbedtls_free( ssl->transform_negotiate );
7697 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007698
7699 ssl->handshake = NULL;
7700 ssl->transform_negotiate = NULL;
7701 ssl->session_negotiate = NULL;
7702
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007703 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007704 }
7705
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007706 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007708 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007709 ssl_handshake_params_init( ssl->handshake );
7710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007712 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7713 {
7714 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007715
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7717 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7718 else
7719 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007720
7721 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007722 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007723#endif
7724
Paul Bakker48916f92012-09-16 19:57:18 +00007725 return( 0 );
7726}
7727
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007728#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007729/* Dummy cookie callbacks for defaults */
7730static int ssl_cookie_write_dummy( void *ctx,
7731 unsigned char **p, unsigned char *end,
7732 const unsigned char *cli_id, size_t cli_id_len )
7733{
7734 ((void) ctx);
7735 ((void) p);
7736 ((void) end);
7737 ((void) cli_id);
7738 ((void) cli_id_len);
7739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007741}
7742
7743static int ssl_cookie_check_dummy( void *ctx,
7744 const unsigned char *cookie, size_t cookie_len,
7745 const unsigned char *cli_id, size_t cli_id_len )
7746{
7747 ((void) ctx);
7748 ((void) cookie);
7749 ((void) cookie_len);
7750 ((void) cli_id);
7751 ((void) cli_id_len);
7752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007754}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007755#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007756
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007757/* Once ssl->out_hdr as the address of the beginning of the
7758 * next outgoing record is set, deduce the other pointers.
7759 *
7760 * Note: For TLS, we save the implicit record sequence number
7761 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7762 * and the caller has to make sure there's space for this.
7763 */
7764
7765static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7766 mbedtls_ssl_transform *transform )
7767{
7768#if defined(MBEDTLS_SSL_PROTO_DTLS)
7769 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7770 {
7771 ssl->out_ctr = ssl->out_hdr + 3;
7772 ssl->out_len = ssl->out_hdr + 11;
7773 ssl->out_iv = ssl->out_hdr + 13;
7774 }
7775 else
7776#endif
7777 {
7778 ssl->out_ctr = ssl->out_hdr - 8;
7779 ssl->out_len = ssl->out_hdr + 3;
7780 ssl->out_iv = ssl->out_hdr + 5;
7781 }
7782
7783 /* Adjust out_msg to make space for explicit IV, if used. */
7784 if( transform != NULL &&
7785 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7786 {
7787 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7788 }
7789 else
7790 ssl->out_msg = ssl->out_iv;
7791}
7792
7793/* Once ssl->in_hdr as the address of the beginning of the
7794 * next incoming record is set, deduce the other pointers.
7795 *
7796 * Note: For TLS, we save the implicit record sequence number
7797 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7798 * and the caller has to make sure there's space for this.
7799 */
7800
7801static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7802 mbedtls_ssl_transform *transform )
7803{
7804#if defined(MBEDTLS_SSL_PROTO_DTLS)
7805 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7806 {
7807 ssl->in_ctr = ssl->in_hdr + 3;
7808 ssl->in_len = ssl->in_hdr + 11;
7809 ssl->in_iv = ssl->in_hdr + 13;
7810 }
7811 else
7812#endif
7813 {
7814 ssl->in_ctr = ssl->in_hdr - 8;
7815 ssl->in_len = ssl->in_hdr + 3;
7816 ssl->in_iv = ssl->in_hdr + 5;
7817 }
7818
7819 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7820 if( transform != NULL &&
7821 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7822 {
7823 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7824 }
7825 else
7826 ssl->in_msg = ssl->in_iv;
7827}
7828
Paul Bakker5121ce52009-01-03 21:22:43 +00007829/*
7830 * Initialize an SSL context
7831 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007832void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7833{
7834 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7835}
7836
7837/*
7838 * Setup an SSL context
7839 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007840
7841static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7842{
7843 /* Set the incoming and outgoing record pointers. */
7844#if defined(MBEDTLS_SSL_PROTO_DTLS)
7845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7846 {
7847 ssl->out_hdr = ssl->out_buf;
7848 ssl->in_hdr = ssl->in_buf;
7849 }
7850 else
7851#endif /* MBEDTLS_SSL_PROTO_DTLS */
7852 {
7853 ssl->out_hdr = ssl->out_buf + 8;
7854 ssl->in_hdr = ssl->in_buf + 8;
7855 }
7856
7857 /* Derive other internal pointers. */
7858 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7859 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7860}
7861
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007862int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007863 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007864{
Paul Bakker48916f92012-09-16 19:57:18 +00007865 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007866
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007867 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007868
7869 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007870 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007871 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007872
7873 /* Set to NULL in case of an error condition */
7874 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007875
Angus Grattond8213d02016-05-25 20:56:48 +10007876 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7877 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 {
Angus Grattond8213d02016-05-25 20:56:48 +10007879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007880 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007881 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007882 }
7883
7884 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7885 if( ssl->out_buf == NULL )
7886 {
7887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007888 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007889 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007890 }
7891
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007892 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007893
Paul Bakker48916f92012-09-16 19:57:18 +00007894 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007895 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007896
7897 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007898
7899error:
7900 mbedtls_free( ssl->in_buf );
7901 mbedtls_free( ssl->out_buf );
7902
7903 ssl->conf = NULL;
7904
7905 ssl->in_buf = NULL;
7906 ssl->out_buf = NULL;
7907
7908 ssl->in_hdr = NULL;
7909 ssl->in_ctr = NULL;
7910 ssl->in_len = NULL;
7911 ssl->in_iv = NULL;
7912 ssl->in_msg = NULL;
7913
7914 ssl->out_hdr = NULL;
7915 ssl->out_ctr = NULL;
7916 ssl->out_len = NULL;
7917 ssl->out_iv = NULL;
7918 ssl->out_msg = NULL;
7919
k-stachowiak9f7798e2018-07-31 16:52:32 +02007920 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007921}
7922
7923/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007924 * Reset an initialized and used SSL context for re-use while retaining
7925 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007926 *
7927 * If partial is non-zero, keep data in the input buffer and client ID.
7928 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007929 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007930static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007931{
Paul Bakker48916f92012-09-16 19:57:18 +00007932 int ret;
7933
Hanno Becker7e772132018-08-10 12:38:21 +01007934#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7935 !defined(MBEDTLS_SSL_SRV_C)
7936 ((void) partial);
7937#endif
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007940
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007941 /* Cancel any possibly running timer */
7942 ssl_set_timer( ssl, 0 );
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#if defined(MBEDTLS_SSL_RENEGOTIATION)
7945 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007946 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007947
7948 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7950 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007951#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007953
Paul Bakker7eb013f2011-10-06 12:37:39 +00007954 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007955 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007956
7957 ssl->in_msgtype = 0;
7958 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007960 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007961 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007964 ssl_dtls_replay_reset( ssl );
7965#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007966
7967 ssl->in_hslen = 0;
7968 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007969
7970 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007971
7972 ssl->out_msgtype = 0;
7973 ssl->out_msglen = 0;
7974 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7976 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007977 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007978#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007979
Hanno Becker19859472018-08-06 09:40:20 +01007980 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7981
Paul Bakker48916f92012-09-16 19:57:18 +00007982 ssl->transform_in = NULL;
7983 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007984
Hanno Becker78640902018-08-13 16:35:15 +01007985 ssl->session_in = NULL;
7986 ssl->session_out = NULL;
7987
Angus Grattond8213d02016-05-25 20:56:48 +10007988 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007989
7990#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007991 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007992#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7993 {
7994 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007995 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007996 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7999 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8002 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8005 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008006 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008007 }
8008#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008009
Paul Bakker48916f92012-09-16 19:57:18 +00008010 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 mbedtls_ssl_transform_free( ssl->transform );
8013 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008014 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008015 }
Paul Bakker48916f92012-09-16 19:57:18 +00008016
Paul Bakkerc0463502013-02-14 11:19:38 +01008017 if( ssl->session )
8018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 mbedtls_ssl_session_free( ssl->session );
8020 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008021 ssl->session = NULL;
8022 }
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008025 ssl->alpn_chosen = NULL;
8026#endif
8027
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008028#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008029#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008030 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008031#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008032 {
8033 mbedtls_free( ssl->cli_id );
8034 ssl->cli_id = NULL;
8035 ssl->cli_id_len = 0;
8036 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008037#endif
8038
Paul Bakker48916f92012-09-16 19:57:18 +00008039 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8040 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008041
8042 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008043}
8044
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008045/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008046 * Reset an initialized and used SSL context for re-use while retaining
8047 * all application-set variables, function pointers and data.
8048 */
8049int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8050{
8051 return( ssl_session_reset_int( ssl, 0 ) );
8052}
8053
8054/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008055 * SSL set accessors
8056 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008057void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008059 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008060}
8061
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008062void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008064 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008065}
8066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008068void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008070 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008071}
8072#endif
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008075void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008076{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008077 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008078}
8079#endif
8080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008082
Hanno Becker1841b0a2018-08-24 11:13:57 +01008083void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8084 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008085{
8086 ssl->disable_datagram_packing = !allow_packing;
8087}
8088
8089void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8090 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008092 conf->hs_timeout_min = min;
8093 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008094}
8095#endif
8096
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008097void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008098{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008099 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008100}
8101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008103void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008104 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008105 void *p_vrfy )
8106{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008107 conf->f_vrfy = f_vrfy;
8108 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008111
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008112void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008113 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008114 void *p_rng )
8115{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008116 conf->f_rng = f_rng;
8117 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008118}
8119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008120void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008121 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 void *p_dbg )
8123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008124 conf->f_dbg = f_dbg;
8125 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008126}
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008129 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008130 mbedtls_ssl_send_t *f_send,
8131 mbedtls_ssl_recv_t *f_recv,
8132 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008133{
8134 ssl->p_bio = p_bio;
8135 ssl->f_send = f_send;
8136 ssl->f_recv = f_recv;
8137 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008138}
8139
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008140#if defined(MBEDTLS_SSL_PROTO_DTLS)
8141void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8142{
8143 ssl->mtu = mtu;
8144}
8145#endif
8146
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008147void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008148{
8149 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008150}
8151
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008152void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8153 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008154 mbedtls_ssl_set_timer_t *f_set_timer,
8155 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008156{
8157 ssl->p_timer = p_timer;
8158 ssl->f_set_timer = f_set_timer;
8159 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008160
8161 /* Make sure we start with no timer running */
8162 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008163}
8164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008166void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008167 void *p_cache,
8168 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8169 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008170{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008171 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008172 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008173 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_CLI_C)
8178int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008179{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008180 int ret;
8181
8182 if( ssl == NULL ||
8183 session == NULL ||
8184 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008185 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008188 }
8189
Hanno Becker52055ae2019-02-06 14:30:46 +00008190 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8191 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008192 return( ret );
8193
Paul Bakker0a597072012-09-25 21:55:46 +00008194 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008195
8196 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008199
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008200void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008201 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008202{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8206 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008207}
8208
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008209void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008210 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008211 int major, int minor )
8212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008214 return;
8215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008217 return;
8218
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008219 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008220}
8221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008223void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008224 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008225{
8226 conf->cert_profile = profile;
8227}
8228
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008229/* Append a new keycert entry to a (possibly empty) list */
8230static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8231 mbedtls_x509_crt *cert,
8232 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008233{
niisato8ee24222018-06-25 19:05:48 +09008234 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008235
niisato8ee24222018-06-25 19:05:48 +09008236 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8237 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008238 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008239
niisato8ee24222018-06-25 19:05:48 +09008240 new_cert->cert = cert;
8241 new_cert->key = key;
8242 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008243
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008244 /* Update head is the list was null, else add to the end */
8245 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008246 {
niisato8ee24222018-06-25 19:05:48 +09008247 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008248 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008249 else
8250 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008251 mbedtls_ssl_key_cert *cur = *head;
8252 while( cur->next != NULL )
8253 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008254 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008255 }
8256
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008257 return( 0 );
8258}
8259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008260int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008261 mbedtls_x509_crt *own_cert,
8262 mbedtls_pk_context *pk_key )
8263{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008264 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008265}
8266
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008267void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008268 mbedtls_x509_crt *ca_chain,
8269 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008270{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008271 conf->ca_chain = ca_chain;
8272 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008273
8274#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8275 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8276 * cannot be used together. */
8277 conf->f_ca_cb = NULL;
8278 conf->p_ca_cb = NULL;
8279#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008280}
Hanno Becker5adaad92019-03-27 16:54:37 +00008281
8282#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8283void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008284 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008285 void *p_ca_cb )
8286{
8287 conf->f_ca_cb = f_ca_cb;
8288 conf->p_ca_cb = p_ca_cb;
8289
8290 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8291 * cannot be used together. */
8292 conf->ca_chain = NULL;
8293 conf->ca_crl = NULL;
8294}
8295#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008297
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008298#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8299int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8300 mbedtls_x509_crt *own_cert,
8301 mbedtls_pk_context *pk_key )
8302{
8303 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8304 own_cert, pk_key ) );
8305}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008306
8307void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8308 mbedtls_x509_crt *ca_chain,
8309 mbedtls_x509_crl *ca_crl )
8310{
8311 ssl->handshake->sni_ca_chain = ca_chain;
8312 ssl->handshake->sni_ca_crl = ca_crl;
8313}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008314
8315void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8316 int authmode )
8317{
8318 ssl->handshake->sni_authmode = authmode;
8319}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008320#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8321
Hanno Becker8927c832019-04-03 12:52:50 +01008322#if defined(MBEDTLS_X509_CRT_PARSE_C)
8323void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8324 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8325 void *p_vrfy )
8326{
8327 ssl->f_vrfy = f_vrfy;
8328 ssl->p_vrfy = p_vrfy;
8329}
8330#endif
8331
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008332#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008333/*
8334 * Set EC J-PAKE password for current handshake
8335 */
8336int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8337 const unsigned char *pw,
8338 size_t pw_len )
8339{
8340 mbedtls_ecjpake_role role;
8341
Janos Follath8eb64132016-06-03 15:40:57 +01008342 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8344
8345 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8346 role = MBEDTLS_ECJPAKE_SERVER;
8347 else
8348 role = MBEDTLS_ECJPAKE_CLIENT;
8349
8350 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8351 role,
8352 MBEDTLS_MD_SHA256,
8353 MBEDTLS_ECP_DP_SECP256R1,
8354 pw, pw_len ) );
8355}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008356#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008359
8360static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8361{
8362 /* Remove reference to existing PSK, if any. */
8363#if defined(MBEDTLS_USE_PSA_CRYPTO)
8364 if( conf->psk_opaque != 0 )
8365 {
8366 /* The maintenance of the PSK key slot is the
8367 * user's responsibility. */
8368 conf->psk_opaque = 0;
8369 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008370 /* This and the following branch should never
8371 * be taken simultaenously as we maintain the
8372 * invariant that raw and opaque PSKs are never
8373 * configured simultaneously. As a safeguard,
8374 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008375#endif /* MBEDTLS_USE_PSA_CRYPTO */
8376 if( conf->psk != NULL )
8377 {
8378 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8379
8380 mbedtls_free( conf->psk );
8381 conf->psk = NULL;
8382 conf->psk_len = 0;
8383 }
8384
8385 /* Remove reference to PSK identity, if any. */
8386 if( conf->psk_identity != NULL )
8387 {
8388 mbedtls_free( conf->psk_identity );
8389 conf->psk_identity = NULL;
8390 conf->psk_identity_len = 0;
8391 }
8392}
8393
Hanno Becker7390c712018-11-15 13:33:04 +00008394/* This function assumes that PSK identity in the SSL config is unset.
8395 * It checks that the provided identity is well-formed and attempts
8396 * to make a copy of it in the SSL config.
8397 * On failure, the PSK identity in the config remains unset. */
8398static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8399 unsigned char const *psk_identity,
8400 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008401{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008402 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008403 if( psk_identity == NULL ||
8404 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008405 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008406 {
8407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8408 }
8409
Hanno Becker7390c712018-11-15 13:33:04 +00008410 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8411 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008412 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008413
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008414 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008415 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008416
8417 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008418}
8419
Hanno Becker7390c712018-11-15 13:33:04 +00008420int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8421 const unsigned char *psk, size_t psk_len,
8422 const unsigned char *psk_identity, size_t psk_identity_len )
8423{
8424 int ret;
8425 /* Remove opaque/raw PSK + PSK Identity */
8426 ssl_conf_remove_psk( conf );
8427
8428 /* Check and set raw PSK */
8429 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8431 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8432 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8433 conf->psk_len = psk_len;
8434 memcpy( conf->psk, psk, conf->psk_len );
8435
8436 /* Check and set PSK Identity */
8437 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8438 if( ret != 0 )
8439 ssl_conf_remove_psk( conf );
8440
8441 return( ret );
8442}
8443
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008444static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8445{
8446#if defined(MBEDTLS_USE_PSA_CRYPTO)
8447 if( ssl->handshake->psk_opaque != 0 )
8448 {
8449 ssl->handshake->psk_opaque = 0;
8450 }
8451 else
8452#endif /* MBEDTLS_USE_PSA_CRYPTO */
8453 if( ssl->handshake->psk != NULL )
8454 {
8455 mbedtls_platform_zeroize( ssl->handshake->psk,
8456 ssl->handshake->psk_len );
8457 mbedtls_free( ssl->handshake->psk );
8458 ssl->handshake->psk_len = 0;
8459 }
8460}
8461
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008462int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8463 const unsigned char *psk, size_t psk_len )
8464{
8465 if( psk == NULL || ssl->handshake == NULL )
8466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8467
8468 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8470
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008471 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008472
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008473 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008474 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008475
8476 ssl->handshake->psk_len = psk_len;
8477 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8478
8479 return( 0 );
8480}
8481
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008482#if defined(MBEDTLS_USE_PSA_CRYPTO)
8483int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008484 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008485 const unsigned char *psk_identity,
8486 size_t psk_identity_len )
8487{
Hanno Becker7390c712018-11-15 13:33:04 +00008488 int ret;
8489 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008490 ssl_conf_remove_psk( conf );
8491
Hanno Becker7390c712018-11-15 13:33:04 +00008492 /* Check and set opaque PSK */
8493 if( psk_slot == 0 )
8494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008495 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008496
8497 /* Check and set PSK Identity */
8498 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8499 psk_identity_len );
8500 if( ret != 0 )
8501 ssl_conf_remove_psk( conf );
8502
8503 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008504}
8505
8506int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008507 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008508{
8509 if( psk_slot == 0 || ssl->handshake == NULL )
8510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8511
8512 ssl_remove_psk( ssl );
8513 ssl->handshake->psk_opaque = psk_slot;
8514 return( 0 );
8515}
8516#endif /* MBEDTLS_USE_PSA_CRYPTO */
8517
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008518void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008519 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008520 size_t),
8521 void *p_psk )
8522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008523 conf->f_psk = f_psk;
8524 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008527
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008528#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008529
8530#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008531int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008532{
8533 int ret;
8534
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008535 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8536 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8537 {
8538 mbedtls_mpi_free( &conf->dhm_P );
8539 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008540 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008542
8543 return( 0 );
8544}
Hanno Becker470a8c42017-10-04 15:28:46 +01008545#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008546
Hanno Beckera90658f2017-10-04 15:29:08 +01008547int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8548 const unsigned char *dhm_P, size_t P_len,
8549 const unsigned char *dhm_G, size_t G_len )
8550{
8551 int ret;
8552
8553 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8554 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8555 {
8556 mbedtls_mpi_free( &conf->dhm_P );
8557 mbedtls_mpi_free( &conf->dhm_G );
8558 return( ret );
8559 }
8560
8561 return( 0 );
8562}
Paul Bakker5121ce52009-01-03 21:22:43 +00008563
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008564int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008565{
8566 int ret;
8567
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008568 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8569 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8570 {
8571 mbedtls_mpi_free( &conf->dhm_P );
8572 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008573 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008574 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008575
8576 return( 0 );
8577}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008578#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008579
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008580#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8581/*
8582 * Set the minimum length for Diffie-Hellman parameters
8583 */
8584void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8585 unsigned int bitlen )
8586{
8587 conf->dhm_min_bitlen = bitlen;
8588}
8589#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8590
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008591#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008592/*
8593 * Set allowed/preferred hashes for handshake signatures
8594 */
8595void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8596 const int *hashes )
8597{
8598 conf->sig_hashes = hashes;
8599}
Hanno Becker947194e2017-04-07 13:25:49 +01008600#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008601
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008602#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008603/*
8604 * Set the allowed elliptic curves
8605 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008607 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008609 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008610}
Hanno Becker947194e2017-04-07 13:25:49 +01008611#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008612
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008613#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008615{
Hanno Becker947194e2017-04-07 13:25:49 +01008616 /* Initialize to suppress unnecessary compiler warning */
8617 size_t hostname_len = 0;
8618
8619 /* Check if new hostname is valid before
8620 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008621 if( hostname != NULL )
8622 {
8623 hostname_len = strlen( hostname );
8624
8625 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8627 }
8628
8629 /* Now it's clear that we will overwrite the old hostname,
8630 * so we can free it safely */
8631
8632 if( ssl->hostname != NULL )
8633 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008634 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008635 mbedtls_free( ssl->hostname );
8636 }
8637
8638 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008639
Paul Bakker5121ce52009-01-03 21:22:43 +00008640 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008641 {
8642 ssl->hostname = NULL;
8643 }
8644 else
8645 {
8646 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008647 if( ssl->hostname == NULL )
8648 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008649
Hanno Becker947194e2017-04-07 13:25:49 +01008650 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008651
Hanno Becker947194e2017-04-07 13:25:49 +01008652 ssl->hostname[hostname_len] = '\0';
8653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008654
8655 return( 0 );
8656}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008657#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008658
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008659#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008660void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008662 const unsigned char *, size_t),
8663 void *p_sni )
8664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008665 conf->f_sni = f_sni;
8666 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008671int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008672{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008673 size_t cur_len, tot_len;
8674 const char **p;
8675
8676 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008677 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8678 * MUST NOT be truncated."
8679 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008680 */
8681 tot_len = 0;
8682 for( p = protos; *p != NULL; p++ )
8683 {
8684 cur_len = strlen( *p );
8685 tot_len += cur_len;
8686
8687 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008689 }
8690
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008691 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008692
8693 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008694}
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008697{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008698 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008701
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008702void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008704 conf->max_major_ver = major;
8705 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008706}
8707
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008708void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008709{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008710 conf->min_major_ver = major;
8711 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008712}
8713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008715void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008716{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008717 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008718}
8719#endif
8720
Janos Follath088ce432017-04-10 12:42:31 +01008721#if defined(MBEDTLS_SSL_SRV_C)
8722void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8723 char cert_req_ca_list )
8724{
8725 conf->cert_req_ca_list = cert_req_ca_list;
8726}
8727#endif
8728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008730void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008731{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008732 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008733}
8734#endif
8735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008737void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008739 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008740}
8741#endif
8742
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008743#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008744void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008745{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008746 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008747}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008748#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008751int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008754 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008757 }
8758
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008759 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008760
8761 return( 0 );
8762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008766void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008767{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008768 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008773void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008774{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008775 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008776}
8777#endif
8778
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008779void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008780{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008781 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008782}
8783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008785void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008787 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008788}
8789
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008790void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008791{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008792 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008793}
8794
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008795void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008796 const unsigned char period[8] )
8797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008798 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008803#if defined(MBEDTLS_SSL_CLI_C)
8804void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008805{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008806 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008807}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008808#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008809
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008810#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008811void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8812 mbedtls_ssl_ticket_write_t *f_ticket_write,
8813 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8814 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008815{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008816 conf->f_ticket_write = f_ticket_write;
8817 conf->f_ticket_parse = f_ticket_parse;
8818 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008819}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008822
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008823#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8824void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8825 mbedtls_ssl_export_keys_t *f_export_keys,
8826 void *p_export_keys )
8827{
8828 conf->f_export_keys = f_export_keys;
8829 conf->p_export_keys = p_export_keys;
8830}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008831
8832void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8833 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8834 void *p_export_keys )
8835{
8836 conf->f_export_keys_ext = f_export_keys_ext;
8837 conf->p_export_keys = p_export_keys;
8838}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008839#endif
8840
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008841#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008842void mbedtls_ssl_conf_async_private_cb(
8843 mbedtls_ssl_config *conf,
8844 mbedtls_ssl_async_sign_t *f_async_sign,
8845 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8846 mbedtls_ssl_async_resume_t *f_async_resume,
8847 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008848 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008849{
8850 conf->f_async_sign_start = f_async_sign;
8851 conf->f_async_decrypt_start = f_async_decrypt;
8852 conf->f_async_resume = f_async_resume;
8853 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008854 conf->p_async_config_data = async_config_data;
8855}
8856
Gilles Peskine8f97af72018-04-26 11:46:10 +02008857void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8858{
8859 return( conf->p_async_config_data );
8860}
8861
Gilles Peskine1febfef2018-04-30 11:54:39 +02008862void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008863{
8864 if( ssl->handshake == NULL )
8865 return( NULL );
8866 else
8867 return( ssl->handshake->user_async_ctx );
8868}
8869
Gilles Peskine1febfef2018-04-30 11:54:39 +02008870void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008871 void *ctx )
8872{
8873 if( ssl->handshake != NULL )
8874 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008875}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008876#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008877
Paul Bakker5121ce52009-01-03 21:22:43 +00008878/*
8879 * SSL get accessors
8880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008882{
8883 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8884}
8885
Hanno Becker8b170a02017-10-10 11:51:19 +01008886int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8887{
8888 /*
8889 * Case A: We're currently holding back
8890 * a message for further processing.
8891 */
8892
8893 if( ssl->keep_current_message == 1 )
8894 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008896 return( 1 );
8897 }
8898
8899 /*
8900 * Case B: Further records are pending in the current datagram.
8901 */
8902
8903#if defined(MBEDTLS_SSL_PROTO_DTLS)
8904 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8905 ssl->in_left > ssl->next_record_offset )
8906 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008908 return( 1 );
8909 }
8910#endif /* MBEDTLS_SSL_PROTO_DTLS */
8911
8912 /*
8913 * Case C: A handshake message is being processed.
8914 */
8915
Hanno Becker8b170a02017-10-10 11:51:19 +01008916 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8917 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008919 return( 1 );
8920 }
8921
8922 /*
8923 * Case D: An application data message is being processed
8924 */
8925 if( ssl->in_offt != NULL )
8926 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008928 return( 1 );
8929 }
8930
8931 /*
8932 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008933 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008934 * we implement support for multiple alerts in single records.
8935 */
8936
8937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8938 return( 0 );
8939}
8940
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008941uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008942{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008943 if( ssl->session != NULL )
8944 return( ssl->session->verify_result );
8945
8946 if( ssl->session_negotiate != NULL )
8947 return( ssl->session_negotiate->verify_result );
8948
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008949 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008950}
8951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008953{
Paul Bakker926c8e42013-03-06 10:23:34 +01008954 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008955 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008958}
8959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008964 {
8965 switch( ssl->minor_ver )
8966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008968 return( "DTLSv1.0" );
8969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008971 return( "DTLSv1.2" );
8972
8973 default:
8974 return( "unknown (DTLS)" );
8975 }
8976 }
8977#endif
8978
Paul Bakker43ca69c2011-01-15 17:35:19 +00008979 switch( ssl->minor_ver )
8980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008981 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008982 return( "SSLv3.0" );
8983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008985 return( "TLSv1.0" );
8986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008988 return( "TLSv1.1" );
8989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008991 return( "TLSv1.2" );
8992
Paul Bakker43ca69c2011-01-15 17:35:19 +00008993 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008994 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008995 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008996}
8997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008999{
Hanno Becker3136ede2018-08-17 15:28:19 +01009000 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009002 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009003
Hanno Becker78640902018-08-13 16:35:15 +01009004 if( transform == NULL )
9005 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007#if defined(MBEDTLS_ZLIB_SUPPORT)
9008 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9009 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009010#endif
9011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014 case MBEDTLS_MODE_GCM:
9015 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009016 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009018 transform_expansion = transform->minlen;
9019 break;
9020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009022
9023 block_size = mbedtls_cipher_get_block_size(
9024 &transform->cipher_ctx_enc );
9025
Hanno Becker3136ede2018-08-17 15:28:19 +01009026 /* Expansion due to the addition of the MAC. */
9027 transform_expansion += transform->maclen;
9028
9029 /* Expansion due to the addition of CBC padding;
9030 * Theoretically up to 256 bytes, but we never use
9031 * more than the block size of the underlying cipher. */
9032 transform_expansion += block_size;
9033
9034 /* For TLS 1.1 or higher, an explicit IV is added
9035 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009036#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9037 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009038 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009039#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009040
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009041 break;
9042
9043 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009046 }
9047
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009048 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009049}
9050
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009051#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9052size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9053{
9054 size_t max_len;
9055
9056 /*
9057 * Assume mfl_code is correct since it was checked when set
9058 */
Angus Grattond8213d02016-05-25 20:56:48 +10009059 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009060
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009061 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009062 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009063 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009064 {
Angus Grattond8213d02016-05-25 20:56:48 +10009065 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009066 }
9067
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009068 /* During a handshake, use the value being negotiated */
9069 if( ssl->session_negotiate != NULL &&
9070 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9071 {
9072 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9073 }
9074
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009075 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009076}
9077#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9078
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009079#if defined(MBEDTLS_SSL_PROTO_DTLS)
9080static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9081{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009082 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9083 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9084 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9085 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9086 return ( 0 );
9087
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009088 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9089 return( ssl->mtu );
9090
9091 if( ssl->mtu == 0 )
9092 return( ssl->handshake->mtu );
9093
9094 return( ssl->mtu < ssl->handshake->mtu ?
9095 ssl->mtu : ssl->handshake->mtu );
9096}
9097#endif /* MBEDTLS_SSL_PROTO_DTLS */
9098
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009099int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9100{
9101 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9102
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009103#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9104 !defined(MBEDTLS_SSL_PROTO_DTLS)
9105 (void) ssl;
9106#endif
9107
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009108#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9109 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9110
9111 if( max_len > mfl )
9112 max_len = mfl;
9113#endif
9114
9115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009116 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009117 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009118 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009119 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9120 const size_t overhead = (size_t) ret;
9121
9122 if( ret < 0 )
9123 return( ret );
9124
9125 if( mtu <= overhead )
9126 {
9127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9128 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9129 }
9130
9131 if( max_len > mtu - overhead )
9132 max_len = mtu - overhead;
9133 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009134#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009135
Hanno Becker0defedb2018-08-10 12:35:02 +01009136#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9137 !defined(MBEDTLS_SSL_PROTO_DTLS)
9138 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009139#endif
9140
9141 return( (int) max_len );
9142}
9143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144#if defined(MBEDTLS_X509_CRT_PARSE_C)
9145const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009146{
9147 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009148 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009149
Hanno Beckere6824572019-02-07 13:18:46 +00009150#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009151 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009152#else
9153 return( NULL );
9154#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009158#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009159int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9160 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009161{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009162 if( ssl == NULL ||
9163 dst == NULL ||
9164 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009165 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009168 }
9169
Hanno Becker52055ae2019-02-06 14:30:46 +00009170 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009173
Paul Bakker5121ce52009-01-03 21:22:43 +00009174/*
Paul Bakker1961b702013-01-25 14:49:24 +01009175 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009180
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009181 if( ssl == NULL || ssl->conf == NULL )
9182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009185 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009189 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009191#endif
9192
Paul Bakker1961b702013-01-25 14:49:24 +01009193 return( ret );
9194}
9195
9196/*
9197 * Perform the SSL handshake
9198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009200{
9201 int ret = 0;
9202
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009203 if( ssl == NULL || ssl->conf == NULL )
9204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009211
9212 if( ret != 0 )
9213 break;
9214 }
9215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009217
9218 return( ret );
9219}
9220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221#if defined(MBEDTLS_SSL_RENEGOTIATION)
9222#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009223/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009224 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009226static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009227{
9228 int ret;
9229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009231
9232 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9234 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009235
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009236 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009237 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009239 return( ret );
9240 }
9241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009243
9244 return( 0 );
9245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009247
9248/*
9249 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250 * - any side: calling mbedtls_ssl_renegotiate(),
9251 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9252 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009253 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009254 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009258{
9259 int ret;
9260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009262
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009263 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9264 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009265
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009266 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9267 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009271 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009272 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009273 ssl->handshake->out_msg_seq = 1;
9274 else
9275 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009276 }
9277#endif
9278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9280 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009285 return( ret );
9286 }
9287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009289
9290 return( 0 );
9291}
9292
9293/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009294 * Renegotiate current connection on client,
9295 * or request renegotiation on server
9296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009300
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009301 if( ssl == NULL || ssl->conf == NULL )
9302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009305 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009306 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009312
9313 /* Did we already try/start sending HelloRequest? */
9314 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009316
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009317 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009318 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009322 /*
9323 * On client, either start the renegotiation process or,
9324 * if already in progress, continue the handshake
9325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009330
9331 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009334 return( ret );
9335 }
9336 }
9337 else
9338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009342 return( ret );
9343 }
9344 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009346
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009347 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009348}
9349
9350/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009351 * Check record counters and renegotiate if they're above the limit.
9352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009354{
Andres AG2196c7f2016-12-15 17:01:16 +00009355 size_t ep_len = ssl_ep_len( ssl );
9356 int in_ctr_cmp;
9357 int out_ctr_cmp;
9358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9360 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009361 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009362 {
9363 return( 0 );
9364 }
9365
Andres AG2196c7f2016-12-15 17:01:16 +00009366 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9367 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009368 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009369 ssl->conf->renego_period + ep_len, 8 - ep_len );
9370
9371 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009372 {
9373 return( 0 );
9374 }
9375
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009380
9381/*
9382 * Receive application data decrypted from the SSL layer
9383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009385{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009386 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009387 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009388
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009389 if( ssl == NULL || ssl->conf == NULL )
9390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009395 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009398 return( ret );
9399
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009400 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009402 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009403 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009404 return( ret );
9405 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009406 }
9407#endif
9408
Hanno Becker4a810fb2017-05-24 16:27:30 +01009409 /*
9410 * Check if renegotiation is necessary and/or handshake is
9411 * in process. If yes, perform/continue, and fall through
9412 * if an unexpected packet is received while the client
9413 * is waiting for the ServerHello.
9414 *
9415 * (There is no equivalent to the last condition on
9416 * the server-side as it is not treated as within
9417 * a handshake while waiting for the ClientHello
9418 * after a renegotiation request.)
9419 */
9420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009422 ret = ssl_check_ctr_renegotiate( ssl );
9423 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9424 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009427 return( ret );
9428 }
9429#endif
9430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009434 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9435 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009438 return( ret );
9439 }
9440 }
9441
Hanno Beckere41158b2017-10-23 13:30:32 +01009442 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009443 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009444 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009445 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009446 if( ssl->f_get_timer != NULL &&
9447 ssl->f_get_timer( ssl->p_timer ) == -1 )
9448 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009449 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009450 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009451
Hanno Becker327c93b2018-08-15 13:56:18 +01009452 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009453 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009454 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9455 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009456
Hanno Becker4a810fb2017-05-24 16:27:30 +01009457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9458 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009459 }
9460
9461 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009463 {
9464 /*
9465 * OpenSSL sends empty messages to randomize the IV
9466 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009467 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009470 return( 0 );
9471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009473 return( ret );
9474 }
9475 }
9476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009480
Hanno Becker4a810fb2017-05-24 16:27:30 +01009481 /*
9482 * - For client-side, expect SERVER_HELLO_REQUEST.
9483 * - For server-side, expect CLIENT_HELLO.
9484 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9485 */
9486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009490 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009493
9494 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009496 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009497 {
9498 continue;
9499 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009500#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009502 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009503#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009504
Hanno Becker4a810fb2017-05-24 16:27:30 +01009505#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009506 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009510
9511 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009514 {
9515 continue;
9516 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009519 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009520#endif /* MBEDTLS_SSL_SRV_C */
9521
Hanno Becker21df7f92017-10-17 11:03:26 +01009522#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009523 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009524 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9525 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9526 ssl->conf->allow_legacy_renegotiation ==
9527 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9528 {
9529 /*
9530 * Accept renegotiation request
9531 */
Paul Bakker48916f92012-09-16 19:57:18 +00009532
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009533 /* DTLS clients need to know renego is server-initiated */
9534#if defined(MBEDTLS_SSL_PROTO_DTLS)
9535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9536 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9537 {
9538 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9539 }
9540#endif
9541 ret = ssl_start_renegotiation( ssl );
9542 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9543 ret != 0 )
9544 {
9545 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9546 return( ret );
9547 }
9548 }
9549 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009550#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009551 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009552 /*
9553 * Refuse renegotiation
9554 */
9555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558#if defined(MBEDTLS_SSL_PROTO_SSL3)
9559 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009560 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009561 /* SSLv3 does not have a "no_renegotiation" warning, so
9562 we send a fatal alert and abort the connection. */
9563 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9564 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9565 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009566 }
9567 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9569#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9570 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9571 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9574 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9575 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009576 {
9577 return( ret );
9578 }
Paul Bakker48916f92012-09-16 19:57:18 +00009579 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009580 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9582 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009586 }
Paul Bakker48916f92012-09-16 19:57:18 +00009587 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009588
Hanno Becker90333da2017-10-10 11:27:13 +01009589 /* At this point, we don't know whether the renegotiation has been
9590 * completed or not. The cases to consider are the following:
9591 * 1) The renegotiation is complete. In this case, no new record
9592 * has been read yet.
9593 * 2) The renegotiation is incomplete because the client received
9594 * an application data record while awaiting the ServerHello.
9595 * 3) The renegotiation is incomplete because the client received
9596 * a non-handshake, non-application data message while awaiting
9597 * the ServerHello.
9598 * In each of these case, looping will be the proper action:
9599 * - For 1), the next iteration will read a new record and check
9600 * if it's application data.
9601 * - For 2), the loop condition isn't satisfied as application data
9602 * is present, hence continue is the same as break
9603 * - For 3), the loop condition is satisfied and read_record
9604 * will re-deliver the message that was held back by the client
9605 * when expecting the ServerHello.
9606 */
9607 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009608 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009609#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009611 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009612 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009613 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009614 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009617 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009619 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009620 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009621 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9625 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009628 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009629 }
9630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9634 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009635 }
9636
9637 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009638
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009639 /* We're going to return something now, cancel timer,
9640 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009642 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009643
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009645 /* If we requested renego but received AppData, resend HelloRequest.
9646 * Do it now, after setting in_offt, to avoid taking this branch
9647 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009649 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009651 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009652 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009655 return( ret );
9656 }
9657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009659#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009660 }
9661
9662 n = ( len < ssl->in_msglen )
9663 ? len : ssl->in_msglen;
9664
9665 memcpy( buf, ssl->in_offt, n );
9666 ssl->in_msglen -= n;
9667
9668 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009669 {
9670 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009671 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009672 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009674 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009675 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009676 /* more data available */
9677 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009681
Paul Bakker23986e52011-04-24 08:57:21 +00009682 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009683}
9684
9685/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009686 * Send application data to be encrypted by the SSL layer, taking care of max
9687 * fragment length and buffer size.
9688 *
9689 * According to RFC 5246 Section 6.2.1:
9690 *
9691 * Zero-length fragments of Application data MAY be sent as they are
9692 * potentially useful as a traffic analysis countermeasure.
9693 *
9694 * Therefore, it is possible that the input message length is 0 and the
9695 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009696 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009697static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009698 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009699{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009700 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9701 const size_t max_len = (size_t) ret;
9702
9703 if( ret < 0 )
9704 {
9705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9706 return( ret );
9707 }
9708
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009709 if( len > max_len )
9710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009712 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009715 "maximum fragment length: %d > %d",
9716 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009718 }
9719 else
9720#endif
9721 len = max_len;
9722 }
Paul Bakker887bd502011-06-08 13:10:54 +00009723
Paul Bakker5121ce52009-01-03 21:22:43 +00009724 if( ssl->out_left != 0 )
9725 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009726 /*
9727 * The user has previously tried to send the data and
9728 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9729 * written. In this case, we expect the high-level write function
9730 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009735 return( ret );
9736 }
9737 }
Paul Bakker887bd502011-06-08 13:10:54 +00009738 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009739 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009740 /*
9741 * The user is trying to send a message the first time, so we need to
9742 * copy the data into the internal buffers and setup the data structure
9743 * to keep track of partial writes
9744 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009745 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009747 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009748
Hanno Becker67bc7c32018-08-06 11:33:50 +01009749 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009752 return( ret );
9753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009754 }
9755
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009756 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009757}
9758
9759/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009760 * Write application data, doing 1/n-1 splitting if necessary.
9761 *
9762 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009763 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009764 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009767static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009768 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009769{
9770 int ret;
9771
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009772 if( ssl->conf->cbc_record_splitting ==
9773 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009774 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9776 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9777 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009778 {
9779 return( ssl_write_real( ssl, buf, len ) );
9780 }
9781
9782 if( ssl->split_done == 0 )
9783 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009784 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009785 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009786 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009787 }
9788
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009789 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9790 return( ret );
9791 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009792
9793 return( ret + 1 );
9794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009796
9797/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009798 * Write application data (public-facing wrapper)
9799 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009800int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009801{
9802 int ret;
9803
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009805
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009806 if( ssl == NULL || ssl->conf == NULL )
9807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9808
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009809#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009810 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9811 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009812 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009813 return( ret );
9814 }
9815#endif
9816
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009817 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009818 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009819 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009820 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009822 return( ret );
9823 }
9824 }
9825
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009826#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009827 ret = ssl_write_split( ssl, buf, len );
9828#else
9829 ret = ssl_write_real( ssl, buf, len );
9830#endif
9831
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009833
9834 return( ret );
9835}
9836
9837/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009838 * Notify the peer that the connection is being closed
9839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009841{
9842 int ret;
9843
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009844 if( ssl == NULL || ssl->conf == NULL )
9845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009848
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009849 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9855 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9856 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009859 return( ret );
9860 }
9861 }
9862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009864
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009865 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009866}
9867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009869{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009870 if( transform == NULL )
9871 return;
9872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009874 deflateEnd( &transform->ctx_deflate );
9875 inflateEnd( &transform->ctx_inflate );
9876#endif
9877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9879 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009880
Hanno Beckerd56ed242018-01-03 15:32:51 +00009881#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882 mbedtls_md_free( &transform->md_ctx_enc );
9883 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009884#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009885
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009886 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009887}
9888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889#if defined(MBEDTLS_X509_CRT_PARSE_C)
9890static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009893
9894 while( cur != NULL )
9895 {
9896 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009898 cur = next;
9899 }
9900}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009902
Hanno Becker0271f962018-08-16 13:23:47 +01009903#if defined(MBEDTLS_SSL_PROTO_DTLS)
9904
9905static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9906{
9907 unsigned offset;
9908 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9909
9910 if( hs == NULL )
9911 return;
9912
Hanno Becker283f5ef2018-08-24 09:34:47 +01009913 ssl_free_buffered_record( ssl );
9914
Hanno Becker0271f962018-08-16 13:23:47 +01009915 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009916 ssl_buffering_free_slot( ssl, offset );
9917}
9918
9919static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9920 uint8_t slot )
9921{
9922 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9923 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009924
9925 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9926 return;
9927
Hanno Beckere605b192018-08-21 15:59:07 +01009928 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009929 {
Hanno Beckere605b192018-08-21 15:59:07 +01009930 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009931 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009932 mbedtls_free( hs_buf->data );
9933 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009934 }
9935}
9936
9937#endif /* MBEDTLS_SSL_PROTO_DTLS */
9938
Gilles Peskine9b562d52018-04-25 20:32:43 +02009939void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009940{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009941 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9942
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009943 if( handshake == NULL )
9944 return;
9945
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009946#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9947 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9948 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009949 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009950 handshake->async_in_progress = 0;
9951 }
9952#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9953
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009954#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9955 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9956 mbedtls_md5_free( &handshake->fin_md5 );
9957 mbedtls_sha1_free( &handshake->fin_sha1 );
9958#endif
9959#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9960#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009961#if defined(MBEDTLS_USE_PSA_CRYPTO)
9962 psa_hash_abort( &handshake->fin_sha256_psa );
9963#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009964 mbedtls_sha256_free( &handshake->fin_sha256 );
9965#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009966#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009967#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009968#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009969 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009970#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009971 mbedtls_sha512_free( &handshake->fin_sha512 );
9972#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009973#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009974#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976#if defined(MBEDTLS_DHM_C)
9977 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979#if defined(MBEDTLS_ECDH_C)
9980 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009981#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009982#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009983 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009984#if defined(MBEDTLS_SSL_CLI_C)
9985 mbedtls_free( handshake->ecjpake_cache );
9986 handshake->ecjpake_cache = NULL;
9987 handshake->ecjpake_cache_len = 0;
9988#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009989#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009990
Janos Follath4ae5c292016-02-10 11:27:43 +00009991#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9992 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009993 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009995#endif
9996
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009997#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9998 if( handshake->psk != NULL )
9999 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010000 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010001 mbedtls_free( handshake->psk );
10002 }
10003#endif
10004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10006 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010007 /*
10008 * Free only the linked list wrapper, not the keys themselves
10009 * since the belong to the SNI callback
10010 */
10011 if( handshake->sni_key_cert != NULL )
10012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010014
10015 while( cur != NULL )
10016 {
10017 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010019 cur = next;
10020 }
10021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010023
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010024#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010025 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010026 if( handshake->ecrs_peer_cert != NULL )
10027 {
10028 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10029 mbedtls_free( handshake->ecrs_peer_cert );
10030 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010031#endif
10032
Hanno Becker75173122019-02-06 16:18:31 +000010033#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10034 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10035 mbedtls_pk_free( &handshake->peer_pubkey );
10036#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038#if defined(MBEDTLS_SSL_PROTO_DTLS)
10039 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010040 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010041 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010042#endif
10043
Hanno Becker4a63ed42019-01-08 11:39:35 +000010044#if defined(MBEDTLS_ECDH_C) && \
10045 defined(MBEDTLS_USE_PSA_CRYPTO)
10046 psa_destroy_key( handshake->ecdh_psa_privkey );
10047#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10048
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010049 mbedtls_platform_zeroize( handshake,
10050 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010051}
10052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010054{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010055 if( session == NULL )
10056 return;
10057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010059 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010060#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010061
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010062#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010064#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010065
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010066 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010067}
10068
Paul Bakker5121ce52009-01-03 21:22:43 +000010069/*
10070 * Free an SSL context
10071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010073{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010074 if( ssl == NULL )
10075 return;
10076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010078
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010079 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010080 {
Angus Grattond8213d02016-05-25 20:56:48 +100010081 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010083 }
10084
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010085 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010086 {
Angus Grattond8213d02016-05-25 20:56:48 +100010087 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010089 }
10090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010092 if( ssl->compress_buf != NULL )
10093 {
Angus Grattond8213d02016-05-25 20:56:48 +100010094 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010096 }
10097#endif
10098
Paul Bakker48916f92012-09-16 19:57:18 +000010099 if( ssl->transform )
10100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 mbedtls_ssl_transform_free( ssl->transform );
10102 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010103 }
10104
10105 if( ssl->handshake )
10106 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010107 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10109 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 mbedtls_free( ssl->handshake );
10112 mbedtls_free( ssl->transform_negotiate );
10113 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010114 }
10115
Paul Bakkerc0463502013-02-14 11:19:38 +010010116 if( ssl->session )
10117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118 mbedtls_ssl_session_free( ssl->session );
10119 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010120 }
10121
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010122#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010123 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010124 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010125 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010127 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010128#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10131 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10134 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010135 }
10136#endif
10137
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010138#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010140#endif
10141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010143
Paul Bakker86f04f42013-02-14 11:20:09 +010010144 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010145 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010146}
10147
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010148/*
10149 * Initialze mbedtls_ssl_config
10150 */
10151void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10152{
10153 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10154}
10155
Simon Butcherc97b6972015-12-27 23:48:17 +000010156#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010157static int ssl_preset_default_hashes[] = {
10158#if defined(MBEDTLS_SHA512_C)
10159 MBEDTLS_MD_SHA512,
10160 MBEDTLS_MD_SHA384,
10161#endif
10162#if defined(MBEDTLS_SHA256_C)
10163 MBEDTLS_MD_SHA256,
10164 MBEDTLS_MD_SHA224,
10165#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010166#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010167 MBEDTLS_MD_SHA1,
10168#endif
10169 MBEDTLS_MD_NONE
10170};
Simon Butcherc97b6972015-12-27 23:48:17 +000010171#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010172
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010173static int ssl_preset_suiteb_ciphersuites[] = {
10174 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10175 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10176 0
10177};
10178
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010179#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010180static int ssl_preset_suiteb_hashes[] = {
10181 MBEDTLS_MD_SHA256,
10182 MBEDTLS_MD_SHA384,
10183 MBEDTLS_MD_NONE
10184};
10185#endif
10186
10187#if defined(MBEDTLS_ECP_C)
10188static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10189 MBEDTLS_ECP_DP_SECP256R1,
10190 MBEDTLS_ECP_DP_SECP384R1,
10191 MBEDTLS_ECP_DP_NONE
10192};
10193#endif
10194
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010195/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010196 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010197 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010198int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010199 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010200{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010201#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010202 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010203#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010204
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010205 /* Use the functions here so that they are covered in tests,
10206 * but otherwise access member directly for efficiency */
10207 mbedtls_ssl_conf_endpoint( conf, endpoint );
10208 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010209
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010210 /*
10211 * Things that are common to all presets
10212 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010213#if defined(MBEDTLS_SSL_CLI_C)
10214 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10215 {
10216 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10217#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10218 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10219#endif
10220 }
10221#endif
10222
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010223#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010224 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010225#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010226
10227#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10228 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10229#endif
10230
10231#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10232 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10233#endif
10234
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010235#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10236 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10237#endif
10238
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010239#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010240 conf->f_cookie_write = ssl_cookie_write_dummy;
10241 conf->f_cookie_check = ssl_cookie_check_dummy;
10242#endif
10243
10244#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10245 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10246#endif
10247
Janos Follath088ce432017-04-10 12:42:31 +010010248#if defined(MBEDTLS_SSL_SRV_C)
10249 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10250#endif
10251
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010252#if defined(MBEDTLS_SSL_PROTO_DTLS)
10253 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10254 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10255#endif
10256
10257#if defined(MBEDTLS_SSL_RENEGOTIATION)
10258 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010259 memset( conf->renego_period, 0x00, 2 );
10260 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010261#endif
10262
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010263#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10264 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10265 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010266 const unsigned char dhm_p[] =
10267 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10268 const unsigned char dhm_g[] =
10269 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10270
Hanno Beckera90658f2017-10-04 15:29:08 +010010271 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10272 dhm_p, sizeof( dhm_p ),
10273 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010274 {
10275 return( ret );
10276 }
10277 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010278#endif
10279
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010280 /*
10281 * Preset-specific defaults
10282 */
10283 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010284 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010285 /*
10286 * NSA Suite B
10287 */
10288 case MBEDTLS_SSL_PRESET_SUITEB:
10289 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10290 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10291 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10292 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10293
10294 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10295 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10296 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10297 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10298 ssl_preset_suiteb_ciphersuites;
10299
10300#if defined(MBEDTLS_X509_CRT_PARSE_C)
10301 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010302#endif
10303
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010304#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010305 conf->sig_hashes = ssl_preset_suiteb_hashes;
10306#endif
10307
10308#if defined(MBEDTLS_ECP_C)
10309 conf->curve_list = ssl_preset_suiteb_curves;
10310#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010311 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010312
10313 /*
10314 * Default
10315 */
10316 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010317 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10318 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10319 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10320 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10321 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10322 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10323 MBEDTLS_SSL_MIN_MINOR_VERSION :
10324 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010325 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10326 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10327
10328#if defined(MBEDTLS_SSL_PROTO_DTLS)
10329 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10330 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10331#endif
10332
10333 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10334 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10335 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10336 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10337 mbedtls_ssl_list_ciphersuites();
10338
10339#if defined(MBEDTLS_X509_CRT_PARSE_C)
10340 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10341#endif
10342
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010343#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010344 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010345#endif
10346
10347#if defined(MBEDTLS_ECP_C)
10348 conf->curve_list = mbedtls_ecp_grp_id_list();
10349#endif
10350
10351#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10352 conf->dhm_min_bitlen = 1024;
10353#endif
10354 }
10355
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010356 return( 0 );
10357}
10358
10359/*
10360 * Free mbedtls_ssl_config
10361 */
10362void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10363{
10364#if defined(MBEDTLS_DHM_C)
10365 mbedtls_mpi_free( &conf->dhm_P );
10366 mbedtls_mpi_free( &conf->dhm_G );
10367#endif
10368
10369#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10370 if( conf->psk != NULL )
10371 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010372 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010373 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010374 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010375 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010376 }
10377
10378 if( conf->psk_identity != NULL )
10379 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010380 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010381 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010382 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010383 conf->psk_identity_len = 0;
10384 }
10385#endif
10386
10387#if defined(MBEDTLS_X509_CRT_PARSE_C)
10388 ssl_key_cert_free( conf->key_cert );
10389#endif
10390
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010391 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010392}
10393
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010394#if defined(MBEDTLS_PK_C) && \
10395 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010396/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401#if defined(MBEDTLS_RSA_C)
10402 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10403 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405#if defined(MBEDTLS_ECDSA_C)
10406 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10407 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010408#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010409 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010410}
10411
Hanno Becker7e5437a2017-04-28 17:15:26 +010010412unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10413{
10414 switch( type ) {
10415 case MBEDTLS_PK_RSA:
10416 return( MBEDTLS_SSL_SIG_RSA );
10417 case MBEDTLS_PK_ECDSA:
10418 case MBEDTLS_PK_ECKEY:
10419 return( MBEDTLS_SSL_SIG_ECDSA );
10420 default:
10421 return( MBEDTLS_SSL_SIG_ANON );
10422 }
10423}
10424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010426{
10427 switch( sig )
10428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429#if defined(MBEDTLS_RSA_C)
10430 case MBEDTLS_SSL_SIG_RSA:
10431 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010433#if defined(MBEDTLS_ECDSA_C)
10434 case MBEDTLS_SSL_SIG_ECDSA:
10435 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010436#endif
10437 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010439 }
10440}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010441#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010442
Hanno Becker7e5437a2017-04-28 17:15:26 +010010443#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10444 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10445
10446/* Find an entry in a signature-hash set matching a given hash algorithm. */
10447mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10448 mbedtls_pk_type_t sig_alg )
10449{
10450 switch( sig_alg )
10451 {
10452 case MBEDTLS_PK_RSA:
10453 return( set->rsa );
10454 case MBEDTLS_PK_ECDSA:
10455 return( set->ecdsa );
10456 default:
10457 return( MBEDTLS_MD_NONE );
10458 }
10459}
10460
10461/* Add a signature-hash-pair to a signature-hash set */
10462void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10463 mbedtls_pk_type_t sig_alg,
10464 mbedtls_md_type_t md_alg )
10465{
10466 switch( sig_alg )
10467 {
10468 case MBEDTLS_PK_RSA:
10469 if( set->rsa == MBEDTLS_MD_NONE )
10470 set->rsa = md_alg;
10471 break;
10472
10473 case MBEDTLS_PK_ECDSA:
10474 if( set->ecdsa == MBEDTLS_MD_NONE )
10475 set->ecdsa = md_alg;
10476 break;
10477
10478 default:
10479 break;
10480 }
10481}
10482
10483/* Allow exactly one hash algorithm for each signature. */
10484void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10485 mbedtls_md_type_t md_alg )
10486{
10487 set->rsa = md_alg;
10488 set->ecdsa = md_alg;
10489}
10490
10491#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10492 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10493
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010494/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010495 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010498{
10499 switch( hash )
10500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501#if defined(MBEDTLS_MD5_C)
10502 case MBEDTLS_SSL_HASH_MD5:
10503 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010504#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505#if defined(MBEDTLS_SHA1_C)
10506 case MBEDTLS_SSL_HASH_SHA1:
10507 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509#if defined(MBEDTLS_SHA256_C)
10510 case MBEDTLS_SSL_HASH_SHA224:
10511 return( MBEDTLS_MD_SHA224 );
10512 case MBEDTLS_SSL_HASH_SHA256:
10513 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010515#if defined(MBEDTLS_SHA512_C)
10516 case MBEDTLS_SSL_HASH_SHA384:
10517 return( MBEDTLS_MD_SHA384 );
10518 case MBEDTLS_SSL_HASH_SHA512:
10519 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010520#endif
10521 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010523 }
10524}
10525
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010526/*
10527 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10528 */
10529unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10530{
10531 switch( md )
10532 {
10533#if defined(MBEDTLS_MD5_C)
10534 case MBEDTLS_MD_MD5:
10535 return( MBEDTLS_SSL_HASH_MD5 );
10536#endif
10537#if defined(MBEDTLS_SHA1_C)
10538 case MBEDTLS_MD_SHA1:
10539 return( MBEDTLS_SSL_HASH_SHA1 );
10540#endif
10541#if defined(MBEDTLS_SHA256_C)
10542 case MBEDTLS_MD_SHA224:
10543 return( MBEDTLS_SSL_HASH_SHA224 );
10544 case MBEDTLS_MD_SHA256:
10545 return( MBEDTLS_SSL_HASH_SHA256 );
10546#endif
10547#if defined(MBEDTLS_SHA512_C)
10548 case MBEDTLS_MD_SHA384:
10549 return( MBEDTLS_SSL_HASH_SHA384 );
10550 case MBEDTLS_MD_SHA512:
10551 return( MBEDTLS_SSL_HASH_SHA512 );
10552#endif
10553 default:
10554 return( MBEDTLS_SSL_HASH_NONE );
10555 }
10556}
10557
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010558#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010559/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010560 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010561 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010562 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010563int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010564{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010565 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010566
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010567 if( ssl->conf->curve_list == NULL )
10568 return( -1 );
10569
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010570 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010571 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010572 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010573
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010574 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010575}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010576#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010577
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010578#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010579/*
10580 * Check if a hash proposed by the peer is in our list.
10581 * Return 0 if we're willing to use it, -1 otherwise.
10582 */
10583int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10584 mbedtls_md_type_t md )
10585{
10586 const int *cur;
10587
10588 if( ssl->conf->sig_hashes == NULL )
10589 return( -1 );
10590
10591 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10592 if( *cur == (int) md )
10593 return( 0 );
10594
10595 return( -1 );
10596}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010597#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599#if defined(MBEDTLS_X509_CRT_PARSE_C)
10600int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10601 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010602 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010603 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010604{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010605 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010607 int usage = 0;
10608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010610 const char *ext_oid;
10611 size_t ext_len;
10612#endif
10613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10615 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010616 ((void) cert);
10617 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010618 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010619#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10622 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010623 {
10624 /* Server part of the key exchange */
10625 switch( ciphersuite->key_exchange )
10626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 case MBEDTLS_KEY_EXCHANGE_RSA:
10628 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010629 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010630 break;
10631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10633 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10634 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10635 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010636 break;
10637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10639 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010640 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010641 break;
10642
10643 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 case MBEDTLS_KEY_EXCHANGE_NONE:
10645 case MBEDTLS_KEY_EXCHANGE_PSK:
10646 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10647 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010648 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010649 usage = 0;
10650 }
10651 }
10652 else
10653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10655 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010656 }
10657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010659 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010660 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010661 ret = -1;
10662 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010663#else
10664 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10668 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10671 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010672 }
10673 else
10674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10676 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010677 }
10678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010680 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010681 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010682 ret = -1;
10683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010685
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010686 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010689
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010690/*
10691 * Convert version numbers to/from wire format
10692 * and, for DTLS, to/from TLS equivalent.
10693 *
10694 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010695 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010696 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10697 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010700 unsigned char ver[2] )
10701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702#if defined(MBEDTLS_SSL_PROTO_DTLS)
10703 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010706 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10707
10708 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10709 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10710 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010711 else
10712#else
10713 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010714#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010715 {
10716 ver[0] = (unsigned char) major;
10717 ver[1] = (unsigned char) minor;
10718 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010719}
10720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010721void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010722 const unsigned char ver[2] )
10723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724#if defined(MBEDTLS_SSL_PROTO_DTLS)
10725 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010726 {
10727 *major = 255 - ver[0] + 2;
10728 *minor = 255 - ver[1] + 1;
10729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010731 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10732 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010733 else
10734#else
10735 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010736#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010737 {
10738 *major = ver[0];
10739 *minor = ver[1];
10740 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010741}
10742
Simon Butcher99000142016-10-13 17:21:01 +010010743int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10744{
10745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10746 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10747 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10748
10749 switch( md )
10750 {
10751#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10752#if defined(MBEDTLS_MD5_C)
10753 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010754 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010755#endif
10756#if defined(MBEDTLS_SHA1_C)
10757 case MBEDTLS_SSL_HASH_SHA1:
10758 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10759 break;
10760#endif
10761#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10762#if defined(MBEDTLS_SHA512_C)
10763 case MBEDTLS_SSL_HASH_SHA384:
10764 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10765 break;
10766#endif
10767#if defined(MBEDTLS_SHA256_C)
10768 case MBEDTLS_SSL_HASH_SHA256:
10769 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10770 break;
10771#endif
10772 default:
10773 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10774 }
10775
10776 return 0;
10777#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10778 (void) ssl;
10779 (void) md;
10780
10781 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10783}
10784
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010785#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10786 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10787int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10788 unsigned char *output,
10789 unsigned char *data, size_t data_len )
10790{
10791 int ret = 0;
10792 mbedtls_md5_context mbedtls_md5;
10793 mbedtls_sha1_context mbedtls_sha1;
10794
10795 mbedtls_md5_init( &mbedtls_md5 );
10796 mbedtls_sha1_init( &mbedtls_sha1 );
10797
10798 /*
10799 * digitally-signed struct {
10800 * opaque md5_hash[16];
10801 * opaque sha_hash[20];
10802 * };
10803 *
10804 * md5_hash
10805 * MD5(ClientHello.random + ServerHello.random
10806 * + ServerParams);
10807 * sha_hash
10808 * SHA(ClientHello.random + ServerHello.random
10809 * + ServerParams);
10810 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010811 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010812 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010814 goto exit;
10815 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010816 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010817 ssl->handshake->randbytes, 64 ) ) != 0 )
10818 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010820 goto exit;
10821 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010822 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010823 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010825 goto exit;
10826 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010827 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010828 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010830 goto exit;
10831 }
10832
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010833 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010834 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010836 goto exit;
10837 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010838 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010839 ssl->handshake->randbytes, 64 ) ) != 0 )
10840 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010842 goto exit;
10843 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010844 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010845 data_len ) ) != 0 )
10846 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010848 goto exit;
10849 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010850 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010851 output + 16 ) ) != 0 )
10852 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010854 goto exit;
10855 }
10856
10857exit:
10858 mbedtls_md5_free( &mbedtls_md5 );
10859 mbedtls_sha1_free( &mbedtls_sha1 );
10860
10861 if( ret != 0 )
10862 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10863 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10864
10865 return( ret );
10866
10867}
10868#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10869 MBEDTLS_SSL_PROTO_TLS1_1 */
10870
10871#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10872 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010873
10874#if defined(MBEDTLS_USE_PSA_CRYPTO)
10875int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10876 unsigned char *hash, size_t *hashlen,
10877 unsigned char *data, size_t data_len,
10878 mbedtls_md_type_t md_alg )
10879{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010880 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010881 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010882 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10883
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010884 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010885
10886 if( ( status = psa_hash_setup( &hash_operation,
10887 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010888 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010889 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010890 goto exit;
10891 }
10892
Andrzej Kurek814feff2019-01-14 04:35:19 -050010893 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10894 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010895 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010896 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010897 goto exit;
10898 }
10899
Andrzej Kurek814feff2019-01-14 04:35:19 -050010900 if( ( status = psa_hash_update( &hash_operation,
10901 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010902 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010903 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010904 goto exit;
10905 }
10906
Andrzej Kurek814feff2019-01-14 04:35:19 -050010907 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10908 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010909 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010910 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010911 goto exit;
10912 }
10913
10914exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010915 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010916 {
10917 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10918 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010919 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010920 {
10921 case PSA_ERROR_NOT_SUPPORTED:
10922 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010923 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010924 case PSA_ERROR_BUFFER_TOO_SMALL:
10925 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10926 case PSA_ERROR_INSUFFICIENT_MEMORY:
10927 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10928 default:
10929 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10930 }
10931 }
10932 return( 0 );
10933}
10934
10935#else
10936
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010937int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010938 unsigned char *hash, size_t *hashlen,
10939 unsigned char *data, size_t data_len,
10940 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010941{
10942 int ret = 0;
10943 mbedtls_md_context_t ctx;
10944 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010945 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010946
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010948
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010949 mbedtls_md_init( &ctx );
10950
10951 /*
10952 * digitally-signed struct {
10953 * opaque client_random[32];
10954 * opaque server_random[32];
10955 * ServerDHParams params;
10956 * };
10957 */
10958 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10959 {
10960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10961 goto exit;
10962 }
10963 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10964 {
10965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10966 goto exit;
10967 }
10968 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10969 {
10970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10971 goto exit;
10972 }
10973 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10974 {
10975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10976 goto exit;
10977 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010978 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010979 {
10980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10981 goto exit;
10982 }
10983
10984exit:
10985 mbedtls_md_free( &ctx );
10986
10987 if( ret != 0 )
10988 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10989 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10990
10991 return( ret );
10992}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010993#endif /* MBEDTLS_USE_PSA_CRYPTO */
10994
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010995#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10996 MBEDTLS_SSL_PROTO_TLS1_2 */
10997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998#endif /* MBEDTLS_SSL_TLS_C */